Skip to content
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 beam eigenemittances to reduced diagnostics. #702

Merged
merged 54 commits into from
Oct 10, 2024
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
54 commits
Select commit Hold shift + click to select a range
3000016
Add eigenemittance calculation.
cemitch99 Sep 13, 2024
9584c62
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Sep 13, 2024
176405a
Update CovarianceMatrixMath.H
cemitch99 Sep 13, 2024
b51293b
Update EmittanceInvariants.cpp
cemitch99 Sep 13, 2024
6b1e911
Update EmittanceInvariants.H
cemitch99 Sep 13, 2024
4b3fdd1
Update EmittanceInvariants.cpp
cemitch99 Sep 13, 2024
62a11c5
Treat special case of vanishing discriminant in Cardano's formula.
cemitch99 Sep 13, 2024
55ec468
Test from ReducedDiagnostics; update ej ordering.
cemitch99 Sep 13, 2024
ecde532
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Sep 13, 2024
8f22292
Add diagnostic output.
cemitch99 Sep 13, 2024
2297ae5
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Sep 13, 2024
db26dde
Comment print statements used for debugging.
cemitch99 Sep 13, 2024
d4f0a14
Update ReducedBeamCharacteristics.cpp
cemitch99 Sep 13, 2024
939909d
Add benchmark example.
cemitch99 Sep 14, 2024
beccb40
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Sep 14, 2024
d12e44c
Add documentation.
cemitch99 Sep 14, 2024
b3fe78a
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Sep 14, 2024
7bd8c5a
Update CMakeLists.txt
cemitch99 Sep 14, 2024
8d001eb
Update CMakeLists.txt
cemitch99 Sep 14, 2024
27e9882
Normalize momenta by mc (dynamical units) for eigenemittances.
cemitch99 Sep 14, 2024
f33bd59
Add alternative routine for cubic roots.
cemitch99 Sep 16, 2024
f22eaae
Update EmittanceInvariants.cpp
cemitch99 Sep 16, 2024
e4a9421
Update CovarianceMatrixMath.H
cemitch99 Sep 16, 2024
1a62b70
Temporarily comment pytest test_transformation.
cemitch99 Sep 16, 2024
8fdee82
Update test_transformation.py
cemitch99 Sep 16, 2024
9070654
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Sep 16, 2024
8cfafc6
Delete commented debugging print statements.
cemitch99 Sep 16, 2024
ea114ff
Update EmittanceInvariants.H
cemitch99 Sep 16, 2024
101803c
Update EmittanceInvariants.cpp
cemitch99 Sep 16, 2024
2ee73f4
Apply suggestions from code review
cemitch99 Sep 19, 2024
96818f2
Update src/particles/diagnostics/EmittanceInvariants.cpp
cemitch99 Sep 19, 2024
e6bd898
Update EmittanceInvariants.cpp
cemitch99 Sep 19, 2024
06b63c2
Add emittance_1,2,3 print.
cemitch99 Sep 24, 2024
0e8ecf1
Finalize print of emittance_1,2,3.
cemitch99 Sep 24, 2024
71bb155
Add ParmParse for optional calculation.
cemitch99 Sep 24, 2024
49d5677
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Sep 24, 2024
3684108
Update test_transformation.py
cemitch99 Sep 24, 2024
c883ee8
Apply suggestions from code review
cemitch99 Sep 25, 2024
0172e10
Update EmittanceInvariants.cpp
cemitch99 Sep 25, 2024
587a095
Update CovarianceMatrixMath.H
cemitch99 Sep 26, 2024
851b46a
Update src/particles/diagnostics/CovarianceMatrixMath.H
cemitch99 Sep 26, 2024
45f21b9
Update CovarianceMatrixMath.H
cemitch99 Sep 26, 2024
59f5b4d
Update CovarianceMatrixMath.H
cemitch99 Sep 26, 2024
53024fa
Use amrex:: math for complex functions.
cemitch99 Sep 26, 2024
534e40f
Avoid copying matrices
ax3l Oct 2, 2024
ed52ed7
Formatting: Whitespaces
ax3l Oct 2, 2024
b922bb4
More general odd-ness test
ax3l Oct 2, 2024
41bee2b
Add conditional emittance output columns.
cemitch99 Oct 5, 2024
e27c30e
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 5, 2024
4f2d689
Add documentation for user-facing input control.
cemitch99 Oct 5, 2024
d8128ac
Update dataanalysis documentation of eigenemittances.
cemitch99 Oct 5, 2024
4b8f7b3
Update CovarianceMatrixMath.H
cemitch99 Oct 8, 2024
6d61630
Apply suggestions from code review
cemitch99 Oct 8, 2024
345f5b1
Update tests/python/test_transformation.py
ax3l Oct 10, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion docs/source/dataanalysis/dataanalysis.rst
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ The code writes out the values in an ASCII file prefixed ``reduced_beam_characte
* ``sig_px``, ``sig_py``, ``sig_pt``
Standard deviation of the particle momentum deviations (energy difference for ``pt``) normalized by the magnitude of the reference particle momentum (unit: dimensionless)
* ``emittance_x``, ``emittance_y``, ``emittance_t``
Normalized rms beam emittance (unit: meter)
cemitch99 marked this conversation as resolved.
Show resolved Hide resolved
Unnormalized rms beam emittances (unit: meter)
* ``alpha_x``, ``alpha_y``, ``alpha_t``
Courant-Snyder (Twiss) alpha (unit: dimensionless). Transverse Twiss functions are calculated after removing correlations with particle energy.
* ``beta_x``, ``beta_y``, ``beta_t``
Expand All @@ -93,8 +93,11 @@ The code writes out the values in an ASCII file prefixed ``reduced_beam_characte
Horizontal and vertical dispersion (unit: meter)
* ``dispersion_px``, ``dispersion_py``
Derivative of horizontal and vertical dispersion (unit: dimensionless)
* ``emittance_xn``, ``emittance_yn``, ``emittance_tn``
Normalized rms beam emittances (unit: meter)
* ``emittance_1``, ``emittance_2``, ``emittance_3``
cemitch99 marked this conversation as resolved.
Show resolved Hide resolved
Normalized rms beam eigenemittances (aka mode emittances) (unit: meter)
These three diagnostics are written optionally if the flag eigenemittances = True.
* ``charge``
Total beam charge (unit: Coulomb)

Expand Down
8 changes: 6 additions & 2 deletions docs/source/usage/parameters.rst
Original file line number Diff line number Diff line change
Expand Up @@ -683,8 +683,8 @@ Diagnostics and output
This option is ignored for the openPMD output elements (remove them from the lattice to disable).

* ``diag.slice_step_diagnostics`` (``boolean``, optional, default: ``false``)
By default, diagnostics is performed at the beginning and end of the simulation.
Enabling this flag will write diagnostics every step and slice step
By default, diagnostics are computed and written at the beginning and end of the simulation.
Enabling this flag will write diagnostics at every step and slice step.

* ``diag.file_min_digits`` (``integer``, optional, default: ``6``)
The minimum number of digits used for the step number appended to the diagnostic file names.
Expand All @@ -694,6 +694,10 @@ Diagnostics and output
Diagnostics for particles lost in apertures, stored as ``diags/openPMD/particles_lost.*`` at the end of the simulation.
See the ``beam_monitor`` element for backend values.

* ``diag.eigenemittances`` (``boolean``, optional, default: ``false``)
If this flag is enabled, the 3 eigenemittances of the 6D beam distribution are computed and written as diagnostics.
This flag is disabled by default to reduce computational cost.


.. _running-cpp-parameters-diagnostics-insitu:

Expand Down
7 changes: 7 additions & 0 deletions docs/source/usage/python.rst
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,13 @@ Collective Effects & Overall Simulation Parameters
Diagnostics for particles lost in apertures.
See the ``BeamMonitor`` element for backend values.

.. py:property:: eigenemittances

Enable (``True``) or disable (``False``) output of eigenemittances at every slice step in elements (default: ``False``).

If this flag is enabled, the 3 eigenemittances of the 6D beam distribution are computed and written as diagnostics.
This flag is disabled by default to reduce computational cost.

.. py:method:: init_grids()

Initialize AMReX blocks/grids for domain decomposition & space charge mesh.
Expand Down
10 changes: 10 additions & 0 deletions src/particles/diagnostics/CovarianceMatrixMath.H
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#define COVARIANCE_MATRIX_MATH_H

#include <ablastr/constant.H>
#include <ablastr/warn_manager/WarnManager.H>

#include <AMReX_Array.H>
#include <AMReX_REAL.H>
Expand Down Expand Up @@ -62,6 +63,15 @@ namespace impactx::diagnostics
amrex::ParticleReal tol = 1.0e-12; //allow for roundoff error
if (discriminant > tol) {

ablastr::warn_manager::WMRecordWarning(
"Impactx::diagnostics::CubicRootsTrig",
"Polynomial appearing in CubicRootsTrig has one or more complex "
"(non-real) roots. Only the real part is returned. This "
"suggests a loss of numerical precision in computation of the "
"eigenemittances. Treat eigenemittance values with caution.",
ablastr::warn_manager::WarnPriority::medium
);

std::cout << "Polynomial in CubicRoots has one or more complex roots." << "\n";
ax3l marked this conversation as resolved.
Show resolved Hide resolved

} else if (Q == 0.0_prt) { // Special case of a triple root
Expand Down
56 changes: 54 additions & 2 deletions src/particles/diagnostics/DiagnosticOutput.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,14 @@ namespace impactx::diagnostics
if (otype == OutputType::PrintRefParticle) {
file_handler << "step s beta gamma beta_gamma x y z t px py pz pt\n";
} else if (otype == OutputType::PrintReducedBeamCharacteristics) {
file_handler << "step" << " " << "s" << " "

// determine whether to output eigenemittances
amrex::ParmParse pp_diag("diag");
bool compute_eigenemittances = false;
pp_diag.queryAdd("eigenemittances", compute_eigenemittances);

if (compute_eigenemittances) {
file_handler << "step" << " " << "s" << " "
<< "x_mean" << " " << "x_min" << " " << "x_max" << " "
<< "y_mean" << " " << "y_min" << " " << "y_max" << " "
<< "t_mean" << " " << "t_min" << " " << "t_max" << " "
Expand All @@ -57,9 +64,29 @@ namespace impactx::diagnostics
<< "beta_x" << " " << "beta_y" << " " << "beta_t" << " "
<< "dispersion_x" << " " << "dispersion_px" << " "
<< "dispersion_y" << " " << "dispersion_py" << " "
<< "emittance_xn" << " " << "emittance_yn" << " " << "emittance_tn" << " "
<< "emittance_1" << " " << "emittance_2" << " " << "emittance_3" << " "
<< "charge_C" << " "
<< "\n";
} else {
file_handler << "step" << " " << "s" << " "
<< "x_mean" << " " << "x_min" << " " << "x_max" << " "
<< "y_mean" << " " << "y_min" << " " << "y_max" << " "
<< "t_mean" << " " << "t_min" << " " << "t_max" << " "
<< "sig_x" << " " << "sig_y" << " " << "sig_t" << " "
<< "px_mean" << " " << "px_min" << " " << "px_max" << " "
<< "py_mean" << " " << "py_min" << " " << "py_max" << " "
<< "pt_mean" << " " << "pt_min" << " " << "pt_max" << " "
<< "sig_px" << " " << "sig_py" << " " << "sig_pt" << " "
<< "emittance_x" << " " << "emittance_y" << " " << "emittance_t" << " "
<< "alpha_x" << " " << "alpha_y" << " " << "alpha_t" << " "
<< "beta_x" << " " << "beta_y" << " " << "beta_t" << " "
<< "dispersion_x" << " " << "dispersion_px" << " "
<< "dispersion_y" << " " << "dispersion_py" << " "
<< "emittance_xn" << " " << "emittance_yn" << " " << "emittance_tn" << " "
<< "charge_C" << " "
<< "\n";
}
}
}

Expand Down Expand Up @@ -93,7 +120,13 @@ namespace impactx::diagnostics

amrex::ParticleReal const s = pc.GetRefParticle().s;

file_handler << step << " " << s << " "
// determine whether to output eigenemittances
amrex::ParmParse pp_diag("diag");
bool compute_eigenemittances = false;
pp_diag.queryAdd("eigenemittances", compute_eigenemittances);

if (compute_eigenemittances) {
file_handler << step << " " << s << " "
<< rbc.at("x_mean") << " " << rbc.at("x_min") << " " << rbc.at("x_max") << " "
<< rbc.at("y_mean") << " " << rbc.at("y_min") << " " << rbc.at("y_max") << " "
<< rbc.at("t_mean") << " " << rbc.at("t_min") << " " << rbc.at("t_max") << " "
Expand All @@ -107,8 +140,27 @@ namespace impactx::diagnostics
<< rbc.at("beta_x") << " " << rbc.at("beta_y") << " " << rbc.at("beta_t") << " "
<< rbc.at("dispersion_x") << " " << rbc.at("dispersion_px") << " "
<< rbc.at("dispersion_y") << " " << rbc.at("dispersion_py") << " "
<< rbc.at("emittance_xn") << " " << rbc.at("emittance_yn") << " " << rbc.at("emittance_tn") << " "
<< rbc.at("emittance_1") << " " << rbc.at("emittance_2") << " " << rbc.at("emittance_3") << " "
<< rbc.at("charge_C") << "\n";
} else {
file_handler << step << " " << s << " "
<< rbc.at("x_mean") << " " << rbc.at("x_min") << " " << rbc.at("x_max") << " "
<< rbc.at("y_mean") << " " << rbc.at("y_min") << " " << rbc.at("y_max") << " "
<< rbc.at("t_mean") << " " << rbc.at("t_min") << " " << rbc.at("t_max") << " "
<< rbc.at("sig_x") << " " << rbc.at("sig_y") << " " << rbc.at("sig_t") << " "
<< rbc.at("px_mean") << " " << rbc.at("px_min") << " " << rbc.at("px_max") << " "
<< rbc.at("py_mean") << " " << rbc.at("py_min") << " " << rbc.at("py_max") << " "
<< rbc.at("pt_mean") << " " << rbc.at("pt_min") << " " << rbc.at("pt_max") << " "
<< rbc.at("sig_px") << " " << rbc.at("sig_py") << " " << rbc.at("sig_pt") << " "
<< rbc.at("emittance_x") << " " << rbc.at("emittance_y") << " " << rbc.at("emittance_t") << " "
<< rbc.at("alpha_x") << " " << rbc.at("alpha_y") << " " << rbc.at("alpha_t") << " "
<< rbc.at("beta_x") << " " << rbc.at("beta_y") << " " << rbc.at("beta_t") << " "
<< rbc.at("dispersion_x") << " " << rbc.at("dispersion_px") << " "
<< rbc.at("dispersion_y") << " " << rbc.at("dispersion_py") << " "
<< rbc.at("emittance_xn") << " " << rbc.at("emittance_yn") << " " << rbc.at("emittance_tn") << " "
<< rbc.at("charge_C") << "\n";
}
} // if( otype == OutputType::PrintReducedBeamCharacteristics)

// TODO: add as an option to the monitor element
Expand Down
2 changes: 1 addition & 1 deletion src/particles/diagnostics/EmittanceInvariants.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ namespace impactx::diagnostics
// a change of sign.
for (int i = 1; i < 7; i++) {
for (int j = 1; j < 7; j++) {
if (j % 2 == 1) {
if (j % 2 != 0) {
S1(i,j) = -Sigma(i,j+1); // if j is odd
}
else {
Expand Down
26 changes: 17 additions & 9 deletions src/particles/diagnostics/ReducedBeamCharacteristics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -305,19 +305,22 @@ namespace impactx::diagnostics
amrex::ParticleReal const alpha_y = - ypy_d / emittance_yd;
amrex::ParticleReal const alpha_t = - tpt / emittance_t;

// Calculate eigenemittances (optional)
amrex::Array2D<amrex::ParticleReal, 1, 6, 1, 6> Sigma;
amrex::ParticleReal emittance_1 = emittance_x * bg;
amrex::ParticleReal emittance_2 = emittance_y * bg;
amrex::ParticleReal emittance_3 = emittance_t * bg;
// Calculate normalized emittances
amrex::ParticleReal emittance_xn = emittance_x * bg;
amrex::ParticleReal emittance_yn = emittance_y * bg;
amrex::ParticleReal emittance_tn = emittance_t * bg;

// Parse the diagnostic parameters
// Determine whether to calculate eigenemittances, and initialize
amrex::ParmParse pp_diag("diag");
bool compute_eigenemittances = false;
ax3l marked this conversation as resolved.
Show resolved Hide resolved
pp_diag.queryAdd("eigenemittances", compute_eigenemittances);
amrex::ParticleReal emittance_1 = emittance_xn;
amrex::ParticleReal emittance_2 = emittance_yn;
amrex::ParticleReal emittance_3 = emittance_tn;

if (compute_eigenemittances) {
// Store the covariance matrix in dynamical variables:
amrex::Array2D<amrex::ParticleReal, 1, 6, 1, 6> Sigma;
Sigma(1,1) = x_ms;
Sigma(1,2) = xpx * bg;
Sigma(1,3) = xy;
Expand Down Expand Up @@ -400,9 +403,14 @@ namespace impactx::diagnostics
data["dispersion_y"] = dispersion_y;
data["dispersion_py"] = dispersion_py;
data["charge_C"] = charge;
data["emittance_1"] = emittance_1;
data["emittance_2"] = emittance_2;
data["emittance_3"] = emittance_3;
data["emittance_xn"] = emittance_xn;
data["emittance_yn"] = emittance_yn;
data["emittance_tn"] = emittance_tn;
if (compute_eigenemittances) {
data["emittance_1"] = emittance_1;
data["emittance_2"] = emittance_2;
data["emittance_3"] = emittance_3;
}

return data;
}
Expand Down
2 changes: 1 addition & 1 deletion tests/python/test_transformation.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ def test_transformation():
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 np.isclose(val, rbc_s[key], rtol=rtol, atol=atol)
ax3l marked this conversation as resolved.
Show resolved Hide resolved
# assert that the t-based beam is different, at least in the following keys:
large_st_diff_keys = [
"beta_x",
Expand Down
Loading