# integrating functional in C++ (Cahn-Hilliard equation)

Asked by Payman Tohidifar on 2013-03-07

Hello,

I need to compute the integral of functional u over a domain. I used assemble(u*dx) in python and it worked pretty well. However, I don't know how to employ assemble function in C++ specially in Cahn-Hilliard problem (C++ Demo) to integrate functional since u is a pointer. I would appreciate if you could help me out.

Thanks

## Question information

Language:
English Edit question
Status:
Solved
For:
DOLFIN Edit question
Assignee:
No assignee Edit question
Solved by:
Jan Blechta
Solved:
2013-03-09
Last query:
2013-03-09
2013-03-07
 Jan Blechta (blechta) said on 2013-03-07: #1

You need to define this 0-form (rank 0 because it has no trial nor test functions and is scalar) in UFL file and compile with ffc -ldolfin. Then in your C++ code you assign your computed u to this form and assemble. Not tested.

Jan

 Payman Tohidifar (payman-che) said on 2013-03-07: #2

Thanks Jan for your response. But I don't think if I get your comment very
well. Would you please give me a short example? . I'd really appreciate it.

Payman

 Jan Blechta (blechta) said on 2013-03-07: #3

form.ufl:
_______________________________________
elem = FiniteElement("CG", triangle, 1) # or whatever element you need
u = Coefficient(elem)
M = u*dx()
________________________________________

cpp:
_______________________
#include "form.h"
...
Mesh mesh;
Function u(V);
...
form::Functional F(mesh);
F.u = u;
double f = assemble(F);
...
_______________________

Not tested. Just try it with some mesh and u.

 Payman Tohidifar (payman-che) said on 2013-03-09: #4

Thanks a lot Jan. It worked pretty well for me.

Payman

 Payman Tohidifar (payman-che) said on 2013-03-09: #5

Thanks Jan Blechta, that solved my question.