Debian Codegraph @

demo/undocumented/contact-vi-snes/cpp/main.cpp

raw
1 // Copyright (C) 2012 Corrado Maurini
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 Corrado Maurini 2013
19 // Modified by Johannes Ring 2013
20 //
21 // First added: 2012-09-03
22 // Last changed: 2013-11-21
23 //
24 // This demo program uses the PETSc nonlinear solver for variational
25 // inequalities to solve a contact mechanics problems in FEniCS. The
26 // example considers a heavy elastic circle in a box of the same size.
27
28 #include <dolfin.h>
29 #include "HyperElasticity.h"
30
31 using namespace dolfin;
32
33 // Sub domain for symmetry condition
34 class SymmetryLine : public SubDomain
35 {
36 bool inside(const Array<double>& x, bool on_boundary) const
37 { return (std::abs(x[0]) < DOLFIN_EPS); }
38 };
39
40 // Lower bound for displacement
41 class LowerBound : public Expression
42 {
43 public:
44 LowerBound() : Expression(2) {}
45 void eval(Array<double>& values, const Array<double>& x) const
46 {
47 const double xmin = -1.0 - DOLFIN_EPS;
48 const double ymin = -1.0;
49 values[0] = xmin - x[0];
50 values[1] = ymin - x[1];
51 }
52 };
53
54 // Upper bound for displacement
55 class UpperBound : public Expression
56 {
57 public:
58 UpperBound() : Expression(2) {}
59 void eval(Array<double>& values, const Array<double>& x) const
60 {
61 const double xmax = 1.0 + DOLFIN_EPS;
62 const double ymax = 2.0;
63 values[0] = xmax - x[0];
64 values[1] = ymax - x[1];
65 }
66 };
67
68 int main()
69 {
70 #ifdef HAS_PETSC
71
72 // Read mesh and create function space
73 auto mesh = std::make_shared<Mesh>("../circle_yplane.xml.gz");
74
75 // Create function space
76 auto V = std::make_shared<HyperElasticity::FunctionSpace>(mesh);
77
78 // Create Dirichlet boundary conditions
79 auto zero = std::make_shared<Constant>(0.0);
80 auto s = std::make_shared<SymmetryLine>();
81 auto bc = std::make_shared<DirichletBC>(V->sub(0), zero, s, "pointwise");
82
83 // Define source and boundary traction functions
84 auto B = std::make_shared<Constant>(0.0, -0.05);
85
86 // Define solution function
87 auto u = std::make_shared<Function>(V);
88
89 // Set material parameters
90 const double E = 10.0;
91 const double nu = 0.3;
92 auto mu = std::make_shared<Constant>(E/(2*(1 + nu)));
93 auto lambda = std::make_shared<Constant>(E*nu/((1.0 + nu)*(1.0 - 2.0*nu)));
94
95 // Create (linear) form defining (nonlinear) variational problem
96 auto F = std::make_shared<HyperElasticity::ResidualForm>(V);
97 F->mu = mu; F->lmbda = lambda; F->B = B; F->u = u;
98
99 // Create jacobian dF = F' (for use in nonlinear solver).
100 auto J = std::make_shared<HyperElasticity::JacobianForm>(V, V);
101 J->mu = mu; J->lmbda = lambda; J->u = u;
102
103 // Interpolate expression for upper bound
104 UpperBound umax_exp;
105 Function umax(V);
106 umax.interpolate(umax_exp);
107
108 // Interpolate expression for lower bound
109 LowerBound umin_exp;
110 Function umin(V);
111 umin.interpolate(umin_exp);
112
113 // Set up the non-linear problem
114 std::vector<std::shared_ptr<const DirichletBC>> bcs = {bc};
115 auto problem = std::make_shared<NonlinearVariationalProblem>(F, u, bcs, J);
116 problem->set_bounds(umin, umax);
117
118 // Set up the non-linear solver
119 NonlinearVariationalSolver solver(problem);
120 solver.parameters["nonlinear_solver"] = "snes";
121 solver.parameters("snes_solver")["linear_solver"] = "lu";
122 solver.parameters("snes_solver")["maximum_iterations"] = 20;
123 solver.parameters("snes_solver")["report"] = true;
124 solver.parameters("snes_solver")["error_on_nonconvergence"] = false;
125
126 // Solve the problems
127 std::pair<std::size_t, bool> out;
128 out = solver.solve();
129
130 // Check for convergence. Convergence is one modifies the loading
131 // and the mesh size
132 cout << out.second;
133 if (out.second != true)
134 {
135 warning("This demo is a complex nonlinear problem. Convergence is not guaranteed when modifying parameters.");
136 }
137
138 // Save solution in VTK format
139 File file("displacement.pvd");
140 file << *u;
141
142 #endif
143
144 return 0;
145 }