4#include <structmember.h>
9#include <boost/python.hpp>
10#include <boost/noncopyable.hpp>
11#include <boost/mpl/front.hpp>
47namespace PyOpalObjectNS {
50template <
class C>
class PyOpalObject;
51template <
class C>
struct PyOpalObjectGetProperty;
52template <
class C>
struct PyOpalObjectSetProperty;
137 inline boost::python::class_<PyC>
make_class(
const char* className);
141 template <
class PYCLASS>
146 template <
class PYCLASS>
152 template <
class PYCLASS>
158 template <
class PYCLASS>
164 template <
class PYCLASS>
175 template <
class PYCLASS>
196 template <
class PYCLASS>
198 double distanceUnits,
double timeUnits,
199 double bfieldUnits,
double efieldUnits);
204 template <
class ValueType>
210 template <
class ValueType>
249 double x,
double y,
double z,
double t);
264template <
class ValueType>
273 double x,
double y,
double z,
double t) {
278 if (component ==
nullptr) {
280 "Failed to deduce Component from ElementBase.");
282 Vector_t R(x*distanceUnits_m, y*distanceUnits_m, z*distanceUnits_m);
287 bool outOfBounds = component->
apply(
R, P, t, E, B);
288 return boost::python::make_tuple(outOfBounds,
289 B[0]*bfieldUnits_m, B[1]*bfieldUnits_m, B[2]*bfieldUnits_m,
290 E[0]*efieldUnits_m, E[1]*efieldUnits_m, E[2]*efieldUnits_m);
312 template <
class ArgumentPackage>
313 PyObject*
postcall(ArgumentPackage
const&, PyObject* result);
343 template <
class ArgumentPackage>
344 PyObject*
postcall(ArgumentPackage
const& args, PyObject* result);
360 std::shared_ptr<C> objectPtr = pyobject.getOpalShared();
361 objectPtr->execute();
366 C* wrappedC = pyobject.getOpalShared().get();
368 if (objectPtr ==
nullptr) {
370 "Trying to register something that was not a Opal Object");
379 return objectPtr->getOpalName();
385 objectPtr->setOpalName(
name);
390 std::shared_ptr<OpalElement> elementPtr =
391 std::dynamic_pointer_cast<OpalElement, C>(pyobject.
getOpalShared());
392 if (elementPtr.get() ==
nullptr) {
394 "Wrapped object was not an OpalElement");
396 PyOpalObject<OpalElement> element(elementPtr);
397 boost::python::object pyelement(element);
413 "Object was not initialised");
415 Attribute* attribute = object_m->findAttribute(opalName);
416 if (attribute ==
nullptr) {
418 "Failed to parse attribute "+opalName);
430 pyvalue = PyFloat_FromDouble(value);
436 pyvalue = PyLong_FromDouble(value);
444 pyvalue = PyUnicode_FromString(value.c_str());
460 pyvalue = PyList_New(value.size());
461 for (
size_t i = 0; i < value.size(); ++i) {
462 PyList_SetItem(pyvalue, i, PyFloat_FromDouble(value[i]));
469 pyvalue = PyList_New(value.size());
470 for (
size_t i = 0; i < value.size(); ++i) {
471 PyList_SetItem(pyvalue, i, PyUnicode_FromString(value[i].c_str()));
490 "Element was not initialised");
492 Attribute* attribute = object_m->findAttribute(opalName);
493 if (attribute ==
nullptr) {
495 "Failed to parse attribute "+opalName);
500 double value = PyFloat_AsDouble(pyvalue);
506 double value = PyLong_AsDouble(pyvalue);
512 std::string value = PyUnicode_AsUTF8(pyvalue);
522 std::string value = PyUnicode_AsUTF8(pyvalue);
528 std::string value = PyUnicode_AsUTF8(pyvalue);
534 bool value = PyObject_IsTrue(pyvalue);
540 Py_ssize_t listSize = PyList_Size(pyvalue);
541 std::vector<double> value(listSize);
542 for (Py_ssize_t i = 0; i < listSize; ++i) {
543 double value_i = PyFloat_AsDouble(PyList_GetItem(pyvalue, i));
551 Py_ssize_t listSize = PyList_Size(pyvalue);
552 std::vector<std::string> value(listSize);
553 for (Py_ssize_t i = 0; i < listSize; ++i) {
554 PyObject* pyvalue_i = PyList_GetItem(pyvalue, i);
555 std::string value_i = PyUnicode_AsUTF8(pyvalue_i);
575 Attribute* attribute = object_m->findAttribute(def.opalName_m);
576 if (attribute ==
nullptr) {
578 "Failed to parse attribute "+def.opalName_m);
581 if (def.docString_m !=
"") {
582 docString = def.pyName_m+
" ("+
attributeName[def.type_m]+
"): "+def.docString_m;
594 typedef boost::python::class_<PyOpalObject<C> > PyClass;
595 boost::python::docstring_options docop(
true,
true,
false);
596 PyClass pyclass = PyClass(className);
598 addAttributes(pyclass);
599 addGetOpalName(pyclass);
600 addSetOpalName(pyclass);
602 std::cerr <<
"Failed to initialise class because '" << exc.
what()
611template <
class PYCLASS>
617template <
class PYCLASS>
624template <
class PYCLASS>
630template <
class PYCLASS>
636template <
class PYCLASS>
642template <
class PYCLASS>
644 double distanceUnits,
double timeUnits,
645 double efieldUnits,
double bfieldUnits) {
646 distanceUnits_m = distanceUnits;
647 timeUnits_m = timeUnits;
648 bfieldUnits_m = bfieldUnits;
649 efieldUnits_m = efieldUnits;
650 pyclass.def(
"get_field_value",
652 boost::python::args(
"x",
"y",
"z",
"t"),
653 getFieldValueDocString.c_str());
657template <
class PYCLASS>
662 std::string docString = getDocString(*iter);
663 std::string pyname = iter->pyName_m.c_str();
664 switch (iter->type_m) {
667 pyclass.add_property(pyname.c_str(),
668 boost::python::make_function(&PyC::dummyGet<double>, getProp),
669 boost::python::make_function(&PyC::dummySet<double>, setProp),
676 pyclass.add_property(pyname.c_str(),
677 boost::python::make_function(&PyC::dummyGet<int>, getProp),
678 boost::python::make_function(&PyC::dummySet<int>, setProp),
687 pyclass.add_property(pyname.c_str(),
688 boost::python::make_function(&PyC::dummyGet<std::string>, getProp),
689 boost::python::make_function(&PyC::dummySet<std::string>, setProp),
696 pyclass.add_property(pyname.c_str(),
697 boost::python::make_function(&PyC::dummyGet<bool>, getProp),
698 boost::python::make_function(&PyC::dummySet<bool>, setProp),
706 pyclass.add_property(pyname.c_str(),
707 boost::python::make_function(&PyC::dummyGet<boost::python::list>, getProp),
708 boost::python::make_function(&PyC::dummySet<boost::python::list>, setProp),
718 throw OpalException(
"PyOpalObject<C>::addAttributes",
"Type not implemented");
730 "Get the field value at a point in the field map.\n"
732 "The field lookup is performed against the last RINGDEFINITION that was\n"
733 "instantiated. This should be instantiated by calling\n"
734 "pyopal.parser.initialise_from_opal_file\n"
749 "The function returns a tuple containing 7 values:\n"
750 "out of bounds : int\n"
751 " 1 if the event was out of the field map boundary, else 0.\n"
753 " x magnetic field [T]\n"
755 " y magnetic field [T]\n"
757 " z magnetic field [T]\n"
759 " x electric field [MV/m]\n"
761 " y electric field [MV/m]\n"
763 " z electric field [MV/m]\n";
769template <
class ArgumentPackage>
772 result = object_m->getAttribute(type_m, opalName_m);
782template <
class ArgumentPackage>
786 if (!PyArg_ParseTuple(args,
"OO", &pyObject, &value)) {
790 object_m->setAttribute(type_m, opalName_m, value);
Inform & endl(Inform &inf)
std::map< AttributeType, std::string > attributeName
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::string::iterator iterator
boost::function< boost::tuple< double, bool >(arguments_t)> type
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 void registerObject(PyOpalObject< C > &pyobject)
static std::vector< AttributeDef > attributes
void addAttributes(PYCLASS &pyclass)
void addGetOpalElement(PYCLASS &pyclass)
static double timeUnits_m
static double bfieldUnits_m
static std::string classDocstring
PyOpalObject(const PyOpalObject< C > &rhs)
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)
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)
void addExecute(PYCLASS &pyclass)
static const std::string getFieldValueDocString
static void execute(PyOpalObject< C > &pyobject)
static bool converterRegistered
std::shared_ptr< C > object_m
static boost::python::object getPyOpalElement(PyOpalObject< C > &pyobject)
static double efieldUnits_m
static void registerObject(PyOpalObject< C > &pyobject)
std::shared_ptr< C > getOpalShared() const
static void setOpalName(PyOpalObject< C > &pyobject, std::string name)
void setAttribute(AttributeType type, std::string opalName, PyObject *value)
boost::python::class_< PyC > make_class(const char *className)
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)
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)
The base class for all OPAL exceptions.
virtual const std::string & what() const
Return the message string for the exception.