-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathbar2D.py
72 lines (57 loc) · 1.76 KB
/
bar2D.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
"""
FEniCS tutorial demo program: Linear elastic problem.
-div(sigma(u)) = f
The model is used to simulate an elastic beam clamped at
its left end and deformed under its own weight.
"""
from __future__ import print_function
from fenics import *
import matplotlib.pyplot as plt
# Scaled variables
L = 1
W = 0.2
mu = 1
rho = 1
delta = W / L
gamma = 0.4 * delta**2
beta = 1.25
lambda_ = beta
g = gamma
# Create mesh and define function space
mesh = RectangleMesh(Point(0, 0), Point(L, W), 10, 3)
V = VectorFunctionSpace(mesh, 'P', 1)
tol = 1e-2
def clamped_boundary(x, on_boundary):
return on_boundary and x[0] < tol
bc = DirichletBC(V, Constant((0, 0)), clamped_boundary)
# Define strain and stress
epsilon = lambda u: 0.5 * (nabla_grad(u) + nabla_grad(u).T)
sigma = lambda u: lambda_ * nabla_div(u) * Identity(d) + 2 * mu * epsilon(u)
# Define variational problem
u = TrialFunction(V)
d = u.geometric_dimension() # space dimension
v = TestFunction(V)
f = Constant((0, -rho * g))
T = Constant((0, 0))
a = inner(sigma(u), epsilon(v)) * dx
L = dot(f, v) * dx + dot(T, v) * ds
# Compute solution
u = Function(V)
solve(a == L, u, bc)
plot(u, title='Displacement', mode='displacement')
# Plot stress
s = sigma(u) - (1. / 3) * tr(sigma(u)) * Identity(d) # deviatoric stress
von_Mises = sqrt(3. / 2 * inner(s, s))
V = FunctionSpace(mesh, 'P', 1)
von_Mises = project(von_Mises, V)
# Compute magnitude of displacement
u_magnitude = sqrt(dot(u, u))
u_magnitude = project(u_magnitude, V)
print('min/max u:',
u_magnitude.vector().get_local().min(),
u_magnitude.vector().get_local().max())
# Save solution to file in VTK format
File('output/bar2D/displacement.pvd') << u
File('output/bar2D/von_mises.pvd') << von_Mises
File('output/bar2D/magnitude.pvd') << u_magnitude
plt.show()