Debian Codegraph @

demo/documented/hyperelasticity/cpp/main.cpp.rst

raw
1 Hyperelasticity (C++)
2 =====================
3
4
5 Background
6 ----------
7
8 See the section :ref:`hyperelasticity` for some mathematical
9 background on this demo.
10
11
12 Implementation
13 --------------
14
15 The implementation is split in two files: a form file containing the
16 definition of the variational forms expressed in UFL and a C++ file
17 containing the actual solver.
18
19 Running this demo requires the files: :download:`main.cpp`,
20 :download:`HyperElasticity.ufl` and :download:`CMakeLists.txt`.
21
22
23 UFL form file
24 ^^^^^^^^^^^^^
25
26 The UFL file is implemented in :download:`Hyperelasticity.ufl`, and
27 the explanation of the UFL file can be found at :doc:`here
28 <Hyperelasticity.ufl>`.
29
30
31 C++ program
32 ^^^^^^^^^^^
33
34 The main solver is implemented in the :download:`main.cpp` file.
35
36 At the top, we include the DOLFIN header file and the generated header
37 file "HyperElasticity.h" containing the variational forms and function
38 spaces. For convenience we also include the DOLFIN namespace.
39
40 .. code-block:: cpp
41
42 #include <dolfin.h>
43 #include "HyperElasticity.h"
44
45 using namespace dolfin;
46
47 We begin by defining two classes, deriving from :cpp:class:`SubDomain`
48 for later use when specifying domains for the boundary conditions.
49
50 .. code-block:: cpp
51
52 // Sub domain for clamp at left end
53 class Left : public SubDomain
54 {
55 bool inside(const Array<double>& x, bool on_boundary) const
56 {
57 return (std::abs(x[0]) < DOLFIN_EPS) && on_boundary;
58 }
59 };
60
61 // Sub domain for rotation at right end
62 class Right : public SubDomain
63 {
64 bool inside(const Array<double>& x, bool on_boundary) const
65 {
66 return (std::abs(x[0] - 1.0) < DOLFIN_EPS) && on_boundary;
67 }
68 };
69
70 We also define two classes, deriving from :cpp:class:`Expression`, for
71 later use when specifying values for the boundary conditions.
72
73 .. code-block:: cpp
74
75 // Dirichlet boundary condition for clamp at left end
76 class Clamp : public Expression
77 {
78 public:
79
80 Clamp() : Expression(3) {}
81
82 void eval(Array<double>& values, const Array<double>& x) const
83 {
84 values[0] = 0.0;
85 values[1] = 0.0;
86 values[2] = 0.0;
87 }
88
89 };
90
91 // Dirichlet boundary condition for rotation at right end
92 class Rotation : public Expression
93 {
94 public:
95
96 Rotation() : Expression(3) {}
97
98 void eval(Array<double>& values, const Array<double>& x) const
99 {
100 const double scale = 0.5;
101
102 // Center of rotation
103 const double y0 = 0.5;
104 const double z0 = 0.5;
105
106 // Large angle of rotation (60 degrees)
107 double theta = 1.04719755;
108
109 // New coordinates
110 double y = y0 + (x[1] - y0)*cos(theta) - (x[2] - z0)*sin(theta);
111 double z = z0 + (x[1] - y0)*sin(theta) + (x[2] - z0)*cos(theta);
112
113 // Rotate at right end
114 values[0] = 0.0;
115 values[1] = scale*(y - x[1]);
116 values[2] = scale*(z - x[2]);
117 }
118 };
119
120 Next:
121
122 .. code-block:: cpp
123
124 int main()
125 {
126
127 Inside the ``main`` function, we begin by defining a tetrahedral mesh
128 of the domain and the function space on this mesh. Here, we choose to
129 create a unit cube mesh with 25 ( = 24 + 1) verices in one direction
130 and 17 ( = 16 + 1) vertices in the other two directions. With this
131 mesh, we initialize the (finite element) function space defined by the
132 generated code.
133
134 .. code-block:: cpp
135
136 // Create mesh and define function space
137 auto mesh = std::make_shared<UnitCubeMesh>(24, 16, 16);
138 auto V = std::make_shared<HyperElasticity::FunctionSpace>(mesh);
139
140 Now, the Dirichlet boundary conditions can be created using the class
141 :cpp:class:`DirichletBC`, the previously initialized
142 :cpp:class:`FunctionSpace` ``V`` and instances of the previously
143 listed classes ``Left`` (for the left boundary) and ``Right`` (for the
144 right boundary), and ``Clamp`` (for the value on the left boundary)
145 and ``Rotation`` (for the value on the right boundary).
146
147 .. code-block:: cpp
148
149 // Define Dirichlet boundaries
150 auto left = std::make_shared<Left>();
151 auto right = std::make_shared<Right>();
152
153 // Define Dirichlet boundary functions
154 auto c = std::make_shared<Clamp>();
155 auto r = std::make_shared<Rotation>();
156
157 // Create Dirichlet boundary conditions
158 DirichletBC bcl(V, c, left);
159 DirichletBC bcr(V, r, right);
160 std::vector<const DirichletBC*> bcs = {{&bcl, &bcr}};
161
162 The two boundary conditions are collected in the container ``bcs``.
163
164 We use two instances of the class :cpp:class:`Constant` to define the
165 source ``B`` and the traction ``T``.
166
167 .. code-block:: cpp
168
169 // Define source and boundary traction functions
170 auto B = std::make_shared<Constant>(0.0, -0.5, 0.0);
171 auto T = std::make_shared<Constant>(0.1, 0.0, 0.0);
172
173 The solution for the displacement will be an instance of the class
174 :cpp:class:`Function`, living in the function space ``V``; we define
175 it here:
176
177 .. code-block:: cpp
178
179 // Define solution function
180 auto u = std::make_shared<Function>(V);
181
182 Next, we set the material parameters
183
184 .. code-block:: cpp
185
186 // Set material parameters
187 const double E = 10.0;
188 const double nu = 0.3;
189 auto mu = std::make_shared<Constant>(E/(2*(1 + nu)));
190 auto lambda = std::make_shared<Constant>(E*nu/((1 + nu)*(1 - 2*nu)));
191
192 Now, we can initialize the bilinear and linear forms (``a``, ``L``)
193 using the previously defined :cpp:class:`FunctionSpace` ``V``. We
194 attach the material parameters and previously initialized functions to
195 the forms.
196
197 .. code-block:: cpp
198
199 // Create (linear) form defining (nonlinear) variational problem
200 HyperElasticity::ResidualForm F(V);
201 F.mu = mu; F.lmbda = lambda; F.u = u;
202 F.B = B; F.T = T;
203
204 // Create Jacobian dF = F' (for use in nonlinear solver).
205 HyperElasticity::JacobianForm J(V, V);
206 J.mu = mu; J.lmbda = lambda; J.u = u;
207
208 Now, we have specified the variational forms and can consider the
209 solution of the variational problem.
210
211 .. code-block:: cpp
212
213 // Solve nonlinear variational problem F(u; v) = 0
214 solve(F == 0, *u, bcs, J);
215
216 Finally, the solution ``u`` is saved to a file named
217 ``displacement.pvd`` in VTK format.
218
219 .. code-block:: cpp
220
221 // Save solution in VTK format
222 File file("displacement.pvd");
223 file << *u;
224
225 return 0;
226 }