# Get vorticity from velocity

**URL:** <https://fenicsproject.discourse.group/t/get-vorticity-from-velocity/6971>\
**Category:** variational formulation\
**Created:** [November 2, 2021, 8:54pm UTC](https://fenicsproject.discourse.group/t/get-vorticity-from-velocity/6971 "2021-11-02T20:54:37Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![christinaz](https://avatars.discourse-cdn.com/v4/letter/c/a88e57/32.png) [@christinaz](https://fenicsproject.discourse.group/u/christinaz)\
**Post date:** [November 2, 2021, 8:54pm UTC](https://fenicsproject.discourse.group/t/get-vorticity-from-velocity/6971/1 "2021-11-02T20:54:38Z")

</div>

Hi, I am trying to extract vorticity from the velocity field for fluid simulation. This is my code and extracted field. I am wondering why the vorticity field is so large that it ranges from -160 to 160. Am I doing the computation wrongly?

```auto
// We have a known mesh in the file fname
mesh = gen_mesh_from_file(fnames)
nodes = mesh.coordinates()
cell = mesh.cells()

// define function space
V = VectorFunctionSpace(mesh, "Lagrange", 2)
Q = FunctionSpace(mesh, "Lagrange", 1)
u_n = Function(V)

// We have a vector field u_n in second order VectorFunctionSpace, and we compute vorticity from that 
ux, uy = u_n.split(deepcopy=True)
vortex = ux.dx(1)-uy.dx(0)
vor = Function(Q)
vor = project(vortex,Q)

// We plot the vorticity 
np_solution = vor.compute_vertex_values()
plot_fig = plt.tricontourf(nodes[:,0], nodes[:,1],cell,np_solution,levels=100,cmap=cmocean.cm.thermal)

```

velocity field:

 ![v_5000](https://global.discourse-cdn.com/free1/uploads/fenicsproject1/original/2X/6/6cc3f17792757476c66be976f4e6788d8d045a39.png)

vorticity:  
 ![vortex_5000](https://global.discourse-cdn.com/free1/uploads/fenicsproject1/original/2X/4/4311d30ff02ffc7173fa43178c3e4f00198c597c.png)

---

<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:** [November 2, 2021, 9:04pm UTC](https://fenicsproject.discourse.group/t/get-vorticity-from-velocity/6971/2 "2021-11-02T21:04:27Z")

</div>

Please follow the guidelines for questions: [Read before posting: How do I get my question answered?](https://fenicsproject.discourse.group/t/read-before-posting-how-do-i-get-my-question-answered/21)  
and create a Minimal code example that reproduces the issue.

---

<div class="post-metadata">

**Author:** ![christinaz](https://avatars.discourse-cdn.com/v4/letter/c/a88e57/32.png) [@christinaz](https://fenicsproject.discourse.group/u/christinaz)\
**Post date:** [November 2, 2021, 9:20pm UTC](https://fenicsproject.discourse.group/t/get-vorticity-from-velocity/6971/3 "2021-11-02T21:20:55Z")

</div>

Hi, I have tried to update my question. I can also provide more detailed codes with the mesh file if needed. Thanks a ton.

---

<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:** [November 2, 2021, 9:50pm UTC](https://fenicsproject.discourse.group/t/get-vorticity-from-velocity/6971/4 "2021-11-02T21:50:30Z")

</div>

There are several things that is not clear in your post.

1. what space is `u_n` in?
2. what space is `Q`?

---

<div class="post-metadata">

**Author:** ![christinaz](https://avatars.discourse-cdn.com/v4/letter/c/a88e57/32.png) [@christinaz](https://fenicsproject.discourse.group/u/christinaz)\
**Post date:** [November 2, 2021, 10:04pm UTC](https://fenicsproject.discourse.group/t/get-vorticity-from-velocity/6971/5 "2021-11-02T22:04:45Z")

</div>

I have updated the questions!

---

<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:** [November 2, 2021, 10:36pm UTC](https://fenicsproject.discourse.group/t/get-vorticity-from-velocity/6971/6 "2021-11-02T22:36:50Z")

</div>

If `u_n` is in a second order continuous Lagrange space, then the vorticity is in a Discontinuous Lagrange space of order 1.

Thus your projection to CG-1 is introducing an error between the actual and shown vorticity.

Please use a DG-1 space for the projection, and `XDMFFile.write_checkpoint` to being able to visualize the DG-1 space in Paraview, as compute\_vertex\_values only does a CG-1 interpolation of any function.

---

<div class="post-metadata">

**Author:** ![christinaz](https://avatars.discourse-cdn.com/v4/letter/c/a88e57/32.png) [@christinaz](https://fenicsproject.discourse.group/u/christinaz)\
**Post date:** [November 2, 2021, 10:54pm UTC](https://fenicsproject.discourse.group/t/get-vorticity-from-velocity/6971/7 "2021-11-02T22:54:04Z")

</div>

Hi Dokken, thanks a lot for pointing this out.

I am trying to simulate the fluid dynamics and get the velocity field out of it. Now I am assuming sensors are placed at the node of the mesh. To get the v-field, I am doing this:

```auto
V = VectorFunctionSpace(mesh, "Lagrange", 2)
u_1 = Function(V)
// some process to set value for u_1
ux, uy = u_1.split()
u_solution[0, :] = ux.compute_vertex_values()
u_solution[1, :] = uy.compute_vertex_values()  

```

Do you think it is a correct way of doing this? Here I have `u_1` in CG-2 and try to compute\_vertex\_values() to get the field value.

Another question is that with `u_solution`, we won’t be able to get `u_1` right? As there are many dof are missing. Is it possible to get a `u_solution`, so that:

```auto
V1 = VectorFunctionSpace(mesh, 'P', 1)
u_solution = u_solution.T.reshape(-1)
u1= Function(V1)
d2v_low = dof_to_vertex_map(V1)
u1.vector()[:] = init_u[d2v_low]
        
V = VectorFunctionSpace(mesh, 'P', 2)
u = project(u1,V)

```

we have `u==u_1`.

---

<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:** [November 2, 2021, 11:13pm UTC](https://fenicsproject.discourse.group/t/get-vorticity-from-velocity/6971/8 "2021-11-02T23:13:19Z")

</div>

Why don’t you use the  
`V.tabulate_dof_coordinates()` to get the coordinates of all the degrees of freedom?

As to your second question, you are correct that you cannot recover `u_1` by this method, as you have discarded all information about the higher order space when computing the vertex values. You can easily verify this by comparing the underlying arrays of `u` and `u_1` by doing `print(u.vector().get_local()-u_1.vector().get_local())`

I would still suggest you use XDMFFile.write\_checkpoint and Paraview to look at your vorticity.

---

<div class="post-metadata">

**Author:** ![christinaz](https://avatars.discourse-cdn.com/v4/letter/c/a88e57/32.png) [@christinaz](https://fenicsproject.discourse.group/u/christinaz)\
**Post date:** [November 2, 2021, 11:48pm UTC](https://fenicsproject.discourse.group/t/get-vorticity-from-velocity/6971/9 "2021-11-02T23:48:23Z")

</div>

Thanks again Dokken for your help!

It’s a great idea to get V.tabulate\_dof\_coordinates() to get the coordinates for all the degrees of freedom. Can I also get the value for those nodes? Do you know how should I get this?

One thing I can come up with is to value the field at those coordiantes. Though I am not sure if this is the correct way of doing this.

---

<div class="post-metadata">

**Author:** ![christinaz](https://avatars.discourse-cdn.com/v4/letter/c/a88e57/32.png) [@christinaz](https://fenicsproject.discourse.group/u/christinaz)\
**Post date:** [November 3, 2021, 12:20am UTC](https://fenicsproject.discourse.group/t/get-vorticity-from-velocity/6971/10 "2021-11-03T00:20:40Z")

</div>

Hi Dokken, I think I figure this out, I am thinking that I can use

```auto
u.vector().get_local() 

```

to get the value for each DOF

---

<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:** [November 3, 2021, 6:33am UTC](https://fenicsproject.discourse.group/t/get-vorticity-from-velocity/6971/11 "2021-11-03T06:33:51Z")

</div>

Yes, the ith entry in `u.vector().get_local()` corresponds to the ith row of `V.tabulate_dof_coordinates()`

In particular, see: [How to get the solution obatained by fenics as a vector? - #7 by dokken](https://fenicsproject.discourse.group/t/how-to-get-the-solution-obatained-by-fenics-as-a-vector/5377/7) for handling of vector spaces
