Problem with nonlinear variational problem

Hi everyone,

the following python code:

domain=mshr.Cylinder(Point(0,0,0),Point(2,0,0),1,1)
mesh = mshr.generate_mesh(domain, nCells)
StressTensorElements = VectorElement('P', tetrahedron, 1)
VelocityElements = FiniteElement('P', tetrahedron, 1)
element = StressTensorElements * VelocityElements
V = FunctionSpace(mesh, element)
C, u1 = TrialFunctions(V)
CV, v1 = TestFunctions(V)
u_n = interpolate(Constant((0,0,1,0),V)
F = (C[0] - u_n.vector()[0] + 1 / Lambda * (C[0])) * CV[0] * dx + dt / Lambda * u1 * CV[0].dx(1) * dx
dt =0.1
U = Function(V)
solve(F==0, U)

produced the following error:

***Error:   Unable to define nonlinear variational problem F(u; v) = 0 for all v.
*** Reason:  Expecting the residual F to be a linear form (not rank 2).
*** Where:   This error was encountered inside NonlinearVariationalProblem.cpp.
*** Process: 0
*** 
*** DOLFIN version: 2018.1.0
*** Git changeset:  unknown

Which I don’t understand because F is linear in the trial and test functions?
Another interesting problem appears when changing the form to
F = (C[0] - u_n.vector()[0] + 1 / Lambda * (C[0]- Constant(1))) * self.CV[0] * dx + dt / Lambda * u1 * CV[0].dx(1) * dx

then I get the following exception:
ArityMismatch: “Adding expressions with non-matching form arguments”

I shortened the exception a bit to make it more readable. Thank you for taking the time to help me out!

Since you are solving a linear problem, you should not call F==0, but lhs(F)==rhs(F).

V = FunctionSpace(mesh, element)
C, u1 = TrialFunctions(V)
CV, v1 = TestFunctions(V)
u_n = interpolate(Constant((0,0,1,0)),V)
F = (C[0] - u_n.vector()[0] + 1 / Lambda * (C[0])) * CV[0] * dx  + dt / Lambda * u1 * CV[0].dx(1) * dx
dt =0.1
U = Function(V)
solve(lhs(F)==rhs(F), U)

This gets rid of your second error as well

Thanks this works perfectly now!

Here is my code:

'mesh = UnitSquareMesh(32, 32)
V = FunctionSpace(mesh, "Lagrange", 1)
        
def boundary(x, on_boundary):
    return on_boundary
    
u0 = Expression("cos(10*x[0])", degree=1)
bc = DirichletBC(V, u0, boundary)
        
u = TrialFunction(V)
v = TestFunction(V)
f = Expression("(x[0] - 0.5)*(x[0] - 0.5)", degree=1)
        
# Missing variational formulation
a = inner(grad(u),grad(v))*dx
L = f*v
        
u = Function(V)
solve(a == L, u, bc)
        
print ("The norm of u is {}".format(u.vector().norm("l2")))'

It gives me the following error:
*** Error: Unable to define nonlinear variational problem F(u; v) = 0 for all v.
*** Reason: Expecting the residual F to be a linear form (not rank 2).
*** Where: This error was encountered inside NonlinearVariationalProblem.cpp.
*** Process: 0

I read on the official documentation that one should not use TrialFunction in a non-linear problem, see for instance:
https://bitbucket.org/fenics-project/dolfin/src/master/python/demo/documented/nonlinear-poisson/demo_nonlinear-poisson.py.rst

I replaced ‘u = TrialFunction(V)’ with ‘u = Function(V)’ and removed the latter ‘u = Function(V)’.

Though I was able to get an output of the norm, it was a wrong answer.

Please let me know what is wrong with the code.

Thanks,
Durganshu

This should be L=f*v*dx

Oh! That was a blunder. Thank you so much!!!