Dimensions don't match between numpy array and ngsolve LF

Hello,

I have a problem of shapes with array on NGSolve / Numpy.

The problem is the following, I have a “pwc” Compressed on a certain specific region (reducing the size of all element linked to pwc). Then I calculate an Integral element_wise, but the dimensions of this integral is on all the mesh and not only where I defined it…

pwc = Compress(L2(mesh,order=0,definedon=mesh.Materials(VARIABLE)))
gfRho = GridFunction(pwc)  

Ates = Integrate(gfRho,mesh, element_wise=True,definedon=mesh.Materials(VARIABLE))
Ates = np.array(Ates)

dJdrho = LinearForm(pwc)   
dJdrho.vec.FV().NumPy()[:] = Ates  # ERROR, dimensions don't match Ates >> dJdrho

The previous code is a simplified one, I really need to convert the Integrate to a numpy array, I also really need to compress over the definition domain pwc. Also gfRho has a certain value I don’t show in the previous simplified code, but is also defined on pwc…

The question is : How can I “compress” the integrale on a specific domain, so that Ates and dJdrho can have the same size.

Thank you a lot !

Yes elementwise integral gives value for each element. You can create a mask for the elements of your material in numpy:

# Get array with 1 == material, 0 other material
mat = mesh.Materials(VARIABLE)
els = sum((mesh.ngmesh.Elements2D().NumPy()["index"] == index+1 for index, val in enumerate(mat.Mask()) if val), start=np.zeros(mesh.ne))
# Set only on selected elements
dJdrho.vec.FV().NumPy()[:] = Ates.NumPy()[els.astype(bool)]

Best
Christopher