Debian Codegraph @

python/demo/undocumented/buckling-tao/demo_buckling-tao.py

raw
1 """This demo uses PETSc's TAO solver for nonlinear (bound-constrained)
2 optimisation problems to solve a buckling problem in FEniCS.
3 We consider here a hyperelastic beam constrained in a box
4 under axial compression.
5
6 The box is designed such that this beam will lose stability and move
7 upwards (and not downwards) in order to minimise the potential energy."""
8
9 # Copyright (C) 2014 Tianyi Li
10 #
11 # This file is part of DOLFIN.
12 #
13 # DOLFIN is free software: you can redistribute it and/or modify
14 # it under the terms of the GNU Lesser General Public License as published by
15 # the Free Software Foundation, either version 3 of the License, or
16 # (at your option) any later version.
17 #
18 # DOLFIN is distributed in the hope that it will be useful,
19 # but WITHOUT ANY WARRANTY; without even the implied warranty of
20 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 # GNU Lesser General Public License for more details.
22 #
23 # You should have received a copy of the GNU Lesser General Public License
24 # along with DOLFIN. If not, see <http://www.gnu.org/licenses/>.
25 #
26 # First added: 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 at least with PETSc 3.6 to run this demo.")
35 exit(0)
36
37 # Read mesh and refine once
38 mesh = Mesh("../buckling.xml.gz")
39 mesh = refine(mesh)
40
41 # Create function space
42 V = VectorFunctionSpace(mesh, "Lagrange", 1)
43
44 # Create solution, trial and test functions
45 u, du, v = Function(V), TrialFunction(V), TestFunction(V)
46
47 # Elasticity parameters
48 E, nu = 10.0, 0.3
49 mu = Constant(E/(2.0*(1.0+nu)))
50 lmbda = Constant(E*nu/((1.0+nu)*(1.0-2.0*nu)))
51
52 # Compressible neo-Hookean model
53 I = Identity(mesh.geometry().dim())
54 F = I + grad(u)
55 C = F.T*F
56 Ic = tr(C)
57 J = det(F)
58 psi = (mu/2)*(Ic-2)-mu*ln(J)+(lmbda/2)*(ln(J))**2
59
60 # Surface force
61 f = Constant((-0.08, 0.0))
62
63 # The displacement u must be such that the current configuration
64 # doesn't escape the box [xmin, xmax] x [ymin, ymax]
65 constraint_u = Expression(("xmax-x[0]", "ymax-x[1]"), xmax=10.0, ymax=2.0, degree=1)
66 constraint_l = Expression(("xmin-x[0]", "ymin-x[1]"), xmin=0.0, ymin=-0.2, degree=1)
67 u_min = interpolate(constraint_l, V)
68 u_max = interpolate(constraint_u, V)
69
70 # Symmetry condition (to block rigid body rotations)
71 class Left(SubDomain):
72 def inside(self, x, on_boundary):
73 return on_boundary and near(x[0], 0)
74 class Right(SubDomain):
75 def inside(self, x, on_boundary):
76 return on_boundary and near(x[0], 10)
77 boundaries = MeshFunction("size_t", mesh, mesh.topology().dim()-1)
78 boundaries.set_all(0)
79 left = Left()
80 left.mark(boundaries, 1)
81 right = Right()
82 right.mark(boundaries, 2)
83 ds = Measure('ds', domain=mesh, subdomain_data=boundaries)
84 bc = DirichletBC(V, Constant([0.0, 0.0]), boundaries, 1)
85 bc.apply(u_min.vector())
86 bc.apply(u_max.vector())
87
88 # Variational formulation
89 elastic_energy = psi*dx - dot(f, u)*ds(2)
90 grad_elastic_energy = derivative(elastic_energy, u, v)
91 H_elastic_energy = derivative(grad_elastic_energy, u, du)
92
93 # Define the minimisation problem by using OptimisationProblem class
94 class BucklingProblem(OptimisationProblem):
95
96 def __init__(self):
97 OptimisationProblem.__init__(self)
98
99 # Objective function
100 def f(self, x):
101 u.vector()[:] = x
102 return assemble(elastic_energy)
103
104 # Gradient of the objective function
105 def F(self, b, x):
106 u.vector()[:] = x
107 assemble(grad_elastic_energy, tensor=b)
108
109 # Hessian of the objective function
110 def J(self, A, x):
111 u.vector()[:] = x
112 assemble(H_elastic_energy, tensor=A)
113
114 # Create the PETScTAOSolver
115 solver = PETScTAOSolver()
116
117 # Set some parameters
118 solver.parameters["method"] = "tron"
119 solver.parameters["monitor_convergence"] = True
120 solver.parameters["report"] = True
121
122 # Uncomment this line to see the available parameters
123 # info(parameters, True)
124
125 # Parse (PETSc) parameters
126 parameters.parse()
127
128 # Solve the problem
129 solver.solve(BucklingProblem(), u.vector(), u_min.vector(), u_max.vector())
130
131 # Save solution in XDMF format if available
132 out = XDMFFile(mesh.mpi_comm(), "u.xdmf")
133 if has_hdf5():
134 out.write(u)
135 elif MPI.size(mesh.mpi_comm()) == 1:
136 encoding = XDMFFile.Encoding.ASCII
137 out.write(u, encoding)
138 else:
139 # Save solution in vtk format
140 out = File("u.pvd")
141 out << u
142
143 # Plot the current configuration
144 plot(u, mode="displacement", wireframe=True, title="Displacement field")
145 plt.show()