Debian Codegraph @

demo/undocumented/elastodynamics/cpp/main.cpp

raw
1 // Copyright (C) 2009 Mirko Maraldi and Garth N. Wells
2 //
3 // This file is part of DOLFIN.
4 //
5 // DOLFIN is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU Lesser General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // DOLFIN is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU Lesser General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public License
16 // along with DOLFIN. If not, see <http://www.gnu.org/licenses/>.
17 //
18 // Modified by Anders Logg, 2011
19 //
20 // First added: 2009-01-22
21 // Last changed: 2011-06-28
22
23 #include <dolfin.h>
24 #include "ElastoDynamics.h"
25 #include "DG0_eps_xx.h"
26
27 using namespace dolfin;
28
29 // External load
30 class Pressure : public Expression
31 {
32 public:
33
34 Pressure(const double& t, const double& dt, bool old)
35 : Expression(2), t(t), dt(dt), old(old) {}
36
37 void eval(Array<double>& values, const Array<double>& x) const
38 {
39 double time = t;
40 if(old && time > 0.0)
41 time -= dt;
42
43 const double cutoff_time = 10.0*1.0/32.0;
44 if (time < cutoff_time)
45 {
46 values[0] = 1.0*time/cutoff_time;
47 values[1] = 0.0;
48 }
49 else
50 {
51 values[0] = 1.0;
52 values[1] = 0.0;
53 }
54 }
55
56 private:
57
58 const double& t;
59 const double& dt;
60 const bool old;
61 };
62
63 // Right boundary
64 class RightBoundary : public SubDomain
65 {
66 bool inside(const Array<double>& x, bool on_boundary) const
67 {
68 if (1.0 - x[0] < DOLFIN_EPS && on_boundary)
69 return true;
70 else
71 return false;
72 }
73 };
74
75 class LeftBoundary : public SubDomain
76 {
77 bool inside(const Array<double>& x, bool on_boundary) const
78 {
79 if (x[0] < DOLFIN_EPS)
80 return true;
81 else
82 return false;
83 }
84 };
85
86 // Acceleration update
87 void update_a(Function& a, const Function& u, const Function& a0,
88 const Function& v0, const Function& u0,
89 double beta, double dt)
90 {
91 // a = 1/(2*beta)*((u - u0 - v0*dt)/(0.5*dt*dt) - (1-2*beta)*a0)
92 *a.vector() = *u.vector();
93 *a.vector() -= *u0.vector();
94 *a.vector() *= 1.0/dt;
95 *a.vector() -= *v0.vector();
96 *a.vector() *= 1.0/((0.5-beta)*dt);
97 *a.vector() -= *a0.vector();
98 *a.vector() *= (0.5-beta)/beta;
99 }
100
101 // Velocity update
102 void update_v(Function& v, const Function& a, const Function& a0,
103 const Function& v0, double gamma, double dt)
104 {
105 // v = dt * ((1-gamma)*a0 + gamma*a) + v0
106 *v.vector() = *a0.vector();
107 *v.vector() *= (1.0-gamma)/gamma;
108 *v.vector() += *a.vector();
109 *v.vector() *= dt*gamma;
110 *v.vector() += *v0.vector();
111 }
112
113 int main(int argc, char* argv[])
114 {
115 init(argc, argv);
116
117 // Create mesh
118 auto mesh = std::make_shared<Mesh>("../dolfin_fine.xml.gz");
119
120 // Create function space
121 auto V = std::make_shared<ElastoDynamics::FunctionSpace>(mesh);
122
123 // Material parameters
124 auto rho = std::make_shared<Constant>(1.0); // mass density
125 auto eta = std::make_shared<Constant>(0.25); // damping coefficient
126 double E = 1.0; // Youngs modulus
127 double nu = 0.0; // Poisson ratio
128 auto lambda = std::make_shared<Constant>((nu*E)/((1.0 + nu)*(1.0 - 2.0 * nu))); // Lame coefficient
129 auto mu = std::make_shared<Constant>(E/(2.0*(1.0 + nu))); // Lame coefficient
130
131 // Time stepping parameters
132 auto alpha_m = std::make_shared<Constant>(0.2);
133 auto alpha_f = std::make_shared<Constant>(0.4);
134 auto beta = std::make_shared<Constant>(0.36);
135 auto gamma = std::make_shared<Constant>(0.7);
136 auto dt = std::make_shared<Constant>(1.0/32.0); // time step
137 double t = 0.0; // initial time
138 double T = 200*(*dt); // final time
139
140 // Body force
141 auto f = std::make_shared<Constant>(0.0, 0.0);
142
143 // External load
144 RightBoundary right_boundary;
145 auto right_boundary_function
146 = std::make_shared<MeshFunction<std::size_t>>(mesh, 1);
147 right_boundary.mark(*right_boundary_function, 3);
148 auto p = std::make_shared<Pressure>(t, *dt, false);
149 auto p0 = std::make_shared<Pressure>(t, *dt, true);
150
151 // Dirichlet boundary conditions
152 auto left_boundary = std::make_shared<LeftBoundary>();
153 auto zero = std::make_shared<Constant>(0.0, 0.0);
154 DirichletBC bc(V, zero, left_boundary);
155
156 // Define solution vectors
157 auto u = std::make_shared<Function>(V); // displacement
158 auto u0 = std::make_shared<Function>(V);
159 Function v(V); // velocity
160 auto v0 = std::make_shared<Function>(V);
161 Function a(V); // acceleration
162 auto a0 = std::make_shared<Function>(V);
163
164 // Set initial conditions and initialise acceleration function
165 u0->vector()->zero();
166 v0->vector()->zero();
167 a0->vector()->zero();
168
169 // Create forms
170 ElastoDynamics::BilinearForm a_form(V, V);
171 ElastoDynamics::LinearForm L(V);
172
173 // Attach functions
174 a_form.lmbda = lambda; a_form.mu = mu; a_form.rho = rho;
175 a_form.eta = eta;
176 a_form.beta = beta;
177 a_form.gamma = gamma;
178 a_form.dt = dt;
179 a_form.alpha_m = alpha_m;
180 a_form.alpha_f = alpha_f;
181
182 L.lmbda = lambda; L.mu = mu; L.rho = rho;
183 L.eta = eta;
184 L.beta = beta;
185 L.gamma = gamma;
186 L.dt = dt;
187 L.alpha_m = alpha_m;
188 L.alpha_f = alpha_f;
189 L.u0 = u0; L.v0 = v0; L.a0 = a0;
190 L.f = f;
191 L.p = p;
192 L.p0 = p0;
193
194 // Attach subdomains
195 a_form.ds = right_boundary_function;
196 L.ds = right_boundary_function;
197
198 // Create projection to compute the normal strain eps_xx
199 auto Vdg = std::make_shared<DG0_eps_xx::FunctionSpace>(mesh);
200 DG0_eps_xx::BilinearForm a_eps(Vdg, Vdg);
201 DG0_eps_xx::LinearForm L_eps(Vdg);
202 L_eps.u = u;
203 Function eps_xx(Vdg);
204
205 // Create output files
206 File file_u("u.pvd", "compressed");
207 File file_eps("eps_xx.pvd", "compressed");
208
209 // Start time stepping
210 std::size_t step = 0;
211 while (t < T)
212 {
213 // Update for next time step
214 t += *dt;
215 cout << "Time: " << t << endl;
216
217 // Solve
218 solve(a_form == L, *u, {&bc});
219 solve(a_eps == L_eps, eps_xx);
220
221 // Update velocity and acceleration
222 update_a(a, *u, *a0, *v0, *u0, *beta, *dt);
223 update_v(v, a, *a0, *v0, *gamma, *dt);
224 *u0 = *u; *v0 = v; *a0 = a;
225
226 // Save solutions to file
227 if (step % 2 == 0)
228 {
229 file_u << *u;
230 file_eps << eps_xx;
231 }
232 ++step;
233 }
234
235 return 0;
236 }