Skip to content

Conversation

RemiLehe
Copy link
Member

@RemiLehe RemiLehe commented Oct 23, 2024

It is often useful to compute the total electromagnetic energy in the box. (This is oftentimes a good proxy for the laser energy.) This PR adds this function in LpaDiagnostic.

@RemiLehe RemiLehe changed the title [WIP] Add calculation of EM energy in RZ Add calculation of EM energy Oct 23, 2024
# Compute the energy
energy_density = const.epsilon_0/2.*(Ex**2 + Ey**2 + Ez**2) + 1./(2*const.mu_0)*(Bx**2 + By**2 + Bz**2)
volume = info.dx*info.dy*info.dz # Cell volume
E = (energy_density*volume).sum()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A small optimization is to do the sum first, avoiding multiplying the whole array be the volume. Or is the volume an array also, the volume of each grid cell?

E = energy_density.sum()*volume

# Check that the fields have either the thetaMode or 3dcartesian geometry
if (self.fields_metadata['E']['geometry'] not in ['thetaMode', '3dcartesian']) or \
(self.fields_metadata['B']['geometry'] not in ['thetaMode', '3dcartesian']):
raise ValueError('The electromagnetic energy can only be computed for 3D Cartesian and cylindrical simulations.')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like this should work for 2D and 1D, though depending on how info.dy and info.dz are defined.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, this is a bit tricky. info.dy does not exist in 2D and 1D in openPMD-viewer.
Technically, we could multiply only by info.dx (instead of info.dx and info.dy) and get a result in J/m (instead of J), but I wanted to avoid this, so that the user does not get confused.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For comparison, this is what is done in the WarpX reduced field energy diagnostic. The volume is calculated with the assumption that the extra dimensions have a length of 1. This would be easy to implement here so might as well for completeness so you wouldn't have to come back and submit another change if this is requested in the future.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, sounds good. Will do.

@RemiLehe RemiLehe requested a review from dpgrote February 21, 2025 22:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants