# Projecting gradient

**URL:** <https://fenicsproject.discourse.group/t/projecting-gradient/14287>\
**Category:** General\
**Tags:** mesh\
**Created:** [April 16, 2024, 8:34pm UTC](https://fenicsproject.discourse.group/t/projecting-gradient/14287 "2024-04-16T20:34:42Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Bas](https://avatars.discourse-cdn.com/v4/letter/b/a587f6/32.png) [@Bas](https://fenicsproject.discourse.group/u/Bas)\
**Post date:** [April 16, 2024, 8:34pm UTC](https://fenicsproject.discourse.group/t/projecting-gradient/14287/1 "2024-04-16T20:34:43Z")

</div>

I have potential values at the centroid of a DG0 space. I found the electric field for the same. if I have n number of centroids, the electric field components are n\*3( for 100 centroid, the potential values obtained are 300). A minimal code would be. I have potentials for my mesh from a different software which is stored as phi\_values.

```
        from dolfin import *
        mesh = UnitSquareMesh(10, 10)

         V = functionSpace(mesh.'CG',1)
         proj = Function(V)
        proj.vector().set_local(phi_values)
        phi0 = proj.vector().get_local()

        subV = FunctionSpace(mesh, "DG", 0) 
        subproj = project(proj, subV, solver_type="cg", preconditioner_type="amg")
        coords= subV.tabulate_dof_coordinates()   
        phi_cen = subproj.vector().get_local()
                         
        # Finding EF 

        V1 = VectorFunctionSpace(self.geometry.mesh,'DG',0)
        V_phi = V
        phi_values = self.phi.vector().get_local()
        phi = Function(V_phi)
        phi.vector().set_local(phi_values)
        grad_phi = project(grad(phi), V,solver_type="cg", preconditioner_type="amg")
        E = Function(V1)
        E.vector().set_local(-grad_phi.vector().get_local())
        E_values = E.vector().get_local()
        E_values = np.column_stack(E_values)
        File("electric_field.pvd") << E

```

I could get the electric field, which is n_3 potential values. I am confused about the values. How do I interpret it ? i mean electric field for a 3D space has 3 components, so at each centroid, it should have 3 components , so the shape should be (n,3) rather than n_3.

---

<div class="post-metadata">

**Author:** ![chenyongxin](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/chenyongxin/32/6501_2.png) [@chenyongxin](https://fenicsproject.discourse.group/u/chenyongxin)\
**Post date:** [April 17, 2024, 1:40am UTC](https://fenicsproject.discourse.group/t/projecting-gradient/14287/2 "2024-04-17T01:40:34Z")

</div>

Your code is with many mistakes: indentation, copied from a class with `self` keyword, upper and lower case typos, etc… Make sure the code is reproducible.

To answer your question: `Function` is a class which contains a data array of `PETSc.Vec`. The `PETSc.Vec` is a concept of vector, which only involves **one-dimensional** data. So it has `n3` values (we say the size should be `3n`) in your case but not multidimensional `(n, 3)` data. If you want to extract each component of it, you may want to use `Function.sub(d)` where d traverses from 0 to 2 in 3D space.

See for documentation:  
[https://fenicsproject.org/olddocs/dolfin/2019.1.0/python/\_autogenerated/dolfin.cpp.function.html?highlight=functionspace#dolfin.cpp.function.Function.sub](https://fenicsproject.org/olddocs/dolfin/2019.1.0/python/_autogenerated/dolfin.cpp.function.html?highlight=functionspace#dolfin.cpp.function.Function.sub)

---

<div class="post-metadata">

**Author:** ![Bas](https://avatars.discourse-cdn.com/v4/letter/b/a587f6/32.png) [@Bas](https://fenicsproject.discourse.group/u/Bas)\
**Post date:** [April 17, 2024, 6:27am UTC](https://fenicsproject.discourse.group/t/projecting-gradient/14287/3 "2024-04-17T06:27:55Z")

</div>

so if the function space is CG with order one, it should have multidimensional data?

---

<div class="post-metadata">

**Author:** ![chenyongxin](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/chenyongxin/32/6501_2.png) [@chenyongxin](https://fenicsproject.discourse.group/u/chenyongxin)\
**Post date:** [April 17, 2024, 6:31am UTC](https://fenicsproject.discourse.group/t/projecting-gradient/14287/4 "2024-04-17T06:31:24Z")

</div>

No, `Function` always has a vector (`PETSc.Vec`) data which only has a **one-dimensional** data concept.

---

<div class="post-metadata">

**Author:** ![Bas](https://avatars.discourse-cdn.com/v4/letter/b/a587f6/32.png) [@Bas](https://fenicsproject.discourse.group/u/Bas)\
**Post date:** [April 17, 2024, 6:43am UTC](https://fenicsproject.discourse.group/t/projecting-gradient/14287/5 "2024-04-17T06:43:34Z")

</div>

So how do I find the electric field values corresponding to each cell in the mesh ? I was trying to get the electric field at centroids of the mesh and if I am correct I get the ef at centroids. How do I extract it for each cell from 3n?

---

<div class="post-metadata">

**Author:** ![chenyongxin](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/chenyongxin/32/6501_2.png) [@chenyongxin](https://fenicsproject.discourse.group/u/chenyongxin)\
**Post date:** [April 17, 2024, 6:58am UTC](https://fenicsproject.discourse.group/t/projecting-gradient/14287/6 "2024-04-17T06:58:11Z")

</div>

> [@Bas](#):
>
> So how do I find the electric field values corresponding to each cell in the mesh ?

I am only familar with the FEniCSx part. Within a specific partition in a parallel run, you can traverse all the cells and extract the associated DOFs `cell_dofs` with a poosible implementation:

```auto
num_cells = msh.topology.index_map(
        msh.topology.dim).size_local + msh.topology.index_map(
        msh.topology.dim).num_ghosts

cells = np.arange(num_cells)
cell_dofs = []

for cell in cells:
    cell_dofs.append(V.dofmap.cell_dofs(cell))

```

For FEniCS implementation, see the documentation: [https://fenicsproject.org/olddocs/dolfin/2019.1.0/python/\_autogenerated/dolfin.cpp.fem.html?highlight=cell\_dof#dolfin.cpp.fem.DofMap.cell\_dofs](https://fenicsproject.org/olddocs/dolfin/2019.1.0/python/_autogenerated/dolfin.cpp.fem.html?highlight=cell_dof#dolfin.cpp.fem.DofMap.cell_dofs)
