OK, I constructed an example below which hopefully contains some answers for you.
from dolfin import *
def solve_linear_elasticity(mesh, boundaries, d):
c = Constant(d)
V = VectorFunctionSpace(mesh, "Lagrange", 1)
u = TrialFunction(V)
v = TestFunction(V)
E, nu = 10.0, 0.3
mu = E/(2.0*(1.0 + nu))
lmbda = E*nu/((1.0 + nu)*(1.0 -2.0*nu))
sigma = 2*mu*sym(grad(u)) + lmbda*tr(grad(u))*Identity(3)
F = inner(sigma, grad(v))*dx
a, L = lhs(F), rhs(F)
bcs = [DirichletBC(V, Constant((0.0, 0.0, 0.0)), boundaries, 1),
DirichletBC(V.sub(0), c, boundaries, 2)]
displacement = Function(V)
solve(a==L, displacement, bcs)
return displacement
def update_mesh(mesh, displacement, boundaries):
new_mesh = Mesh(mesh)
new_boundaries = MeshFunction("size_t", new_mesh, 2)
new_boundaries.set_values(boundaries.array())
ALE.move(new_mesh, displacement)
return new_mesh, new_boundaries
# Original mesh
mesh = UnitCubeMesh(8, 8, 8)
subdomain1 = CompiledSubDomain("near(x[1], 0)")
subdomain2 = CompiledSubDomain("near(x[1], 1)")
boundaries = MeshFunction("size_t", mesh, 2)
boundaries.set_all(0)
subdomain1.mark(boundaries, 1)
subdomain2.mark(boundaries, 2)
plot(mesh, title = "Original mesh")
# First iteration (accepted)
displacement1 = solve_linear_elasticity(mesh, boundaries, 0.1)
mesh1, boundaries1 = update_mesh(mesh, displacement1, boundaries)
plot(mesh1, title = "First iteration (accepted)")
# Second iteration
displacement2 = solve_linear_elasticity(mesh1, boundaries1, 0.5)
mesh2, boundaries2 = update_mesh(mesh1, displacement2, boundaries1)
plot(mesh2, title = "Second iteration (rejected)")
# Something went wrong move mesh back to previous
displacement3 = Function(displacement2.function_space())
displacement3.vector()[:] = -1.0*displacement2.vector()
mesh3, boundaries3 = update_mesh(mesh2, displacement3, boundaries2)
plot(mesh3, title = "Moved back from second iteration (same as first iteration)")
from numpy.linalg import norm as np_norm
print np_norm(mesh3.coordinates() - mesh1.coordinates())
interactive()