Debian Codegraph @

python/demo/documented/hyperelasticity/demo_hyperelasticity.py.rst

raw
1
2 .. _demo_hyperelasticity:
3
4 Hyperelasticity
5 ===============
6
7 This demo is implemented in a single Python file,
8 :download:`demo_hyperelasticity.py`, which contains both the
9 variational forms and the solver.
10
11
12 Background
13 ----------
14
15 This example demonstrates the solution of a three-dimensional
16 elasticity problem. In addition to illustrating how to use
17 FunctionSpaces, Expressions and how to apply Dirichlet boundary
18 conditions, it focuses on how to:
19
20 * Minimise a non-quadratic functional
21 * Use automatic computation of the directional derivative
22 * Solve a nonlinear variational problem
23 * Define compiled sub-domains
24 * Use specific form compiler optimization options
25
26 Equation and problem definition
27 -------------------------------
28
29 By definition, boundary value problems for hyperelastic media can be
30 expressed as minimisation problems, and the minimization approach is
31 adopted in this example. For a domain :math:`\Omega \subset
32 \mathbb{R}^{d}`, where :math:`d` denotes the spatial dimension, the
33 task is to find the displacement field :math:`u: \Omega \rightarrow
34 \mathbb{R}^{d}` that minimises the total potential energy :math:`\Pi`:
35
36 .. math::
37 \min_{u \in V} \Pi,
38
39 where :math:`V` is a suitable function space that satisfies boundary
40 conditions on :math:`u`. The total potential energy is given by
41
42 .. math::
43 \Pi = \int_{\Omega} \psi(u) \, {\rm d} x
44 - \int_{\Omega} B \cdot u \, {\rm d} x
45 - \int_{\partial\Omega} T \cdot u \, {\rm d} s,
46
47 where :math:`\psi` is the elastic stored energy density, :math:`B` is a
48 body force (per unit reference volume) and :math:`T` is a traction force
49 (per unit reference area).
50
51 At minimum points of :math:`\Pi`, the directional derivative of :math:`\Pi`
52 with respect to change in :math:`u`
53
54 .. math::
55 :label: first_variation
56
57 L(u; v) = D_{v} \Pi = \left. \frac{d \Pi(u + \epsilon v)}{d\epsilon} \right|_{\epsilon = 0}
58
59 is equal to zero for all :math:`v \in V`:
60
61 .. math::
62 L(u; v) = 0 \quad \forall \ v \in V.
63
64 To minimise the potential energy, a solution to the variational
65 equation above is sought. Depending on the potential energy
66 :math:`\psi`, :math:`L(u; v)` can be nonlinear in :math:`u`. In such a
67 case, the Jacobian of :math:`L` is required in order to solve this
68 problem using Newton's method. The Jacobian of :math:`L` is defined as
69
70 .. math::
71 :label: second_variation
72
73 a(u; du, v) = D_{du} L = \left. \frac{d L(u + \epsilon du; v)}{d\epsilon} \right|_{\epsilon = 0} .
74
75
76
77 Elastic stored energy density
78 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
79
80 To define the elastic stored energy density, consider the deformation
81 gradient :math:`F`
82
83 .. math::
84
85 F = I + \nabla u,
86
87 the right Cauchy-Green tensor :math:`C`
88
89 .. math::
90
91 C = F^{T} F,
92
93 and the scalars :math:`J` and :math:`I_{c}`
94
95 .. math::
96 J &= \det(F), \\
97 I_{c} &= {\rm trace}(C).
98
99 This demo considers a common neo-Hookean stored energy model of the form
100
101 .. math::
102 \psi = \frac{\mu}{2} (I_{c} - 3) - \mu \ln(J) + \frac{\lambda}{2}\ln(J)^{2},
103
104 where :math:`\mu` and :math:`\lambda` are the Lame parameters. These
105 can be expressed in terms of the more common Young's modulus :math:`E`
106 and Poisson ratio :math:`\nu` by:
107
108 .. math::
109 \lambda = \frac{E \nu}{(1 + \nu)(1 - 2\nu)}, \quad \quad
110 \mu = \frac{E}{2(1 + \nu)} .
111
112
113 Demo parameters
114 ^^^^^^^^^^^^^^^
115
116 We consider a unit cube domain:
117
118 * :math:`\Omega = (0, 1) \times (0, 1) \times (0, 1)` (unit cube)
119
120 We use the following definitions of the boundary and boundary conditions:
121
122 * :math:`\Gamma_{D_{0}} = 0 \times (0, 1) \times (0, 1)` (Dirichlet boundary)
123
124 * :math:`\Gamma_{D_{1}} = 1 \times (0, 1) \times (0, 1)` (Dirichlet boundary)
125
126 * :math:`\Gamma_{N} = \partial \Omega \backslash \Gamma_{D}` (Neumann boundary)
127
128 * On :math:`\Gamma_{D_{0}}`: :math:`u = (0, 0, 0)`
129
130 * On :math:`\Gamma_{D_{1}}`
131 .. math::
132 u = (&0, \\
133 &(0.5 + (y - 0.5)\cos(\pi/3) - (z - 0.5)\sin(\pi/3) - y)/2, \\
134 &(0.5 + (y - 0.5)\sin(\pi/3) + (z - 0.5)\cos(\pi/3) - z))/2)
135
136 * On :math:`\Gamma_{N}`: :math:`T = (0.1, 0, 0)`
137
138 These are the body forces and material parameters used:
139
140 * :math:`B = (0, -0.5, 0)`
141
142 * :math:`E = 10.0`
143
144 * :math:`\nu = 0.3`
145
146 With the above input the solution for :math:`u` will look as follows:
147
148 .. image:: hyperelasticity_u0.png
149 :scale: 75
150 :align: center
151
152 .. image:: hyperelasticity_u1.png
153 :scale: 75
154 :align: center
155
156
157 Implementation
158 --------------
159
160 This demo is implemented in the :download:`demo_hyperelasticity.py`
161 file.
162
163 First, the required modules are imported::
164
165 import matplotlib.pyplot as plt
166 from dolfin import *
167
168 The behavior of the form compiler FFC can be adjusted by prescribing
169 various parameters. Here, we want to use the UFLACS backend of FFC::
170
171 # Optimization options for the form compiler
172 parameters["form_compiler"]["cpp_optimize"] = True
173 parameters["form_compiler"]["representation"] = "uflacs"
174
175 The first line tells the form compiler to use C++ compiler optimizations when
176 compiling the generated code. The remainder is a dictionary of options which
177 will be passed to the form compiler. It lists the optimizations strategies
178 that we wish the form compiler to use when generating code.
179
180 .. index:: VectorFunctionSpace
181
182 First, we need a tetrahedral mesh of the domain and a function space
183 on this mesh. Here, we choose to create a unit cube mesh with 25 ( =
184 24 + 1) vertices in one direction and 17 ( = 16 + 1) vertices in the
185 other two direction. On this mesh, we define a function space of
186 continuous piecewise linear vector polynomials (a Lagrange vector
187 element space)::
188
189 # Create mesh and define function space
190 mesh = UnitCubeMesh(24, 16, 16)
191 V = VectorFunctionSpace(mesh, "Lagrange", 1)
192
193 Note that :py:class:`VectorFunctionSpace
194 <dolfin.functions.functionspace.VectorFunctionSpace>` creates a
195 function space of vector fields. The dimension of the vector field
196 (the number of components) is assumed to be the same as the spatial
197 dimension, unless otherwise specified.
198
199 .. index:: compiled subdomain
200
201 The portions of the boundary on which Dirichlet boundary conditions
202 will be applied are now defined::
203
204 # Mark boundary subdomians
205 left = CompiledSubDomain("near(x[0], side) && on_boundary", side = 0.0)
206 right = CompiledSubDomain("near(x[0], side) && on_boundary", side = 1.0)
207
208 The boundary subdomain ``left`` corresponds to the part of the
209 boundary on which :math:`x=0` and the boundary subdomain ``right``
210 corresponds to the part of the boundary on which :math:`x=1`. Note
211 that C++ syntax is used in the :py:func:`CompiledSubDomain`
212 <dolfin.compilemodules.subdomains.CompiledSubDomain>` function since
213 the function will be automatically compiled into C++ code for
214 efficiency. The (built-in) variable ``on_boundary`` is true for points
215 on the boundary of a domain, and false otherwise.
216
217 .. index:: compiled expression
218
219 The Dirichlet boundary values are defined using compiled expressions::
220
221 # Define Dirichlet boundary (x = 0 or x = 1)
222 c = Constant((0.0, 0.0, 0.0))
223 r = Expression(("scale*0.0",
224 "scale*(y0 + (x[1] - y0)*cos(theta) - (x[2] - z0)*sin(theta) - x[1])",
225 "scale*(z0 + (x[1] - y0)*sin(theta) + (x[2] - z0)*cos(theta) - x[2])"),
226 scale = 0.5, y0 = 0.5, z0 = 0.5, theta = pi/3, degree=2)
227
228 Note the use of setting named parameters in the :py:class:`Expression
229 <dolfin.functions.expression.Expression>` for ``r``.
230
231 The boundary subdomains and the boundary condition expressions are
232 collected together in two :py:class:`DirichletBC
233 <dolfin.fem.bcs.DirichletBC>` objects, one for each part of the
234 Dirichlet boundary::
235
236 bcl = DirichletBC(V, c, left)
237 bcr = DirichletBC(V, r, right)
238 bcs = [bcl, bcr]
239
240 The Dirichlet (essential) boundary conditions are constraints on the
241 function space :math:`V`. The function space is therefore required as
242 an argument to :py:class:`DirichletBC <dolfin.fem.bcs.DirichletBC>`.
243
244 .. index:: TestFunction, TrialFunction, Constant
245
246 Trial and test functions, and the most recent approximate displacement
247 ``u`` are defined on the finite element space ``V``, and two objects
248 of type :py:class:`Constant <dolfin.functions.constant.Constant>` are
249 declared for the body force (``B``) and traction (``T``) terms::
250
251 # Define functions
252 du = TrialFunction(V) # Incremental displacement
253 v = TestFunction(V) # Test function
254 u = Function(V) # Displacement from previous iteration
255 B = Constant((0.0, -0.5, 0.0)) # Body force per unit volume
256 T = Constant((0.1, 0.0, 0.0)) # Traction force on the boundary
257
258 In place of :py:class:`Constant <dolfin.functions.constant.Constant>`,
259 it is also possible to use ``as_vector``, e.g. ``B = as_vector( [0.0,
260 -0.5, 0.0] )``. The advantage of Constant is that its values can be
261 changed without requiring re-generation and re-compilation of C++
262 code. On the other hand, using ``as_vector`` can eliminate some
263 function calls during assembly.
264
265 With the functions defined, the kinematic quantities involved in the model
266 are defined using UFL syntax::
267
268 # Kinematics
269 d = len(u)
270 I = Identity(d) # Identity tensor
271 F = I + grad(u) # Deformation gradient
272 C = F.T*F # Right Cauchy-Green tensor
273
274 # Invariants of deformation tensors
275 Ic = tr(C)
276 J = det(F)
277
278 Next, the material parameters are set and the strain energy density
279 and the total potential energy are defined, again using UFL syntax::
280
281 # Elasticity parameters
282 E, nu = 10.0, 0.3
283 mu, lmbda = Constant(E/(2*(1 + nu))), Constant(E*nu/((1 + nu)*(1 - 2*nu)))
284
285 # Stored strain energy density (compressible neo-Hookean model)
286 psi = (mu/2)*(Ic - 3) - mu*ln(J) + (lmbda/2)*(ln(J))**2
287
288 # Total potential energy
289 Pi = psi*dx - dot(B, u)*dx - dot(T, u)*ds
290
291 Just as for the body force and traction vectors, :py:class:`Constant
292 <dolfin.functions.constant.Constant>` has been used for the model
293 parameters ``mu`` and ``lmbda`` to avoid re-generation of C++ code
294 when changing model parameters. Note that ``lambda`` is a reserved
295 keyword in Python, hence the misspelling ``lmbda``.
296
297 .. index:: directional derivative; derivative, taking variations; derivative, automatic differentiation; derivative
298
299 Directional derivatives are now computed of :math:`\Pi` and :math:`L`
300 (see :eq:`first_variation` and :eq:`second_variation`)::
301
302 # Compute first variation of Pi (directional derivative about u in the direction of v)
303 F = derivative(Pi, u, v)
304
305 # Compute Jacobian of F
306 J = derivative(F, u, du)
307
308 The complete variational problem can now be solved by a single call to
309 :py:func:`solve <dolfin.fem.solving.solve>`::
310
311 # Solve variational problem
312 solve(F == 0, u, bcs, J=J)
313
314 Finally, the solution ``u`` is saved to a file named
315 ``displacement.pvd`` in VTK format, and the deformed mesh is plotted
316 to the screen::
317
318 # Save solution in VTK format
319 file = File("displacement.pvd");
320 file << u;
321
322 # Plot solution
323 plot(u)
324 plt.show()