21#include <structmember.h>
26#include <boost/python.hpp>
27#include <boost/noncopyable.hpp>
28#include <boost/mpl/front.hpp>
156 inline boost::python::class_<PyC>
make_class(
const char* className);
176 template <
class PYCLASS>
181 template <
class PYCLASS>
187 template <
class PYCLASS>
193 template <
class PYCLASS>
199 template <
class PYCLASS>
205 template <
class PYCLASS>
212 template <
class PYCLASS>
223 template <
class PYCLASS>
244 template <
class PYCLASS>
246 double distanceUnits,
double timeUnits,
247 double bfieldUnits,
double efieldUnits);
252 template <
class ValueType>
263 template <
class ValueType>
304 double x,
double y,
double z,
double t);
321 boost::python::dict kwargs);
346template <
class ValueType>
355 double x,
double y,
double z,
double t) {
361 if (component ==
nullptr) {
363 "Failed to deduce Component from ElementBase.");
370 bool outOfBounds = component->
apply(R, P, t, E, B);
371 return boost::python::make_tuple(outOfBounds,
379 boost::python::dict kwargs) {
380 if (boost::python::len(args) == 0) {
382 "Did not find class instance (i.e. 'self') when calling "
384 }
else if (boost::python::len(args) > 1) {
386 "set_attributes cannot take any non-keyword args (except the class"
389 PyOpalObject<C> self = boost::python::extract<PyOpalObject<C> >(args[0]);
390 boost::python::list key_list = kwargs.keys();
391 for (boost::python::ssize_t i = 0; i < boost::python::len(key_list); ++i) {
392 boost::python::object key = key_list[i];
393 std::string keyStr = boost::python::extract<std::string>(key);
394 boost::python::object value = kwargs[key];
398 "Did not recognise attribute '"+keyStr+
"'");
403 boost::python::object obj;
426 template <
class ArgumentPackage>
427 PyObject*
postcall(ArgumentPackage
const&, PyObject* result);
457 template <
class ArgumentPackage>
458 PyObject*
postcall(ArgumentPackage
const& args, PyObject* result);
475 objectPtr->execute();
482 if (objectPtr ==
nullptr) {
484 "Trying to register something that was not a Opal Object");
493 return objectPtr->getOpalName();
499 objectPtr->setOpalName(
name);
505 objectPtr->setOpalName(
name);
506 if (objectPtr->getElement()) {
507 objectPtr->getElement()->setName(
name);
513 std::shared_ptr<OpalElement> elementPtr =
514 std::dynamic_pointer_cast<OpalElement, C>(pyobject.
getOpalShared());
515 if (elementPtr.get() ==
nullptr) {
517 "Wrapped object was not an OpalElement");
520 boost::python::object pyelement(element);
536 "Object was not initialised");
539 if (attribute ==
nullptr) {
541 "Failed to parse attribute "+opalName);
553 pyvalue = PyFloat_FromDouble(value);
559 pyvalue = PyLong_FromDouble(value);
567 pyvalue = PyUnicode_FromString(value.c_str());
583 pyvalue = PyList_New(value.size());
584 for (
size_t i = 0; i < value.size(); ++i) {
585 PyList_SetItem(pyvalue, i, PyFloat_FromDouble(value[i]));
592 pyvalue = PyList_New(value.size());
593 for (
size_t i = 0; i < value.size(); ++i) {
594 PyList_SetItem(pyvalue, i, PyUnicode_FromString(value[i].c_str()));
613 "Element was not initialised");
616 if (attribute ==
nullptr) {
618 "Failed to parse attribute "+opalName);
623 double value = PyFloat_AsDouble(pyvalue);
629 double value = PyLong_AsDouble(pyvalue);
635 std::string value = PyUnicode_AsUTF8(pyvalue);
645 std::string value = PyUnicode_AsUTF8(pyvalue);
651 std::string value = PyUnicode_AsUTF8(pyvalue);
657 bool value = PyObject_IsTrue(pyvalue);
663 Py_ssize_t listSize = PyList_Size(pyvalue);
664 std::vector<double> value(listSize);
665 for (Py_ssize_t i = 0; i < listSize; ++i) {
666 double value_i = PyFloat_AsDouble(PyList_GetItem(pyvalue, i));
674 Py_ssize_t listSize = PyList_Size(pyvalue);
675 std::vector<std::string> value(listSize);
676 for (Py_ssize_t i = 0; i < listSize; ++i) {
677 PyObject* pyvalue_i = PyList_GetItem(pyvalue, i);
678 std::string value_i = PyUnicode_AsUTF8(pyvalue_i);
699 if (attribute ==
nullptr) {
717 typedef boost::python::class_<PyOpalObject<C> > PyClass;
718 boost::python::docstring_options docop(
true,
true,
false);
719 PyClass pyclass = PyClass(className);
725 std::cerr <<
"Failed to initialise class because '" << exc.
what()
728 }
catch (std::exception& exc) {
729 std::cerr <<
"Failed to initialise class because '" << exc.
what()
733 if (
gmsg ==
nullptr) {
735 "Failed to initialise class because gmsg was not initialised.\n"
736 "PyOpal::Globals::Initialise() must be called before attempting to\n"
737 "initialise classes" << std::endl;
739 "Globals not initialised");
760template <
class PYCLASS>
766template <
class PYCLASS>
772template <
class PYCLASS>
778template <
class PYCLASS>
780 pyclass.def(
"set_attributes",
786template <
class PYCLASS>
792template <
class PYCLASS>
798template <
class PYCLASS>
804template <
class PYCLASS>
806 double distanceUnits,
double timeUnits,
807 double efieldUnits,
double bfieldUnits) {
812 pyclass.def(
"get_field_value",
814 boost::python::args(
"x",
"y",
"z",
"t"),
819template <
class PYCLASS>
821 for (std::vector<AttributeDef>::iterator iter =
attributes.begin(); iter !=
attributes.end(); ++iter) {
826 std::string pyname = iter->pyName_m.c_str();
827 switch (iter->type_m) {
830 pyclass.add_property(pyname.c_str(),
839 pyclass.add_property(pyname.c_str(),
850 pyclass.add_property(pyname.c_str(),
859 pyclass.add_property(pyname.c_str(),
869 pyclass.add_property(pyname.c_str(),
881 throw OpalException(
"PyOpalObject<C>::addAttributes",
"Type not implemented");
893 "Get the field value at a point in the field map.\n"
895 "The field lookup is performed against the last RINGDEFINITION that was\n"
896 "instantiated. This should be instantiated by calling\n"
897 "pyopal.parser.initialise_from_opal_file\n"
912 "The function returns a tuple containing 7 values:\n"
913 "out of bounds : int\n"
914 " 1 if the event was out of the field map boundary, else 0.\n"
916 " x magnetic field [T]\n"
918 " y magnetic field [T]\n"
920 " z magnetic field [T]\n"
922 " x electric field [MV/m]\n"
924 " y electric field [MV/m]\n"
926 " z electric field [MV/m]\n";
932template <
class ArgumentPackage>
945template <
class ArgumentPackage>
949 if (!PyArg_ParseTuple(args,
"OO", &pyObject, &value)) {
PartBunchBase< T, Dim >::ConstIterator end(PartBunchBase< T, Dim > const &bunch)
double getReal(const Attribute &attr)
Return real value.
void setRealArray(Attribute &attr, const std::vector< double > &value)
Set array value.
void setBool(Attribute &attr, bool val)
Set logical value.
void setUpperCaseString(Attribute &attr, const std::string &val)
Set uppercase string value.
void setString(Attribute &attr, const std::string &val)
Set string value.
bool getBool(const Attribute &attr)
Return logical value.
void setReal(Attribute &attr, double val)
Set real value.
void setStringArray(Attribute &attr, const std::vector< std::string > &value)
Set string array value.
std::vector< double > getRealArray(const Attribute &attr)
Get array value.
std::vector< std::string > getStringArray(const Attribute &attr)
Get string array value.
void setPredefinedString(Attribute &attr, const std::string &val)
Set predefined string value.
std::string getString(const Attribute &attr)
Get string value.
std::map< AttributeType, std::string > attributeName
void update(PyOpalObjectNS::PyOpalObject< C > pyelement)
A representation of an Object attribute.
const std::string & getHelp() const
Return the help string.
The base class for all OPAL objects.
static OpalData * getInstance()
void define(Object *newObject)
Define a new object.
Interface for a single beam element.
virtual bool apply(const size_t &i, const double &t, Vector_t &E, Vector_t &B)
void addRegister(PYCLASS &pyclass)
void addGetOpalName(PYCLASS &pyclass)
static double distanceUnits_m
void dummySet(ValueType test)
static std::string getOpalName(const PyOpalObject< C > &pyobject)
void addSetOpalName(PYCLASS &pyclass)
static std::vector< AttributeDef > attributes
static void registerObject(PyOpalObject< C > &pyobject)
std::shared_ptr< C > object_m
static std::map< std::string, AttributeDef > pyNameToAttribute
void addAttributes(PYCLASS &pyclass)
void addGetOpalElement(PYCLASS &pyclass)
virtual ~PyOpalObject()=default
static double timeUnits_m
static boost::python::object setAttributes(boost::python::tuple args, boost::python::dict kwargs)
static double bfieldUnits_m
void addSetOpalElementName(PYCLASS &pyclass)
boost::python::class_< PyC > make_generic_class(const char *className)
void addGetFieldValue(PYCLASS &pyclass, double distanceUnits, double timeUnits, double bfieldUnits, double efieldUnits)
ValueType dummyGet() const
std::string getDocString(AttributeDef &def)
PyOpalObject(std::shared_ptr< C > object)
static boost::python::object getFieldValue(PyOpalObjectNS::PyOpalObject< C > &pyobject, double x, double y, double z, double t)
static boost::python::object getPyOpalElement(PyOpalObject< C > &pyobject)
static void setOpalElementName(PyOpalObject< C > &pyobject, std::string name)
static bool converterRegistered
void addSetAttributes(PYCLASS &pyclass)
std::shared_ptr< C > getOpalShared()
PyObject * getAttribute(AttributeType type, std::string opalName) const
boost::python::class_< PyC > make_class(const char *className)
static void execute(PyOpalObject< C > &pyobject)
boost::python::class_< PyC > make_element_class(const char *className)
void addExecute(PYCLASS &pyclass)
static const std::string getFieldValueDocString
static std::string classDocstring
static double efieldUnits_m
std::shared_ptr< C > getOpalShared() const
static void setOpalName(PyOpalObject< C > &pyobject, std::string name)
void setAttribute(AttributeType type, std::string opalName, PyObject *value)
static void setObject(const PyOpalObject< C > *object)
static const PyOpalObject< C > * object_m
~PyOpalObjectGetProperty()
PyObject * postcall(ArgumentPackage const &, PyObject *result)
PyOpalObjectGetProperty(AttributeType type, std::string opalName)
static void setObject(PyOpalObject< C > *object)
~PyOpalObjectSetProperty()
PyObject * postcall(ArgumentPackage const &args, PyObject *result)
static PyOpalObject< C > * object_m
PyOpalObjectSetProperty(AttributeType type, std::string opalName)
The base class for all OPAL exceptions.
virtual const std::string & what() const
Return the message string for the exception.
Vektor< double, 3 > Vector_t