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

Error: Unable to define nonlinear variational problem F(u; v) = 0 for all v.

+1 vote

Hi All,

Please can someone help with this. When I run the NonlinearVariationalProblem I get the following error. I cant seem to figure out what the problem is. Below is my code and the error.

Thanks for any help

from dolfin import *

#Subdomain for Perodic Boundary conditions
class PeriodicBoundary(SubDomain):

# left boundary is "target domain"
def inside(self, x, on_boundary):
    return bool(x[0]< DOLFIN_EPS and x[0]> - DOLFIN_EPS and on_boundary)

# Map right boundary to left boundary
def map(self, x, y):
    y[0] = x[0] - 1.0
    y[1] = x[1]

pbc = PeriodicBoundary()

mesh = RectangleMesh(Point(-1,-1),Point(1,1),200,200) #Domain[-1,1]*[-1,1]

V1= VectorFunctionSpace(mesh, 'CG', degree=2, constrained_domain = pbc)
V2=VectorFunctionSpace(mesh, 'CG', degree=2, constrained_domain= pbc)
Q1 = FunctionSpace(mesh, 'CG', degree=1, constrained_domain = pbc )
Q2 = FunctionSpace(mesh, 'CG', degree=1, constrained_domain = pbc)
VQ = MixedFunctionSpace([V1,V2, Q1, Q2])

 # Define trial and test functions
 duu = TrialFunctions(VQ)
 m,h,q,r = TestFunctions(VQ)

 H= Function(VQ)  
 U,u, phi,p = split(H)


#  Dirichlet boundary condition
# Sub domain for Dirichlet boundary condition
class Top(SubDomain):
     def inside(self, x, on_boundary):
          return (x[1]>1 - DOLFIN_EPS and on_boundary)

 class Bottom(SubDomain):
      def inside(self, x, on_boundary):
        return (x[1]< -1 + DOLFIN_EPS and on_boundary)

UB= Constant((-1,0))
UT= Constant((1,0))
ub= Constant((-1,0))
ut= Constant((1,0))

top= Top()
bott = Bottom()

bcUB= DirichletBC(VQ.sub(0), UB  ,bott)
bcUT= DirichletBC(VQ.sub(0), UB  ,top)
bcub= DirichletBC(VQ.sub(1), ub  ,bott )
bcut= DirichletBC(VQ.sub(1), ub  ,top )

 bcs = [bcUB,bcUT, bcub,bcut]


 #constants
 c = Constant (2/3)
xi0= Constant(1)
n = Constant(2)
alpha = Constant(25)


# initial conditions
U_int = Expression(["x[1]", "x[1]"])   
u_int = Expression(["x[1]", "x[1]"])
phi_int= Constant(0.01)
p_int = Constant(0.0)

U00 = interpolate(U_int, V1)   
u00= interpolate(u_int,V2)
phi00=interpolate(phi_int,Q1)
p00 = interpolate(p_int, Q2)

 U0 = U00       
 u0 = u00       
 phi0 = phi00   
 p0 = p00       


 k_phi = (phi/phi0)**n
eta = exp(-alpha*(phi-phi0))
xi = xi0* eta
dt=0.01

f1 = phi*q*dx - phi0*q*dx - dt*div((1-phi)*U)*q*dx  
f2 = inner(phi*(u-U), m)*dx  + k_phi * p*div(m)*dx
f3 = p*div(h)*dx + eta*inner(grad(U) + grad(U).T, grad(h))*dx - (xi*div(U)) *div(h)*dx +   c*(eta*div(U))*div(h)*dx  
f4 = div(phi*u + (1-phi)*U)*r*dx   

F1 = f1 + f2 + f3 + f4          


T_total = 1
t=dt
 while (t<=T_total):
      F = action(F1, H)
      Jac = derivative(F, H)
  problem = NonlinearVariationalProblem(F,H,bcs, Jac)
  solver = NonlinearVariationalSolver(problem)
  solver.solve()
  t+=dt
  (U00, u00, phi00, p00).assign(H)

problem = NonlinearVariationalProblem(F,H,bcs, Jac)
File "/usr/lib/python2.7/dist-packages/dolfin/fem/solving.py", line 153, in init
cpp.NonlinearVariationalProblem.init(self, F, u, bcs, J)
File "/usr/lib/python2.7/dist-packages/dolfin/cpp/fem.py", line 2627, in init
_fem.NonlinearVariationalProblem_swiginit(self,_fem.new_NonlinearVariationalProblem(*args))
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 define nonlinear variational problem F(u; v) = 0 for all v.
*** Reason: Expecting the residual F to be a linear form (not rank 0).
*** Where: This error was encountered inside NonlinearVariationalProblem.cpp.
*** Process: 0


*** DOLFIN version: 1.6.0
*** Git changeset: unknown
*** -------------------------------------------------------------------------

asked Nov 20, 2015 by Vivian FEniCS Novice (550 points)

1 Answer

+1 vote

The problem is that in your residual form F doesn't exists Trial Functions. The solution for that is use:

# Define trial and test functions
(U, u, phi, p) = TrialFunctions(VQ)
(m, h, q, r)   = TestFunctions(VQ)

H = Function(VQ)  

instead of

# Define trial and test functions
duu = TrialFunctions(VQ)
m,h,q,r = TestFunctions(VQ)

H = Function(VQ)  
U,u, phi,p = split(H)
answered Nov 24, 2015 by hernan_mella FEniCS Expert (19,460 points)
...