# Save fields under binary format

**URL:** <https://community.freefem.org/t/save-fields-under-binary-format/939>\
**Category:** General Discussion\
**Created:** [April 22, 2021, 4:04pm UTC](https://community.freefem.org/t/save-fields-under-binary-format/939 "2021-04-22T16:04:52Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![adrienrvr](https://avatars.discourse-cdn.com/v4/letter/a/5daacb/32.png) [@adrienrvr](https://community.freefem.org/u/adrienrvr)\
**Post date:** [April 22, 2021, 4:04pm UTC](https://community.freefem.org/t/save-fields-under-binary-format/939/1 "2021-04-22T16:04:52Z")

</div>

Hi every one,

I would like to export my data under binary format. I use

> {ofstream export(“BF\_binary.btxt”, binary);  
> export \<\< Ux \<\< endl;  
> export \<\< Rd1 \<\< endl;  
> }

but the resulting file _BF\_binary.btxt_ is not a binary file.

To overcome this, I tried to use medit using

> savesol(“Medit.solb”,Th, [Ux,Uy,P], Rd1);

and then

> uu = readsol(“Medit.sol”);

but the solution I read with _readsol_ does not match the one I saved earlier. I see it by a plotting it.

Any solution please ?

Regards,

Adrien

---

<div class="post-metadata">

**Author:** ![fivanci](https://avatars.discourse-cdn.com/v4/letter/f/e95f7d/32.png) [@fivanci](https://community.freefem.org/u/fivanci)\
**Post date:** [April 23, 2021, 5:11am UTC](https://community.freefem.org/t/save-fields-under-binary-format/939/2 "2021-04-23T05:11:29Z")

</div>

Hi !

I have tried your first approach, namely

> [@adrienrvr](#):
>
> {ofstream export(“BF\_binary.btxt”, binary);  
> export \<\< Ux \<\< endl;  
> export \<\< Rd1 \<\< endl;  
> }

and it seems that indeed it saves the date in ordinary .txt format.

Regarding your second approach with “savesol”, you need to be more specific with your choice of “fespace” for “[Ux,Uy,P]”, what is “uu” (from which fespace) and what is Rd1 (integer or fe function) ? Here is also the reference for using “savesol” - [Developers](https://doc.freefem.org/documentation/developers.html#sol-and-solb-file) - I couldn’d really find the “readsol” in documentation (it says “Todo” in [External libraries](https://doc.freefem.org/references/external-libraries.html#readsol), I guess the documentation on this topic isn’t completed yet).

From [Developers](https://doc.freefem.org/documentation/developers.html#sol-and-solb-files) it appears that “order” in “savesol” has to be 0 or 1 (and no other) - are your finite element spaces all P1 or P0 ? This might be the first issue.

1. I assume “int Rd1=1”, i.e. “Rd1” is “order” in your code above. The code below works well for me (only one vector finite element space)

2. In case your “Rd1” is finite element function from scalar fespace (P1). In this case you have two different fespaces of order P1, one vector fespace and one scalar. The following code seems to work well for me:

So, “savesol” saves your functions as

```
    uh[][k] vhx[][2k] vhx[][2k+1] ... for k = 0 : Uh.ndof

```

note, Vh.ndof = 2\*Uh.ndof. Also, you might want to look into how dofs are numbered for vector fespaces.

1. As a general suggestion, its not a bad idea to create very coarse mesh and fespaces on it, and savesol in txt format and then you can personally investigate what is going on.

You can also check your options with vtk/vtu format (for visualizetion in Paraview) - [External libraries](https://doc.freefem.org/references/external-libraries.html#savevtk). Documentation is very clear here.

if

---

<div class="post-metadata">

**Author:** ![adrienrvr](https://avatars.discourse-cdn.com/v4/letter/a/5daacb/32.png) [@adrienrvr](https://community.freefem.org/u/adrienrvr)\
**Post date:** [April 23, 2021, 8:44am UTC](https://community.freefem.org/t/save-fields-under-binary-format/939/3 "2021-04-23T08:44:15Z")

</div>

Thank you very much for this precise answer!

In my case, [Ux, Uy, P] are fields respectively [P2, P2, P1] and Rd1 is an integer. The fact of using P2 fields is surely the source of my error then.

Adrien

---

<div class="post-metadata">

**Author:** ![frederichecht](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/frederichecht/32/15_2.png) [@frederichecht](https://community.freefem.org/u/frederichecht)\
**Post date:** [April 23, 2021, 1:43pm UTC](https://community.freefem.org/t/save-fields-under-binary-format/939/4 "2021-04-23T13:43:23Z")

</div>

Effectivelly,

for finite element [P2,P2,P1], to see which dof are asocial to field P2,P2 ou P1

```
   mesh Th= ...
   fespace Vh(Th,[P2,P2,P1]);
   fespace Uh(Th,P2);
   fespace Ph(Th,P1);
  Uh un;
  Ph pn;
  un[]=0:un[].n-1; // value 0, 1, .... 
  pn[]=0:p[].n-1; // value 0, 1, .... 

  Vh [u1,u2,p]=[1,2,3];
  Vh [v1,v2,q]=[un,un,pn]; 

  cout << u1[] << endl; 
 if( u1[][dof] == 1) dof associated to u1 is v1[][dof] in P2 
 if( u1[][dof] == 2) dof associated to u2 is v1[][dof] in P2 
 if( u1[][dof] == 3) dof associated to u3 is is u1[][dof] in P1
```
