Using P2 Finite elements to calculate P0 elements

Using the Elasticity script to calculate the displacements I want to run some optimization loops. The variable I use to check my computations is a P0 element called dc. It needs to be a P0 element so later on I can reduce the density of each element with small dc. To compute dc I need to calculate the variable ce which is equal to the work W of each triangle. I have the forces applied as a P2 element and I have calculated them with the use of stiffness matrix and the equations F=K*U.
My problem is when I try to calculate:


where fx,ux,fy,uy are P2 elements and ce is a P0 element, my ce returns values equals to 0 which is not correct.
Is there any way use P2 elements to calculate P0 elements?

I think the line you wrote achieves just what you want - it does interpolation from P2 to P0 FE space. Maybe you should calculate a stiffness matrix which maps for P2 to P0, and calculate the forces directly on P0, and calculate the work also using ˙P0

However, I think the P2-P0 might be not ideal - you effectively throw away a lot of grid points and reduce your resolution. I would consider maybe using P1 elements and a finer grid in your case .