# Heat equation with PETSc : How can the global solution be obtained from the solutions on the subdomains?

**URL:** <https://community.freefem.org/t/heat-equation-with-petsc-how-can-the-global-solution-be-obtained-from-the-solutions-on-the-subdomains/4329>\
**Category:** General Discussion\
**Created:** [July 9, 2026, 12:43pm UTC](https://community.freefem.org/t/heat-equation-with-petsc-how-can-the-global-solution-be-obtained-from-the-solutions-on-the-subdomains/4329 "2026-07-09T12:43:46Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![chalmi](https://avatars.discourse-cdn.com/v4/letter/c/4af34b/32.png) [@chalmi](https://community.freefem.org/u/chalmi)\
**Post date:** [July 9, 2026, 12:43pm UTC](https://community.freefem.org/t/heat-equation-with-petsc-how-can-the-global-solution-be-obtained-from-the-solutions-on-the-subdomains/4329/1 "2026-07-09T12:43:46Z")

</div>

Hi FreeFem community,

I am currently using PETSc to solve the transient heat equation (`DMeshCreate`), but at each time step I need to compute cost functionals, which require the global solution. How can I obtain the global solution from the local solutions?

Additionally, I would like to know how to save this global solution for visualization in ParaView.

Thank you in advance for your answers.

---

<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:** [July 9, 2026, 6:24pm UTC](https://community.freefem.org/t/heat-equation-with-petsc-how-can-the-global-solution-be-obtained-from-the-solutions-on-the-subdomains/4329/2 "2026-07-09T18:24:01Z")

</div>

> [@chalmi](#):
>
> but at each time step I need to compute cost functionals, which require the global solution

I doubt that’s true.

> How can I obtain the global solution from the local solutions?

Search for `N2O` in the `examples/hpddm` folder.

---

<div class="post-metadata">

**Author:** ![fb77](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/fb77/32/3796_2.png) [@fb77](https://community.freefem.org/u/fb77)\
**Post date:** [July 10, 2026, 5:43pm UTC](https://community.freefem.org/t/heat-equation-with-petsc-how-can-the-global-solution-be-obtained-from-the-solutions-on-the-subdomains/4329/3 "2026-07-10T17:43:00Z")

</div>

When you say you want to compute a cost functional, probably it implies that you have to compute an integral over the whole domain. There is a standard way to do it: compute each integral in each subdomain, and add up all of them. However, doing that you have to introduce a partition of unity in order to avoid taking twice the integral on the overlapping regions.  
The typical code lines for that are

```auto
//build partition of unity
fespace Vhpart(Th, P0); // P0 elements for partition function
Vhpart part; // Partition function
PartitionCreate(Th, part[], P0); // Create partition using the mesh Th, part[] takes values 0 or 1

real localint = int2d(Th)(part*sol);//integral over the subdomain, without overlapping
real globalint;//global integral (has to be computed)
mpiAllReduce(localint, globalint, mpiCommWorld, mpiSUM);//sum up the local integrals to get the global integral
if (mpirank==0) cout << "The global integral is " << globalint << endl;

```

About visualization with paraview, you can do as (for example)

```auto
fespace V1(Th,P1);
V1 sol;// unknown in P1
sol=(x<0.5)? 0. : 1.;
int[int] myorder=[1];
savevtk("sol.vtu",Th,sol,order=myorder,bin=1);

```

It will create several files: sol\_nbproc.pvd where nbproc is the number of process, and the files sol\_nbproc\_…vtu, one for each proc.  
The file you have to load with paraview is the master file sol\_nbproc.pvd (it will load all the proc files together).

---

<div class="post-metadata">

**Author:** ![chalmi](https://avatars.discourse-cdn.com/v4/letter/c/4af34b/32.png) [@chalmi](https://community.freefem.org/u/chalmi)\
**Post date:** [August 31, 2026, 9:09am UTC](https://community.freefem.org/t/heat-equation-with-petsc-how-can-the-global-solution-be-obtained-from-the-solutions-on-the-subdomains/4329/4 "2026-08-31T09:09:23Z")

</div>

Many many thanks!! It works perfectly.
