Interpolate an expression which contains a test function

Hello,

I would like to do the following

from mpi4py import MPI
import dolfinx
import ufl

mesh = dolfinx.mesh.create_unit_square(MPI.COMM_WORLD, 10, 10)
V = dolfinx.fem.functionspace(mesh, ("Lagrange", 1))

f = dolfinx.fem.Function(V)
f.interpolate(lambda x: x[0] ** 2 + x[1] ** 2)

v = ufl.TestFunction(V)

temp = dolfinx.fem.Function(V)
temp_expr = dolfinx.fem.Expression(ufl.dot(f, v), V.element.interpolation_points())
temp.interpolate(temp_expr)

so I can then visualize the fem.Function within the domain. Is this possible?

If I try this, I get

RuntimeError: Cannot interpolate Expression with Argument

What you are trying to do is to assemble a linear form. Simply assemble it in temp.vector (or temp.x.petsc_vec in newer dolfinx versions) rather than a new vector created on the fly.

Can you elaborate a bit further please.

I managed to do this

temp_expr = ufl.dot(f, v) * ufl.dx
temp_form = dolfinx.fem.form(temp_expr)
b = dolfinx.fem.petsc.assemble_vector(temp_form)

but I am not sure how to proceed next with the temp.vector/temp.x.petsc_vec like you mentioned

Essentialy what I would like to do is to display the RHS vector of the


inside the domain.

I can’t test it now, but something like

temp_expr = ufl.dot(f, v) * ufl.dx
temp_form = dolfinx.fem.form(temp_expr)
temp = dolfinx.fem.Function(V)
dolfinx.fem.petsc.assemble_vector(temp.vector, temp_form)

should do the job

1 Like