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

Function as parameter for another form

+1 vote

Hello,

I would like to calculate the volume under a function I have obtained by solving a linear problem:

Suppose I have a and L defined in an external .ufl file. I compute my solution u with

Poisson::BilinearForm a(V, V);
Poisson::LinearForm L(V);

Function u(V);

solve(a == L, u, bc);

Now, I have an integral.ufl file

element = FiniteElement("Lagrange", triangle, 2)

g = Coefficient(element)
integral = g * dx

forms = [integral]

which is supposed to evaluate the volume under a function g.
In the .cpp file I have

integral::Form_integral integrator(mesh);
integrator.g = u;
double area = dolfin::assemble(integrator);

But this confuses the compiler, since I get the error

error: no match for ‘operator=’ (operand types are ‘dolfin::CoefficientAssigner’ and ‘dolfin::Function’)
       integrator.g = u;
                    ^
note: candidate: void dolfin::CoefficientAssigner::operator=(std::shared_ptr<const dolfin::GenericFunction>)
         void operator= (std::shared_ptr<const GenericFunction> coefficient);
              ^
/usr/include/dolfin/function/CoefficientAssigner.h:50:10: note:   no known conversion for argument 1 from ‘dolfin::Function’ to ‘std::shared_ptr<const dolfin::GenericFunction>’

However, the whole thing works if I set

auto one = std::make_shared<One>();
integral::Form_integral integrator(mesh);
integrator.g = one;
double area = dolfin::assemble(integrator);

where

class One : public Expression
{
    public:
  void eval(Array<double>& values, const Array<double>& x) const
  {
    values[0] = 1.0;
  }
};

Any idea how I can convert my Function u to an acceptable parameter for the integrator form?

Thanks a lot!

asked Nov 14, 2016 by anfneub FEniCS Novice (580 points)

1 Answer

+1 vote
 
Best answer

I think u needs to be a shared pointer. This based on the error message:

note: candidate: void dolfin::CoefficientAssigner::operator=(std::shared_ptr<const dolfin::GenericFunction>)
         void operator= (std::shared_ptr<const GenericFunction> coefficient);
              ^

try:

auto u = std::make_shared<Function>(V);

solve(a == L, *u, bc);
answered Nov 14, 2016 by nate FEniCS Expert (17,050 points)
selected Nov 14, 2016 by anfneub

Yes, that was the problem.

Thank you very much!

...