Debian Codegraph @

demo/undocumented/contact-vi-tao/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 //
20 // First added: 2012-09-03
21 // Last changed: 2013-04-15
22 //
23 // This demo program uses of the interface to TAO solver for
24 // variational inequalities to solve a contact mechanics problem in
25 // FEnics. The example considers a heavy elastic circle in a box of
26 // the same diameter
27
28 #include <dolfin.h>
29 #include "Elasticity.h"
30
31 using namespace dolfin;
32
33 // Lower bound for displacement
34 class LowerBound : public Expression
35 {
36 public:
37 LowerBound() : Expression(2) {}
38 void eval(Array<double>& values, const Array<double>& x) const
39 {
40 const double xmin = -1.0 - DOLFIN_EPS;
41 const double ymin = -1.0;
42 values[0] = xmin - x[0];
43 values[1] = ymin - x[1];
44 }
45 };
46
47 // Upper bound for displacement
48 class UpperBound : public Expression
49 {
50 public:
51 UpperBound() : Expression(2) {}
52 void eval(Array<double>& values, const Array<double>& x) const
53 {
54 const double xmax = 1.0 + DOLFIN_EPS;
55 const double ymax = 1.0;
56 values[0] = xmax-x[0];
57 values[1] = ymax-x[1];
58 }
59 };
60
61 int main()
62 {
63 #ifdef HAS_PETSC
64
65 // Read mesh
66 auto mesh = std::make_shared<Mesh>("../circle_yplane.xml.gz");
67
68 // Create function space
69 auto V = std::make_shared<Elasticity::FunctionSpace>(mesh);
70
71 // Create right-hand side
72 auto f = std::make_shared<Constant>(0.0, -0.1);
73
74 // Set elasticity parameters
75 double E = 10.0;
76 double nu = 0.3;
77 auto mu = std::make_shared<Constant>(E / (2*(1 + nu)));
78 auto lambda = std::make_shared<Constant>(E*nu / ((1 + nu)*(1 - 2*nu)));
79
80 // Define variational problem
81 Elasticity::BilinearForm a(V, V);
82 a.mu = mu; a.lmbda = lambda;
83 Elasticity::LinearForm L(V);
84 L.f = f;
85 Function usol(V);
86 Function Ulower(V); // Create a function
87 Function Uupper(V); // Create a function
88
89 // Assemble the matrix and vector for linear elasticity
90 PETScMatrix A;
91 PETScVector b;
92 assemble(A, a);
93 assemble(b, L);
94
95 // Interpolate expression for Upper bound
96 UpperBound xu_exp;
97 Function xu_f(V);
98 xu_f.interpolate(xu_exp);
99
100 // Interpolate expression for Upper bound
101 LowerBound xl_exp;
102 Function xl_f(V);
103 xl_f.interpolate(xl_exp);
104
105 // Solve the problem with the TAO Solver
106 TAOLinearBoundSolver TAOSolver("tron", "cg");
107
108 // Set some parameters
109 TAOSolver.parameters["monitor_convergence"] = true;
110 TAOSolver.parameters["report"] = true;
111 TAOSolver.parameters("krylov_solver")["monitor_convergence"] = false;
112
113 // Solve the problem
114 TAOSolver.solve(A, *usol.vector(), b, *xl_f.vector(), *xu_f.vector());
115
116 XDMFFile("u.xdmf").write(usol);
117
118 #else
119
120 cout << "This demo requires DOLFIN to be configured with PETSc version 3.6 or later" << endl;
121
122 #endif
123
124 return 0;
125 }