Computing 3d integral for each time step

I had a PDE I solved in FeniCS which gave me a solution P(L,\eta) over L \in[0,1] and \eta\in[1,10] and I wanted to calculate the integral \int_{1}^{10}P(L,\eta)d\eta which I did with the code print(assemble(u*dx)) inside the “length” loop, analogos to the heat equation.

My question is how can I generalise this when I solve a PDE which is of higher dimension, where I have a solution P(L,x,y,z). In this case I want to compute the integral \int_{1}^{10}\int_{0}^{10}\int_{0}^{10}P(L,x,y,z)dxdydz at each “length” step inside the loop. Can this be done?

I’ve seen your other post, and using the exact same code as above yields your desired result. Note that dx corresponds to the volume measure of the mesh it is defined for, i.e.,

dx = Measure('dx', mesh)

so using

 assemble(expression*dx)

will return the volume integral over the mesh. This can also be easily checked by computing the volume via

assemble(1*dx)

For other coordinates, e.g., time coordinates or your L coordinate, you have to approximate the correspondiing integral via quadrature.