GridFunction interpolation not compatible with mesh.Refine()

Dear community,

I ran into a small curiosity today while attempting a simple grid refinement study. For the problem at hand, I do not have access to an analytic solution, so I am comparing solutions at gradually finer meshes to establish convergence.

I have noticed that if I create a new mesh at every refinement level, NGSolve is able to automatically interpolate the GridFunction containing the numerical solution on the old mesh onto the new mesh. However, if I use mesh.Refine(), this automatic interpolation no longer works (cf. the MWE below, where, when using mesh.Refine(), the second call to Draw simply results in a drawing of the zero function). This behavior struck me as somewhat peculiar, so I wanted to bring it to your attention in case it should be addressed somehow.

Also, if there are better ways to perform grid refinement studies in NGSolve for problems without a known analytic solution, I would be grateful for any suggestions.

from netgen.geom2d import unit_square from ngsolve.webgui import Draw import ngsolve as ng

mesh = ng.Mesh(unit_square.GenerateMesh(maxh=0.1))

V = ng.H1(mesh, order=3, dirichlet=“left|right|top”)
u,v = V.TnT()
T = ng.GridFunction(V)

a = ng.BilinearForm(V, symmetric=True)
a += ng.grad(u) * ng.grad(v) * ng.dx
f = ng.LinearForm(V)
f += 32 * (ng.y*(1-ng.y)+ng.x*(1-ng.x)) * v * ng.dx
f.Assemble() = a.mat.Inverse(V.FreeDofs(), inverse=“sparsecholesky”) * f.vec

Draw(T, mesh)
mesh.Refine() # This does NOT work
#mesh = ng.Mesh(unit_square.GenerateMesh(maxh=0.05)) # This works
Draw(T, mesh)

Best regards,

Hi Sindre,

your issue is related to topic Autoupdate for GridFunctions

To use your Gridfunction on a refined mesh you have to add the auoupdate flag in the FESpace and GridFunction and, if you do not have the latest version of NGSolve, the nested flag for the Gridfunction

V = ng.H1(mesh, order=3, dirichlet="left|right|top", autoupdate=True)
T = ng.GridFunction(V, autoupdate=True, nested=True)


1 Like