← Back to team overview

dolfin team mailing list archive

Re: [Question #125106]: C++ code for user-defined Expression

 

I think your problem is the way you call the Expression constructor:

  Expression(1)

This creates a vector-valued expression (with only one component).

Try with Expression() and it should work.

--
Anders


On Sun, Sep 12, 2010 at 10:46:36AM -0000, Øystein Sørensen wrote:
> New question #125106 on DOLFIN:
> https://answers.launchpad.net/dolfin/+question/125106
>
> I have a problem in which I need to define an Expression using "complicated" C++ code. Before making the real code, I am trying to test it, with the following trivial code:
>
> -----------------------------------------------------------
>
> from dolfin import *
>
> code = '''
> class SomeExpr : public Expression
> {
> public:
>     SomeExpr() : Expression(1) {}
>
>     void eval(Array<double>& values, const Data& data) const
> {
>     values[0] = data.x[0];
> }
>
> };
> '''
>
> mesh = UnitInterval(10)
> V = FunctionSpace(mesh, 'CG', 2)
>
> u = TrialFunction(V)
> v = TestFunction(V)
> f = Expression(code)
> a = f*inner(grad(u),grad(v))*dx
> L = Constant(0)*v*dx
>
> -----------------------------------------------------------
>
> When running it, I get the error message:
> Trying to integrate expression of rank 1 with free indices ().
>
> On the other hand, if I just write
> f = Expression('x[0]'),
> which is what I try to do in the above code, no error messages are issued.
>

--
Anders



Follow ups

References