Skip to content

Commit

Permalink
Add periodic masking option to Aperture.
Browse files Browse the repository at this point in the history
  • Loading branch information
cemitch99 committed Oct 11, 2024
1 parent 623cee4 commit 1948629
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 5 deletions.
2 changes: 2 additions & 0 deletions docs/source/usage/parameters.rst
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,8 @@ Lattice Elements

* ``<element_name>.xmax`` (``float``, in meters) maximum value of the horizontal coordinate
* ``<element_name>.ymax`` (``float``, in meters) maximum value of the vertical coordinate
* ``<element_name>.repeat_x`` (``float``, in meters) horizontal period for repeated aperture masking (inactive by default)
* ``<element_name>.repeat_y`` (``float``, in meters) vertical period for repeated aperture masking (inactive by default)
* ``<element_name>.shape`` (``string``) shape of the aperture boundary: ``rectangular`` (default) or ``elliptical``
* ``<element_name>.dx`` (``float``, in meters) horizontal translation error
* ``<element_name>.dy`` (``float``, in meters) vertical translation error
Expand Down
2 changes: 2 additions & 0 deletions docs/source/usage/python.rst
Original file line number Diff line number Diff line change
Expand Up @@ -936,6 +936,8 @@ This module provides elements for the accelerator lattice.

:param xmax: maximum allowed value of the horizontal coordinate (meter)
:param ymax: maximum allowed value of the vertical coordinate (meter)
:param repeat_x: horizontal period for repeated aperture masking (inactive by default) (meter)
:param repeat_y: vertical period for repeated aperture masking (inactive by default) (meter)
:param shape: aperture boundary shape: ``"rectangular"`` (default) or ``"elliptical"``
:param dx: horizontal translation error in m
:param dy: vertical translation error in m
Expand Down
6 changes: 5 additions & 1 deletion src/initialization/InitElement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -376,17 +376,21 @@ namespace detail
auto a = detail::query_alignment(pp_element);

amrex::Real xmax, ymax;
amrex::ParticleReal repeat_x = 0.0;
amrex::ParticleReal repeat_y = 0.0;
std::string shape_str = "rectangular";
pp_element.get("xmax", xmax);
pp_element.get("ymax", ymax);
pp_element.queryAdd("repeat_x", repeat_x);
pp_element.queryAdd("repeat_y", repeat_y);
pp_element.queryAdd("shape", shape_str);
AMREX_ALWAYS_ASSERT_WITH_MESSAGE(shape_str == "rectangular" || shape_str == "elliptical",
element_name + ".shape must be \"rectangular\" or \"elliptical\"");
Aperture::Shape shape = shape_str == "rectangular" ?
Aperture::Shape::rectangular :
Aperture::Shape::elliptical;

m_lattice.emplace_back( Aperture(xmax, ymax, shape, a["dx"], a["dy"], a["rotation_degree"], element_name) );
m_lattice.emplace_back( Aperture(xmax, ymax, repeat_x, repeat_y, shape, a["dx"], a["dy"], a["rotation_degree"], element_name) );
} else if (element_type == "beam_monitor")
{
std::string openpmd_name = element_name;
Expand Down
21 changes: 18 additions & 3 deletions src/particles/elements/Aperture.H
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ namespace impactx
* @param shape aperture shape
* @param xmax maximum value of horizontal coordinate (m)
* @param ymax maximum value of vertical coordinate (m)
* @param repeat_x horizontal period for repeated masking, optional (m)
* @param repeat_y vertical period for repeated masking, optional (m)
* @param dx horizontal translation error in m
* @param dy vertical translation error in m
* @param rotation_degree rotation error in the transverse plane [degrees]
Expand All @@ -59,6 +61,8 @@ namespace impactx
Aperture (
amrex::ParticleReal xmax,
amrex::ParticleReal ymax,
amrex::ParticleReal repeat_x,
amrex::ParticleReal repeat_y,
Shape shape,
amrex::ParticleReal dx = 0,
amrex::ParticleReal dy = 0,
Expand All @@ -67,7 +71,7 @@ namespace impactx
)
: Named(name),
Alignment(dx, dy, rotation_degree),
m_shape(shape), m_xmax(xmax), m_ymax(ymax)
m_shape(shape), m_xmax(xmax), m_ymax(ymax), m_repeat_x(repeat_x), m_repeat_y(repeat_y)
{
}

Expand Down Expand Up @@ -104,8 +108,17 @@ namespace impactx
shift_in(x, y, px, py);

// scale horizontal and vertical coordinates
amrex::ParticleReal const u = x / m_xmax;
amrex::ParticleReal const v = y / m_ymax;
amrex::ParticleReal u;
amrex::ParticleReal v;
// first case: a single aperture
if (m_repeat_x == 0.0 || m_repeat_y == 0.0) {
u = x / m_xmax;
v = y / m_ymax;
// second case: a periodically repeated aperture
} else {
u = std::fmod(x,m_repeat_x) / m_xmax;
v = std::fmod(y,m_repeat_y) / m_ymax;
}

// compare against the aperture boundary
switch (m_shape)
Expand Down Expand Up @@ -133,6 +146,8 @@ namespace impactx
Shape m_shape; //! aperture type (rectangular, elliptical)
amrex::ParticleReal m_xmax; //! maximum horizontal coordinate
amrex::ParticleReal m_ymax; //! maximum vertical coordinate
amrex::ParticleReal m_repeat_x; //! horizontal period for repeated masking
amrex::ParticleReal m_repeat_y; //! vertical period for repeated masking

};

Expand Down
16 changes: 15 additions & 1 deletion src/python/elements.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,8 @@ void init_elements(py::module& m)
.def(py::init([](
amrex::ParticleReal xmax,
amrex::ParticleReal ymax,
amrex::ParticleReal repeat_x,
amrex::ParticleReal repeat_y,
std::string const & shape,
amrex::ParticleReal dx,
amrex::ParticleReal dy,
Expand All @@ -299,10 +301,12 @@ void init_elements(py::module& m)
Aperture::Shape const s = shape == "rectangular" ?
Aperture::Shape::rectangular :
Aperture::Shape::elliptical;
return new Aperture(xmax, ymax, s, dx, dy, rotation_degree, name);
return new Aperture(xmax, ymax, repeat_x, repeat_y, s, dx, dy, rotation_degree, name);
}),
py::arg("xmax"),
py::arg("ymax"),
py::arg("repeat_x") = 0,
py::arg("repeat_y") = 0,
py::arg("shape") = "rectangular",
py::arg("dx") = 0,
py::arg("dy") = 0,
Expand Down Expand Up @@ -336,6 +340,16 @@ void init_elements(py::module& m)
[](Aperture & ap, amrex::ParticleReal ymax) { ap.m_ymax = ymax; },
"maximum vertical coordinate"
)
.def_property("repeat_x",
[](Aperture & ap) { return ap.m_repeat_x; },
[](Aperture & ap, amrex::ParticleReal repeat_x) { ap.m_repeat_x = repeat_x; },
"horizontal period for repeated aperture masking"
)
.def_property("repeat_y",
[](Aperture & ap) { return ap.m_repeat_y; },
[](Aperture & ap, amrex::ParticleReal repeat_y) { ap.m_repeat_y = repeat_y; },
"vertical period for repeated aperture masking"
)
;
register_beamoptics_push(py_Aperture);

Expand Down

0 comments on commit 1948629

Please sign in to comment.