# How to use interpolate and assign functions in parallel?

Hi,

I would like to use `interpolate` and `assign` functions in a simulation that runs in parallel.
So far I have been able to use both functions in parallel without problem. However, I came across two issues when I tried to run my application on a different mesh. These issues only occur when running in parallel (I use `mpirun -n 2 python mycode.py`) and seem to be somehow mesh-related since the same code runs without problems on a different mesh.

Below is a MWE that reproduces the two errors.
The mesh files `biX2_2D.xdmf` and `biX2_2D.h5` can be downloaded from here.

``````from dolfin import *
from mshr import *

# definition of domain and mesh
# domain = Rectangle(Point(-2*pi, -2*pi), Point(2*pi, 2*pi))
# mesh = generate_mesh(domain, 800)
mesh = Mesh() #Mesh("biX2_2D.xml")
f = XDMFFile(MPI.comm_world, "biX2_2D.xdmf")
print("Number of cells =", mesh.num_cells())

# definition of FunctionSpace and Functions
Vd = VectorFunctionSpace(mesh, "CG", 1, dim=2)
d = Function(Vd, name="Damage")
d_prev = Function(Vd, name="Previous Damage")

# some expression
f0 = Expression(('cos(x[0])', 'sin(x[1])'), degree=1)

# comment line 26 and uncomment line 30 to get rid of
# the first error with interpolate by using project instead
# this error only occurs when running in parallel, e.g. with mpirun -n 2 python
# *** Error:   Unable to interpolate function into function space.
# *** Reason:  Wrong size of vector.
d = interpolate(f0,Vd)

# using project instead of interpolate allows to get rid of the previous error
# and to move to the next error obtained with the function assign
#d = project(f0,Vd)

# the second error also occurs only when running in parallel
# *** Error:   Unable to collapse function space.
# *** Reason:  Function space is not a subspace.
d_prev.assign(d)
``````

If the rectangle mesh (commented out in the MWE) is used instead of `biX2_2D.xdmf`, then both functions `interpolate` and `assign` do not produce any errors and seem to work fine.
The full error messages are as follows:

``````Traceback (most recent call last):
File "/home/scherer/Documents/Postdoc/GitLab/polycrystals/damage_gradient/assign_bug.py", line 28, in <module>
Traceback (most recent call last):
File "/home/scherer/Documents/Postdoc/GitLab/polycrystals/damage_gradient/assign_bug.py", line 28, in <module>
d = interpolate(f0,Vd)
File "/home/scherer/anaconda3/envs/fenics/lib/python3.9/site-packages/dolfin/fem/interpolation.py", line 71, in interpolate
d = interpolate(f0,Vd)
File "/home/scherer/anaconda3/envs/fenics/lib/python3.9/site-packages/dolfin/fem/interpolation.py", line 71, in interpolate
Pv.interpolate(v._cpp_object)
File "/home/scherer/anaconda3/envs/fenics/lib/python3.9/site-packages/dolfin/function/function.py", line 365, in interpolate
Pv.interpolate(v._cpp_object)
File "/home/scherer/anaconda3/envs/fenics/lib/python3.9/site-packages/dolfin/function/function.py", line 365, in interpolate
self._cpp_object.interpolate(u)
RuntimeError:

*** -------------------------------------------------------------------------
*** DOLFIN encountered an error. If you are not able to resolve this issue
*** using the information listed below, you can ask for help at
***
***
*** Remember to include the error message listed below and, if possible,
*** include a *minimal* running example to reproduce the error.
***
*** -------------------------------------------------------------------------
*** Error:   Unable to interpolate function into function space.
*** Reason:  Wrong size of vector.
*** Where:   This error was encountered inside FunctionSpace.cpp.
*** Process: 0
***
*** DOLFIN version: 2019.1.0
*** Git changeset:  640ef70247ae212e16ae9f24fc9bc603b506f78a
*** -------------------------------------------------------------------------

self._cpp_object.interpolate(u)
``````

and

``````Traceback (most recent call last):
File "/home/scherer/Documents/Postdoc/GitLab/polycrystals/damage_gradient/assign_bug.py", line 35, in <module>
File "/home/scherer/Documents/Postdoc/GitLab/polycrystals/damage_gradient/assign_bug.py", line 35, in <module>
d_prev.assign(d)
File "/home/scherer/anaconda3/envs/fenics/lib/python3.9/site-packages/dolfin/function/function.py", line 408, in assign
d_prev.assign(d)
File "/home/scherer/anaconda3/envs/fenics/lib/python3.9/site-packages/dolfin/function/function.py", line 408, in assign
self._cpp_object._assign(rhs._cpp_object)
RuntimeError:

*** -------------------------------------------------------------------------
*** DOLFIN encountered an error. If you are not able to resolve this issue
*** using the information listed below, you can ask for help at
***
***
*** Remember to include the error message listed below and, if possible,
*** include a *minimal* running example to reproduce the error.
***
*** -------------------------------------------------------------------------
*** Error:   Unable to collapse function space.
*** Reason:  Function space is not a subspace.
*** Where:   This error was encountered inside FunctionSpace.cpp.
*** Process: 0
***
*** DOLFIN version: 2019.1.0
*** Git changeset:  640ef70247ae212e16ae9f24fc9bc603b506f78a
*** -------------------------------------------------------------------------

self._cpp_object._assign(rhs._cpp_object)
``````

Any thoughts on what might be wrong with my code/mesh?

jean-michel