Negative values for exponential function using ".Set()"

Hi all,
we have experienced strange behaviour: Set create a coefficient function which is basically a Gaussian and use the .Set-function to set it to an H^1-function on a relatively coarse grid. Then, the resulting vector has negative entries. A minimal example is the following:

from ngsolve import *
from ngsolve.meshes import MakeStructured2DMesh, MakeHexMesh

mesh = Mesh(unit_square.GenerateMesh(maxh=0.1, quad_dominated=False))

V=H1(mesh, order=1)
gfu = GridFunction(V, name="gfu")

sigma = 0.1
tt = (x-0.25-0.5*z)**2+(y-0.25-0.5*z)**2
rhoEx = exp(-1/2/sigma**2*tt)

gfu.Set(rhoEx)
print(gfu.vec)

If we mesh with maxh=0.01 instead, all values are positive. We are using Version v6.2.2404-16-g6091669e

Can you help us with what we are missing?

Many thanks!

Set does an element-wise L2 projection (and averages the dofs afterwards). The L2 projection may lead to negative values, especially in the case of underresolution.

Interpolating with interpolate may slightly improve the situation (at least vertex values will be point evaluations).

Thanks for the explanation, that helped a lot :slight_smile: