All terms in form must have same rank

Asked by al6

I have just started implementing nonlinear incompressible elasticity (neo-hookean material). In my simple problem there is a unit cube which is fixed at x=0; a traction force is applied at x=1 (ForceBoundary). I cannot understand what is wrong with the following code:

from dolfin import *

mesh = UnitCube(5,5,5)
V = VectorFunctionSpace(mesh, 'Lagrange', 2)
Q = FunctionSpace(mesh,'Lagrange',1)
W = V*Q

mu = 4E5

(u,p) = TrialFunctions(W)
(v,q) = TestFunctions(W)
t = Constant((1.0,0.0,0.0))

I = Identity(3)

F = variable(I + grad(u))
J = variable(det(F))
S = variable(mu*F-p*cofac(F))

boundary_parts = \
  MeshFunction("uint", mesh, mesh.topology().dim()-1)

class ForceBoundary(SubDomain):
    def inside(self, x, on_boundary):
        tol = 1E-14 # tolerance for coordinate comparisons
        return on_boundary and abs(x[0]-1) < tol

Gamma_0 = ForceBoundary()
Gamma_0.mark(boundary_parts, 1)

a = inner(S,nabla_grad(v))*dx + q*(J-1)*dx - inner(t,v)*ds(1)

def left_boundary(x, on_boundary):
 return on_boundary and abs(x[0]) < DOLFIN_EPS

bc = DirichletBC(V, Constant((0.0,0.0,0.0)), left_boundary)

w = Function(W)
problem = NonlinearVariationalProblem(a, w, bc)
solver = NonlinearVariationalSolver(problem)
set_log_level(PROGRESS)

solver.solve()

I get this output:

Calling FFC just-in-time (JIT) compiler, this may take some time.
All terms in form must have same rank.
Traceback (most recent call last):
  File "/Users/eyelash/Desktop/neohooke_traction.py", line 39, in <module>
    problem = NonlinearVariationalProblem(a, w, bc)
  File "/Applications/FEniCS.app/Contents/Resources/lib/python2.7/site-packages/dolfin/fem/solving.py", line 116, in __init__
    F = Form(F, form_compiler_parameters=form_compiler_parameters)
  File "/Applications/FEniCS.app/Contents/Resources/lib/python2.7/site-packages/dolfin/fem/form.py", line 56, in __init__
    common_cell)
  File "/Applications/FEniCS.app/Contents/Resources/lib/python2.7/site-packages/dolfin/compilemodules/jit.py", line 66, in mpi_jit
    return local_jit(*args, **kwargs)
  File "/Applications/FEniCS.app/Contents/Resources/lib/python2.7/site-packages/dolfin/compilemodules/jit.py", line 154, in jit
    return jit_compile(form, parameters=p, common_cell=common_cell)
  File "/Applications/FEniCS.app/Contents/Resources/lib/python2.7/site-packages/ffc/jitcompiler.py", line 73, in jit
    return jit_form(ufl_object, parameters, common_cell)
  File "/Applications/FEniCS.app/Contents/Resources/lib/python2.7/site-packages/ffc/jitcompiler.py", line 130, in jit_form
    common_cell=common_cell)
  File "/Applications/FEniCS.app/Contents/Resources/lib/python2.7/site-packages/ffc/compiler.py", line 150, in compile_form
    analysis = analyze_forms(forms, object_names, parameters, common_cell)
  File "/Applications/FEniCS.app/Contents/Resources/lib/python2.7/site-packages/ffc/analysis.py", line 64, in analyze_forms
    common_cell) for form in forms)
  File "/Applications/FEniCS.app/Contents/Resources/lib/python2.7/site-packages/ffc/analysis.py", line 64, in <genexpr>
    common_cell) for form in forms)
  File "/Applications/FEniCS.app/Contents/Resources/lib/python2.7/site-packages/ffc/analysis.py", line 152, in _analyze_form
    "All terms in form must have same rank.")
  File "/Applications/FEniCS.app/Contents/Resources/lib/python2.7/site-packages/ffc/log.py", line 46, in ffc_assert
    condition or error(*message)
  File "<string>", line 1, in <lambda>
  File "/Applications/FEniCS.app/Contents/Resources/lib/python2.7/site-packages/ufl/log.py", line 148, in error
    raise self._exception_type(self._format_raw(*message))
Exception: All terms in form must have same rank.

Can you help me, please?

Question information

Language:
English Edit question
Status:
Solved
For:
DOLFIN Edit question
Assignee:
No assignee Edit question
Solved by:
Anders Logg
Solved:
Last query:
Last reply:
Revision history for this message
Best Anders Logg (logg) said :
#1

Change from "TrialFunctions" to "Functions".

--
Anders

On Wed, Apr 18, 2012 at 06:05:56PM -0000, alucantonio wrote:
> New question #194057 on DOLFIN:
> https://answers.launchpad.net/dolfin/+question/194057
>
> I have just started implementing nonlinear incompressible elasticity (neo-hookean material). In my simple problem there is a unit cube which is fixed at x=0; a traction force is applied at x=1 (ForceBoundary). I cannot understand what is wrong with the following code:
>
> from dolfin import *
>
> mesh = UnitCube(5,5,5)
> V = VectorFunctionSpace(mesh, 'Lagrange', 2)
> Q = FunctionSpace(mesh,'Lagrange',1)
> W = V*Q
>
> mu = 4E5
>
> (u,p) = TrialFunctions(W)
> (v,q) = TestFunctions(W)
> t = Constant((1.0,0.0,0.0))
>
> I = Identity(3)
>
> F = variable(I + grad(u))
> J = variable(det(F))
> S = variable(mu*F-p*cofac(F))
>
> boundary_parts = \
> MeshFunction("uint", mesh, mesh.topology().dim()-1)
>
> class ForceBoundary(SubDomain):
> def inside(self, x, on_boundary):
> tol = 1E-14 # tolerance for coordinate comparisons
> return on_boundary and abs(x[0]-1) < tol
>
> Gamma_0 = ForceBoundary()
> Gamma_0.mark(boundary_parts, 1)
>
> a = inner(S,nabla_grad(v))*dx + q*(J-1)*dx - inner(t,v)*ds(1)
>
> def left_boundary(x, on_boundary):
> return on_boundary and abs(x[0]) < DOLFIN_EPS
>
> bc = DirichletBC(V, Constant((0.0,0.0,0.0)), left_boundary)
>
> w = Function(W)
> problem = NonlinearVariationalProblem(a, w, bc)
> solver = NonlinearVariationalSolver(problem)
> set_log_level(PROGRESS)
>
> solver.solve()
>
> I get this output:
>
> Calling FFC just-in-time (JIT) compiler, this may take some time.
> All terms in form must have same rank.
> Traceback (most recent call last):
> File "/Users/eyelash/Desktop/neohooke_traction.py", line 39, in <module>
> problem = NonlinearVariationalProblem(a, w, bc)
> File "/Applications/FEniCS.app/Contents/Resources/lib/python2.7/site-packages/dolfin/fem/solving.py", line 116, in __init__
> F = Form(F, form_compiler_parameters=form_compiler_parameters)
> File "/Applications/FEniCS.app/Contents/Resources/lib/python2.7/site-packages/dolfin/fem/form.py", line 56, in __init__
> common_cell)
> File "/Applications/FEniCS.app/Contents/Resources/lib/python2.7/site-packages/dolfin/compilemodules/jit.py", line 66, in mpi_jit
> return local_jit(*args, **kwargs)
> File "/Applications/FEniCS.app/Contents/Resources/lib/python2.7/site-packages/dolfin/compilemodules/jit.py", line 154, in jit
> return jit_compile(form, parameters=p, common_cell=common_cell)
> File "/Applications/FEniCS.app/Contents/Resources/lib/python2.7/site-packages/ffc/jitcompiler.py", line 73, in jit
> return jit_form(ufl_object, parameters, common_cell)
> File "/Applications/FEniCS.app/Contents/Resources/lib/python2.7/site-packages/ffc/jitcompiler.py", line 130, in jit_form
> common_cell=common_cell)
> File "/Applications/FEniCS.app/Contents/Resources/lib/python2.7/site-packages/ffc/compiler.py", line 150, in compile_form
> analysis = analyze_forms(forms, object_names, parameters, common_cell)
> File "/Applications/FEniCS.app/Contents/Resources/lib/python2.7/site-packages/ffc/analysis.py", line 64, in analyze_forms
> common_cell) for form in forms)
> File "/Applications/FEniCS.app/Contents/Resources/lib/python2.7/site-packages/ffc/analysis.py", line 64, in <genexpr>
> common_cell) for form in forms)
> File "/Applications/FEniCS.app/Contents/Resources/lib/python2.7/site-packages/ffc/analysis.py", line 152, in _analyze_form
> "All terms in form must have same rank.")
> File "/Applications/FEniCS.app/Contents/Resources/lib/python2.7/site-packages/ffc/log.py", line 46, in ffc_assert
> condition or error(*message)
> File "<string>", line 1, in <lambda>
> File "/Applications/FEniCS.app/Contents/Resources/lib/python2.7/site-packages/ufl/log.py", line 148, in error
> raise self._exception_type(self._format_raw(*message))
> Exception: All terms in form must have same rank.
>
> Can you help me, please?
>

Revision history for this message
corrado maurini (corrado-maurini) said :
#2

You may also have a look to https://answers.launchpad.net/dolfin/+question/177822, where a code for incompressible elasticity is reported.

Corrado

Revision history for this message
al6 (al6) said :
#3

Ok, changing from "TrialFunctions" to "Functions" and using some hints from the link by corrado, the compilation goes further, but now I get another error:

Traceback (most recent call last):
  File "/Users/eyelash/Desktop/neohooke_traction.py", line 44, in <module>
    problem = NonlinearVariationalProblem(a, up, bc, Jac)
  File "/Applications/FEniCS.app/Contents/Resources/lib/python2.7/site-packages/dolfin/fem/solving.py", line 118, in __init__
    J = Form(J, form_compiler_parameters=form_compiler_parameters)
  File "/Applications/FEniCS.app/Contents/Resources/lib/python2.7/site-packages/dolfin/fem/form.py", line 56, in __init__
    common_cell)
  File "/Applications/FEniCS.app/Contents/Resources/lib/python2.7/site-packages/dolfin/compilemodules/jit.py", line 66, in mpi_jit
    return local_jit(*args, **kwargs)
  File "/Applications/FEniCS.app/Contents/Resources/lib/python2.7/site-packages/dolfin/compilemodules/jit.py", line 154, in jit
    return jit_compile(form, parameters=p, common_cell=common_cell)
  File "/Applications/FEniCS.app/Contents/Resources/lib/python2.7/site-packages/ffc/jitcompiler.py", line 73, in jit
    return jit_form(ufl_object, parameters, common_cell)
  File "/Applications/FEniCS.app/Contents/Resources/lib/python2.7/site-packages/ffc/jitcompiler.py", line 130, in jit_form
    common_cell=common_cell)
  File "/Applications/FEniCS.app/Contents/Resources/lib/python2.7/site-packages/ffc/compiler.py", line 155, in compile_form
    ir = compute_ir(analysis, parameters, common_cell)
  File "/Applications/FEniCS.app/Contents/Resources/lib/python2.7/site-packages/ffc/representation.py", line 83, in compute_ir
    for (i, fd) in enumerate(form_datas)]
  File "/Applications/FEniCS.app/Contents/Resources/lib/python2.7/site-packages/ffc/representation.py", line 214, in _compute_integral_ir
    common_cell)
  File "/Applications/FEniCS.app/Contents/Resources/lib/python2.7/site-packages/ffc/quadrature/quadraturerepresentation.py", line 133, in compute_integral_ir
    ir["trans_integrals"] = _transform_integrals(transformer, integrals_dict, domain_type)
  File "/Applications/FEniCS.app/Contents/Resources/lib/python2.7/site-packages/ffc/quadrature/quadraturerepresentation.py", line 304, in _transform_integrals
    terms = transformer.generate_terms(integrand)
  File "/Applications/FEniCS.app/Contents/Resources/lib/python2.7/site-packages/ffc/quadrature/quadraturetransformerbase.py", line 748, in generate_terms
    loop, entry = self._create_loop_entry(key, f_nzc)
  File "/Applications/FEniCS.app/Contents/Resources/lib/python2.7/site-packages/ffc/quadrature/quadraturetransformerbase.py", line 791, in _create_loop_entry
    index_k, entry_k, range_k, space_dim_k = key1
TypeError: 'int' object is not iterable

The code is the following:

from dolfin import *

mesh = UnitCube(5,5,5)
V = VectorFunctionSpace(mesh, 'Lagrange', 2)
Q = FunctionSpace(mesh,'Lagrange',1)
W = V*Q

mu = 4E5

vq = TestFunction(W)
v,q = split(vq)

up = Function(W)
u,p = split(up)
t = Constant((1.0,0.0,0.0))

I = Identity(3)

F = I + grad(u)
J = det(F)
S = mu*F-p*cofac(F)

boundary_parts = \
  MeshFunction("uint", mesh, mesh.topology().dim()-1)

class ForceBoundary(SubDomain):
    def inside(self, x, on_boundary):
        return on_boundary and abs(x[0]-1) < DOLFIN_EPS

Gamma_0 = ForceBoundary()
Gamma_0.mark(boundary_parts, 1)

a = inner(S,nabla_grad(v))*dx + q*(J-1)*dx - inner(t,v)*ds(1)

def left_boundary(x, on_boundary):
 return on_boundary and abs(x[0]) < DOLFIN_EPS

bc = DirichletBC(V, Constant((0.0,0.0,0.0)), left_boundary)

Jac = derivative(a,up,vq)
problem = NonlinearVariationalProblem(a, up, bc, Jac)
solver = NonlinearVariationalSolver(problem)
set_log_level(PROGRESS)

solver.solve()

================================================
Meantime, thanks for your help!

Revision history for this message
al6 (al6) said :
#4

I realized that there was an error in the computation of the Jacobian: the last argument in the derivative function must be a TrialFunction object, not a TestFunction object (vq). Now the solution process starts, but I get no convergence (the error is 10^10 and it increases), even if I remove the force (I should find the zero solution). Maybe I need to implement the newton solver manually, as corrado did.

Revision history for this message
al6 (al6) said :
#5

Thanks Anders Logg, that solved my question.

Revision history for this message
al6 (al6) said :
#6

I had to write explicitly the expressions for the components of the stress tensor S, without resorting to automatic symbolic manipulation, to make it work. I used the automatic nonlinear solver (NonlinearVariationalSolver - NonlinearVariationalSolver).