# Save a DG vector field as a. csv file

**URL:** <https://fenicsproject.discourse.group/t/save-a-dg-vector-field-as-a-csv-file/15468>\
**Category:** General\
**Created:** [August 7, 2024, 6:06pm UTC](https://fenicsproject.discourse.group/t/save-a-dg-vector-field-as-a-csv-file/15468 "2024-08-07T18:06:21Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![wanghao](https://avatars.discourse-cdn.com/v4/letter/w/96bed5/32.png) [@wanghao](https://fenicsproject.discourse.group/u/wanghao)\
**Post date:** [August 7, 2024, 6:06pm UTC](https://fenicsproject.discourse.group/t/save-a-dg-vector-field-as-a-csv-file/15468/1 "2024-08-07T18:06:21Z")

</div>

Hi ,guys!!  
I am writing the output file to a **csv** file.  
At first, I output _ **u. bp4** _ file.  
Import **u.bp4** into **Paraview** , and then export it as **u.csv** using Paraview.  
This is my **first** method of obtaining csv files.

The **second** method is to output directly in **dolfinx**  
code are as follows:

> WW = fem.functionspace(msh, (“Discontinuous Lagrange”, 2, (gdim,)))

> dof\_coordinates = WW.tabulate\_dof\_coordinates()  
> out = open(“u.csv”, “w”)  
> for x, val in zip(dof\_coordinates, u\_vis.x.array):  
> print(f"{x[0]},{x[1]},{x[2]},{val} ", file=out)  
> out.close()  
> But what format do I want：

> X , Y , Z , u\_x ,u\_y , u\_z  
> 1 , 1 , 2 , 0.1 , 0.2 , 0.3  
> 1 , 2 , 3 , 0.2 , 0.2 , 0.3

where u\_x is x component of u.  
**How can I obtain the component of u?**

I found that the number of coordinate points in the **u. csv** file obtained by method 1 is higher than method 2.  
The visual results of the two are the same.This means that there are duplicate points being output, which is caused by DG.  
Is there an explanation for this phenomenon？

---

<div class="post-metadata">

**Author:** ![dokken](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/dokken/32/1560_2.png) [@dokken](https://fenicsproject.discourse.group/u/dokken)\
**Post date:** [August 7, 2024, 6:23pm UTC](https://fenicsproject.discourse.group/t/save-a-dg-vector-field-as-a-csv-file/15468/2 "2024-08-07T18:23:45Z")

</div>

> [@wanghao](#):
>
> How can I obtain the component of u?

Use `u_vis.x.array.reshape(-1,gdim)`  
which will order the values in (x,y,z) order for each coordinate.

> [@wanghao](#):
>
> The visual results of the two are the same.This means that there are duplicate points being output, which is caused by DG.  
> Is there an explanation for this phenomenon？

Is this the result from running your code in parallel? If so, what ghost mode are you using?

As you haven’t provided a minimal reproducible example (say on a unit cube), it is hard to give further guidance.

---

<div class="post-metadata">

**Author:** ![wanghao](https://avatars.discourse-cdn.com/v4/letter/w/96bed5/32.png) [@wanghao](https://fenicsproject.discourse.group/u/wanghao)\
**Post date:** [August 8, 2024, 5:36am UTC](https://fenicsproject.discourse.group/t/save-a-dg-vector-field-as-a-csv-file/15468/3 "2024-08-08T05:36:34Z")

</div>

Thanks! dokken !

> [@wanghao](#):
>
> The visual results of the two are the same.This means that there are duplicate points being output, which is caused by DG.  
> Is there an explanation for this phenomenon？

As you think, it is indeed caused by parallel operation.The extra nodes are ghost nodes.
