-
Notifications
You must be signed in to change notification settings - Fork 50
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add calculation of EM energy #431
Open
RemiLehe
wants to merge
6
commits into
dev
Choose a base branch
from
get_em_energy
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
2267874
Add calculation of EM energy in RZ
RemiLehe d312cde
Update calculation
RemiLehe d0aa6b0
Add automated test
RemiLehe 5771a8d
Add checks
RemiLehe 6129bb4
Add automated test
RemiLehe acb8d25
Apply suggestions from code review
RemiLehe File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
""" | ||
This test file is part of the openPMD-viewer. | ||
|
||
It checks that the function `get_electromagnetic_energy` works correctly | ||
|
||
Usage: | ||
This file is meant to be run from the root directory of openPMD-viewer, | ||
by any of the following commands | ||
$ python tests/test_laser_energy.py | ||
$ py.test | ||
$ python -m pytest tests | ||
|
||
Copyright 2024, openPMD-viewer contributors | ||
Authors: Remi Lehe | ||
License: 3-Clause-BSD-LBNL | ||
""" | ||
from openpmd_viewer.addons import LpaDiagnostics | ||
from scipy.constants import c | ||
import numpy as np | ||
|
||
# Download required datasets | ||
import os | ||
def download_if_absent( dataset_name ): | ||
"Function that downloads and decompress a chosen dataset" | ||
if os.path.exists( dataset_name ) is False: | ||
import wget, tarfile | ||
tar_name = "%s.tar.gz" %dataset_name | ||
url = "https://github.com/openPMD/openPMD-example-datasets/raw/draft/%s" %tar_name | ||
wget.download(url, tar_name) | ||
with tarfile.open( tar_name ) as tar_file: | ||
tar_file.extractall() | ||
os.remove( tar_name ) | ||
download_if_absent( 'example-thetaMode' ) | ||
|
||
def test_laser_energy(): | ||
""" | ||
Check that the function `get_electromagnetic_energy` gives the same result | ||
as the engineering formula for a Gaussian pulse | ||
|
||
E = 2.7e-5*(a0*w0/lambd)**2*(tau[fs]) | ||
""" | ||
ts = LpaDiagnostics('./example-thetaMode/hdf5') | ||
iteration = 300 | ||
|
||
# Evaluate the laser energy using the engineering formula | ||
a0 = ts.get_a0(iteration=iteration, pol='y') | ||
w0 = ts.get_laser_waist(iteration=iteration, pol='y') | ||
tau_fs = ts.get_ctau(iteration=iteration, pol='y') / c * 1e15 | ||
omega = ts.get_main_frequency(iteration=iteration, pol='y') | ||
lambd = 2*np.pi*c/omega | ||
E_eng = 2.7e-5*(a0*w0/lambd)**2*tau_fs | ||
|
||
# Evaluate the laser energy using the function | ||
E_func = ts.get_electromagnetic_energy(iteration=iteration) | ||
|
||
# Compare the two results | ||
assert np.isclose(E_eng, E_func, rtol=0.04) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 inopenPMD-viewer
.Technically, we could multiply only by
info.dx
(instead ofinfo.dx
andinfo.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.There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.