# Computing divergence/gradient under parallelization

**URL:** <https://community.freefem.org/t/computing-divergence-gradient-under-parallelization/2915>\
**Category:** General Discussion\
**Created:** [January 30, 2024, 12:54pm UTC](https://community.freefem.org/t/computing-divergence-gradient-under-parallelization/2915 "2024-01-30T12:54:24Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![yhao](https://avatars.discourse-cdn.com/v4/letter/y/5fc32e/32.png) [@yhao](https://community.freefem.org/u/yhao)\
**Post date:** [January 30, 2024, 12:54pm UTC](https://community.freefem.org/t/computing-divergence-gradient-under-parallelization/2915/1 "2024-01-30T12:54:24Z")

</div>

Hi,

I have a question regarding computing gradient when the domain is decomposed and paralleled. I solved a solid mechanics problem and got the corresponding u which is the displacement solution. I want to compute dx(u1)+dy(u2)+dz(u3) which is the volume strain of the object.  
I find that the result I get in a parallel context differs from the result when I mpiallreduce the displacement to global and then compute the volume strain. I am guessing the global one is the correct one. Is there a way to properly compute the gradient of a vector field under parallelization with PETSc?

Thank you!

---

<div class="post-metadata">

**Author:** ![prj](https://avatars.discourse-cdn.com/v4/letter/p/ecae2f/32.png) [@prj](https://community.freefem.org/u/prj)\
**Post date:** [January 30, 2024, 1:46pm UTC](https://community.freefem.org/t/computing-divergence-gradient-under-parallelization/2915/2 "2024-01-30T13:46:41Z")

</div>

Are you properly updating the values on the ghost elements? How do you handle the multiple summations on values off the ghost elements?

---

<div class="post-metadata">

**Author:** ![yhao](https://avatars.discourse-cdn.com/v4/letter/y/5fc32e/32.png) [@yhao](https://community.freefem.org/u/yhao)\
**Post date:** [January 30, 2024, 8:31pm UTC](https://community.freefem.org/t/computing-divergence-gradient-under-parallelization/2915/3 "2024-01-30T20:31:34Z")

</div>

Hi prj,

Thank you for your response. With parallelization, I just do dx(u1)+dy(u2)+dz(u3). As you mentioned, the ghost values need to be properly handled in this case. But I have no idea how to do it. Do you have any examples that I can refer to?

---

<div class="post-metadata">

**Author:** ![prj](https://avatars.discourse-cdn.com/v4/letter/p/ecae2f/32.png) [@prj](https://community.freefem.org/u/prj)\
**Post date:** [January 30, 2024, 8:40pm UTC](https://community.freefem.org/t/computing-divergence-gradient-under-parallelization/2915/4 "2024-01-30T20:40:25Z")

</div>

For example, [FreeFem-sources/examples/hpddm/diffusion-2d-PETSc-complex.edp at 220957ed86797a6c1ed771f1fe1c27078e94f28a · FreeFem/FreeFem-sources · GitHub](https://github.com/FreeFem/FreeFem-sources/blob/220957ed86797a6c1ed771f1fe1c27078e94f28a/examples/hpddm/diffusion-2d-PETSc-complex.edp#L28).

---

<div class="post-metadata">

**Author:** ![yhao](https://avatars.discourse-cdn.com/v4/letter/y/5fc32e/32.png) [@yhao](https://community.freefem.org/u/yhao)\
**Post date:** [January 30, 2024, 9:42pm UTC](https://community.freefem.org/t/computing-divergence-gradient-under-parallelization/2915/5 "2024-01-30T21:42:40Z")

</div>

The problem is resolved! Thank you so much!

---

<div class="post-metadata">

**Author:** ![yhao](https://avatars.discourse-cdn.com/v4/letter/y/5fc32e/32.png) [@yhao](https://community.freefem.org/u/yhao)\
**Post date:** [January 31, 2024, 5:49pm UTC](https://community.freefem.org/t/computing-divergence-gradient-under-parallelization/2915/6 "2024-01-31T17:49:34Z")

</div>

Hi prj,

I met a few problems related to this exchange function:

1. It seems exchange() does not work for P0 elements.
2. If I have a vector field u based on P1 and I compute the gradient dx(u[0]), would it be better to assign this gradient to a P0 element, i.e. f=dx(u[0]) and f is based on P0?

---

<div class="post-metadata">

**Author:** ![prj](https://avatars.discourse-cdn.com/v4/letter/p/ecae2f/32.png) [@prj](https://community.freefem.org/u/prj)\
**Post date:** [January 31, 2024, 6:06pm UTC](https://community.freefem.org/t/computing-divergence-gradient-under-parallelization/2915/7 "2024-01-31T18:06:28Z")

</div>

1. It should work, do you have a reproducer?
2. Yes.

---

<div class="post-metadata">

**Author:** ![yhao](https://avatars.discourse-cdn.com/v4/letter/y/5fc32e/32.png) [@yhao](https://community.freefem.org/u/yhao)\
**Post date:** [January 31, 2024, 6:46pm UTC](https://community.freefem.org/t/computing-divergence-gradient-under-parallelization/2915/8 "2024-01-31T18:46:21Z")

</div>

Hi,  
Here I attach the files to reproduce the result. It reads local solutions (with 4 cores) and meshes then computes the gradient based on the solution. However, inconsistency can be still found in the vtk result after the implementation of exchange.  
[test.zip](https://community.freefem.org/uploads/short-url/l54fQmcBV89QAfhRD9eq7rG9YAT.zip) (1.5 MB)

---

<div class="post-metadata">

**Author:** ![prj](https://avatars.discourse-cdn.com/v4/letter/p/ecae2f/32.png) [@prj](https://community.freefem.org/u/prj)\
**Post date:** [January 31, 2024, 7:17pm UTC](https://community.freefem.org/t/computing-divergence-gradient-under-parallelization/2915/9 "2024-01-31T19:17:16Z")

</div>

What inconsistency are you talking about? You are exporting a piecewise constant function as a piecewise linear one, so that is probably why you are seeing inconsistencies. If I fix the order of the output function, I don’t see inconsistencies in ParaView.

---

<div class="post-metadata">

**Author:** ![yhao](https://avatars.discourse-cdn.com/v4/letter/y/5fc32e/32.png) [@yhao](https://community.freefem.org/u/yhao)\
**Post date:** [January 31, 2024, 9:13pm UTC](https://community.freefem.org/t/computing-divergence-gradient-under-parallelization/2915/10 "2024-01-31T21:13:27Z")

</div>

Thank you! Now I understand the issue.
