Error in solver in a dolfin adjoint problem

sir, i don’t know where is the mistake, I think my solver is not working properly.

``````from dolfin import *
from ufl.operators import Max
import matplotlib.pyplot as plt
from math import log as ln
set_log_level(LogLevel.ERROR)
# Needed to have a nested conditional
parameters["form_compiler"]["representation"] = "uflacs"
nx = 4
mesh = UnitSquareMesh(nx,nx)
V = FiniteElement("Lagrange", mesh.ufl_cell(), 1)
M = FunctionSpace(mesh, "Lagrange", 1)
T = VectorElement("Lagrange", mesh.ufl_cell(), 1)
me = MixedElement([V,T])
W = FunctionSpace(mesh,me)
(u,sig) = TrialFunctions(W)
(v,tau) = TestFunctions(W)
# The source term
u_e = Expression("(1-x[0])*x[0]*(1-x[1])*x[1]",degree=4,domain=mesh)
class source(UserExpression):
def eval(self, value, x):
if x[0] <= 0.5:
value[0] = 0
else:
value[0] = 2*(x[1]-x[1]*x[1]+x[0]-x[0]*x[0])
def value_shape(self):
return ()
class g_exact(UserExpression):
def eval(self, value, x):
if x[0] <= 0.5:
value[0] = (1-x[0])*x[0]*(1-x[1])*x[1]
else:
value[0] = 0
def value_shape(self):
return ()
f = source(degree = 4)
g = interpolate(g_exact(degree=4), M)
def smoothmax(r, eps=1e-4):
return conditional(gt(r, eps), r-eps/2, conditional(lt(r, 0), 0, r**2/(2*eps)))
eps = Constant(pow(10, -8))
F = dot(div(sig),div(tau))*dx + dot(grad(u)-sig,grad(v)-tau)*dx  - 1 / eps * inner(smoothmax(-u+g), v) * dx + dot(f,div(tau))*dx
bc = DirichletBC(W.sub(0), 0.0, "on_boundary")
w= Function(W)
solve(F==0, w, bc)
(u,sig) = w.split()
vtksol = File("obstacle2/u.pvd")
vtksol << u
vtkobs = File("obstacle2/g.pvd")
vtkobs << g
E = errornorm(u_e,u, 'L2')
print(E)
E3 = errornorm(sig_e,sig,'L2')
print(E3)
``````

output:

``````Calling FFC just-in-time (JIT) compiler, this may take some time.
Traceback (most recent call last):
File "obstacle2.py", line 51, in <module>
solve(F == 0, w, bc, solver_parameters=newton_solver_parameters())
File "/home/aparna/.local/lib/python3.8/site-packages/fenics_adjoint/solving.py", line 52, in solve
output = backend.solve(*args, **kwargs)
File "/usr/lib/petsc/lib/python3/dist-packages/dolfin/fem/solving.py", line 233, in solve
_solve_varproblem(*args, **kwargs)
File "/usr/lib/petsc/lib/python3/dist-packages/dolfin/fem/solving.py", line 308, in _solve_varproblem
problem = NonlinearVariationalProblem(eq.lhs, u, bcs, J,
File "/usr/lib/petsc/lib/python3/dist-packages/dolfin/fem/problem.py", line 157, in __init__
F = Form(F, form_compiler_parameters=form_compiler_parameters)
File "/usr/lib/petsc/lib/python3/dist-packages/dolfin/fem/form.py", line 43, in __init__
ufc_form = ffc_jit(form, form_compiler_parameters=form_compiler_parameters,
File "/usr/lib/petsc/lib/python3/dist-packages/dolfin/jit/jit.py", line 50, in mpi_jit
return local_jit(*args, **kwargs)
File "/usr/lib/petsc/lib/python3/dist-packages/dolfin/jit/jit.py", line 100, in ffc_jit
return ffc.jit(ufl_form, parameters=p)
File "/usr/lib/python3/dist-packages/ffc/jitcompiler.py", line 217, in jit
module = jit_build(ufl_object, module_name, parameters)
File "/usr/lib/python3/dist-packages/ffc/jitcompiler.py", line 130, in jit_build
module, signature = dijitso.jit(jitable=ufl_object,
File "/usr/lib/python3/dist-packages/dijitso/jit.py", line 165, in jit
header, source, dependencies = generate(jitable, name, signature, params["generator"])
File "/usr/lib/python3/dist-packages/ffc/jitcompiler.py", line 65, in jit_generate
code_h, code_c, dependent_ufl_objects = compile_object(ufl_object,
File "/usr/lib/python3/dist-packages/ffc/compiler.py", line 142, in compile_form
return compile_ufl_objects(forms, "form", object_names,
File "/usr/lib/python3/dist-packages/ffc/compiler.py", line 185, in compile_ufl_objects
analysis = analyze_ufl_objects(ufl_objects, kind, parameters)
File "/usr/lib/python3/dist-packages/ffc/analysis.py", line 89, in analyze_ufl_objects
form_datas = tuple(_analyze_form(form, parameters)
File "/usr/lib/python3/dist-packages/ffc/analysis.py", line 89, in <genexpr>
form_datas = tuple(_analyze_form(form, parameters)
File "/usr/lib/python3/dist-packages/ffc/analysis.py", line 169, in _analyze_form
form_data = compute_form_data(form,
File "/usr/lib/python3/dist-packages/ufl/algorithms/compute_form_data.py", line 407, in compute_form_data
check_form_arity(preprocessed_form, self.original_form.arguments(), complex_mode)  # Currently testing how fast this is
File "/usr/lib/python3/dist-packages/ufl/algorithms/check_arities.py", line 177, in check_form_arity
check_integrand_arity(itg.integrand(), arguments, complex_mode)
File "/usr/lib/python3/dist-packages/ufl/algorithms/check_arities.py", line 159, in check_integrand_arity
arg_tuples = map_expr_dag(rules, expr, compress=False)
File "/usr/lib/python3/dist-packages/ufl/corealg/map_dag.py", line 36, in map_expr_dag
result, = map_expr_dags(function, [expression], compress=compress,
File "/usr/lib/python3/dist-packages/ufl/corealg/map_dag.py", line 97, in map_expr_dags
r = handlers[v._ufl_typecode_](v)
File "/usr/lib/python3/dist-packages/ufl/algorithms/check_arities.py", line 41, in nonlinear_operator
raise ArityMismatch("Applying nonlinear operator {0} to expression depending on form argument {1}.".format(o._ufl_class_.__name__, t))
ufl.algorithms.check_arities.ArityMismatch: Applying nonlinear operator Power to expression depending on form argument v_1.
``````

As you are solving a non-linear problem this should be

``````w = Function(W)
u, sig = ufl.split(w)
``````

Sir, after changing this, It shows error:

``````raceback (most recent call last):
File "b.py", line 17, in <module>
u, sig = ufl.split(w)
NameError: name 'ufl' is not defined
``````

You can try simply `split` or

``````import ufl
``````

i split simply as

``````from dolfin import *
from ufl.operators import Max
import matplotlib.pyplot as plt
from math import log as ln
set_log_level(LogLevel.ERROR)
# Needed to have a nested conditional
parameters["form_compiler"]["representation"] = "uflacs"
nx = 4
mesh = UnitSquareMesh(nx,nx)
V = FiniteElement("Lagrange", mesh.ufl_cell(), 1)
M = FunctionSpace(mesh, "Lagrange", 1)
T = VectorElement("Lagrange", mesh.ufl_cell(), 1)
me = MixedElement([V,T])
W = FunctionSpace(mesh,me)
w = Function(W)
u, sig = w.split()
(v,tau) = TestFunctions(W)
# The source term
u_e = Expression("(1-x[0])*x[0]*(1-x[1])*x[1]",degree=4,domain=mesh)
class source(UserExpression):
def eval(self, value, x):
if x[0] <= 0.5:
value[0] = 0
else:
value[0] = 2*(x[1]-x[1]*x[1]+x[0]-x[0]*x[0])
def value_shape(self):
return ()
class g_exact(UserExpression):
def eval(self, value, x):
if x[0] <= 0.5:
value[0] = (1-x[0])*x[0]*(1-x[1])*x[1]
else:
value[0] = 0
def value_shape(self):
return ()
f = source(degree = 4)
g = interpolate(g_exact(degree=4), M)
def smoothmax(r, eps=1e-4):
return conditional(gt(r, eps), r-eps/2, conditional(lt(r, 0), 0, r**2/(2*eps)))
eps = Constant(pow(10, -8))
F = div(sig)*div(tau)*dx + inner(grad(u)-sig,grad(v)-tau)*dx  - 1 / eps * inner(smoothmax(-u+g),v ) * dx + inner(f,div(tau))*dx
bc = DirichletBC(W.sub(0), 0.0, "on_boundary")
y= Function(W)
solve(F==0, y, bc)
(u,sig) = y.split()
vtksol = File("obstacle2/u.pvd")
vtksol << u
vtkobs = File("obstacle2/g.pvd")
vtkobs << g
E = errornorm(u_e,u, 'L2')
print(E)
E3 = errornorm(sig_e,sig,'L2')
print(E3)
``````

it shows error:

``````Traceback (most recent call last):
File "b.py", line 46, in <module>
solve(F==0, y, bc)
File "/home/aparna/.local/lib/python3.8/site-packages/fenics_adjoint/solving.py", line 52, in solve
output = backend.solve(*args, **kwargs)
File "/usr/lib/petsc/lib/python3/dist-packages/dolfin/fem/solving.py", line 233, in solve
_solve_varproblem(*args, **kwargs)
File "/usr/lib/petsc/lib/python3/dist-packages/dolfin/fem/solving.py", line 314, in _solve_varproblem
solver.solve()
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
***
***
*** Remember to include the error message listed below and, if possible,
*** include a *minimal* running example to reproduce the error.
***
*** -------------------------------------------------------------------------
*** Error:   Unable to successfully call PETSc function 'MatSetValuesLocal'.
*** Reason:  PETSc error code is: 63 (Argument out of range).
*** Where:   This error was encountered inside /build/dolfin-rUo2od/dolfin-2019.2.0~git20220407.d29e24d/dolfin/la/PETScMatrix.cpp.
*** Process: 0
***
*** DOLFIN version: 2019.2.0.dev0
*** Git changeset:  unknown
*** -------------------------------------------------------------------------
``````

You should not create a new function here. You should send in `w` Which you used to define your variational form.

i do as you as saying
it shows error

``````Traceback (most recent call last):
File "b.py", line 46, in <module>
solve(F==0, w, bc)
File "/home/aparna/.local/lib/python3.8/site-packages/fenics_adjoint/solving.py", line 52, in solve
output = backend.solve(*args, **kwargs)
File "/usr/lib/petsc/lib/python3/dist-packages/dolfin/fem/solving.py", line 233, in solve
_solve_varproblem(*args, **kwargs)
File "/usr/lib/petsc/lib/python3/dist-packages/dolfin/fem/solving.py", line 314, in _solve_varproblem
solver.solve()
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
***
***
*** Remember to include the error message listed below and, if possible,
*** include a *minimal* running example to reproduce the error.
***
*** -------------------------------------------------------------------------
*** Error:   Unable to successfully call PETSc function 'MatSetValuesLocal'.
*** Reason:  PETSc error code is: 63 (Argument out of range).
*** Where:   This error was encountered inside /build/dolfin-rUo2od/dolfin-2019.2.0~git20220407.d29e24d/dolfin/la/PETScMatrix.cpp.
*** Process: 0
***
*** DOLFIN version: 2019.2.0.dev0
*** Git changeset:  unknown
*** -----------------------------------------------------
``````