I tried to define a 1D mesh embedded in 3D space, similar to what is implemented for surfaces in MakeStructuredSurfaceMesh. However, I am getting an error when trying to evaluate coefficient functions or gridfunctions at physical points of this 1D mesh.
In particular, evaluating via gfu(ngsmesh(x,y,z)) fails with `NgException: Meshpoint not in mesh!`, even though the point should lie on the mesh. The same approach works for the structured surface mesh (2D surface embedded in 3D space), but not for my 1D implementation.
from ngsolve import *
from ngsolve.webgui import Draw
from netgen.meshing import Mesh as NetMesh, MeshPoint, Element1D, Element0D, Pnt
mesh = NetMesh(dim=3)
n = 10
pids = []
for i in range(n + 1):
t = i / n
pids.append(mesh.Add(MeshPoint(Pnt(t, 0, 0))))
idx_inner = mesh.AddRegion("inner", dim=1)
idx_left = mesh.AddRegion("left", dim=0)
idx_right = mesh.AddRegion("right", dim=0)
for i in range(n):
mesh.Add(Element1D([pids[i], pids[i + 1]], index=idx_inner))
mesh.Add(Element0D(pids[0], index=idx_left))
mesh.Add(Element0D(pids[-1], index=idx_right))
mapping = lambda s: (cos(s*pi),sin(s*pi), 0.3*cos(2*pi*s)) # curved line in 3D space
# mapping = lambda s: (s, 0, 0) # straight line in 3D space
for p in mesh.Points():
xp,yp,zp = p.p
xp,yp,zp = mapping(xp)
p[0],p[1],p[2] = xp,yp,zp
ngsmesh = Mesh(mesh)
print("Materials : ", ngsmesh.GetMaterials()) # With 3D line mesh --> this is the inner region
print("Boundaries : ", ngsmesh.GetBoundaries()) # With 3D line mesh --> no surfaces
print("BBoundaries : ", ngsmesh.GetBBoundaries()) # With 3D line mesh --> this is the inner region
print("BBBoundaries: ", ngsmesh.GetBBBoundaries()) # With 3D line mesh --> these are the edges
Draw(ngsmesh)
gfu = GridFunction(H1(ngsmesh))
gfu.Set(x, definedon=ngsmesh.BBoundaries("inner"))
# Access gfu anywhere on mesh:
pt = mapping(0.5)
print("Physical point :", pt)
print("GF evaluation :", gfu(ngsmesh(*pt))) # throws error --> NgException: Meshpoint not in mesh!
Is my 1D mesh formulation missing anything for the proper 3D embedding? Any help is much appreciated!
Best regards, Lucca
mwe_mesh.ipynb (2.6 KB)