MPI - Gather and distribute a function to all processes (That I can evaluate at a point)

Consider something as the following. This code first checks if there is any cell on the process containing the point, then, if the point is found evaluates at this point. If the point is not on the process, it returns -1

import numpy as np
from dolfin import *
from IPython import embed

mesh = UnitSquareMesh(32, 32)
V = VectorFunctionSpace(mesh, 'CG', 1)
f_ = Expression(('x[0]+x[1]', 'x[0]'), degree=1)
f = interpolate(f_, V)


tree = BoundingBoxTree()
tree.build(mesh, mesh.topology().dim())
num_cells = mesh.num_cells()

def safe_eval(f, x):
    cell_on_proc = tree.compute_first_entity_collision(Point(x))
    val = np.zeros(2)
    if cell_on_proc < num_cells:
        f.eval_cell(val, x, Cell(mesh, cell_on_proc))
        return val
    else:
        return -1

if MPI.comm_world.rank == 0:
    x = np.array([0.7, 0.9])
elif MPI.comm_world.rank == 1:
    x = np.array([0.1, 0.8])
else:
    x = np.array([0.6, 0.1])

print(MPI.comm_world.rank, x, safe_eval(f, x))
1 Like