# Output velocity in txt form with PETSc (transfer values from one mesh to another)

**URL:** <https://community.freefem.org/t/output-velocity-in-txt-form-with-petsc-transfer-values-from-one-mesh-to-another/1174>\
**Category:** General Discussion\
**Created:** [August 24, 2021, 10:13pm UTC](https://community.freefem.org/t/output-velocity-in-txt-form-with-petsc-transfer-values-from-one-mesh-to-another/1174 "2021-08-24T22:13:02Z")\
**Posts on this page:** 8\
**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:** [August 24, 2021, 10:13pm UTC](https://community.freefem.org/t/output-velocity-in-txt-form-with-petsc-transfer-values-from-one-mesh-to-another/1174/1 "2021-08-24T22:13:02Z")

</div>

Dear all,

I am running a Navier-stokes 2d problem with PETSc. Now I need to export velocity [ux, uy] in .txt format. I have followed the video ([[Paraview Tutorial 4] How to display node and elements data in Paraview - YouTube](https://www.youtube.com/watch?v=XdB4hNU2XSY)) to convert the information from local partitions to global mesh and save it in .txt format (using `ofstream`). However, the number of values I get is more than 140000 which is way higher than the number of triangles (30920) and the number of vertices (15761). I want to ask the following questions:

1. How does the field data (like velocity) saved in FreeFem? Is it saved as a value per cell or per vertex? I am confused by the large number of values saved by the `ofstream` function. It is not supposed to be that much.

2. If the field data is associated to the node or elements, how do I output the data with the sequence of cell numbering or node number?

3. My ultimate goal is to output the data from one 240 \* 60 rectangular mesh domain to a smaller irregular mesh domain. This small domain is actually a part of the larger domain but the meshes are different (as shown below). I am thinking to do like this: output value+output corresponding coordinates and then do interpolation to the new mesh. it is a bit cumbersome. Is there any clever way to do it in freefem?

![Screen Shot 2021-08-25 at 12.00.15 AM](https://canada1.discourse-cdn.com/flex030/uploads/freefem/original/1X/72b419c4b1c416c054e37508cd63608d4db1bf1a.png)

 ![Screen Shot 2021-08-25 at 12.09.28 AM](https://canada1.discourse-cdn.com/flex030/uploads/freefem/original/1X/be645f9d1c3d05505c6b8a2b43d448a6925cd329.jpeg)

Thank you very much!

---

<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:** [August 25, 2021, 6:24am UTC](https://community.freefem.org/t/output-velocity-in-txt-form-with-petsc-transfer-values-from-one-mesh-to-another/1174/2 "2021-08-25T06:24:41Z")

</div>

Why are you not interpolating the velocity field directly in FreeFEM? Why are you not using the `.vtu` format if you want to postprocess results 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:** [August 25, 2021, 10:29am UTC](https://community.freefem.org/t/output-velocity-in-txt-form-with-petsc-transfer-values-from-one-mesh-to-another/1174/3 "2021-08-25T10:29:35Z")

</div>

Hi, I actually want to interpolate this field directly in FreeFEM. This is not a post-process for me. These are two meshes for two simulations, and I want to run these two simulations in terms while passing the velocity in between them. I have checked the `interpolation` matrix before, but since one of my meshes is irregular, I don’t know how to use it on my meshes.

---

<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:** [August 25, 2021, 10:32am UTC](https://community.freefem.org/t/output-velocity-in-txt-form-with-petsc-transfer-values-from-one-mesh-to-another/1174/4 "2021-08-25T10:32:48Z")

</div>

Like with any other mesh, see the example at the bottom of this section [Finite element](https://doc.freefem.org/documentation/finite-element.html#a-fast-finite-element-interpolator).

---

<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:** [August 25, 2021, 5:45pm UTC](https://community.freefem.org/t/output-velocity-in-txt-form-with-petsc-transfer-values-from-one-mesh-to-another/1174/5 "2021-08-25T17:45:45Z")

</div>

Thank you for the information. The interpolation function works. I have another question regarding the read and write the solutions. Is there any way to save both the solution and mesh and then read them in another `.edp` file?

---

<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:** [August 25, 2021, 6:18pm UTC](https://community.freefem.org/t/output-velocity-in-txt-form-with-petsc-transfer-values-from-one-mesh-to-another/1174/6 "2021-08-25T18:18:49Z")

</div>

Yes, please read the documentation.

---

<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:** [August 31, 2021, 7:08pm UTC](https://community.freefem.org/t/output-velocity-in-txt-form-with-petsc-transfer-values-from-one-mesh-to-another/1174/8 "2021-08-31T19:08:59Z")

</div>

Thank you! I am now working on the Navier Stokes equation with some external force in parallelization, the code I am using is:

```auto
varf navierstokes ( [u, v, p], [uh, vh, ph]) =
   int2d(Th) ( rho * u * uh / dt )
 - int2d(Th) ( rho * convect ([uold,vold], -dt, uold) * uh / dt )
 + int2d(Th) ( rho * v * vh / dt )
 - int2d(Th) ( rho * convect ([uold,vold], -dt, vold) * vh / dt )
 + int2d(Th) ( nu * dx(u) * dx(uh) + nu * dy(u) * dy(uh) )
 + int2d(Th) ( nu * dx(v) * dx(vh) + nu * dy(v) * dy(vh) )
 + int2d(Th) ( kcoe * u * uh)
 + int2d(Th) ( kcoe * v * vh)
 - int2d(Th) ( p * dx(uh) + p * dy(vh) )
 - int2d(Th) ( dx(u) * ph + dy(v) * ph )
 // + int1d(Th, Outlet) ( nu * dx(u) * N.x * uh + nu * dy(u) * N.y * uh )
 // + int1d(Th, Outlet) ( nu * dx(v) * N.x * vh + nu * dy(v) * N.y * vh )
 + on ( Wall, u=0.0, v=0.0 )
 + on ( Outlet, p=0 )
 + on ( Inlet, u=uin, v=0);

set(NS, sparams = "-pc_type lu");

```

However, when I run it with `nu = 0.00002`, and some coefficients `kcoe < 1e-5`, it will show an error like

```auto
nan != nan diff nan => Sorry error in Optimization add: (p) int2d(Th,optimize=0)(...)
 remark if you add (.. , optimize=2) then you remove this check (be carefull); 
  current line = 239 mpirank 1 / 4

```

but when `nu = 0.0002`, it can run successfully. And if I add `optimize=0` to the integrals, it can run but I get null values. So is the problem is one of the coefficients in the Navier Stokes equation is too small? Is there any other way that I can fix this problem?

---

<div class="post-metadata">

**Author:** ![Qianrui](https://avatars.discourse-cdn.com/v4/letter/q/9fc348/32.png) [@Qianrui](https://community.freefem.org/u/Qianrui)\
**Post date:** [January 15, 2022, 3:06am UTC](https://community.freefem.org/t/output-velocity-in-txt-form-with-petsc-transfer-values-from-one-mesh-to-another/1174/9 "2022-01-15T03:06:51Z")

</div>

Hello, I have a similar problem, what is the cause? Seek guidance
