Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Knobs as double or tpsa: C++ implementation details handled by wrapper #4

Merged
merged 6 commits into from
Sep 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion python/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,15 @@ target_link_libraries(lib
)

pybind11_extension(lib)
pybind11_strip(lib)

# I want not to strip
if(CMAKE_BUILD_TYPE STREQUAL Debug)
message(STATUS "${CMAKE_BUILD_TYPE} not 'Debug': stripping python binary")

# pybind11_strip(lib)
else()
message(STATUS "${CMAKE_BUILD_TYPE} 'Debug': not stripping python binary")
endif()
set_target_properties(lib PROPERTIES CXX_VISIBILITY_PRESET "hidden"
CUDA_VISIBILITY_PRESET "hidden")

Expand Down
98 changes: 41 additions & 57 deletions python/src/elements.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
#include <thor_scsi/elements/octupole.h>
#include <thor_scsi/elements/corrector.h>
#include <thor_scsi/elements/cavity.h>

#include <gtpsa/python/utils.h>

namespace tse = thor_scsi::elements;
namespace tsc = thor_scsi::core;
Expand Down Expand Up @@ -138,10 +138,8 @@ template<typename double_type, typename Class>
void field_kick_add_set_methods(py::class_<Class> t_mapper)
{
t_mapper
.def("set_dx", [](Class &kick, const double_type& dx)
{kick.getTransform()->setDx(dx);})
.def("set_dy", [](Class &kick, const double_type& dy)
{kick.getTransform()->setDy(dy);})
.def("set_dx", [](Class &kick, const double_type& dx) { kick.getTransform()->setDx(dx) ; })
.def("set_dy", [](Class &kick, const double_type& dy) { kick.getTransform()->setDy(dy) ; })
/*
* Todo: seems to crash the interpreter
*/
Expand All @@ -151,58 +149,47 @@ void field_kick_add_set_methods(py::class_<Class> t_mapper)
;
}

namespace gpy = gtpsa::python;


template<typename Class>
void field_kick_add_get_methods(py::class_<Class> t_mapper)
{
t_mapper
.def("get_dx", [](Class &kick) { return gpy::to_pyobject( kick.getTransform()->getDx()); })
.def("get_dy", [](Class &kick) { return gpy::to_pyobject( kick.getTransform()->getDy()); })
.def("get_roll", [](Class &kick) { return gpy::to_pyobject( kick.getTransform()->getRoll()); })
;
}

template<typename double_type, typename Class>
void field_kick_add_methods(py::class_<Class> t_mapper)
{

//using double_type;
field_kick_add_set_methods<double_type, Class>(t_mapper);
t_mapper
field_kick_add_get_methods(t_mapper);

.def("get_dx",
[](Class &kick)
{return kick.getTransform()->getDx();})
.def("get_dy",
[](Class &kick)
{return kick.getTransform()->getDy();})
.def("get_roll",
[](Class &kick){kick.getTransform()->getRoll();})
.def("is_thick",
&Class::isThick)
.def("as_thick",
&Class::asThick)
.def("get_number_of_integration_steps",
&Class::getNumberOfIntegrationSteps)
.def("set_number_of_integration_steps",
&Class::setNumberOfIntegrationSteps)
.def("get_integration_method",
&Class::getIntegrationMethod)
.def("get_curvature",
&Class::getCurvature)
.def("set_curvature",
&Class::setCurvature)
.def("assuming_curved_trajectory",
&Class::assumingCurvedTrajectory)
.def("get_bending_angle",
&Class::getBendingAngle)
.def("set_bending_angle",
&Class::setBendingAngle)
.def("set_entrance_angle",
&Class::setEntranceAngle)
.def("get_entrance_angle",
&Class::getEntranceAngle)
.def("set_exit_angle",
&Class::setExitAngle)
.def("get_exit_angle",
&Class::getExitAngle)
.def("get_radiation_delegate",
&Class::getRadiationDelegate)
.def("set_radiation_delegate",
&Class::setRadiationDelegate)
.def("get_field_interpolator",
&Class::getFieldInterpolator)
.def("set_field_interpolator",
&Class::setFieldInterpolator)
t_mapper
.def("is_thick", &Class::isThick )
.def("as_thick", &Class::asThick )
.def("get_number_of_integration_steps", &Class::getNumberOfIntegrationSteps )
.def("set_number_of_integration_steps", &Class::setNumberOfIntegrationSteps )
.def("get_integration_method", &Class::getIntegrationMethod )
.def("assuming_curved_trajectory", &Class::assumingCurvedTrajectory )
.def("set_curvature", &Class::setCurvature )
.def("set_bending_angle", &Class::setBendingAngle )
.def("set_entrance_angle", &Class::setEntranceAngle )
.def("set_exit_angle", &Class::setExitAngle )
.def("get_radiation_delegate", &Class::getRadiationDelegate )
.def("set_radiation_delegate", &Class::setRadiationDelegate )
.def("get_field_interpolator", &Class::getFieldInterpolator )
.def("set_field_interpolator", &Class::setFieldInterpolator )

.def("get_curvature", [](const Class& kick) { return gpy::to_pyobject( kick.getCurvature() ); })
.def("get_bending_angle", [](const Class& kick) { return gpy::to_pyobject( kick.getBendingAngle() ); })
.def("get_entrance_angle", [](const Class& kick) { return gpy::to_pyobject( kick.getEntranceAngle() ); })
.def("get_exit_angle", [](const Class& kick) { return gpy::to_pyobject( kick.getExitAngle() ); })
;
}

Expand Down Expand Up @@ -231,8 +218,9 @@ void classical_magnet_add_methods(py::class_<Class> t_mapper)
// .def("set_multipoles",&Class::setMultipoles)
.def("get_main_multipole_number",
&Class::getMainMultipoleNumber)
.def("get_main_multipole_strength",
&Class::getMainMultipoleStrength)
.def("get_main_multipole_strength", [](Class& inst){
return gpy::to_pyobject(inst.getMainMultipoleStrength());
})
.def("get_main_multipole_strength_component",
&Class::getMainMultipoleStrengthComponent)

Expand Down Expand Up @@ -319,11 +307,7 @@ struct TemplatedClasses
py::class_<QuadK, std::shared_ptr<QuadK>>
(this->m_module, quad_name.c_str(), cm)
.def(py::init<const Config &>());
#if 0
// J.B. 14/07/23: test.
.def("Q_init", &QuadK::Q_init)
#endif
;

std::string sext_name = "Sextupole" + this->m_suffix;
typedef tse::SextupoleTypeWithKnob<C> SextK;
py::class_<SextK, std::shared_ptr<SextK>>
Expand Down
28 changes: 20 additions & 8 deletions python/src/interpolation.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@
#include <pybind11/operators.h>
#include <pybind11/stl.h>
#include <pybind11/pytypes.h>
#include <gtpsa/python/utils.h>
#include <thor_scsi/core/field_interpolation.h>
#include <thor_scsi/core/multipoles.h>
#include <complex>

namespace py = pybind11;
namespace gpy = gtpsa::python;
namespace tsc = thor_scsi::core;

typedef std::complex<double> cdbl;
Expand Down Expand Up @@ -108,8 +110,10 @@ template<typename Types, typename Class>
void add_methods_multipoles(py::class_<Class> t_mapper)
{
t_mapper
.def("get_multipole", &Class::getMultipole)
.def("set_multipole", &Class::setMultipole)
.def("get_multipole", [](const Class& inst, int n){ return gpy::to_pyobject(inst.getMultipole(n)); })
.def("set_multipole", [](Class& inst, int n, typename Types::complex_type coeff){
inst.setMultipole(n, coeff);
})
.def("apply_roll_angle", &Class::applyRollAngle)
.def("__str__", &Class::pstr)
.def("__repr__", &Class::repr)
Expand Down Expand Up @@ -268,19 +272,27 @@ void py_thor_scsi_init_field_interpolation(py::module &m) {
> multipoles_tpsa_base(m, "_MultipolesBaseTpsa", field2dintpvar);
add_methods_multipoles<tsc::TpsaVariantType, tsc::TwoDimensionalMultipolesKnobbed<tsc::TpsaVariantType>>(multipoles_tpsa_base);

py::class_<tsc::TwoDimensionalMultipolesTpsa, std::shared_ptr<tsc::TwoDimensionalMultipolesTpsa>>
multipoles_tpsa(m, "TwoDimensionalMultipolesTpsa", multipoles_tpsa_base //, py::buffer_protocol()
);
multipoles_tpsa
.def("set_multipole", [](tsc::TwoDimensionalMultipolesTpsa& inst, const unsigned int n, gtpsa::ctpsa& obj){
multipoles_tpsa_base
.def("set_multipole", [](tsc::TwoDimensionalMultipolesKnobbed<tsc::TpsaVariantType>& inst, const unsigned int n, gtpsa::ctpsa& obj){
inst.setMultipole(n, obj);
})
.def("set_multipole", [](tsc::TwoDimensionalMultipolesTpsa& inst, const unsigned int n, std::complex<double>& obj){
.def("set_multipole", [](tsc::TwoDimensionalMultipolesKnobbed<tsc::TpsaVariantType>& inst, const unsigned int n, std::complex<double> obj){
inst.setMultipole(n, obj);
})
;
py::class_<tsc::TwoDimensionalMultipolesTpsa, std::shared_ptr<tsc::TwoDimensionalMultipolesTpsa>>
multipoles_tpsa(m, "TwoDimensionalMultipolesTpsa", multipoles_tpsa_base //, py::buffer_protocol()
);
add_methods_multipoles<tsc::TpsaVariantType, tsc::TwoDimensionalMultipolesTpsa>(multipoles_tpsa);
multipoles_tpsa
// why do I need that here again ...
// does it not cast down here from above?
.def("set_multipole", [](tsc::TwoDimensionalMultipolesTpsa& inst, const unsigned int n, gtpsa::ctpsa& obj){
inst.setMultipole(n, obj);
})
.def("set_multipole", [](tsc::TwoDimensionalMultipolesTpsa& inst, const unsigned int n, std::complex<double> obj){
inst.setMultipole(n, obj);
})
.def(py::init<const std::complex<double>, const unsigned int>(), "initalise multipoles",
py::arg("default_value"), py::arg("h_max") = tsc::max_multipole)
// shall one return a list of base objects ?
Expand Down
3 changes: 2 additions & 1 deletion python/tests/knobbable_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def test_knob_quadrupole():
assert nquad.name == quad.name
assert nquad.get_length() == pytest.approx(quad.get_length(), rel=1e-12)
# test of to_object should be avoided
K_check = nquad.get_main_multipole_strength().to_object()
K_check = nquad.get_main_multipole_strength()
assert K_check == pytest.approx(K, rel=1e-12)


Expand Down Expand Up @@ -193,6 +193,7 @@ def test_knobbable_dx_set_from_float():
def test_knobbalbe_muls_set_from_float():
from thor_scsi.lib import TwoDimensionalMultipolesTpsa as Mul2DTpsa
m = Mul2DTpsa(0)
print(type(m))
m.set_multipole(3, 1e-3+1e-2j)


Expand Down
25 changes: 13 additions & 12 deletions python/thor_scsi/utils/knobs.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ def make_magnet_offset_knobbable(
Returns:

"""
dxv = magnet.get_dx().to_object()
dyv = magnet.get_dy().to_object()
dxv = magnet.get_dx()
dyv = magnet.get_dy()
dx = gtpsa.tpsa(desc, po, mapping=named_index)
dy = gtpsa.tpsa(desc, po, mapping=named_index)
dx.set_knob(dxv, "dx")
Expand All @@ -42,15 +42,15 @@ def make_magnet_offset_knobbable(
# dy.set_variable(dyv, "dy")
dx.name = magnet.name + "_dx"
dy.name = magnet.name + "_dy"
magnet.set_dx(gtpsa.TpsaOrDouble(dx))
magnet.set_dy(gtpsa.TpsaOrDouble(dy))
magnet.set_dx(dx)
magnet.set_dy(dy)


def make_magnet_offset_unknobbable(magnet: tslib.FieldKick):
dx = float(magnet.get_dx().to_object().get())
dy = float(magnet.get_dy().to_object().get())
magnet.set_dx(gtpsa.TpsaOrDouble(dx))
magnet.set_dy(gtpsa.TpsaOrDouble(dy))
dx = float(magnet.get_dx())
dy = float(magnet.get_dy())
magnet.set_dx(dx)
magnet.set_dy(dy)


def make_magnet_strength_knobbable(
Expand All @@ -63,21 +63,22 @@ def make_magnet_strength_knobbable(
):
if multipole_number is None:
multipole_number = magnet.get_main_multipole_number()
k_orig = magnet.get_main_multipole_strength().to_object()
k_orig = magnet.get_main_multipole_strength()
k = gtpsa.ctpsa(desc, po, mapping=named_index)
k.name = magnet.name + "_K"
k.set_knob(k_orig, "K")
# k.set_variable(k_orig, "K")
magnet.get_multipoles().set_multipole(multipole_number, gtpsa.CTpsaOrComplex(k))
print(type(magnet.get_multipoles()))
magnet.get_multipoles().set_multipole(multipole_number, k)


def make_magnet_strength_unknobbable(magnet: tslib.Mpole, multipole_number: int = None):
if multipole_number is None:
multipole_number = magnet.get_main_multipole_number()
mul = complex(
magnet.get_multipoles().get_multipole(multipole_number).to_object().get()
magnet.get_multipoles().get_multipole(multipole_number)
)
magnet.get_multipoles().set_multipole(multipole_number, gtpsa.CTpsaOrComplex(mul))
magnet.get_multipoles().set_multipole(multipole_number, mul)


def make_magnet_knobbable(
Expand Down