Skip to content

Commit

Permalink
1D CSR Model (#627)
Browse files Browse the repository at this point in the history
* Commit for adding new wake function scripts and charge deposition script

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* CSR Model C++ Draft

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Revert changes to examples.

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Delete examples/cfbend/Wakefield_Functions_Bend_copy.py

Not part of this PR.

* Delete examples/chicane/open_PMD_to_ASCII_copy.py

Not part of this PR.

* Delete examples/fodo/openPMD_to_ASCII.py

Not part of this PR.

* Created wakefields directory and moved all relevant wake function files into it. Created 2 new files for the wake force momentum kick.

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Delete src/particles/ChargeBinning.H

* Delete src/particles/ChargeBinning.cpp

* Delete src/particles/WakeConvolution.H

* Delete src/particles/WakeConvolution.cpp

* Added new paths to files to compile

* Added necessary paths to new wakefields directory for compilation

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Update ImpactX.cpp by deleting lines responsible for plotting

* Update ImpactX.cpp by adding  plotting script (commented)

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Removed variable hardcoding by reading in all function parameters

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Update src/particles/wakefields/WakePush.cpp

Co-authored-by: Chad Mitchell <[email protected]>

* Update src/particles/wakefields/WakePush.cpp

Co-authored-by: Chad Mitchell <[email protected]>

* Update src/particles/wakefields/WakePush.cpp

Co-authored-by: Chad Mitchell <[email protected]>

* Access variables directly using ablastr

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Update WakePush.cpp

* Added changes to python binding modules to include wakefield python test.

* Added python bindings for wakefield python test

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* CMake: `ImpactX_FFT` Option

Modernize FFT build.

* CMake: Ensure non-FFT Builds Still Work

Guard FFTW logic and throw runtime errors if used but not built.

* Docs: FFTW Installation

* Added wake copy test with binning functions

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Add binning python bindings to all relevant files

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Added working wakefield plotting test script in python

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Update test_wake_copy.py

* Updated wakefield files giving expected emittance growth

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Added CSR scripts to run and analyze CSR wake effects in chicane

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Update analysis_chicane_csr.py

* Added CSR tests to examples/CMakeLists.txt

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Update README.rst

Added CSR example description.

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Update CMakeLists.txt

Modify to lowercase.

* Added CSR inputs

* Added CSR inputs

* Delete tests/python/test_wake.py

* Delete tests/python/test_wake_copy.py

* Added wakefield plotting script

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Removed unused variables from wakefields files

* Edited files for checks

* Addressing check errors in wake files

* Edited WakePush to allow force indexing

* Update WakePush.cpp

* Update WakePush.H

* Update WakePush.cpp

* Ensure test_wake.py only runs for MPI turned off

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Update test_wake.py

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Delete tests/python/test_wake.py

* Update CMakeLists.txt

* Update test_charge_deposition.py

* Update test_charge_deposition.py

* Update test_charge_deposition.py

* Update CMakeLists.txt

* Simplify FFTs: R2C and C2R

* CMake: Fix ABLASTR FFTW Superbuild

Forgot to forward FFT request.

* FFTW -> AnyFFT from ABLASTR

This requires a PR to WarpX/ABLASTR that we will post separately.

* C++ Files: Copyright Headers

* Updated variable double to Real type

* Update src/particles/ImpactXParticleContainer.H

Co-authored-by: Axel Huebl <[email protected]>

* Update main.cpp

* Update src/particles/wakefields/ChargeBinning.cpp

Co-authored-by: Axel Huebl <[email protected]>

* Update src/particles/wakefields/WakePush.H

Co-authored-by: Axel Huebl <[email protected]>

* Update src/particles/wakefields/WakePush.cpp

Co-authored-by: Axel Huebl <[email protected]>

* Update WakePush.cpp

* Update src/ImpactX.cpp

Co-authored-by: Axel Huebl <[email protected]>

* Minor edits to import statements and declarations in wakefield files

* Encapsulate all wakefield header files in wakefields namespace

* Added namespace changes to .cpp files and ImpactX.cpp

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Add Docstrings, Formatting

Co-authored-by: Alex Bojanich <[email protected]>

* Fix Signed/Unsigned Warning

* Added template functions for handling CSR bend elements

* Remove global variable declaration for charge

* Added last function for calculating mean transverse position. Created singular function call for wakefield effects. Updated function that accesses bend elements.

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Update Doxygen Argument Name

* Cleaning: Leftover Changes

Remove leftover changes that change files we do not touch.
Minimize the diff of this PR.

* More Cleaning

* GPU Support

* CMake: Comment in CSR Test

* add_impactx_test: new simpler interface

* MPI Support

* Add .rst CSR effects documentation. Ensure momentum update values are valid.

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Sphinx/rst Formatting

* Verbatim Options

* Update src/particles/wakefields/WakePush.cpp

Co-authored-by: Axel Huebl <[email protected]>

* Update src/particles/wakefields/WakePush.cpp

Co-authored-by: Axel Huebl <[email protected]>

* Update src/particles/wakefields/WakePush.cpp

Co-authored-by: Axel Huebl <[email protected]>

* Update src/particles/wakefields/WakePush.cpp

Co-authored-by: Axel Huebl <[email protected]>

* Update docs/source/usage/parameters.rst

Co-authored-by: Chad Mitchell <[email protected]>

* Update docs/source/usage/parameters.rst

Co-authored-by: Chad Mitchell <[email protected]>

* Update examples/chicane/README.rst

Co-authored-by: Chad Mitchell <[email protected]>

* Add variable descriptions to WakePush

* Bugfix: Vectors Default-Init Zero

For some reason, AMReX device vector does not auto-allocate to
default value type (as `std::vector` does). This fixes isfinite
issues in slope.

* FFT: Fix FFT Array Sizes

Leftover from C2C rewrite to R2C & C2R FFTs: The array sizes
vary in that case for input and output.
https://www.fftw.org/fftw3_doc/One_002dDimensional-DFTs-of-Real-Data.html

* Fix: `bin_size`

Upper bound had an off-by-one error for binning.

* Fix Wake Function Eval and Simplify Padding

* Python: User-Facing Bindings & Docs

* Updated w_l_csr function to deal with s=0 case

* Cleaning: Debug and non-GPU Print

- does not work on GPU
- super costly in the hot loop, debug only

* Update WakePush.cpp

Comment unused variable.

* Update WakePush.cpp

Restore cw_size - it is used after all.

* Update WakePush.cpp

Try a conditional declaration of cw_size.

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Update WakeConvolution.H

Add bin_size to documentation in w_l_csr.

* FFT & CSR: Earlier User-Facing Abort

and some cleaning

* Test: Only with FFT

* Docs: Minor Updates

* CSR: Support `ExactSbend`

* Include Cleanup

* CI: FFTW on Windows

* Doc: Split Example Table of Content

* Inline Comments: OpenMP

For follow-up PRs on how to add OpenMP support.

* Rename: `ExecuteWakefield.H` -> `HandleWakefield.H`

To match the function and macros within.

* ChargeBinning: OpenMP

Works, but will not be as fast as it could be.

* Review: Fix Default & Docstring

Co-authored-by: Chad Mitchell <[email protected]>

* Tighter Tolerances

Co-authored-by: Chad Mitchell <[email protected]>

* Analysis: Remove hard-coded number of particles

* Update analysis_chicane_csr.py

Relax tolerance 2.5 -> 3.5.

* Fix Typo: Convolved

Co-authored-by: Chad Mitchell <[email protected]>

---------

Co-authored-by: Alex Bojanich <[email protected]>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Chad Mitchell <[email protected]>
Co-authored-by: Axel Huebl <[email protected]>
  • Loading branch information
5 people authored Aug 29, 2024
1 parent 69f180f commit 9049898
Show file tree
Hide file tree
Showing 28 changed files with 1,435 additions and 9 deletions.
35 changes: 35 additions & 0 deletions .github/workflows/windows.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ jobs:
ccache-windows-winmsvc-
- name: Download Dependencies
run: |
Invoke-WebRequest http://fftw.org/fftw-3.3.10.tar.gz -OutFile fftw-3.3.10.tar.gz
7z.exe x -r fftw-3.3.10.tar.gz
7z.exe x -r fftw-3.3.10.tar
Invoke-WebRequest https://github.com/HDFGroup/hdf5/archive/refs/tags/hdf5-1_12_2.tar.gz -OutFile hdf5-1_12_2.tar.gz
7z.exe x -r hdf5-1_12_2.tar.gz
7z.exe x -r hdf5-1_12_2.tar
Expand All @@ -40,6 +44,15 @@ jobs:
# https://github.com/actions/runner-images/issues/10004
CXXFLAGS: "/D_DISABLE_CONSTEXPR_MUTEX_CONSTRUCTOR"
run: |
cmake -S fftw-3.3.10 -B build_fftw `
-DBUILD_SHARED_LIBS=OFF `
-DBUILD_TESTS=OFF `
-DCMAKE_BUILD_TYPE=RelWithDebInfo `
-DDISABLE_FORTRAN=ON
if(!$?) { Exit $LASTEXITCODE }
cmake --build build_fftw --config RelWithDebInfo --target install --parallel 4
if(!$?) { Exit $LASTEXITCODE }
cmake -S hdf5-hdf5-1_12_2 -B build_hdf5 `
-DCMAKE_BUILD_TYPE=RelWithDebInfo `
-DBUILD_SHARED_LIBS=OFF `
Expand Down Expand Up @@ -68,6 +81,8 @@ jobs:
# Work-around for windows-latest GH runner issue, see
# https://github.com/actions/runner-images/issues/10004
CXXFLAGS: "/D_DISABLE_CONSTEXPR_MUTEX_CONSTRUCTOR"
# FFTW3 install prefix path
FFTW3_DIR: "C:/Program Files (x86)/fftw/"
run: |
$env:HDF5_DIR = "C:/Program Files/HDF_Group/HDF5/1.12.2/cmake/"
cmake -S . -B build `
Expand All @@ -76,6 +91,7 @@ jobs:
-DCMAKE_VERBOSE_MAKEFILE=ON `
-DHDF5_USE_STATIC_LIBRARIES=ON `
-DImpactX_COMPUTE=NOACC `
-DImpactX_FFT=ON `
-DImpactX_MPI=OFF `
-DImpactX_PYTHON=ON `
-DopenPMD_USE_HDF5=ON `
Expand Down Expand Up @@ -121,6 +137,10 @@ jobs:
- uses: seanmiddleditch/gha-setup-ninja@master
- name: Download Dependencies
run: |
Invoke-WebRequest http://fftw.org/fftw-3.3.10.tar.gz -OutFile fftw-3.3.10.tar.gz
7z.exe x -r fftw-3.3.10.tar.gz
7z.exe x -r fftw-3.3.10.tar
Invoke-WebRequest https://github.com/HDFGroup/hdf5/archive/refs/tags/hdf5-1_12_2.tar.gz -OutFile hdf5-1_12_2.tar.gz
7z.exe x -r hdf5-1_12_2.tar.gz
7z.exe x -r hdf5-1_12_2.tar
Expand All @@ -129,6 +149,17 @@ jobs:
run: |
call "C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\vc\Auxiliary\build\vcvarsall.bat" x64
cmake -S fftw-3.3.10 ^
-B build_fftw ^
-G "Ninja" ^
-DBUILD_TESTS=OFF ^
-DBUILD_SHARED_LIBS=OFF ^
-DCMAKE_BUILD_TYPE=Release ^
-DDISABLE_FORTRAN=ON
if errorlevel 1 exit 1
cmake --build build_fftw --config Release --target install --parallel 4
if errorlevel 1 exit 1
cmake -S hdf5-hdf5-1_12_2 -B build_hdf5 ^
-G "Ninja" ^
-DCMAKE_C_COMPILER=clang-cl ^
Expand Down Expand Up @@ -157,6 +188,9 @@ jobs:
python3 -m pip install -U openPMD-validator
- name: Build
shell: cmd
env:
# FFTW3 install prefix path
FFTW3_DIR: "C:/Program Files (x86)/fftw/"
run: |
call "C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\vc\Auxiliary\build\vcvarsall.bat" x64
Expand All @@ -170,6 +204,7 @@ jobs:
-DCMAKE_VERBOSE_MAKEFILE=ON ^
-DHDF5_USE_STATIC_LIBRARIES=ON ^
-DImpactX_COMPUTE=OMP ^
-DImpactX_FFT=ON ^
-DImpactX_MPI=OFF ^
-DImpactX_PYTHON=ON ^
-DopenPMD_USE_HDF5=ON ^
Expand Down
12 changes: 11 additions & 1 deletion docs/source/usage/examples.rst
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,11 @@ Single Particle Dynamics
examples/fodo_programmable/README.rst
examples/dogleg/README.rst

Collective Effects
------------------

Space Charge
------------
""""""""""""

.. toctree::
:maxdepth: 1
Expand All @@ -46,6 +48,14 @@ Space Charge
examples/kurth/README.rst
examples/epac2004_benchmarks/README.rst

Coherent Synchrotron Radiation (CSR)
""""""""""""""""""""""""""""""""""""

.. toctree::
:maxdepth: 1

examples/chicane/README_csr.rst


Beam Distributions
------------------
Expand Down
16 changes: 15 additions & 1 deletion docs/source/usage/parameters.rst
Original file line number Diff line number Diff line change
Expand Up @@ -590,9 +590,23 @@ Multigrid-specific numerical options:
Coherent Synchrotron Radiation (CSR)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

CSR effects are included in the simulation for bend lattice elements such as Sbend and CFbend.
These effects are critical in accurately modeling the wakefields generated due to the interaction of particles with the synchrotron radiation field generated by the beam during bending.
Currently, this is the 1D ultrarelativistic steady-state wakefield model (eq. 19 of
`E. L. Saldin et al, NIMA 398, p. 373-394 (1997), DOI:10.1016/S0168-9002(97)00822-X <https://doi.org/10.1016/S0168-9002(97)00822-X>`__).

* ``algo.csr`` (``boolean``, optional, default: ``false``)
Whether to calculate CSR effects.
CSR calculations involve several steps, including charge deposition, wakefield generation, and convolution, all of which are handled within the CSR bending process.

* ``algo.csr_bins`` (``integer`, optional, default: ``150``)
The number of bins used for the CSR calculations along the longitudinal direction. Increasing the number of bins can lead to more accurate wakefield resolution at the cost of higher computational expense.

.. note::

Coming soon, see `PR #627 <https://github.com/ECP-WarpX/impactx/pull/627>`__.
CSR effects are only calculated for lattice elements that include bending, such as ``Sbend``, ``ExactSbend`` and ``CFbend``.

CSR effects require the compilation flag ``-DImpactX_FFT=ON``.


.. _running-cpp-parameters-parser:
Expand Down
18 changes: 18 additions & 0 deletions docs/source/usage/python.rst
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,24 @@ Collective Effects & Overall Simulation Parameters
Currently MLMG solver looks for verbosity levels from 0-5.
A higher number results in more verbose output.

.. py:property:: csr
Enable (``True``) or disable (``False``) space charge calculations (default: ``False``).

Whether to calculate Coherent Synchrotron Radiation (CSR) effects (default: disabled).
Currently, this is the 1D ultrarelativistic steady-state wakefield model (eq. 19 of
`E. L. Saldin et al, NIMA 398, p. 373-394 (1997), DOI:10.1016/S0168-9002(97)00822-X <https://doi.org/10.1016/S0168-9002(97)00822-X>`__).

.. note::

CSR effects are only calculated for lattice elements that include bending, such as ``Sbend``, ``ExactSbend`` and ``CFbend``.

CSR effects require the compilation flag ``-DImpactX_FFT=ON``.

.. py:property:: csr_bins
Enable or disable Coherent Synchrotron Radiation (CSR) calculations (default: ``150``).

.. py:property:: diagnostics
Enable (``True``) or disable (``False``) diagnostics generally (default: ``True``).
Expand Down
21 changes: 19 additions & 2 deletions examples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -875,7 +875,7 @@ add_impactx_test(initialize_from_array.py
examples/initialize_from_array/visualize_from_array.py
)

# Achromatic Spectrometer ########################################################
# Achromatic Spectrometer ####################################################
#
# w/o space charge
add_impactx_test(spectrometer
Expand All @@ -891,7 +891,24 @@ add_impactx_test(spectrometer.py
OFF # no plot script yet
)

# Dogleg ########################################################
# Chicane with CSR ###########################################################
#
if(ImpactX_FFT)
add_impactx_test(chicane_csr
examples/chicane/input_chicane_csr.in
OFF # ImpactX MPI-parallel
examples/chicane/analysis_chicane_csr.py
OFF # no plot script yet
)
add_impactx_test(chicane_csr.py
examples/chicane/run_chicane_csr.py
OFF # ImpactX MPI-parallel
examples/chicane/analysis_chicane_csr.py
OFF # no plot script yet
)
endif()

# Dogleg #####################################################################
#
# w/o space charge
add_impactx_test(dogleg
Expand Down
6 changes: 3 additions & 3 deletions examples/chicane/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@
Chicane
=======

Berlin-Zeuthen magnetic bunch compression chicane:
https://www.desy.de/csr/
This is the Berlin-Zeuthen magnetic bunch compression chicane, which is a standardized community benchmark.

All parameters can be found online.
`All parameters can be found online <https://www.desy.de/csr/>`__.
A 5 GeV electron bunch with normalized transverse rms emittance of 1 um undergoes longitudinal compression by a factor of 10 in a standard 4-bend chicane.

The emittances should be preserved, and the rms pulse length should decrease by the compression factor (10).

In this test, the initial and final values of :math:`\lambda_x`, :math:`\lambda_y`, :math:`\lambda_t`, :math:`\epsilon_x`, :math:`\epsilon_y`, and :math:`\epsilon_t` must agree with nominal values.

We also have :ref:`a variation of this test that includes CSR effects in the bending magnets <examples-chicane-csr>`.

Run
---
Expand Down
50 changes: 50 additions & 0 deletions examples/chicane/README_csr.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
.. _examples-chicane-csr:

Chicane with CSR
================

This is the :ref:`Berlin-Zeuthen magnetic bunch compression chicane <examples-chicane>` example, but this time with coherent synchrotron radiation (CSR) modelled in the bending magnets.

`All parameters can be found online <https://www.desy.de/csr/>`__.
A 5 GeV electron bunch with normalized transverse rms emittance of 1 um undergoes longitudinal compression by a factor of 10 in a standard 4-bend chicane.
The rms pulse length should decrease by the compression factor (10).

An ultrarelativistic model of steady-state CSR in the bending dipoles is included, resulting in a horizontal emittance growth of 19%. Note that this value is smaller than the horizontal emittance growth of 57% that is obtained when transient (bend-entry and -exit) effects are included.

In this test, the initial and final values of :math:`\lambda_x`, :math:`\lambda_y`, :math:`\lambda_t`, :math:`\epsilon_x`, :math:`\epsilon_y`, and :math:`\epsilon_t` must agree with nominal values.


Run
---

This example can be run **either** as:

* **Python** script: ``python3 run_chicane_csr.py`` or
* ImpactX **executable** using an input file: ``impactx input_chicane_csr.in``

For `MPI-parallel <https://www.mpi-forum.org>`__ runs, prefix these lines with ``mpiexec -n 4 ...`` or ``srun -n 4 ...``, depending on the system.

.. tab-set::

.. tab-item:: Python: Script

.. literalinclude:: run_chicane_csr.py
:language: python3
:caption: You can copy this file from ``examples/chicane/run_chicane_csr.py``.

.. tab-item:: Executable: Input File

.. literalinclude:: input_chicane_csr.in
:language: ini
:caption: You can copy this file from ``examples/chicane/input_chicane_csr.in``.

Analyze
-------

We run the following script to analyze correctness:

.. dropdown:: Script ``analysis_chicane_csr.py``

.. literalinclude:: analysis_chicane_csr.py
:language: python3
:caption: You can copy this file from ``examples/chicane/analysis_chicane_csr.py``.
106 changes: 106 additions & 0 deletions examples/chicane/analysis_chicane_csr.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
#!/usr/bin/env python3
#
# Copyright 2022-2023 ImpactX contributors
# Authors: Axel Huebl, Chad Mitchell
# License: BSD-3-Clause-LBNL
#

import numpy as np
import openpmd_api as io
from scipy.stats import moment


def get_moments(beam):
"""Calculate standard deviations of beam position & momenta
and emittance values
Returns
-------
sigx, sigy, sigt, emittance_x, emittance_y, emittance_t
"""
sigx = moment(beam["position_x"], moment=2) ** 0.5 # variance -> std dev.
sigpx = moment(beam["momentum_x"], moment=2) ** 0.5
sigy = moment(beam["position_y"], moment=2) ** 0.5
sigpy = moment(beam["momentum_y"], moment=2) ** 0.5
sigt = moment(beam["position_t"], moment=2) ** 0.5
sigpt = moment(beam["momentum_t"], moment=2) ** 0.5

epstrms = beam.cov(ddof=0)

# Check for valid values before calculating emittance
if np.any(np.isnan([sigx, sigpx, sigy, sigpy, sigt, sigpt])) or np.any(
np.isnan(epstrms)
):
raise ValueError(
"Invalid value detected in standard deviations or covariance matrix."
)

emittance_x = (sigx**2 * sigpx**2 - epstrms["position_x"]["momentum_x"] ** 2) ** 0.5
emittance_y = (sigy**2 * sigpy**2 - epstrms["position_y"]["momentum_y"] ** 2) ** 0.5
emittance_t = (sigt**2 * sigpt**2 - epstrms["position_t"]["momentum_t"] ** 2) ** 0.5

return (sigx, sigy, sigt, emittance_x, emittance_y, emittance_t)


# initial/final beam
series = io.Series("diags/openPMD/monitor.h5", io.Access.read_only)
last_step = list(series.iterations)[-1]
initial = series.iterations[1].particles["beam"].to_df()
final = series.iterations[last_step].particles["beam"].to_df()

# compare number of particles
num_particles = len(initial)
assert num_particles == len(initial)
assert num_particles == len(final)

print("Initial Beam:")
sigx, sigy, sigt, emittance_x, emittance_y, emittance_t = get_moments(initial)
print(f" sigx={sigx:e} sigy={sigy:e} sigt={sigt:e}")
print(
f" emittance_x={emittance_x:e} emittance_y={emittance_y:e} emittance_t={emittance_t:e}"
)

atol = 0.0 # ignored
rtol = 3.5 * num_particles**-0.5 # from random sampling of a smooth distribution
print(f" rtol={rtol} (ignored: atol~={atol})")

assert np.allclose(
[sigx, sigy, sigt, emittance_x, emittance_y, emittance_t],
[
6.4214719960819659e-005,
3.6603372435649773e-005,
1.9955175623579313e-004,
1.0198263116327677e-010,
1.0308359092878036e-010,
4.0035161705244885e-010,
],
rtol=rtol,
atol=atol,
)


print("")
print("Final Beam:")
sigx, sigy, sigt, emittance_x, emittance_y, emittance_t = get_moments(final)
print(f" sigx={sigx:e} sigy={sigy:e} sigt={sigt:e}")
print(
f" emittance_x={emittance_x:e} emittance_y={emittance_y:e} emittance_t={emittance_t:e}"
)

atol = 0.0 # ignored
rtol = 3.5 * num_particles**-0.5 # from random sampling of a smooth distribution
print(f" rtol={rtol} (ignored: atol~={atol})")

assert np.allclose(
[sigx, sigy, sigt, emittance_x, emittance_y, emittance_t],
[
2.3928429374387210e-005,
8.4424535301423173e-005,
1.9976426324802290e-005,
1.2135933108429935e-010,
1.0308356090529235e-010,
3.870603e-09,
],
rtol=rtol,
atol=atol,
)
Loading

0 comments on commit 9049898

Please sign in to comment.