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

mixed function space formulation, same rank error

–2 votes

I am trying to solve a variational formulation with 3 weighting functions.

from dolfin import *
import numpy, sys

create mesh and function spaces

degree = int(sys.argv[1])
divisions =[int(arg) for arg in (sys.argv[2:])]
mesh=UnitSquareMesh(*divisio M is mixed function spacens)

V = FunctionSpace(mesh,"Lagrange",degree) # Function space for u ( Temperature)
Q= FunctionSpace(mesh,"Lagrange",degree) # Function space for \mu ( materi
L = FunctionSpace(mesh,"Lagrange",degree) # Function space for \lembda
M = MixedFunctionSpace([V,Q,L])

# Boundary Conditions
def boundary_ul(x,on_boundary):
on_boundary and near(x[0],0.0) and near(x[0],1.0) \
and near(x[1],0.0) and near(x[1],1.0)
def boundary_mu(x,on_boundary):
on_boundary and near(x[1],0.0) and near(x[0],1.0)

u is prescribed on entire boundary

PrescribedU=Expression("0.25(pow(x[0],2)+pow(x[1],2))-x[0]x[1]0.5-x[0]+0.5x[1]")
bcs_u = DirichletBC(M.sub(0),PrescribedU , boundary_ul)

\mu prescribed where vector(a).vector(n) < 0 ,i.e inflow boudaries

inflow boundaries are x[0]=1. and x[1]=0.

PrescribedMU= Expression("(2/(exp(2)-1))(exp(2x[0])+2*x[1])")
bcs_mu=DirichletBC(M.sub(1),PrescribedMU,boundary_mu)

\lmd is 0. on entire boundary

bcs_lmd=DirichletBC(M.sub(2),Constant(0.0),boundary_ul)

bcs=[bcs_u ,bcs_mu , bcs_lmd]

Trial and test functions

Tstfnc = TestFunction(M)
(v,q,w) =split(Tstfnc)
Trlfnc = Function(M)
(u,mu,lmd) = split(Trlfnc)

Weak form

F=vudx + qmudx +wlmddx

solving the variational problem

results=Function(M)
solve(F==0,results,bcs)

Output , Error message

Traceback (most recent call last):
File "objmin_var.py", line 71, in
solve(F==0,results,bcs)
File "/usr/lib/python2.7/dist-packages/dolfin/fem/solving.py", line 268, in solve
_solve_varproblem(*args, **kwargs)
File "/usr/lib/python2.7/dist-packages/dolfin/fem/solving.py", line 311, in _solve_varproblem
form_compiler_parameters=form_compiler_parameters)
File "/usr/lib/python2.7/dist-packages/dolfin/fem/solving.py", line 118, in __init__
J = Form(J, form_compiler_parameters=form_compiler_parameters)
File "/usr/lib/python2.7/dist-packages/dolfin/fem/form.py", line 56, in __init__
common_cell)
File "/usr/lib/python2.7/dist-packages/dolfin/compilemodules/jit.py", line 66, in mpi_jit
return local_jit(*args, **kwargs)
File "/usr/lib/python2.7/dist-packages/dolfin/compilemodules/jit.py", line 154, in jit
return jit_compile(form, parameters=p, common_cell=common_cell)
File "/usr/lib/python2.7/dist-packages/ffc/jitcompiler.py", line 77, in jit
return jit_form(ufl_object, parameters, common_cell)
File "/usr/lib/python2.7/dist-packages/ffc/jitcompiler.py", line 159, in jit_form
element_mapping=element_mapping)
File "/usr/lib/python2.7/dist-packages/ufl/form.py", line 123, in compute_form_data
element_mapping=element_mapping)
File "/usr/lib/python2.7/dist-packages/ufl/algorithms/preprocess.py", line 210, in preprocess
ufl_assert(len(compute_form_arities(form)) == 1, "All terms in form must have same rank.")
File "/usr/lib/python2.7/dist-packages/ufl/assertions.py", line 37, in ufl_assert
if not condition: error(*message)
File "/usr/lib/python2.7/dist-packages/ufl/log.py", line 154, in error
raise self._exception_type(self._format_raw(*message))
ufl.log.UFLException: All terms in form must have same rank.

Please let me know what mistake I am doing. Thanks.

asked Dec 18, 2013 by mt.fan FEniCS Novice (260 points)

1 Answer

+3 votes

Hi, your weak form has no TrialFunction. It should be defined with

Test = TestFunction(M)
(v, q, w) = split(Test)
Trial = TrialFunction(M)
(u, mu, lmd) = split(Trial)

Further, the form is linear and you call solve as if it were to solve nolinear variational problem. That call should be

solve(lhs(F) == rhs(F), results, bcs)

Finally, the functions that define where the boundary conditions should be prescribed are strange.
They don't return anything and for example

on_boundary and near(x[1],0.0) and near(x[0],1.0)

will give you True for a point that is on the boundary and has coordinates [1, 0]. That could well be what you want, but I suspect you want to mark the whole line x = 1 and y = 0. The function boundary_ul will never return True even if it actually did feature a return statement. Maybe you wanted those function to be like this

def boundary_ul(x, on_boundary):
    return on_boundary

def boundary_mu(x,on_boundary):
    return on_boundary and (near(x[1],0.0) or near(x[0],1.0)) 
answered Dec 19, 2013 by MiroK FEniCS Expert (80,920 points)

Thanks for your reply. I have taken care of the boundary condition. Basically I have a weak form with 3 weighting function spaces. These 3 spaces have 3 different dirichlet boundary conditions . My basic questions ais how to do mixed space formulation for more than 2 function spaces in fenics. After incorporating your suggestions my code is :

`from dolfin import *
import numpy, sys 
degree = int(sys.argv[1])
divisions =[int(arg) for arg in (sys.argv[2:])]
mesh=UnitSquareMesh(*divisions)

V = FunctionSpace(mesh,"Lagrange",degree) 
Q = FunctionSpace(mesh,"Lagrange",degree) 
L = FunctionSpace(mesh,"Lagrange",degree)  
M = MixedFunctionSpace([V,Q,L])

Test= TestFunction(M)
(v,q,w) = split(Test)
Trial = TrialFunction(M)
(u,mu,lmd) = split(Trial)

F=inner(nabla_grad(lmd),nabla_grad(v))*mu*dx +inner(nabla_grad(lmd),nabla_grad(u))*q*dx
 +inner(nabla_grad(w),nabla_grad(u))*mu*dx


def boundary_ul(x,on_boundary):
    on_boundary and (near(x[0],0.0) or near(x[0],1.0) \
            or near(x[1],0.0) or near(x[1],1.0))
def boundary_mu(x,on_boundary):
    on_boundary and (near(x[1],0.0) or near(x[0],1.0))

PrescribedU=Expression("0.25*(pow(x[0],2)+pow(x[1],2))-x[0]*x[1]*0.5-x[0]+0.5*x[1]")
bcs_u = DirichletBC(M.sub(0),PrescribedU , boundary_ul)
PrescribedMU= Expression("(2/(exp(2)-1))*(exp(2*x[0])+2*x[1])")
bcs_mu=DirichletBC(M.sub(1),PrescribedMU,boundary_mu)
bcs_lmd=DirichletBC(M.sub(2),Constant(0.0),boundary_ul)

bcs=[bcs_u ,bcs_mu , bcs_lmd]

solution = Function(M)

solve(lhs(F)==rhs(F),solution, bcs)

With the above code I get the following error message.
Unable to extract all indices.
Traceback (most recent call last):
File "objmin_var.py", line 82, in
solve(lhs(F)==rhs(F),solution, bcs)
File "/usr/lib/python2.7/dist-packages/dolfin/fem/solving.py", line 268, in solve
_solve_varproblem(*args, **kwargs)
File "/usr/lib/python2.7/dist-packages/dolfin/fem/solving.py", line 292, in _solve_varproblem
form_compiler_parameters=form_compiler_parameters)
File "/usr/lib/python2.7/dist-packages/dolfin/fem/solving.py", line 80, in __init__
a = Form(a, form_compiler_parameters=form_compiler_parameters)
File "/usr/lib/python2.7/dist-packages/dolfin/fem/form.py", line 56, in __init__
common_cell)
File "/usr/lib/python2.7/dist-packages/dolfin/compilemodules/jit.py", line 66, in mpi_jit
return local_jit(*args, **kwargs)
File "/usr/lib/python2.7/dist-packages/dolfin/compilemodules/jit.py", line 154, in jit
return jit_compile(form, parameters=p, common_cell=common_cell)

...