How to use same vector-valued mathematical expressions with both ufl and numpy?

Hi all,

I’m using this kind of technique to avoid having to write an expression both in `ufl` (eg for generating the exact right hand side `f`) and `numpy` (eg for pointwise evaluations):

``````import dolfinx
import ufl
from mpi4py import MPI
import numpy as np

mesh = dolfinx.mesh.create_unit_square(MPI.COMM_WORLD, 5, 5)

def u_exact(m):
return lambda x: m.sin(m.pi * x[0]) * m.sin(m.pi * x[1])

x = ufl.SpatialCoordinate(mesh)
pts = np.array([[0.5, 0.1], [0.1, 0.9]])
u0 = u_exact(np)(pts)
``````

What I cannot get to work is how to do this if `u_exact` is a vector-valued function. I’m trying to be clever with mixing `np.stack` and `ufl.as_vector` but without success. Is there an example somewhere I can look at?

Thanks!

For legacy dolfin there used to be GitHub - MiroK/ulfy: Traslate UFL to SymPy to Expression (ufl ↔ sympy, but then from sympy one could use `sympy.lambdify` to numpy). I am not aware of similar projects for dolfinx.

The `fem.Expression` mechanism would be a solution if your points are defined via the same coordinates on the reference element. I am not sure if something is similar with points defined by their global coordinates

Maybe this ?

For point evaluation, you need to specify what cell the point is in, as specified in: Implementation — FEniCSx tutorial

One can of course go through `dolfinx.fem.Expression` for more advanced code, such as shown in: Evaluation of UFL expression at mesh points - #2 by dokken and subsequent posts.

Other similar resources: