Debian Codegraph @

demo/undocumented/elasticity/cpp/main.cpp

raw
1 // Copyright (C) 2014 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 // This demo solves the equations of static linear elasticity for a
19 // pulley subjected to centripetal accelerations. The solver uses
20 // smoothed aggregation algerbaric multigri
21
22 #include <dolfin.h>
23 #include "Elasticity.h"
24
25 using namespace dolfin;
26
27 // Function to compute the near nullspace for elasticity - it is made
28 // up of the six rigid body modes
29 dolfin::VectorSpaceBasis build_near_nullspace(const dolfin::FunctionSpace& V,
30 const GenericVector& x)
31 {
32 // Get subspaces
33 auto V0 = V.sub(0);
34 auto V1 = V.sub(1);
35 auto V2 = V.sub(2);
36
37 // Create vectors for nullspace basis
38 std::vector<std::shared_ptr<dolfin::GenericVector>> basis(6);
39 for (std::size_t i = 0; i < basis.size(); ++i)
40 basis[i] = x.copy();
41
42 // x0, x1, x2 translations
43 V0->dofmap()->set(*basis[0], 1.0);
44 V1->dofmap()->set(*basis[1], 1.0);
45 V2->dofmap()->set(*basis[2], 1.0);
46
47 // Rotations
48 V0->set_x(*basis[3], -1.0, 1);
49 V1->set_x(*basis[3], 1.0, 0);
50
51 V0->set_x(*basis[4], 1.0, 2);
52 V2->set_x(*basis[4], -1.0, 0);
53
54 V2->set_x(*basis[5], 1.0, 1);
55 V1->set_x(*basis[5], -1.0, 2);
56
57 // Apply
58 for (std::size_t i = 0; i < basis.size(); ++i)
59 basis[i]->apply("add");
60
61 // Create vector space and orthonormalize
62 VectorSpaceBasis vector_space(basis);
63 vector_space.orthonormalize();
64 return vector_space;
65 }
66
67
68 int main()
69 {
70 #ifdef HAS_PETSC
71
72 // Set backend to PETSC
73 parameters["linear_algebra_backend"] = "PETSc";
74
75 // Inner surface subdomain
76 class InnerSurface : public SubDomain
77 {
78 bool inside(const Array<double>& x, bool on_boundary) const
79 {
80 const double r = 3.75 - x[2]*0.17;
81 return (x[0]*x[0] + x[1]*x[1]) < r*r and on_boundary;
82 }
83 };
84
85 // Centripetal loading function
86 class CentripetalLoading : public Expression
87 {
88 public:
89
90 CentripetalLoading() : Expression(3) {}
91 void eval(Array<double>& values, const Array<double>& x) const
92 {
93 const double omega = 300.0;
94 const double rho = 10.0;
95 values[0] = rho*omega*omega*x[0];;
96 values[1] = rho*omega*omega*x[1];
97 values[2] = 0.0;
98 }
99 };
100
101 // Read mesh
102 auto mesh = std::make_shared<Mesh>();
103 XDMFFile file("../pulley.xdmf");
104 file.read(*mesh);
105
106 // Create right-hand side loading function
107 auto f = std::make_shared<CentripetalLoading>();
108
109 // Set elasticity parameters
110 double E = 10.0;
111 double nu = 0.3;
112 auto mu = std::make_shared<Constant>(E/(2.0*(1.0 + nu)));
113 auto lambda = std::make_shared<Constant>(E*nu/((1.0 + nu)*(1.0 - 2.0*nu)));
114
115 // Create function space
116 auto V = std::make_shared<Elasticity::Form_a::TestSpace>(mesh);
117
118 // Define variational problem
119 Elasticity::Form_a a(V, V);
120 a.mu = mu; a.lmbda = lambda;
121 Elasticity::Form_L L(V);
122 L.f = f;
123
124 // Set up boundary condition on inner surface
125 auto inner_surface = std::make_shared<InnerSurface>();
126 auto zero = std::make_shared<Constant>(0.0, 0.0, 0.0);
127 auto bc = std::make_shared<DirichletBC>(V, zero, inner_surface);
128
129 // Assemble system, applying boundary conditions and preserving
130 // symmetry)
131 PETScMatrix A;
132 PETScVector b;
133 assemble_system(A, b, a, L, {bc});
134
135 // Create solution function
136 auto u = std::make_shared<Function>(V);
137
138 // Create near null space basis (required for smoothed aggregation
139 // AMG). The solution vector is passed so that it can be copied to
140 // generate compatible vectors for the nullspace.
141 VectorSpaceBasis near_null_space = build_near_nullspace(*V, *u->vector());
142
143 // Attach near nullspace to matrix
144 A.set_near_nullspace(near_null_space);
145
146 // Create PETSc smoothed aggregation AMG preconditioner
147 auto pc = std::make_shared<PETScPreconditioner>("petsc_amg");
148
149 // Use Chebyshev smoothing for multigrid
150 PETScOptions::set("mg_levels_ksp_type", "chebyshev");
151 PETScOptions::set("mg_levels_pc_type", "jacobi");
152
153 // Improve estimate of eigenvalues for Chebyshev smoothing
154 PETScOptions::set("mg_levels_esteig_ksp_type", "cg");
155 PETScOptions::set("mg_levels_ksp_chebyshev_esteig_steps", 50);
156
157 // Create CG PETSc linear solver and turn on convergence monitor
158 PETScKrylovSolver solver("cg", pc);
159 solver.parameters["monitor_convergence"] = true;
160
161 // Solve
162 solver.solve(A, *(u->vector()), b);
163
164 // Extract solution components (deep copy)
165 Function ux = (*u)[0];
166 Function uy = (*u)[1];
167 Function uz = (*u)[2];
168 std::cout << "Norm (u vector): " << u->vector()->norm("l2") << std::endl;
169 std::cout << "Norm (ux, uy, uz): " << ux.vector()->norm("l2") << " "
170 << uy.vector()->norm("l2") << " "
171 << uz.vector()->norm("l2") << std::endl;
172
173 // Save solution in VTK format
174 File vtk_file("elasticity.pvd", "compressed");
175 vtk_file << *u;
176
177 // Extract stress and write in VTK format
178 auto W = std::make_shared<Elasticity::Form_a_s::TestSpace>(mesh);
179 Elasticity::Form_a_s a_s(W, W);
180 Elasticity::Form_L_s L_s(W);
181 L_s.mu = mu;
182 L_s.lmbda = lambda;
183 L_s.disp = u;
184
185 // Compute stress for visualisation
186 Function stress(W);
187 LocalSolver local_solver(std::shared_ptr<Form>(&a_s, NoDeleter()),
188 std::shared_ptr<Form>(&L_s, NoDeleter()),
189 LocalSolver::SolverType::Cholesky);
190 local_solver.solve_local_rhs(stress);
191
192 File file_stress("stress.pvd");
193 file_stress << stress;
194
195 // Save colored mesh paritions in VTK format if running in parallel
196 if (dolfin::MPI::size(mesh->mpi_comm()) > 1)
197 {
198 MeshFunction<std::size_t>
199 partitions(mesh, mesh->topology().dim(), dolfin::MPI::rank(mesh->mpi_comm()));
200 File file("partitions.pvd");
201 file << partitions;
202 }
203
204 // Displace mesh and write
205 ALE::move(*mesh, *u);
206 XDMFFile("deformed_mesh.xdmf").write(*mesh);
207
208 #else
209 dolfin::cout << "DOLFIN must be configured with PETSc to run this demo."
210 << dolfin::endl;
211 #endif
212
213 return 0;
214 }