| 1 |
"""This demo solves the equations of static linear elasticity for a |
| 2 |
pulley subjected to centripetal accelerations. The solver uses |
| 3 |
smoothed aggregation algerbaric multigrid.""" |
| 4 |
|
| 5 |
# Copyright (C) 2014 Garth N. Wells |
| 6 |
# |
| 7 |
# This file is part of DOLFIN. |
| 8 |
# |
| 9 |
# DOLFIN is free software: you can redistribute it and/or modify |
| 10 |
# it under the terms of the GNU Lesser General Public License as published by |
| 11 |
# the Free Software Foundation, either version 3 of the License, or |
| 12 |
# (at your option) any later version. |
| 13 |
# |
| 14 |
# DOLFIN is distributed in the hope that it will be useful, |
| 15 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 |
# GNU Lesser General Public License for more details. |
| 18 |
# |
| 19 |
# You should have received a copy of the GNU Lesser General Public License |
| 20 |
# along with DOLFIN. If not, see <http://www.gnu.org/licenses/>. |
| 21 |
|
| 22 |
|
| 23 |
from dolfin import * |
| 24 |
import matplotlib.pyplot as plt |
| 25 |
|
| 26 |
|
| 27 |
# Test for PETSc |
| 28 |
if not has_linear_algebra_backend("PETSc"): |
| 29 |
print("DOLFIN has not been configured with PETSc. Exiting.") |
| 30 |
exit() |
| 31 |
|
| 32 |
# Set backend to PETSC |
| 33 |
parameters["linear_algebra_backend"] = "PETSc" |
| 34 |
|
| 35 |
def build_nullspace(V, x): |
| 36 |
"""Function to build null space for 3D elasticity""" |
| 37 |
|
| 38 |
# Create list of vectors for null space |
| 39 |
nullspace_basis = [x.copy() for i in range(6)] |
| 40 |
|
| 41 |
# Build translational null space basis |
| 42 |
V.sub(0).dofmap().set(nullspace_basis[0], 1.0); |
| 43 |
V.sub(1).dofmap().set(nullspace_basis[1], 1.0); |
| 44 |
V.sub(2).dofmap().set(nullspace_basis[2], 1.0); |
| 45 |
|
| 46 |
# Build rotational null space basis |
| 47 |
V.sub(0).set_x(nullspace_basis[3], -1.0, 1); |
| 48 |
V.sub(1).set_x(nullspace_basis[3], 1.0, 0); |
| 49 |
V.sub(0).set_x(nullspace_basis[4], 1.0, 2); |
| 50 |
V.sub(2).set_x(nullspace_basis[4], -1.0, 0); |
| 51 |
V.sub(2).set_x(nullspace_basis[5], 1.0, 1); |
| 52 |
V.sub(1).set_x(nullspace_basis[5], -1.0, 2); |
| 53 |
|
| 54 |
for x in nullspace_basis: |
| 55 |
x.apply("insert") |
| 56 |
|
| 57 |
# Create vector space basis and orthogonalize |
| 58 |
basis = VectorSpaceBasis(nullspace_basis) |
| 59 |
basis.orthonormalize() |
| 60 |
|
| 61 |
return basis |
| 62 |
|
| 63 |
|
| 64 |
# Load mesh from file |
| 65 |
mesh = Mesh() |
| 66 |
XDMFFile(MPI.comm_world, "../pulley.xdmf").read(mesh) |
| 67 |
|
| 68 |
# Function to mark inner surface of pulley |
| 69 |
def inner_surface(x, on_boundary): |
| 70 |
r = 3.75 - x[2]*0.17 |
| 71 |
return (x[0]*x[0] + x[1]*x[1]) < r*r and on_boundary |
| 72 |
|
| 73 |
# Rotation rate and mass density |
| 74 |
omega = 300.0 |
| 75 |
rho = 10.0 |
| 76 |
|
| 77 |
# Loading due to centripetal acceleration (rho*omega^2*x_i) |
| 78 |
f = Expression(("rho*omega*omega*x[0]", "rho*omega*omega*x[1]", "0.0"), |
| 79 |
omega=omega, rho=rho, degree=2) |
| 80 |
|
| 81 |
# Elasticity parameters |
| 82 |
E = 1.0e9 |
| 83 |
nu = 0.3 |
| 84 |
mu = E/(2.0*(1.0 + nu)) |
| 85 |
lmbda = E*nu/((1.0 + nu)*(1.0 - 2.0*nu)) |
| 86 |
|
| 87 |
# Stress computation |
| 88 |
def sigma(v): |
| 89 |
return 2.0*mu*sym(grad(v)) + lmbda*tr(sym(grad(v)))*Identity(len(v)) |
| 90 |
|
| 91 |
# Create function space |
| 92 |
V = VectorFunctionSpace(mesh, "Lagrange", 1) |
| 93 |
|
| 94 |
# Define variational problem |
| 95 |
u = TrialFunction(V) |
| 96 |
v = TestFunction(V) |
| 97 |
a = inner(sigma(u), grad(v))*dx |
| 98 |
L = inner(f, v)*dx |
| 99 |
|
| 100 |
# Set up boundary condition on inner surface |
| 101 |
c = Constant((0.0, 0.0, 0.0)) |
| 102 |
bc = DirichletBC(V, c, inner_surface) |
| 103 |
|
| 104 |
# Assemble system, applying boundary conditions and preserving |
| 105 |
# symmetry) |
| 106 |
A, b = assemble_system(a, L, bc) |
| 107 |
|
| 108 |
# Create solution function |
| 109 |
u = Function(V) |
| 110 |
|
| 111 |
# Create near null space basis (required for smoothed aggregation |
| 112 |
# AMG). The solution vector is passed so that it can be copied to |
| 113 |
# generate compatible vectors for the nullspace. |
| 114 |
null_space = build_nullspace(V, u.vector()) |
| 115 |
|
| 116 |
# Attach near nullspace to matrix |
| 117 |
as_backend_type(A).set_near_nullspace(null_space) |
| 118 |
|
| 119 |
# Create PETSC smoothed aggregation AMG preconditioner and attach near |
| 120 |
# null space |
| 121 |
pc = PETScPreconditioner("petsc_amg") |
| 122 |
|
| 123 |
# Use Chebyshev smoothing for multigrid |
| 124 |
PETScOptions.set("mg_levels_ksp_type", "chebyshev") |
| 125 |
PETScOptions.set("mg_levels_pc_type", "jacobi") |
| 126 |
|
| 127 |
# Improve estimate of eigenvalues for Chebyshev smoothing |
| 128 |
PETScOptions.set("mg_levels_esteig_ksp_type", "cg") |
| 129 |
PETScOptions.set("mg_levels_ksp_chebyshev_esteig_steps", 50) |
| 130 |
|
| 131 |
# Create CG Krylov solver and turn convergence monitoring on |
| 132 |
solver = PETScKrylovSolver("cg", pc) |
| 133 |
solver.parameters["monitor_convergence"] = True |
| 134 |
|
| 135 |
# Set matrix operator |
| 136 |
solver.set_operator(A); |
| 137 |
|
| 138 |
# Compute solution |
| 139 |
solver.solve(u.vector(), b); |
| 140 |
|
| 141 |
# Save solution to VTK format |
| 142 |
File("elasticity.pvd", "compressed") << u |
| 143 |
|
| 144 |
# Save colored mesh partitions in VTK format if running in parallel |
| 145 |
if MPI.size(mesh.mpi_comm()) > 1: |
| 146 |
File("partitions.pvd") << MeshFunction("size_t", mesh, mesh.topology().dim(), \ |
| 147 |
MPI.rank(mesh.mpi_comm())) |
| 148 |
|
| 149 |
# Project and write stress field to post-processing file |
| 150 |
W = TensorFunctionSpace(mesh, "Discontinuous Lagrange", 0) |
| 151 |
stress = project(sigma(u), V=W) |
| 152 |
File("stress.pvd") << stress |
| 153 |
|
| 154 |
# Plot solution |
| 155 |
plot(u) |
| 156 |
plt.show() |