# A question about calculation of the energy based on finite elements

**URL:** https://community.freefem.org/t/a-question-about-calculation-of-the-energy-based-on-finite-elements/2759
**Category:** General Discussion
**Created:** [October 25, 2023, 10:46pm UTC](https://community.freefem.org/t/a-question-about-calculation-of-the-energy-based-on-finite-elements/2759 "2023-10-25T22:46:21Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![m31phy](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/m31phy/32/1216_2.png) [@m31phy](https://community.freefem.org/u/m31phy)
#### Post date: [October 25, 2023, 10:46pm UTC](https://community.freefem.org/t/a-question-about-calculation-of-the-energy-based-on-finite-elements/2759/1 "2023-10-25T22:46:21Z")

</div>

Dear all.

I have a quick question about calculating the energy or other functions in FreeFEM++.

I already obtained proper solutions (e.g., finite elements in space) using FreeFEM++. For instance, let’s assume that we calculate the electric field distribution in space, and we want to know the energy of this static electric field.

As we know, the energy from the static electric field can be written as;

\begin{equation} \epsilon = \int \vec{E}\cdot\vec{E} dV \end{equation}

For simplicity, I omit the coefficient. So we already obtained the electric field distributions. In this case, how can we write the code for calculating the above energy in FreeFEM++? If it is possible, could you write simple code for the above expression in FreeFEM++?

Sincerely

---

<div class="post-metadata">

### Author: ![m31phy](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/m31phy/32/1216_2.png) [@m31phy](https://community.freefem.org/u/m31phy)
#### Post date: [November 6, 2023, 7:01pm UTC](https://community.freefem.org/t/a-question-about-calculation-of-the-energy-based-on-finite-elements/2759/2 "2023-11-06T19:01:57Z")

</div>

Just for the other people who faced the same confusion, I wrote the answer that I found, even though it is relatively simple.

So, if you want to calculate \epsilon = \int \vec{E}\cdot\vec{E}d\tau through the FreeFEM++, then the following codes are as follows;

```auto
mesh Th = buildmesh(....);
fespace Vh(Th, P1);
Vh Ex, Ey, Ez; 

epsilon = int2d(Th)(Ex * Ex + Ey*Ey + Ez*Ez);

```
