How do I add or move mesh vertices to specific coordinates?

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