|
| 1 | +/* Copyright 2022-2024 The ImpactX Community |
| 2 | + * |
| 3 | + * Authors: Axel Huebl, Chad Mitchell |
| 4 | + * License: BSD-3-Clause-LBNL |
| 5 | + */ |
| 6 | +#include "pyImpactX.H" |
| 7 | + |
| 8 | +#include <CompensatedReal.H> |
| 9 | +#include <AMReX_REAL.H> |
| 10 | + |
| 11 | +#include <cmath> |
| 12 | + |
| 13 | +namespace py = pybind11; |
| 14 | +using namespace impactx; |
| 15 | + |
| 16 | + |
| 17 | +void init_compensatedreal(py::module& m) |
| 18 | +{ |
| 19 | + // Expose the CompensatedParticleReal typedef |
| 20 | + py::class_<CompensatedParticleReal>(m, "CompensatedParticleReal") |
| 21 | + .def(py::init<>(), |
| 22 | + "Default (zero) constructor for compensated ParticleReal type" |
| 23 | + ) |
| 24 | + .def(py::init<amrex::ParticleReal>(), |
| 25 | + py::arg("value"), |
| 26 | + "Constructor from a ParticleReal value" |
| 27 | + ) |
| 28 | + .def(py::init<CompensatedParticleReal const&>(), |
| 29 | + py::arg("other"), |
| 30 | + "Copy constructor" |
| 31 | + ) |
| 32 | + .def("assign", &CompensatedParticleReal::assign, |
| 33 | + py::arg("value"), |
| 34 | + py::return_value_policy::reference_internal, |
| 35 | + "Assign a new value and reset compensation" |
| 36 | + ) |
| 37 | + |
| 38 | + // Arithmetic operators |
| 39 | + .def(py::self += amrex::ParticleReal()) |
| 40 | + .def(py::self -= amrex::ParticleReal()) |
| 41 | + .def(py::self + amrex::ParticleReal()) |
| 42 | + .def(py::self - amrex::ParticleReal()) |
| 43 | + .def(amrex::ParticleReal() + py::self) |
| 44 | + .def(amrex::ParticleReal() - py::self) |
| 45 | + .def(py::self += py::self) |
| 46 | + .def(py::self -= py::self) |
| 47 | + .def(py::self + py::self) |
| 48 | + .def(py::self - py::self) |
| 49 | + |
| 50 | + // Comparison operators |
| 51 | + .def(py::self == amrex::ParticleReal()) |
| 52 | + .def(py::self != amrex::ParticleReal()) |
| 53 | + .def(py::self < amrex::ParticleReal()) |
| 54 | + .def(py::self <= amrex::ParticleReal()) |
| 55 | + .def(py::self > amrex::ParticleReal()) |
| 56 | + .def(py::self >= amrex::ParticleReal()) |
| 57 | + .def(py::self == py::self) |
| 58 | + .def(py::self != py::self) |
| 59 | + .def(py::self < py::self) |
| 60 | + .def(py::self <= py::self) |
| 61 | + .def(py::self > py::self) |
| 62 | + .def(py::self >= py::self) |
| 63 | + |
| 64 | + // Value access |
| 65 | + .def_property_readonly("value", &CompensatedParticleReal::value, |
| 66 | + "Get the final compensated value (main + first-order + second-order compensation)" |
| 67 | + ) |
| 68 | + |
| 69 | + // conversion to float for compatibility |
| 70 | + .def("__float__", &CompensatedParticleReal::value, |
| 71 | + "Convert to Python float" |
| 72 | + ) |
| 73 | + .def("__abs__", [](CompensatedParticleReal const& self) { |
| 74 | + return std::abs(self.value()); |
| 75 | + }, "Convert to absolute value") |
| 76 | + |
| 77 | + // Utility methods |
| 78 | + .def("reset_compensation", &CompensatedParticleReal::reset_compensation, |
| 79 | + py::return_value_policy::reference_internal, |
| 80 | + "Reset compensation variables to zero" |
| 81 | + ) |
| 82 | + |
| 83 | + // String representation |
| 84 | + .def("__repr__", [](CompensatedParticleReal const& self) { |
| 85 | + return "CompensatedParticleReal(" + std::to_string(self.value()) + ")"; |
| 86 | + }) |
| 87 | + .def("__str__", [](CompensatedParticleReal const& self) { |
| 88 | + return std::to_string(self.value()); |
| 89 | + }) |
| 90 | + |
| 91 | + .def_readonly_static("__doc__", |
| 92 | + "A compensated floating-point type specialized for ParticleReal values.\n" |
| 93 | + "\n" |
| 94 | + "This provides high-precision arithmetic for particle tracking calculations\n" |
| 95 | + "using the second-order iterative Kahan-Babuska algorithm.\n" |
| 96 | + "\n" |
| 97 | + "The algorithm follows Klein (2006) to provide high-precision arithmetic that avoids\n" |
| 98 | + "floating-point precision errors when dealing with large numbers of small values.\n" |
| 99 | + "\n" |
| 100 | + "References:\n" |
| 101 | + "- https://en.wikipedia.org/wiki/Kahan_summation_algorithm#Further_enhancements\n" |
| 102 | + "- Klein (2006). \"A generalized Kahan–Babuška-Summation-Algorithm\". in\n" |
| 103 | + " Computing. 76 (3–4). Springer-Verlag: 279–293. doi:10.1007/s00607-005-0139-x" |
| 104 | + ) |
| 105 | + ; |
| 106 | +} |
0 commit comments