# How to calculate potential energy in 2d linear elasticity?

**URL:** <https://community.freefem.org/t/how-to-calculate-potential-energy-in-2d-linear-elasticity/1951>\
**Category:** General Discussion\
**Created:** [August 21, 2022, 11:51pm UTC](https://community.freefem.org/t/how-to-calculate-potential-energy-in-2d-linear-elasticity/1951 "2022-08-21T23:51:42Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![sosome](https://avatars.discourse-cdn.com/v4/letter/s/2bfe46/32.png) [@sosome](https://community.freefem.org/u/sosome)\
**Post date:** [August 21, 2022, 11:51pm UTC](https://community.freefem.org/t/how-to-calculate-potential-energy-in-2d-linear-elasticity/1951/1 "2022-08-21T23:51:42Z")

</div>

Dear all,

I am trying to calculate the potential energy in a linear elasticity problem.

In this code, How to calculate potential energy in 2d linear elasticity?

```auto
//Parameters
real Rho = 8000.; //Density
real E = 210.e9; //Young modulus
real Nu = 0.27; //Poisson ratio

real Gravity = -9.81;	//Gravity

//Mesh
real nn = 10; //Mesh quality
real L = 20.; //Beam length
real H = 1.; //Beam height
int Fixed = 1; //Beam fixed label
int Free = 2; //Beam free label
border b1(t=0., L){x=t; y=0.; label=Free;};
border b2(t=0., H){x=L; y=t; label=Fixed;};
border b3(t=0., L){x=L-t; y=H; label=Free;};
border b4(t=0., H){x=0.; y=H-t; label=Fixed;};

int nnL = max(2., nn*L);
int nnH = max(2., nn*H);
mesh Th = buildmesh(b1(nnL) + b2(nnH) + b3(nnL) + b4(nnH));

//Fespace
func Pk = P1;
fespace Uh(Th, [Pk, Pk]);
Uh [ux, uy];

//Macro
real sqrt2 = sqrt(2.);
macro Epsilon(ux, uy) [dx(ux), dy(uy), (dy(ux)+dx(uy))/sqrt2] //
macro Divergence(ux, uy) (dx(ux) + dy(uy)) //

//Problem
real Mu = E/(2.*(1.+Nu));
real Lambda = E*Nu/((1.+ Nu)*(1.-2.*Nu));

varf vElasticity ([ux,uy], [vx, vy])
	= int2d(Th)(
		  Lambda * Divergence(vx, vy) * Divergence(ux, uy)
		+ 2. * Mu * (
			  Epsilon(vx, vy)' * Epsilon(ux, uy)
		)
	)
	+ int2d(Th)(
		  Rho * Gravity * vy
	)
	+ on(Fixed, ux=0, uy=0)
	;

matrix<real> Elasticity = vElasticity(Uh, Uh, solver=sparsesolver);
real[int] ElasticityBoundary = vElasticity(0, Uh);
ux[] = Elasticity^-1 * ElasticityBoundary;

//Movemesh
Th = movemesh(Th, [x+ux, y+uy]);
[ux, uy] = [ux, uy];

//Plot
plot([ux, uy], value=true, cmm="u");

```

---

<div class="post-metadata">

**Author:** ![Alexandru](https://avatars.discourse-cdn.com/v4/letter/a/e19b73/32.png) [@Alexandru](https://community.freefem.org/u/Alexandru)\
**Post date:** [August 22, 2022, 2:12pm UTC](https://community.freefem.org/t/how-to-calculate-potential-energy-in-2d-linear-elasticity/1951/2 "2022-08-22T14:12:32Z")

</div>

Hi,  
Once you have the solution, you have to use the definition of the potential energy which is in your case : the elastic energy minus the work done by body forces on the displacement solution, i.e. K(u)=\int\_D (eps : sigma) dV - \int\_D u\cdot T^d dS where I use T^d for the given body forces, in your case -rho_gravity_e\_y.

You have the solution (ux,uy); you can compute (epsxx, epsxy, epsyy) using their definitions and then (sigmaxx, sigmaxy, sigmayy) again using their definitions.

Best regards, Alex
