Making a copy of mesh domain

Hello,
How can I make a copy of an instance of dolfinx.mesh.Mesh? I tried deepcopy

copy.deepcopy(domain)

----------------------------------
TypeError: cannot pickle 'Mesh' object

and mesh.Mesh()

domain2=mesh.Mesh(MPI.COMM_WORLD,domain.topology,domain.geometry,domain)

It seems to be working fine and for example (as can be seen in the figure below) I can access domain2.geometry.x. But I can’t access this property through dot accessor. Where is the problem and what is the correct way to copy meshes?

Many thanks in advance!

See: Deepcopy of meshes - #2 by dokken

2 Likes

Hello,
I created a mesh and my problem is solved for it. Then I made a copy of this mesh through the code below:

mesh.Mesh(domain.comm,domain.topology,domain.geometry,domain.ufl_domain())

Then I tried to solve the same problem with the original mesh (not the copied mesh) but I get the following error. Does copying a mesh change the mesh itself?

31 Ez = ufl.TrialFunction(V)
     32 v = ufl.TestFunction(V)
---> 34 a = fem.form(
     35     (ufl.inner(ufl.grad(Ez), ufl.grad(v))-k**2*epsilon*Ez*ufl.conj(v)) * ufl.dx \
     36     -1j*k*ufl.sqrt(epsilon)*ufl.inner(Ez, v)* ufl.ds
     37     )
     38 A=fem.petsc.assemble_matrix(a)
     39 A.assemble()

File /usr/local/dolfinx-complex/lib/python3.10/dist-packages/dolfinx/fem/forms.py:168, in form(form, dtype, form_compiler_params, jit_params)
    165         return list(map(lambda sub_form: _create_form(sub_form), form))
    166     return form
--> 168 return _create_form(form)

File /usr/local/dolfinx-complex/lib/python3.10/dist-packages/dolfinx/fem/forms.py:163, in form.<locals>._create_form(form)
...
     52 ffi = cffi.FFI()
---> 53 super().__init__(ffi.cast("uintptr_t", ffi.addressof(self._ufcx_form)),
     54                  V, coeffs, constants, subdomains, mesh)

RuntimeError: Incompatible mesh```