# Dirichlet boundary conditions and ALE.move()

The following code produces a satisfactory Taylor test:

from dolfin import *
import numpy as np

mesh = UnitSquareMesh(10, 10)
V = FunctionSpace(mesh, "CG", 1)
VD = VectorFunctionSpace(mesh, "CG", 1)

W = Function(VD)
W.vector()[:] = .005
ALE.move(mesh, W)

u = TrialFunction(V)
v = TestFunction(V)

l = v * dx(mesh)

u = Function(V)

dbc = DirichletBC(V, Constant(1.0), "on_boundary")

# works
# solve(a == l, u, dbc)

# works
A = assemble(a)
b = assemble(l)
dbc.apply(A, b)
solve(A, u.vector(), b)

J = assemble(u ** 2 * dx(mesh))
j = ReducedFunctional(J, [Control(W)])

H = Function(VD)
H.vector()[:] = (np.random.rand(VD.dim())*.5 - 1) * 1e-3

taylor_test(j, W, H)



Changing the Dirichlet boundary like below, yields a bad Taylor test:

dbc = DirichletBC(V, Expression("x", degree=3), "on_boundary")


But with these new boundary conditions and imposing the displacements W, H to be zero on the boundary, we recover the convergence in the residuals:

mesh = UnitSquareMesh(10, 10)
V = FunctionSpace(mesh, "CG", 1)
VD = VectorFunctionSpace(mesh, "CG", 1)

W = Function(VD)
W.vector()[:] = .005
dbcv = DirichletBC(VD, Constant([0,0]), "on_boundary")
dbcv.apply(W.vector())
ALE.move(mesh, W)

u = TrialFunction(V)
v = TestFunction(V)

l = v * dx(mesh)

u = Function(V)

dbc = DirichletBC(V, Expression("x", degree=3), "on_boundary")

# works
# solve(a == l, u, dbc)

# works
A = assemble(a)
b = assemble(l)
dbc.apply(A, b)
solve(A, u.vector(), b)

J = assemble(u ** 2 * dx(mesh))
j = ReducedFunctional(J, [Control(W)])

H = Function(VD)
H.vector()[:] = (np.random.rand(VD.dim())*.5 - 1) * 1e-3
dbcv.apply(H.vector())
taylor_test(j, W, H)


From here I am tempted to conlcude that the code:

A = assemble(a)
b = assemble(l)
dbc.apply(A, b)
solve(A, u.vector(), b)


works for shape optimization, as long as:

• the dirichlet conditions don’t depend on the mesh
• or the boundary of the mesh isn’t moved

Could anybody perhaps further explain what’s going on and in case confirm/deny that my observation is correct? Thanks in advance!

There is no way of differentiating Expressions. If you instead project spatial coordinates into the function space of the boundary condition and use this function in the boundary condition, the Taylor test should pass. (You could Also impose the DirichletBC weakly with Nitsches method).