Dear FEniCS community,
I would like to extract coefficients (not necessarily constants, possibly expressions) from bilinear/linear forms in order to apply a preprocessing to them before assembling the forms.
For instance, consider the following code
from dolfin import *
mesh = UnitSquareMesh(10, 10)
V = FunctionSpace(mesh, "Lagrange", 2)
expr1 = Expression("x[0]", element=V.ufl_element())
expr2 = Expression("x[1]", element=V.ufl_element())
expr3 = Expression("x[0]", element=V.ufl_element())
expr4 = Expression("x[1]", element=V.ufl_element())
u = TrialFunction(V)
v = TestFunction(V)
a = expr1*inner(grad(u), grad(v))*dx + expr2*u.dx(0)*v*dx + expr3*u*v*dx
f = expr4*v*dx
Given a and f, my final goal would be to extract expr1, expr2, expr3 and expr4, apply some preprocessing on them (obtaining expr1_new, expr2_new, expr3_new and expr4_new) and then assemble the following modified forms:
a_new = expr1_new*inner(grad(u), grad(v))*dx + expr2_new*u.dx(0)*v*dx + expr3_new*u*v*dx
f_new = expr4_new*v*dx
assemble(a_new)
assemble(f_new)
Q1. Is it possible to extract the coefficients and then plug them back in the forms? If so, how? I have had a look at
for integral in a.integrals():
print integral.integrand()
but I am not sure on how to parse it
Q2. Will this work also for VectorFunctionSpaces, where expressions may be matrices or vectors?
Q3. Do I have a smart way to detect if two or more of the extracted coefficients are the same? For instance, in the example above, I would need to apply my preprocessing only to expr1 and expr2, since the remaining ones are the same.
Q4. Would integration over dx vs ds make any difference in the procedure?
I know that in my example I can just apply my preprocessing to each expression and then assemble directly a_new and f_new. However, the actual need is to apply it e.g. to a form obtained with derivative(), so in that case I would be not writing down the bilinear form explicitly and thus I would really need to get each coefficient from the form.
Thanks,
Francesco