Hi,
I would like to generate a mesh and then add or move some vertices to the coordinates contained in the list x and y.
from dolfin import *
from mshr import *
x0 = 0.0
y0 = 0.0
x1 = 100.0
y1 = 100.0
resolution = 30
x = [10.0, 20.0, 80.0]
y = [13.0, 73.0, 33.0]
rect = Rectangle(Point(x0, y0), Point(x1, y1))
mesh = generate_mesh(rect, resolution)
Hi you could try using MeshEditor
. Consider for instance the following.
import pygmsh as pm
from dolfin import *
import numpy as np
import matplotlib.pyplot as plt
x0 = 0.0
y0 = 0.0
x1 = 100.0
y1 = 100.0
geom = pm.opencascade.Geometry()
rec = geom.add_rectangle([x0,y0,0],x1,y1)
msh=pm.generate_mesh(geom)
cells_tri=np.vstack((cell.data for cell in msh.cells if cell.type =="triangle"))
mesh = Mesh()
editor = MeshEditor()
editor.open(mesh, 'triangle', 2, 2)
editor.init_vertices(len(msh.points))
editor.init_cells(len(cells_tri))
for i, point in enumerate(msh.points):
new_point = point[:-1]*(np.random.random()*0.01-0.095)
editor.add_vertex(i, new_point)
for i, cell in enumerate(cells_tri):
editor.add_cell(i, cell)
editor.close()
fig, ax = plt.subplots(1,2,figsize=(15,8))
ax1, ax2 = ax
ax1.triplot(msh.points[:, 0], msh.points[:, 1], cells_tri, "-o")
ax1.set_title(r"Old Mesh", fontsize=18)
fig.sca(ax2)
plot(mesh, "-o")
ax2.set_title(r"Moved Mesh", fontsize=18)
fig.tight_layout()
plt.show()
You could add your points in x
and y
to the modified list of points so long as you provide the corresponding cells. Alternatively you could also use add_point
in pygmsh
with something like
p1 = geom.add_point([10., 13., 0.0], lcar) #lcar is the characteristic length
see here
4 Likes
Thank you so much this was very helpful to me.