Skip to content

Commit 8730e11

Browse files
committed
Push Reference Particle
1 parent f673247 commit 8730e11

2 files changed

Lines changed: 23 additions & 10 deletions

File tree

docs/source/usage/python.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -774,9 +774,9 @@ This module provides elements and methods for the accelerator lattice.
774774
Calculate the transfer map of the elements in the list.
775775

776776
:param ref: A reference particle.
777-
:param order: So far, only the calculation of linear transfer maps are supported in this function.
778-
:param fallback_identity_map: For elements with an undefined transfer map in lattice, assume the identity matrix.
779-
:return: The transfer map map of all elements in the list.
777+
:param order: So far, only the calculation of linear transfer maps is supported.
778+
:param fallback_identity_map: For elements with an undefined transfer map in the lattice, assume the identity matrix.
779+
:return: The transfer map of all elements in the list.
780780
:rtype: Map6x6
781781

782782
.. py:method:: plot_survey(ref=None, ax=None, legend=True, legend_ncols=5)

src/python/elements.cpp

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2620,26 +2620,34 @@ void init_elements(py::module& m)
26202620
"transfer_map",
26212621
[](
26222622
KnownElementsList &v,
2623-
RefPart const & ref,
2623+
RefPart ref, // note: intentional copy
26242624
std::string order,
26252625
bool fallback_identity_map
26262626
)
26272627
{
26282628
if (order != "linear") {
26292629
throw std::runtime_error("So far, only the calculation of linear transfer maps are supported in this function.");
26302630
}
2631-
Map6x6 result = Map6x6::Identity();
2632-
for (auto & el_v : v) {
2633-
std::visit([&result, &ref, &fallback_identity_map](auto const & el) {
2631+
Map6x6 linear_transfer_map = Map6x6::Identity();
2632+
for (auto & el_v : v)
2633+
{
2634+
// advance reference particle
2635+
std::visit([&ref](auto && el) {
2636+
el(ref);
2637+
}, el_v);
2638+
2639+
// extract element transport map, handle fallback
2640+
Map6x6 element_transport_map = Map6x6::Identity();
2641+
std::visit([&linear_transfer_map, &ref, &fallback_identity_map, &element_transport_map](auto const & el) {
26342642
using Element = std::decay_t<decltype(el)>;
26352643
std::string not_impl_msg = "Undefined transfer map in lattice for element ";
26362644
if (el.has_name()) not_impl_msg += el.name() + " ";
26372645
not_impl_msg += std::string("of type ") + Element::type;
26382646

26392647
if constexpr (std::is_base_of_v<elements::mixin::LinearTransport<Element>, Element>) {
26402648
try {
2641-
result = result * el.transport_map(ref);
2642-
} catch (std::exception const & e) {
2649+
element_transport_map = el.transport_map(ref);
2650+
} catch (std::exception const &) {
26432651
if (!fallback_identity_map) {
26442652
throw std::runtime_error(not_impl_msg);
26452653
}
@@ -2650,8 +2658,13 @@ void init_elements(py::module& m)
26502658
}
26512659
}
26522660
}, el_v);
2661+
2662+
// advance linear transfer map
2663+
linear_transfer_map = linear_transfer_map * element_transport_map;
2664+
// TODO: shorthand needs https://github.com/AMReX-Codes/amrex/pull/4880 from AMReX 26.02+
2665+
// result *= element_transport_map;
26532666
}
2654-
return result;
2667+
return linear_transfer_map;
26552668
},
26562669
py::arg("ref"),
26572670
py::arg("order") = "linear",

0 commit comments

Comments
 (0)