| 1 |
"""This demo program uses the interface to SNES solver for variational |
| 2 |
inequalities to solve a contact mechanics problems in FEniCS. The |
| 3 |
example considers a heavy hyperelastic circle in a box of the same |
| 4 |
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 Corrado Maurini 2013 |
| 24 |
|
| 25 |
from dolfin import * |
| 26 |
import matplotlib.pyplot as plt |
| 27 |
|
| 28 |
|
| 29 |
# This demo requires PETSc |
| 30 |
if not has_petsc(): |
| 31 |
print("DOLFIN must be compiled with PETSc to run this demo.") |
| 32 |
exit(0) |
| 33 |
|
| 34 |
# Create mesh |
| 35 |
mesh = Mesh("../circle_yplane.xml.gz") |
| 36 |
|
| 37 |
V = VectorFunctionSpace(mesh, "Lagrange", 1) |
| 38 |
|
| 39 |
# Define functions |
| 40 |
du = TrialFunction(V) # Incremental displacement |
| 41 |
v = TestFunction(V) # Test function |
| 42 |
u = Function(V) # Displacement from previous iteration |
| 43 |
B = Constant((0.0, -0.05)) # Body force per unit volume |
| 44 |
|
| 45 |
# Kinematics |
| 46 |
I = Identity(len(u)) # Identity tensor |
| 47 |
F = I + grad(u) # Deformation gradient |
| 48 |
C = F.T*F # Right Cauchy-Green tensor |
| 49 |
|
| 50 |
# Invariants of deformation tensors |
| 51 |
Ic = tr(C) |
| 52 |
J = det(F) |
| 53 |
|
| 54 |
# Elasticity parameters |
| 55 |
E, nu = 10.0, 0.3 |
| 56 |
mu, lmbda = Constant(E/(2*(1 + nu))), Constant(E*nu/((1 + nu)*(1 - 2*nu))) |
| 57 |
|
| 58 |
# Stored strain energy density (compressible neo-Hookean model) |
| 59 |
psi = (mu/2)*(Ic - 2) - mu*ln(J) + (lmbda/2)*(ln(J))**2 |
| 60 |
|
| 61 |
# Total potential energy |
| 62 |
Pi = psi*dx - dot(B, u)*dx |
| 63 |
|
| 64 |
# Compute first variation of Pi (directional derivative about u in the |
| 65 |
# direction of v) |
| 66 |
F = derivative(Pi, u, v) |
| 67 |
|
| 68 |
# Compute Jacobian of F |
| 69 |
J = derivative(F, u, du) |
| 70 |
|
| 71 |
# Symmetry condition (to block rigid body rotations) |
| 72 |
tol = mesh.hmin() |
| 73 |
def symmetry_line(x): |
| 74 |
return abs(x[0]) < DOLFIN_EPS |
| 75 |
bc = DirichletBC(V.sub(0), 0., symmetry_line, method="pointwise") |
| 76 |
|
| 77 |
# The displacement u must be such that the current configuration x+u |
| 78 |
# remains in the box [xmin,xmax] x [umin,ymax] |
| 79 |
constraint_u = Expression(("xmax - x[0]","ymax - x[1]"), |
| 80 |
xmax=1.0+DOLFIN_EPS, ymax=1.0, degree=1) |
| 81 |
constraint_l = Expression(("xmin - x[0]","ymin - x[1]"), |
| 82 |
xmin=-1.0-DOLFIN_EPS, ymin=-1.0, degree=1) |
| 83 |
umin = interpolate(constraint_l, V) |
| 84 |
umax = interpolate(constraint_u, V) |
| 85 |
|
| 86 |
# Define the solver parameters |
| 87 |
snes_solver_parameters = {"nonlinear_solver": "snes", |
| 88 |
"snes_solver": {"linear_solver": "lu", |
| 89 |
"maximum_iterations": 20, |
| 90 |
"report": True, |
| 91 |
"error_on_nonconvergence": False}} |
| 92 |
|
| 93 |
# Set up the non-linear problem |
| 94 |
problem = NonlinearVariationalProblem(F, u, bc, J=J) |
| 95 |
problem.set_bounds(umin, umax) |
| 96 |
|
| 97 |
# Set up the non-linear solver |
| 98 |
solver = NonlinearVariationalSolver(problem) |
| 99 |
solver.parameters.update(snes_solver_parameters) |
| 100 |
info(solver.parameters, True) |
| 101 |
|
| 102 |
# Solve the problem |
| 103 |
(iter, converged) = solver.solve() |
| 104 |
|
| 105 |
# Check for convergence |
| 106 |
if not converged: |
| 107 |
warning("This demo is a complex nonlinear problem. Convergence is not guaranteed when modifying some parameters or using PETSC 3.2.") |
| 108 |
|
| 109 |
# Save solution in VTK format |
| 110 |
file = File("displacement.pvd") |
| 111 |
file << u |
| 112 |
|
| 113 |
# plot the current configuration |
| 114 |
plot(u, mode="displacement", wireframe=True, title="Displacement field") |
| 115 |
plt.show() |