I am trying to define a funcional obj = gamma * norm(u, 2) + norm(a - L, 2)
in order to use some PETSc TAO solver like tron or cg to an anternative
approach to Singular Poisson problem (singular_poisson.py).
How can I define this functional in a correc way?
from dolfin import *
parameters["linear_algebra_backend"] = "PETSc"
# Create mesh
k = 16
mesh = UnitSquareMesh(k, k)
# Create function space
V = FunctionSpace(mesh, "Lagrange", 1)
# Create solution, trial and test functions
u, du, v = Function(V), TrialFunction(V), TestFunction(V)
# Variational formulation
f = Expression("10*exp(-(pow(x[0] - 0.5, 2) + pow(x[1] - 0.5, 2)) / 0.02)")
g = Expression("-sin(5*x[0])")
a = inner(grad(u), grad(u))*dx
L = f * u * dx + g * u * ds
gamma = 0.1
obj = gamma * norm(u, 2) + norm(a - L, 2)
grad_obj = derivative(obj, u, v)
hess_obj = derivative(grad_obj, u, du)
# Box constrains
constraint_l = Expression("x_min", x_min=-1.2)
constraint_u = Expression("x_max", x_max=1.2)
u_min = interpolate(constraint_l, V)
u_max = interpolate(constraint_u, V)
# Define the minimisation problem by using OptimisationProblem class
class Problem(OptimisationProblem):
def __init__(self):
OptimisationProblem.__init__(self)
# Objective function
def f(self, x):
u.vector()[:] = x
return assemble(obj)
# Gradient of the objective function
def F(self, b, x):
u.vector()[:] = x
assemble(grad_obj, tensor=b)
# Hessian of the objective function
def J(self, A, x):
u.vector()[:] = x
assemble(hess_obj, tensor=A)
# Create the PETScTAOSolver
solver = PETScTAOSolver()
# Set some parameters
solver.parameters["method"] = "tron"
solver.parameters["monitor_convergence"] = True
solver.parameters["report"] = True
solver.parameters["maximum_iterations"] = 1000
#info(parameters, True) # Uncomment this line to see the available parameters
# Parse (PETSc) parameters
parameters.parse()
# Solve the problem
set_log_level(DEBUG)
solver.solve(Problem(), u.vector(), u_min.vector(), u_max.vector())
# Save solution in VTK format
ufile_pvd = File("velocity.pvd")
ufile_pvd << u
# Plot solution
plot(u, interactive=True)
I get the error message:
RuntimeError Traceback (most recent call last)
/Users/andremachado/Desktop/demo_singular-poisson_v4_TAO.py in ()
23 gamma = 0.1
24
---> 25 obj = gamma * norm(u, 2) + norm(a - L, 2)
26
27 grad_obj = derivative(obj, u, v)
/Users/andremachado/.hashdist/bld/profile/q3rqj2knkdy7/lib/python2.7/site-packages/dolfin/fem/norms.py in norm(v, norm_type, mesh)
100 cpp.dolfin_error("norms.py",
101 "compute norm",
--> 102 "Norm type must be a string, not " + str(type(norm_type)))
103 if mesh is not None and not isinstance(mesh, Mesh):
104 cpp.dolfin_error("norms.py",
/Users/andremachado/.hashdist/bld/profile/q3rqj2knkdy7/lib/python2.7/site-packages/dolfin/cpp/common.py in dolfin_error(location, task, reason)
2483
2484 """
-> 2485 return _common.dolfin_error(location, task, reason)
2486
2487 def deprecation(feature, version_deprecated, version_remove, message):
RuntimeError:
*** -------------------------------------------------------------------------
*** DOLFIN encountered an error. If you are not able to resolve this issue
*** using the information listed below, you can ask for help at
*** fenics@fenicsproject.org
*** Remember to include the error message listed below and, if possible,
*** include a minimal running example to reproduce the error.
*** -------------------------------------------------------------------------
*** Error: Unable to compute norm.
*** Reason: Norm type must be a string, not .
*** Where: This error was encountered inside norms.py.
*** Process: unknown
*** DOLFIN version: 1.5.0
*** Git changeset:
*** -------------------------------------------------------------------------
What is the correct way to set a funcional
like obj = gamma * norm(u, 2) + norm(a - L, 2),
gamma = 0.01 with ufl in order to use the
powerfull (box constrain) PETSc TAO optimization
solver?