-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add pythonization of transformation (#360)
* add pythonization of transformation * add transformation.cpp to cmakelists.txt * Cleanups * add documentation on coordinate transformation * add test of forward-inverse transformation * clean markdown * docs: fix section formatting * code cleaning * isort/black code cleaning * fix isort/black incompatibility * Apply suggestions from code review Co-authored-by: Axel Huebl <[email protected]> * code review suggestions * more cleaning - remove amrex import * Aesthetic Whitespaces Before Sections * Move to Unit Test Location * Removed unused variable --------- Co-authored-by: Axel Huebl <[email protected]>
- Loading branch information
1 parent
11edb97
commit d004e7e
Showing
11 changed files
with
185 additions
and
3 deletions.
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
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
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 @@ | ||
../../../tests |
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 |
---|---|---|
|
@@ -9,4 +9,5 @@ target_sources(pyImpactX | |
ImpactX.cpp | ||
ImpactXParticleContainer.cpp | ||
ReferenceParticle.cpp | ||
transformation.cpp | ||
) |
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,25 @@ | ||
/* Copyright 2021-2023 The ImpactX Community | ||
* | ||
* Authors: Ryan Sandberg, Axel Huebl | ||
* License: BSD-3-Clause-LBNL | ||
*/ | ||
#include "pyImpactX.H" | ||
|
||
#include <particles/ReferenceParticle.H> | ||
#include <particles/transformation/CoordinateTransformation.H> | ||
|
||
namespace py = pybind11; | ||
using namespace impactx; | ||
|
||
void init_transformation(py::module& m) | ||
{ | ||
m.def("coordinate_transformation", | ||
&transformation::CoordinateTransformation, | ||
"Transform coordinates from fixed s to fixed to or vice versa." | ||
); | ||
|
||
py::enum_<transformation::Direction>(m, "TransformationDirection") | ||
.value("to_fixed_s", transformation::Direction::to_fixed_s) | ||
.value("to_fixed_t", transformation::Direction::to_fixed_t) | ||
.export_values(); | ||
} |
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,93 @@ | ||
#!/usr/bin/env python3 | ||
# | ||
# Copyright 2022-2023 ImpactX contributors | ||
# Authors: Ryan Sandberg, Axel Huebl, Chad Mitchell | ||
# License: BSD-3-Clause-LBNL | ||
# | ||
# -*- coding: utf-8 -*- | ||
|
||
import numpy as np | ||
|
||
from impactx import ( | ||
Config, | ||
ImpactX, | ||
ImpactXParIter, | ||
RefPart, | ||
TransformationDirection, | ||
coordinate_transformation, | ||
distribution, | ||
elements, | ||
) | ||
|
||
|
||
def test_transformation(): | ||
""" | ||
This test ensures s->t and t->s transformations | ||
do round-trip. | ||
""" | ||
sim = ImpactX() | ||
|
||
# set numerical parameters and IO control | ||
sim.particle_shape = 2 # B-spline order | ||
sim.space_charge = False | ||
# sim.diagnostics = False # benchmarking | ||
sim.slice_step_diagnostics = True | ||
|
||
# domain decomposition & space charge mesh | ||
sim.init_grids() | ||
|
||
# load a 1 GeV electron beam with an initial | ||
# unnormalized rms emittance of 2 nm | ||
energy_MeV = 1e3 # reference energy | ||
energy_gamma = energy_MeV / 0.510998950 | ||
bunch_charge_C = 1.0e-9 # used with space charge | ||
npart = 10000 # number of macro particles | ||
|
||
# reference particle | ||
pc = sim.particle_container() | ||
ref = pc.ref_particle() | ||
ref.set_charge_qe(-1.0).set_mass_MeV(0.510998950).set_energy_MeV(energy_MeV) | ||
|
||
# particle bunch | ||
distr = distribution.Gaussian( | ||
sigmaX=3e-6, | ||
sigmaY=3e-6, | ||
sigmaT=1e-2, | ||
sigmaPx=1.33 / energy_gamma, | ||
sigmaPy=1.33 / energy_gamma, | ||
sigmaPt=100 / energy_gamma, | ||
muxpx=-0.5, | ||
muypy=0.4, | ||
mutpt=0.8, | ||
) | ||
sim.add_particles(bunch_charge_C, distr, npart) | ||
|
||
rbc_s0 = pc.reduced_beam_characteristics() | ||
coordinate_transformation(pc, TransformationDirection.to_fixed_t) | ||
rbc_t = pc.reduced_beam_characteristics() | ||
coordinate_transformation(pc, TransformationDirection.to_fixed_s) | ||
rbc_s = pc.reduced_beam_characteristics() | ||
|
||
# clean shutdown | ||
del sim | ||
|
||
# assert that forward-inverse transformation of the beam leaves beam unchanged | ||
atol = 1e-14 | ||
rtol = 1e-10 | ||
for key, val in rbc_s0.items(): | ||
if not np.isclose(val, rbc_s[key], rtol=rtol, atol=atol): | ||
print(f"initial[{key}]={val}, final[{key}]={rbc_s[key]} not equal") | ||
assert np.isclose(val, rbc_s[key], rtol=rtol, atol=atol) | ||
# assert that the t-based beam is different, at least in the following keys: | ||
large_st_diff_keys = [ | ||
"beta_x", | ||
"beta_y", | ||
"emittance_y", | ||
"emittance_x", | ||
"sig_y", | ||
"sig_x", | ||
"t_mean", | ||
] | ||
for key in large_st_diff_keys: | ||
rel_error = (rbc_s0[key] - rbc_t[key]) / rbc_s0[key] | ||
assert abs(rel_error) > 1 |
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,28 @@ | ||
.. _tests-transformation: | ||
|
||
Transformation | ||
============== | ||
|
||
Test the t/s transformations on an electron beam. | ||
|
||
We use a long electron beam, :math:`L_z=1` cm, with significant correlations in :math:`x-px`, :math:`y-py`, and :math:`t-pt`. | ||
The beam has average energy 1 GeV. | ||
|
||
This tests that the t/s transforms are inverses of each other | ||
Specifically, in this test the :math:`t`- and :math:`s`-coordinates of the beam must differ substantially | ||
and the forward-inverse transformed coordinates must agree with the initial coordinates. | ||
That is, we require that :math:`to_fixed_s` ( :math:`to_fixed_t` (initial beam)) = initial beam. | ||
|
||
|
||
Run | ||
--- | ||
|
||
This file is run from :ref:`pytest <developers-testing>`. | ||
|
||
.. tab-set:: | ||
|
||
.. tab-item:: Python Script | ||
|
||
.. literalinclude:: test_transformation.py | ||
:language: python3 | ||
:caption: You can copy this file from ``tests/python/test_transformation.py``. |