Refine() and OptimizeMesh2d()

Hi all,

I’ve encountered a problem when trying to use OptimizeMesh2d(). In particular it only happens when the initial mesh has been obtained by refinement. In that case, after the call of OptimizeMesh2d(), the number of dofs of FE spaces is not updated. Here is my output of the attached minimal file.

``````#initial mesh not refined
BEFORE opti: len(mesh.ngmesh.Points()) =  1581 , fes.ndof =  1581
AFTER opti: len(mesh.ngmesh.Points()) =  868 , fes.ndof =  868 , fes1.ndof =  868

#initial mesh refined by mesh.ngmesh.Refine()
BEFORE opti: len(mesh.ngmesh.Points()) =  6197 , fes.ndof =  6197
AFTER opti: len(mesh.ngmesh.Points()) =  3182 , fes.ndof =  6197 , fes1.ndof =  6197

#initial mesh refined by mesh.Refine()
BEFORE opti: len(mesh.ngmesh.Points()) =  6197 , fes.ndof =  6197
AFTER opti: len(mesh.ngmesh.Points()) =  3108 , fes.ndof =  6197 , fes1.ndof =  6197
``````

and here is the code (the forum does not allow me to upload the file, sorry):

``````from ngsolve import *
from netgen.geom2d import SplineGeometry

refineOption = 0# 1 # 2

#just for creating a distorted mesh...
def moveNGmesh2d(step, displ, mesh):
k = 0
N = len(mesh.ngmesh.Points())
for p in mesh.ngmesh.Points():
v0 = displ.vec.FV()[k]
v1 = displ.vec.FV()[k+N]
p[0] += step*v0
p[1] += step*v1
k += 1
mesh.ngmesh.Update()

geometry = SplineGeometry()

myNgmesh = geometry.GenerateMesh (maxh=0.05)
if refineOption==1:
myNgmesh.Refine()
mesh = Mesh(myNgmesh)
if refineOption==2:
mesh.Refine()

VEC = VectorH1(mesh, order=1)
gfDispl = GridFunction(VEC)
gfDispl.Set((x,0))
moveNGmesh2d(5, gfDispl, mesh)

fes = H1(mesh, order=1)
print("BEFORE opti: len(mesh.ngmesh.Points()) = ", len(mesh.ngmesh.Points()), ", fes.ndof = ", fes.ndof)

mesh.ngmesh.OptimizeMesh2d()
fes.Update()

fes1 = H1(mesh, order=1)
print("AFTER opti: len(mesh.ngmesh.Points()) = ", len(mesh.ngmesh.Points()), ", fes.ndof = ", fes.ndof, ", fes1.ndof = ", fes1.ndof)
``````

Can somebody help me?
Thanks and best wishes,
Peter