# Find dofs from VectorFunctionSpace using mpi with n \>=2

**URL:** <https://fenicsproject.discourse.group/t/find-dofs-from-vectorfunctionspace-using-mpi-with-n-2/3440>\
**Category:** mesh\
**Created:** [June 5, 2020, 1:03am UTC](https://fenicsproject.discourse.group/t/find-dofs-from-vectorfunctionspace-using-mpi-with-n-2/3440 "2020-06-05T01:03:12Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Yuri-Albuquerque](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/yuri-albuquerque/32/1599_2.png) [@Yuri-Albuquerque](https://fenicsproject.discourse.group/u/Yuri-Albuquerque)\
**Post date:** [June 5, 2020, 1:03am UTC](https://fenicsproject.discourse.group/t/find-dofs-from-vectorfunctionspace-using-mpi-with-n-2/3440/1 "2020-06-05T01:03:12Z")

</div>

Hi, I want to run my code with mpi4py, but I’m having some troubles with `vertex_to_dof_map(dolfin.VectorFunctionSpace)`. Here is part of my code:

```python
from dolfin import *
from mpi4py import MPI as pyMPI
comm = pyMPI.COMM_WORLD
nx = 100
nz = 65
mesh = RectangleMesh(comm, Point(0.0, 0.0), Point(1.0, 0.65), nx-1, nz-1)
VF = VectorFunctionSpace(mesh, "Lagrange", 1)
vf_dim = VF.dim()
theta = TrialFunction(VF)
v2d = vertex_to_dof_map(VF)
theta_vec = theta.vector()[v2d]
... 

```

When I run with:  
mpiexec -n 2 python3 .py  
`VF.dim()` returns 13000 points, witch is perfect and correct. But `theta.vector()[:].shape` has only 6502 positions. Where is the other half of theta.vector()[:]? Besides, `vertex_to_dof_map(VF)` also returns 6560 dof positions and i don’t know how to get the other dofs.

But when i run with:  
mpiexec -n 1 python3 .py  
Everything works fine and returns all the 13000 points.

I know this questions can be extremely basic, but I am a mathematician and I’m trying to use FEniCS to compute shape optimization algorithms. Anyway, if you can help me, (please) be patience and explain each step in details 😄

---

<div class="post-metadata">

**Author:** ![dokken](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/dokken/32/1560_2.png) [@dokken](https://fenicsproject.discourse.group/u/dokken)\
**Post date:** [June 5, 2020, 6:34am UTC](https://fenicsproject.discourse.group/t/find-dofs-from-vectorfunctionspace-using-mpi-with-n-2/3440/2 "2020-06-05T06:34:48Z")

</div>

When you run your code in parallel, your mesh is partitioned and distributed over multiple processors. Consider the following illustration:

```auto
from dolfin import *
from mpi4py import MPI as pyMPI
comm = pyMPI.COMM_WORLD
nx = 100
nz = 65
mesh = RectangleMesh(comm, Point(0.0, 0.0), Point(1.0, 0.65), nx-1, nz-1)
VF = VectorFunctionSpace(mesh, "Lagrange", 1)
vf_dim = VF.dim()
theta = Function(VF)
v2d = vertex_to_dof_map(VF)

theta_vec = theta.vector()
print(comm.rank, theta_vec.local_size(), theta_vec.local_range())

```

This prints:

```auto
0 6502 (0, 6502)
1 6498 (6502, 13000)

```

Meaning that processor one holds 6502 dofs, which globally are the first 6502 dofs in the dof map. The second processor holds the remaining 6498 dofs.

However, in general, as you consider shape optimization problems, you should consider using [dolfin-adjoint](http://www.dolfin-adjoint.org/en/latest/), which has examples ([Stokes-problem](http://www.dolfin-adjoint.org/en/latest/documentation/stokes-shape-opt/stokes_problem.html) and [time dependent heat equation](http://www.dolfin-adjoint.org/en/latest/documentation/tube-shape-derivative/tube-shape-derivative.html) of automatically deriving discretely consistent shape derivatives. For even more details, see [this arxiv preprint](https://arxiv.org/pdf/2001.10058.pdf).

---

<div class="post-metadata">

**Author:** ![Yuri-Albuquerque](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/yuri-albuquerque/32/1599_2.png) [@Yuri-Albuquerque](https://fenicsproject.discourse.group/u/Yuri-Albuquerque)\
**Post date:** [June 5, 2020, 6:04pm UTC](https://fenicsproject.discourse.group/t/find-dofs-from-vectorfunctionspace-using-mpi-with-n-2/3440/3 "2020-06-05T18:04:12Z")

</div>

Thank you sir for the precise answer! I’m reading carefully all literature you recommended. It is nice to know that you made a recent contribution on shape optimization. I’m PhD candidate in Applied Mathematics at University of Sao Paulo - Brazil and Shape Optimization is one of my area of interest which I’m in focus now. I sure that this article you recommended will help me a lot.
