Interpolate vector function to scalar function

Hello, how can one interpolate the norm of a vector function to a scalar function in dolfinx?
A function u_vec defined on VectorSpace is solved in advance, defined as:

V = FunctionSpace(mesh, ("N1curl",1))
u_vec = Function(V)

and I’d like to compute it’s L2 norm defined on:

W = FunctionSpace(mesh, ("Lagrange",1))
u_norm = Function(W)

I’ve check dolfinx/test_interpolation.py at 3d4137ed17d08e185abc0912dbe53510888f876e · FEniCS/dolfinx · GitHub but only interpolation from scalar(it’s curl) to vector function is mentioned.
How can I solve this? Either by function interpolate or solving a new variational function is great.

It’s there a function in ufl representing norm? Similar to ufl.curl, ufl.grad ,etc.

I think what you want is this:

from dolfinx import mesh as msh
from dolfinx import fem
from mpi4py import MPI
import ufl
mesh = msh.create_unit_square(MPI.COMM_WORLD, 10, 10)
V = fem.FunctionSpace(mesh, ("N1curl", 1))
u_vec = fem.Function(V)
u_vec.interpolate(lambda x: (-x[1], x[0]))

W = fem.FunctionSpace(mesh, ("Lagrange", 1))

expr = fem.Expression(ufl.inner(u_vec, u_vec), W.element.interpolation_points)
w = fem.Function(W)
w.interpolate(expr)
print(w.x.array)

If not, please clarify as to what you want to interpolate.

1 Like

Yes, that’s exactly what I want. Thanks Dokken.