Copying meshes leads to pickle error

Hi all,
I have a very basic question. I updated my FEniCS version from 2017.2 to 2018.1 and now the following does not work any more:

from fenics import *
from copy import deepcopy
mesh = UnitSquareMesh(2, 2)
mesh_copy = deepcopy(mesh)

The error msg. that I get is:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3.6/copy.py", line 169, in deepcopy
    rv = reductor(4)
TypeError: can't pickle dolfin.cpp.generation.UnitSquareMesh objects

Is this intended behavior? What can I do if I still want a copy of a mesh?
Btw: I am using FEniCS via a Docker Img, if that is of importance.

Thanks in advance for any help.

Why don’t you use a copy constructor

from dolfin import *
import numpy as np

mesh = UnitSquareMesh(32, 32)
mesh_ = Mesh(mesh)

assert np.linalg.norm(mesh.coordinates() - mesh_.coordinates()) < 1E-15
# Modify
mesh_.coordinates()[:] += 2

assert np.linalg.norm(mesh.coordinates() - mesh_.coordinates()) > 2
1 Like

I thought I tried that too, but apparently I was to sleepy and was trying

mesh_copy = UnitSquareMesh(mesh)

which of course is not working…

Thanks for pointing me to back to the solution ^^
Can I mark the question as solved somehow?