Unable to evaluate CFs using NumPy arrays of MeshPoints

Hello everybody,

I would like to parallelize the evaluation of a coefficient function at many different MeshPoints using a NumPy array. Unfortunately, there seems to be a problem with pybind and NumPy, as NumPy doesn’t recognize MeshPoint objects. As a result, it creates generic objects that the call function does not recognize. Here is a minimal working example:

from ngsolve import *
import numpy as np
from netgen.geom2d import unit_square

mesh = Mesh(unit_square.GenerateMesh(maxh=0.2))
func  = CF(x)
np_points = np.array([mesh(0.1,0.1), mesh(0.2,0.2)], dtype=fem.MeshPoint)
print(func(np_points))

According to the documentation, my expected result would be [0.1, 0.2], but instead, I receive the following error message:

TypeError: call(): incompatible function arguments. The following argument types are supported:
1. (self: ngsolve.fem.CoefficientFunction, mip: ngsolve.fem.BaseMappedIntegrationPoint) → object
2. (self: ngsolve.fem.CoefficientFunction, x: float, y: Optional[float] = None, z: Optional[float] = None) → ngcomp::PointEvaluationFunctional
3. (self: ngsolve.fem.CoefficientFunction, arg0: ngsolve.fem.CoordinateTrafo) → ngsolve.fem.CoefficientFunction
4. (self: ngsolve.fem.CoefficientFunction, arg0: ngsolve.fem.MeshPoint) → object
5. (self: ngsolve.fem.CoefficientFunction, arg0: numpy.ndarray[ngsolve.fem.MeshPoint]) → numpy.ndarray
Invoked with: <ngsolve.fem.CoefficientFunction object at 0x72a9dedd4050>, array([<ngsolve.fem.MeshPoint object at 0x72a9df5c7c30>,
<ngsolve.fem.MeshPoint object at 0x72a9df5c5770>], dtype=object)

Greetings,
Fabian

The syntax is different:

xvals = [0.1, 0.2]
yvals = [0.1,0.2]
np_points = mesh(xvals, yvals)
print(func(np_points))

best

Thank you. The syntax from the example was derived from the following code:

IntPoints = []
Integrate(levelset_domain={"levelset": lsetp1, "domain_type": IF}, cf=0, mesh=mesh, deformation=deformation, ip_container=IntPoints, heapsize=1000000000)
EvalPoints = np.array(IntPoints, dtype=fem.MeshPoint)

Is there an (efficient) way to store IntPoints in a numpy array?

Hi Fabian, I am not entirely sure whether I got this right, but assuming you aim at evaluating some CF in parallel for just these points, generated by the “ip_container - hack”, I would generate arrays along the lines of xvals = [ x(ip) for ip in ip_container ] (or numpy variants thereof) and follow the route suggested above from there on. Best, Fabian