- Thank you received: 0
Convert netgen mesh to numpy arrays?
3 years 7 months ago #3618
by rick42
Convert netgen mesh to numpy arrays? was created by rick42
Is there an efficient way to extract or convert a netgen mesh to numpy arrays in Python. I'm currently using the following
which isn't very efficient. Is there a better way?
Code:
p = mesh.Points()
vertices = np.empty(shape=(len(p), 3), dtype='float32')
for i, p_i in enumerate(p):
vertices[i] = list(p_i)
pass
faceels = mesh.Elements2D()
faces = np.empty(shape=(len(faceels), 3), dtype='uint32')
for i, el in enumerate(faceels):
faces[i] = [p.nr-1 for p in el.points]
pass
which isn't very efficient. Is there a better way?
3 years 7 months ago #3633
by rick42
Replied by rick42 on topic Convert netgen mesh to numpy arrays?
Is there anyone who can comment on this (is it possible at all?), or point out something in the documentation (as I couldn't find anything about this)?
- christopher
- Offline
- Administrator
Less
More
- Thank you received: 101
3 years 7 months ago - 3 years 7 months ago #3637
by christopher
Replied by christopher on topic Convert netgen mesh to numpy arrays?
Hm depends what you want to get from python with regards to efficiency, but you could write it more compact and more efficient like this:
Code:
from netgen.geom2d import unit_square
mesh = unit_square.GenerateMesh(maxh=0.2)
import numpy as np
# verts = np.array([v.point for v in mesh.vertices])
verts = np.array([list(p) for p in mesh.Points()])
print(verts)
els = np.array([el.vertices for el in mesh.Elements2D()])
print(els)
Last edit: 3 years 7 months ago by christopher.
Time to create page: 0.109 seconds