Debian Codegraph @

python/demo/undocumented/contact-vi-tao/demo_contact-vi-tao.py

raw
1 """This demo uses PETSc's TAO solver for nonlinear (bound-constrained)
2 optimisation problems to solve a contact mechanics problem in FEniCS.
3 We consider here a heavy hyperelastic circle under body force
4 in a box of the same size."""
5
6 # Copyright (C) 2012 Corrado Maurini
7 #
8 # This file is part of DOLFIN.
9 #
10 # DOLFIN is free software: you can redistribute it and/or modify
11 # it under the terms of the GNU Lesser General Public License as published by
12 # the Free Software Foundation, either version 3 of the License, or
13 # (at your option) any later version.
14 #
15 # DOLFIN is distributed in the hope that it will be useful,
16 # but WITHOUT ANY WARRANTY; without even the implied warranty of
17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 # GNU Lesser General Public License for more details.
19 #
20 # You should have received a copy of the GNU Lesser General Public License
21 # along with DOLFIN. If not, see <http://www.gnu.org/licenses/>.
22 #
23 # Modified by Tianyi Li 2014
24 #
25 # First added: 2012-09-03
26 # Last changed: 2014-07-19
27
28
29 from dolfin import *
30 import matplotlib.pyplot as plt
31
32
33 if not has_petsc():
34 print("DOLFIN must be compiled with PETSc to run this demo.")
35 exit(0)
36
37 # Read mesh
38 mesh = Mesh("../circle_yplane.xml.gz")
39
40 # Create function space
41 V = VectorFunctionSpace(mesh, "Lagrange", 1)
42
43 # Define functions
44 du = TrialFunction(V) # Incremental displacement
45 v = TestFunction(V) # Test function
46 u = Function(V) # Displacement from previous iteration
47 B = Constant((0.0, -1.5)) # Body force per unit volume
48
49 # Kinematics
50 I = Identity(len(u)) # Identity tensor
51 F = I + grad(u) # Deformation gradient
52 C = F.T*F # Right Cauchy-Green tensor
53
54 # Invariants of deformation tensors
55 Ic = tr(C)
56 J = det(F)
57
58 # Elasticity parameters
59 E, nu = 10.0, 0.3
60 mu, lmbda = Constant(E/(2*(1+nu))), Constant(E*nu/((1+nu)*(1-2*nu)))
61
62 # Stored strain energy density (compressible neo-Hookean model)
63 psi = (mu/2)*(Ic-2) - mu*ln(J) + (lmbda/2)*(ln(J))**2
64
65 # Total potential energy
66 Pi = psi*dx - dot(B, u)*dx
67
68 # Compute first variation of Pi (directional derivative about u in the
69 # direction of v)
70 F = derivative(Pi, u, v)
71
72 # Compute Jacobian of F
73 J = derivative(F, u, du)
74
75 # Define the minimisation problem by using OptimisationProblem class
76 class ContactProblem(OptimisationProblem):
77
78 def __init__(self):
79 OptimisationProblem.__init__(self)
80
81 # Objective function
82 def f(self, x):
83 u.vector()[:] = x
84 return assemble(Pi)
85
86 # Gradient of the objective function
87 def F(self, b, x):
88 u.vector()[:] = x
89 assemble(F, tensor=b)
90
91 # Hessian of the objective function
92 def J(self, A, x):
93 u.vector()[:] = x
94 assemble(J, tensor=A)
95
96 # The displacement u must be such that the current configuration
97 # doesn't escape the box [xmin, xmax] x [ymin, ymax]
98 constraint_u = Expression(("xmax-x[0]", "ymax-x[1]"), xmax=1.0, ymax=1.0, degree=1)
99 constraint_l = Expression(("xmin-x[0]", "ymin-x[1]"), xmin=-1.0, ymin=-1.0, degree=1)
100 u_min = interpolate(constraint_l, V)
101 u_max = interpolate(constraint_u, V)
102
103 # Symmetry condition (to block rigid body rotations)
104 def symmetry_line(x, on_boundary):
105 return near(x[0], 0)
106 bc = DirichletBC(V.sub(0), Constant(0.0), symmetry_line)
107 bc.apply(u_min.vector()) # BC will be incorporated into the lower
108 bc.apply(u_max.vector()) # and upper bounds
109
110 # Create the PETScTAOSolver
111 solver = PETScTAOSolver()
112
113 # Set some parameters
114 solver.parameters["method"] = "tron" # when using gpcg make sure that you have a constant Hessian
115 # solver.parameters["linear_solver"] = "nash"
116 solver.parameters["line_search"] = "gpcg"
117 # solver.parameters["preconditioner"] = "ml_amg"
118 solver.parameters["monitor_convergence"] = True
119 solver.parameters["report"] = True
120
121 # Uncomment this line to see the available parameters
122 # info(parameters, True)
123
124 # Parse (PETSc) parameters
125 parameters.parse()
126
127 # Solve the problem
128 solver.solve(ContactProblem(), u.vector(), u_min.vector(), u_max.vector())
129
130 # Save solution in XDMF format if available
131 out = XDMFFile(mesh.mpi_comm(), "u.xdmf")
132 if has_hdf5():
133 out.write(u)
134 elif MPI.size(mesh.mpi_comm()) == 1:
135 encoding = XDMFFile.Encoding.ASCII
136 out.write(u, encoding)
137 else:
138 # Save solution in vtk format
139 out = File("u.pvd")
140 out << u
141
142 # Plot the current configuration
143 plot(u, mode="displacement", wireframe=True, title="Displacement field")
144 plt.show()