Evaluating an integral when using parallel computing

After solving the variational problem a(u,v) = L(v), I want to evaluate the integral of the quantity u over the domain \Omega, i.e. I want to calculate \alpha(u) = \int_\Omega u \,dx.
Because my problem is quite large, I use:

mpirun -n 4 python3 test.py

Then I calculate the integral via:

alpha = dolfinx.fem.assemble_scalar(dolfinx.fem.form(uh*ufl.dx))
print(alpha)

The problem is that this is evaluated for each processor separately, so the result is not correct. I tried:

if mpi4py.MPI.COMM_WORLD.rank == 0:
   alpha = dolfinx.fem.assemble_scalar(dolfinx.fem.form(uh*ufl.dx))
   print(alpha)

This creates a sort of infinite loop, the computation never ends.

Any idea how to compute the integral correctly?

See for instance: Implementation — FEniCSx tutorial

1 Like