Skip to content

Commit

Permalink
Initial files for applying aperture to thick elements.
Browse files Browse the repository at this point in the history
  • Loading branch information
cemitch99 committed Jan 8, 2025
1 parent ef9a7dc commit 35eeb95
Show file tree
Hide file tree
Showing 3 changed files with 137 additions and 2 deletions.
25 changes: 24 additions & 1 deletion src/initialization/InitElement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,28 @@ namespace detail

return values;
}

/** Read the Aperture parameters xmax and ymax from inputs
*
* @param pp_element the element being read
* @return key-value pairs for xmax and ymax
*/
std::map<std::string, amrex::ParticleReal>
query_aperture (amrex::ParmParse& pp_element)
{
amrex::ParticleReal xmax = 0;
amrex::ParticleReal ymax = 0;
pp_element.query("xmax", xmax);
pp_element.query("ymax", ymax);

std::map<std::string, amrex::ParticleReal> values = {
{"xmax", xmax},
{"ymax", ymax}
};

return values;
}

} // namespace detail

/** Read a lattice element
Expand Down Expand Up @@ -128,8 +150,9 @@ namespace detail
{
auto const [ds, nslice] = detail::query_ds(pp_element, nslice_default);
auto a = detail::query_alignment(pp_element);
auto b = detail::query_aperture(pp_element);

m_lattice.emplace_back( Drift(ds, a["dx"], a["dy"], a["rotation_degree"], nslice, element_name) );
m_lattice.emplace_back( Drift(ds, a["dx"], a["dy"], a["rotation_degree"], b["xmax"], b["ymax"], nslice, element_name) );
} else if (element_type == "sbend")
{
auto const [ds, nslice] = detail::query_ds(pp_element, nslice_default);
Expand Down
14 changes: 13 additions & 1 deletion src/particles/elements/Drift.H
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

#include "particles/ImpactXParticleContainer.H"
#include "mixin/alignment.H"
#include "mixin/aperture.H"
#include "mixin/beamoptic.H"
#include "mixin/thick.H"
#include "mixin/named.H"
Expand All @@ -31,6 +32,7 @@ namespace impactx
public elements::BeamOptic<Drift>,
public elements::Thick,
public elements::Alignment,
public elements::Aperture,
public elements::NoFinalize
{
static constexpr auto type = "Drift";
Expand All @@ -42,6 +44,8 @@ namespace impactx
* @param dx horizontal translation error in m
* @param dy vertical translation error in m
* @param rotation_degree rotation error in the transverse plane [degrees]
* @param xmax horizontal aperture in m
* @param ymax vertical aperture in m
* @param nslice number of slices used for the application of space charge
* @param name a user defined and not necessarily unique name of the element
*/
Expand All @@ -50,12 +54,15 @@ namespace impactx
amrex::ParticleReal dx = 0,
amrex::ParticleReal dy = 0,
amrex::ParticleReal rotation_degree = 0,
amrex::ParticleReal xmax = 0,
amrex::ParticleReal ymax = 0,
int nslice = 1,
std::optional<std::string> name = std::nullopt
)
: Named(std::move(name)),
Thick(ds, nslice),
Alignment(dx, dy, rotation_degree)
Alignment(dx, dy, rotation_degree),
Aperture(xmax, ymax)
{
}

Expand Down Expand Up @@ -121,6 +128,11 @@ namespace impactx
py = pyout;
pt = ptout;

// apply transverse aperture
if (m_xmax > 0 && m_ymax > 0) {
apply_aperture(x, y, idcpu); //value of idcpu?
}

// undo shift due to alignment errors of the element
shift_out(x, y, px, py);
}
Expand Down
100 changes: 100 additions & 0 deletions src/particles/elements/mixin/aperture.H
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/* Copyright 2022-2023 The Regents of the University of California, through Lawrence
* Berkeley National Laboratory (subject to receipt of any required
* approvals from the U.S. Dept. of Energy). All rights reserved.
*
* This file is part of ImpactX.
*
* Authors: Axel Huebl
* License: BSD-3-Clause-LBNL
*/
#ifndef IMPACTX_ELEMENTS_MIXIN_APERTURE_H
#define IMPACTX_ELEMENTS_MIXIN_APERTURE_H

#include "particles/ImpactXParticleContainer.H"

#include <ablastr/constant.H>

#include <AMReX_Math.H>
#include <AMReX_Extension.H>
#include <AMReX_REAL.H>


namespace impactx::elements
{
/** This is a helper class for applying a transverse aperture restriction to thick lattice elements
*/
struct Aperture
{

/** A finite-length element
*
* @param xmax horizontal aperture size in m
* @param ymax vertical aperture size in m
*/
Aperture (
amrex::ParticleReal xmax,
amrex::ParticleReal ymax
)
: m_xmax(xmax), m_ymax(ymax)
{
}

Aperture () = default;
Aperture (Aperture const &) = default;
Aperture& operator= (Aperture const &) = default;
Aperture (Aperture&&) = default;
Aperture& operator= (Aperture&& rhs) = default;

~Aperture () = default;

/** Apply the transverse aperture
*
* @param[inout] x horizontal position relative to reference particle
* @param[inout] y vertical position relative to reference particle
* @param idcpu particle global index
*/
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
void apply_aperture (
amrex::ParticleReal & AMREX_RESTRICT x,
amrex::ParticleReal & AMREX_RESTRICT y,
uint64_t & AMREX_RESTRICT idcpu
) const {

// scale horizontal and vertical coordinates
amrex::ParticleReal const u = x / m_xmax;
amrex::ParticleReal const v = y / m_ymax;

// compare against the aperture boundary
if (std::pow(u,2) + std::pow(v,2) > 1_prt) {
amrex::ParticleIDWrapper{idcpu}.make_invalid();
}

}

/** Horizontal aperture size
*
* @return horizontal aperture size in m
*/
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
amrex::ParticleReal xmax () const
{
return m_xmax;
}

/** Vertical aperture size
*
* @return vertical aperture size in m
*/
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
amrex::ParticleReal ymax () const
{
return m_ymax;
}

amrex::ParticleReal m_xmax = 0; //! horizontal aperture size [m]
amrex::ParticleReal m_ymax = 0; //! vertical aperture size [m]
};

} // namespace impactx::elements

#endif // IMPACTX_ELEMENTS_MIXIN_APERTURE_H

0 comments on commit 35eeb95

Please sign in to comment.