Issue with modification of solution vector in parallel implementation

I am trying to modify my solution vector based on a condition, the condition being if anywhere over the mesh the function has a value greater than 1 then I would like to bound that value to one before writing to the xdmf file.
u.vector().vec().array[u.vector()[:] > 1] = 1
Here is the minimum code

from dolfin import *

# MPI functions 
comm = MPI.comm_world
xdmf = XDMFFile(comm,"output/disp.xdmf")

# Create mesh and define function space
mesh = UnitSquareMesh(80, 80)
V = FunctionSpace(mesh, 'P', 1)

u_D = Expression('2*sin(M_PI *x[0])', degree=1)
u = project(u_D, V) # supposing this is the solution from a solver

u.vector().vec().array[u.vector()[:] > 1] = 1 # This is not working for parallel implementation 
xdmf.write(u)

The above code works perfectly fine in serial, but breaks in parallel.
I have also attached the contour of solution for both serial and parallel implementation.

Any help will be highly appreciated. Thank you.

Don’t forget to update the ghost values:

u.vector().vec().array[u.vector()[:] > 1] = 1 # This is not working for parallel implementation 
u.vector().update_ghost_values()
2 Likes

Thank you so much Nate. It is working :blush: