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

Invalid value rank UFL/C++

0 votes

Dear all,

I have a question: I'm porting a python project to UFL/C++, but I am encountering errors. All the code I am posting here is inspired by the undocumented/dg-poisson/cpp/ demo.

The error is the following:

*** Error:   Unable to assemble form.
*** Reason:  Invalid value rank for coefficient 1 (got 0 but expecting 1). You might have forgotten to specify the value rank correctly in an Expression subclass.
*** Where:   This error was encountered inside AssemblerBase.cpp.

The test is easy: a plate clamped at the bottom and pulled at the top, so one Dirichlet and one Neumann BCs. In my UFL I have (here only the relevant parts, no bulk forces):

cell    = triangle
element = VectorElement("Lagrange", cell, 2)

u = TrialFunction(element)
v = TestFunction(element)
force_vector = Coefficient(element)

# Known term
L = inner(force_vector, v) * ds(1)

My C++ main file is this:

        // Neumann BCs
        EdgeForceExpression force;

        // Boundary markers set by default to 0 (MAYBE!)
        FacetFunction<std::size_t> boundary_markers(mesh, 0);

        // Mark the edge with load as 1
        top.mark(boundary_markers, 1);

        // Set loads RHS
        L.f            = f_bulk;
//        L.force_vector = f_surf;
        L.force_vector = force;
        L.ds           = boundary_markers;

You can see that force_vector is assigned a force object, this is an instance of the following class:

// Force on an edge
class EdgeForceExpression : public Expression
{
    void eval(Array<double>& values, const Array<double>& x) const
    {
        values[0] = 1.0e6;
        values[1] = 0.0;
    }
};

As you can see, in the main file there's a commented assignment for the force to f_surf, defined as Constant f_surf(1.0e6, 0.0);. If I use this, the program runs fine, and produces correct results.

Why can't I use the force as defined by eval in the class? I cannot see how, but I'm a newbie in this...

Thanks!

asked Jun 17, 2015 by senseiwa FEniCS User (2,620 points)

1 Answer

+1 vote
 
Best answer

Hi, despite the eval method your code constructs a scalar valued expression and that's why you get rank 0 in the error message. You need to add a proper constructor to make the expression vector valued (rank 1). This demo shows how to go about it.

answered Jun 17, 2015 by MiroK FEniCS Expert (80,920 points)
selected Jun 17, 2015 by senseiwa

Perfectly clear, I mistakenly assumed that expressions were implicitly 3D.

Thanks!

...