This is a read only copy of the old FEniCS QA forum. Please visit the new QA forum to ask questions

goal function minization with bounds and PDE constrains - AdaptativeNonlinearSolver() with SNES solver

0 votes

I am trying to minimize a goal function subject to constant
and non linear PDE constrains using pure FEniCS and the SNES solver).

I make just a few modifications on demo_contact-vi-snes.py
FEniCS undocumented example in order to put togheter
AdaptativeNonlinearSolver() and SNES solver with constrains:

from dolfin import *

# Check that DOLFIN is configured with PETSc and CGAL
if not has_petsc_snes():
    print "DOLFIN must be compiled with PETSc version > 3.2 to run this demo."
    exit(0)

# Create mesh
mesh = Mesh("../circle_yplane.xml.gz")

V = VectorFunctionSpace(mesh, "Lagrange", 1)

# Define functions
du = TrialFunction(V)            # Incremental displacement
v  = TestFunction(V)             # Test function
u  = Function(V)                 # Displacement from previous iteration
B  = Constant((0.0, -0.05))      # Body force per unit volume

# Kinematics
I = Identity(u.geometric_dimension())  # Identity tensor
F = I + grad(u)             # Deformation gradient
C = F.T*F                   # Right Cauchy-Green tensor

# Invariants of deformation tensors
Ic = tr(C)
J  = det(F)

# Elasticity parameters
E, nu = 10.0, 0.3
mu, lmbda = Constant(E/(2*(1 + nu))), Constant(E*nu/((1 + nu)*(1 - 2*nu)))

# Stored strain energy density (compressible neo-Hookean model)
psi = (mu/2)*(Ic - 2) - mu*ln(J) + (lmbda/2)*(ln(J))**2

# Total potential energy
Pi = psi*dx - dot(B, u)*dx

# Compute first variation of Pi (directional derivative about u in the
# direction of v)
F = derivative(Pi, u, v)

# Compute Jacobian of F
J = derivative(F, u, du)

# Symmetry condition (to block rigid body rotations)
tol = mesh.hmin()
def symmetry_line(x):
    return abs(x[0]) < DOLFIN_EPS
bc = DirichletBC(V.sub(0), 0., symmetry_line, method="pointwise")

# The displacement u must be such that the current configuration x+u
# remains in the box [xmin,xmax] x [umin,ymax]
constraint_u = Expression(("xmax - x[0]","ymax - x[1]"), \
                      xmax=1.0+DOLFIN_EPS,  ymax=1.0)
constraint_l = Expression(("xmin - x[0]","ymin - x[1]"), \
                      xmin=-1.0-DOLFIN_EPS, ymin=-1.0)

umin = interpolate(constraint_l, V)
umax = interpolate(constraint_u, V)

# Define the solver parameters
snes_solver_parameters = {"nonlinear_solver": "snes",
#                          "snes_solver"     : { "linear_solver"   : "lu",
                      "snes_solver"     : { "linear_solver"   : "gmres",
                                            "maximum_iterations": 20,
                                            "report": True,
                                            "error_on_nonconvergence": False,
                                           }}

# Set up the non-linear problem
problem = NonlinearVariationalProblem(F, u, bc, J=J)

## Set up the non-linear solver
#solver  = NonlinearVariationalSolver(problem)

# Set up ADAPTATIVE nonlinear variational solver
M = inner(u, u) * dx
solver = AdaptiveNonlinearVariationalSolver(problem, M)

solver.parameters.update(snes_solver_parameters)
info(solver.parameters, True)

# Solve the problem
#(iter, converged) = solver.solve(umin, umax)

tol = 1e-4
(iter, converged) = solver.solve(umin, umax, tol)

# Check for convergence
if not converged:
    warning("This demo is a complex nonlinear problem. Convergence is not guaranteed when modifying some parameters or using PETSC 3.2.")

# plot the current configuration
#plot(mesh.leaf_node())
plot(u, mode="displacement", wireframe=True, title="Displacement field")
interactive()

I get the error message on IPython:

UMD has deleted: ffc_form_2c6189ee6e7552f3ae7149cd1a66cafddc787e02, ffc_form_f5d86247182239afb8fc545509df8ef49ecfa185.ffc_form_f5d86247182239afb8fc545509df8ef49ecfa185, dolfin_compile_code_ebc572334aeecce84beea04a5d8b711d, _ffc_form_f5d86247182239afb8fc545509df8ef49ecfa185, _ffc_form_2c6189ee6e7552f3ae7149cd1a66cafddc787e02, ffc_form_2c6189ee6e7552f3ae7149cd1a66cafddc787e02.ffc_form_2c6189ee6e7552f3ae7149cd1a66cafddc787e02, _dolfin_compile_code_ebc572334aeecce84beea04a5d8b711d, ffc_form_f5d86247182239afb8fc545509df8ef49ecfa185, _ffc_form_91664149f2882d60bf1907194a3e878266b41577, ffc_form_91664149f2882d60bf1907194a3e878266b41577.ffc_form_91664149f2882d60bf1907194a3e878266b41577, ffc_form_91664149f2882d60bf1907194a3e878266b41577
Traceback (most recent call last):

File "", line 1, in
runfile('/home/parallels/python_scripts/tese/non_linear_poisson/demo_nonlinear-poisson_snes_solver_v2.py', wdir='/home/parallels/python_scripts/tese/non_linear_poisson')

File "/usr/lib/python2.7/dist-packages/spyderlib/widgets/externalshell/sitecustomize.py", line 540, in runfile
execfile(filename, namespace)

File "/home/parallels/python_scripts/tese/non_linear_poisson/demo_nonlinear-poisson_snes_solver_v2.py", line 38, in
solver.parameters.update(snes_solver_parameters)

File "/usr/lib/python2.7/dist-packages/dolfin/cpp/common.py", line 2108, in update
self_value = self[key]

File "/usr/lib/python2.7/dist-packages/dolfin/cpp/common.py", line 2089, in getitem
raise KeyError, "'%s'"%key

KeyError: "'snes_solver'"

How can I solve this problem? Any bound constrained Newton
method with iterative solver sounds well to.

asked Sep 29, 2014 by Andre Machado FEniCS User (1,040 points)

2 Answers

+2 votes
 
Best answer

The AdaptiveNonlinearVariationalSolver interface does not support bound constraints. I think it is difficult to add this functionality because you need to add mesh adaptation inside the nonlinear solver, that for the case of bound-constraints is completely delegated to petsc.

If you absolutely need this functionality, you can try to implement your own Sequential Quadratic Programming algorithm by solving iteratively quadratic problems with bound constraints (a generalization of the Newton algorithm) and adapt the mesh after the solution of each quadratic problem.

answered Oct 7, 2014 by cmaurini FEniCS User (1,130 points)
selected Aug 26, 2015 by Andre Machado

OK CMaurini. Thank you very much.

Andre Machado

+1 vote

The error is telling you that 'snes_solver' is not a key in the Parameters structure. To see your problem, have it report the parameter structure before you try to set it. snes_solver is a key, but not at the top level; it is under 'nonlinear_variational_solver'.

This works for me:

solver.parameters['nonlinear_variational_solver'].update(snes_solver_parameters)

Incidentally, you will need to change line 87 to:

(iter, converged) = solver.solve(tol)

to get it to run. 

answered Sep 29, 2014 by mwelland FEniCS User (8,410 points)

How can I put the bound constrains umin and umax on adaptativenonlinearsolver?

( I make the two modifications but it is not working )

By 'not working' do you mean the software crashes / produces and error or it just doesn't converge?
I don't have an answer to getting everything working together (adaptive + bound solving). Maybe someone else?

Crashes. What I really need is solve a non linear PDE with bound constrains and a
goal function minimization. Thank you.

Can you post the error message please?

...