A question about calculation of the energy based on finite elements

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++?


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;

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

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