Paraview display error after remeshing by OptimizeMesh2d

Hello everyone,
I got a problem while using VTKOutput. When I remesh by mesh.ngmesh.OptimizeMesh2d, the mesh in netgen GUI is correct but incorrect in paraview as follows:


I think OptimizeMesh2d may rearrange the nodes number but don’t know how to get the correct image. Has anyone encountered a similar issue? Are there recommended solutions?

Thank you very much for your help!

can you provide a minimal example?
after optimizing, you might need to create a new ngsolve mesh wrapper around the netgen mesh:

mesh = Mesh(mesh.ngmesh)

Thank you for your reply! When I try ‘mesh = Mesh(mesh.ngmesh)’, it reports that

terminate called after throwing an instance of 'std::bad_array_new_length'
terminate called recursively
  what():  std::bad_array_new_length
Aborted

Here is a simple example of my problem:

from netgen.geom2d import SplineGeometry
from ngsolve import *

def MoveNGmesh(DISP, mesh):
    for p in mesh.ngmesh.Points():
        mip = mesh(p[0],p[1])
        v = DISP(mip)
        p[0] += v[0]
        p[1] += v[1]
    mesh.ngmesh.Update()


geo = SplineGeometry()
geo.AddRectangle((-1.0, -1.0), (1.0, 1.0), bc = 'gamma')
mesh = Mesh(geo.GenerateMesh(maxh = 0.2))

vec = H1(mesh, order=1, dim=2, dirichlet = '', autoupdate=True)
f = GridFunction(vec, autoupdate=True)

f.vec[31][0] = 2*1e-2

for k in range(20):
    MoveNGmesh(f, mesh)
    mesh.ngmesh.OptimizeMesh2d()
    Draw(f, mesh, 'x')
vtk = VTKOutput(mesh,coefs=[f],names=["mesh"],filename="1")
vtk.Do()

When choose ‘Surface with edges’ in Paraview, it shows the same issue.