How do I evaluate the value of a bilinear form a(u,v) in C++?
a collegue of mine and I are doing a project using fenics C++. After solving a variational problem (this goes well):
solve(a == L, u, *dirichlet);
We need to store in a double-variable the effective value of a(u,u). u of course is a vector Function. We tried to add the some lines to the .ufl file in order to create the "integral" in the .h file, but it does not create anything of the type Form_tot_f in the .h file!
element = VectorElement("Lagrange", cell, 1)
testf = Coefficient(element)
tot_f = inner(sigma(testf), epsilon(testf))*dx
We tried also to use member functions of "assemble.h", but the documentation is too poor to try in this way.
forms = [tot_f]
I compute and save arbitrary scalar and vector fields using separate .ufl files in C++. I solve them using an additional "dummy" variational form. For example, I have a problem that uses the total variational flow, and I compute the (strong form) separately in a .ufl file as follows:
element = FiniteElement("Lagrange", triangle, 2) theta = Coefficient(element) v = TestFunction(element) # Test Function u = TrialFunction(element) # Trial Function H = Constant(triangle) T = Constant(triangle) OmegaPhi0 = Coefficient(element) KappaTheta = Coefficient(element) Fgrad = inner(grad(theta), grad(theta)) Ftvf = H*T*div(OmegaPhi0*grad(theta)/sqrt(KappaTheta + Fgrad)) a = inner(u, v)*dx L = inner(Ftvf,v)*dx
In the .cpp file, I define :
#include "jtvf.h" //compiled from: ffc -O -l dolfin jtvf.ufl ... auto mesh = std::make_shared<RectangleMesh>(p0, p1, NumberOfNodes, NumberOfNodes, "crossed"); auto V_tvf = std::make_shared<jtvf::FunctionSpace>(mesh); ... auto tvf = std::make_shared<Function>(V_tvf); jtvf::LinearForm L_tvf(V_tvf); jtvf::BilinearForm a_tvf(V_tvf,V_tvf); ... solve(a_tvf == L_tvf, *tvf); filetvf << *tvf;
I don't know if this is the only way to do this. I tried using this as an additional form within my variational form file, but it did not work as expected (unknown why), so I created a separate form file and solved it using the dummy approach above.
For your problem, in the .ufl file above, just pass in the needed functions via Coefficients and change Ftvf to whatever you want to compute and export