Applying linear transformation on mesh

Hi!
I’m working with a 3D model from an XDMF file on dolfinx and would like to apply the transformation r_0(x-x_0) to the mesh.

with XDMFFile(MPI.COMM_WORLD, "model.xdmf", "r") as xdmf:
    mesh = xdmf.read_mesh(name="Grid")
    ct = xdmf.read_meshtags(mesh, name="Grid")

That is, from mesh, I would like to obtain a new_mesh such that new_mesh=r_0*(mesh-x_0).

Thanks in advance.

1 Like

You can directly access the geometrical coordinates of the mesh with mesh.geometry.x, which returns you a numpy array. You can modify this array with any transformation or reassignment, i.e. consider:

import dolfinx
from mpi4py import MPI
import numpy as np
mesh = dolfinx.mesh.create_unit_square(MPI.COMM_WORLD, 10, 10)
x_copy = mesh.geometry.x
mesh.geometry.x[:, 0] = np.cos(x_copy[:,0])
with dolfinx.io.XDMFFile(mesh.comm, "u.xdmf", "w") as xdmf:
    xdmf.write_mesh(mesh)
1 Like

Thank you very much!