89 #ifndef __GyotoPython_H_ 90 #define __GyotoPython_H_ 210 void track(PyObject*& obj);
320 virtual std::string
module()
const ;
331 virtual void module(
const std::string&);
350 virtual void instance(PyObject * pinstance);
353 virtual std::string
klass()
const ;
368 virtual void klass(
const std::string& c);
371 virtual std::vector<double>
parameters()
const;
378 virtual void parameters(
const std::vector<double>&);
380 virtual bool hasPythonProperty(std::string
const &key)
const ;
381 virtual void setPythonProperty(std::string
const &key,
Value val);
382 virtual void setPythonProperty(std::string
const &key,
Value val, std::string
const &unit);
383 virtual Value getPythonProperty(std::string
const &key)
const;
384 virtual Value getPythonProperty(std::string
const &key, std::string
const &unit)
const;
438 virtual ~Object() {};
442 virtual bool knowsProperty(
const std::string &name)
const {
443 return (O::property(name) || hasPythonProperty(name));
446 virtual void set(std::string
const &key, Value val) {
449 if (hasPythonProperty(key)) {
450 GYOTO_DEBUG <<
"Python key " << key <<
" exists" << std::endl;
451 setPythonProperty(key, val);
453 GYOTO_DEBUG <<
"Python key " << key <<
" does not exist" << std::endl;
458 virtual void set(std::string
const &key, Value val, std::string
const &unit) {
461 if (hasPythonProperty(key)) {
462 GYOTO_DEBUG <<
"Python key " << key <<
" exists" << std::endl;
463 setPythonProperty(key, val, unit);
465 GYOTO_DEBUG <<
"Python key " << key <<
" does not exist" << std::endl;
466 O::set(key, val, unit);
470 virtual void set(Property
const &p, Value val){
471 std::string key=p.name;
473 if (!hasPythonProperty(key)) {
474 GYOTO_DEBUG <<
"calling Generic::set" << std::endl;
478 setPythonProperty(key, val);
481 virtual void set(Property
const &p, Value val, std::string
const &unit) {
483 if (hasPythonProperty(p.name)) {
484 GYOTO_DEBUG <<
"Python key " << p.name <<
" exists" << std::endl;
485 if (unit!=
"")
GYOTO_ERROR(
"units not implemented");
486 setPythonProperty(p.name, val);
488 GYOTO_DEBUG <<
"Python key " << p.name <<
" does not exist" << std::endl;
489 O::set(p, val, unit);
495 virtual Value
get(std::string
const &key)
const {
497 if (!hasPythonProperty(key)) {
498 GYOTO_DEBUG <<
"calling Generic::get" << std::endl;
501 return getPythonProperty(key);
504 virtual Value
get(std::string
const &key, std::string
const &unit)
const {
506 if (!hasPythonProperty(key)) {
507 GYOTO_DEBUG <<
"calling Generic::get" << std::endl;
508 return O::get(key, unit);
510 return getPythonProperty(key, unit);
513 Value
get(Property
const &p,
514 std::string
const &unit)
const {
515 if (!hasPythonProperty(p.name)) {
516 GYOTO_DEBUG <<
"calling Generic::get" << std::endl;
517 return O::get(p, unit);
519 return getPythonProperty(p.name);
522 Value
get(Property
const &p)
const {
523 if (!hasPythonProperty(p.name)) {
524 GYOTO_DEBUG <<
"calling Generic::get" << std::endl;
527 return getPythonProperty(p.name);
530 using O::setParameter;
532 virtual int setParameter(std::string name, std::string content, std::string unit) {
536 if (hasPythonProperty(name)) {
539 p.type=pythonPropertyType(name);
540 GYOTO_DEBUG <<
"Calling setParameter(p, name, content, unit)" << std::endl;
541 setParameter(p, name, content, unit);
544 return O::setParameter(name, content, unit);
549 Property
const &p)
const {
550 if ((p.name ==
"Instance") ||
551 (p.name ==
"Module" &&
module_==
"") ||
555 }
else O::fillProperty(fmp, p);
559 GYOTO_DEBUG <<
"filling C++ properties" << std::endl;
561 GYOTO_DEBUG <<
"filling Python properties" << std::endl;
564 PyObject *pKey, *pVal;
566 if (!PyUnicode_Check(pKey))
GYOTO_ERROR(
"key is not a UNICODE string");
567 std::string key=PyUnicode_AsUTF8(pKey);
568 if (PyDict_Check(pVal)) pVal=PyDict_GetItemString(pVal,
"type");
569 if (!pVal)
GYOTO_ERROR(
"could not get type for property "+key);
570 if (!PyUnicode_Check(pVal))
GYOTO_ERROR(
"type is not a UNICODE string");
571 std::string stype=PyUnicode_AsUTF8(pVal);
572 GYOTO_DEBUG <<
"creating XML element for Python property " 573 << key <<
" of type " << stype << std::endl;
575 const Property p (key, type);
576 this->fillProperty(fmp, p);
582 std::string name=
"", content=
"", unit=
"";
583 FactoryMessenger * child = NULL;
586 std::string xmldir = fmp->
fullPath(
"");
588 GYOTO_DEBUG <<
"adding '"+xmldir+
"' to sys.path" << std::endl;
590 PyObject* sysPath = PySys_GetObject(
"path");
596 PyObject* pyPath = PyUnicode_FromString(xmldir.c_str());
598 Py_ssize_t pathSize = PyList_Size(sysPath);
600 for (Py_ssize_t i = 0; i < pathSize; ++i) {
601 PyObject* item = PyList_GetItem(sysPath, i);
602 if (PyUnicode_Compare(item, pyPath) == 0) {
608 PyList_Insert(sysPath, 0, pyPath);
616 GYOTO_DEBUG <<
"Setting '" << name <<
"' to '" << content
617 <<
"' (unit='"<<unit<<
"')" << std::endl;
618 const Property * prop =NULL;
619 bool need_delete=
false;
620 if (hasPythonProperty(name)) {
622 prop =
new Property(name, pythonPropertyType(name));
625 prop = this->property(name);
628 GYOTO_DEBUG <<
"'" << name <<
"' not found, calling setParameter()" 632 setParameter(name, content, unit);
634 GYOTO_DEBUG <<
"'" << name <<
"' found "<< std::endl;
635 std::vector<std::string> plugins;
636 switch (prop->type) {
638 set(*prop, fmp->
metric());
644 set(*prop, fmp->
screen());
647 content = fmp -> getAttribute(
"kind");
648 child = fmp -> getChild();
649 plugins =
Gyoto::split(fmp -> getAttribute(
"plugin"),
",");
654 content = fmp -> getAttribute(
"kind");
655 child = fmp -> getChild();
656 plugins =
Gyoto::split(fmp -> getAttribute(
"plugin"),
",");
664 setParameter(*prop, name, content, unit);
668 if (need_delete)
delete prop;
671 GYOTO_DEBUG <<
"Done processing parameters" << std::endl;
740 virtual std::string
module() const ;
741 virtual void
module(const std::string&);
744 virtual std::string
klass() const ;
745 virtual void
klass(const std::string&);
753 virtual void
instance(size_t address);
756 virtual std::vector<double>
parameters() const;
757 virtual void
parameters(const std::vector<double>&);
759 virtual double operator()(double nu) const;
760 virtual double operator()(double nu, double opacity, double ds) const;
762 virtual double
integrate(double nu1, double nu2) ;
800 PyObject * pChristoffel_;
815 PyObject * pGetSpecificAngularMomentum_;
820 PyObject * pGetPotential_;
825 PyObject * pIsStopCondition_;
830 PyObject * pCircularVelocity_;
843 void spherical(bool);
844 bool spherical() const;
845 virtual std::string
module() const ;
846 virtual void
module(const std::string&);
849 virtual std::string
klass() const ;
850 virtual void
klass(const std::string&);
858 virtual void
instance(size_t address);
861 virtual std::vector<double>
parameters() const;
862 virtual void
parameters(const std::vector<double>&);
864 virtual void mass(double m);
867 void gmunu(double g[4][4], const double * x) const ;
868 int christoffel(double dst[4][4][4], const double * x) const ;
871 double getRmb() const;
872 double getRms() const;
873 double getSpecificAngularMomentum(double rr) const;
874 double getPotential(double const pos[4], double l_cst) const;
875 int isStopCondition(double const coord[8]) const;
876 void circularVelocity(double const pos[4], double vel[4],
877 double dir=1.) const ;
896 PyObject *pEmission_, *pIntegrateEmission_, *pTransmission_, *
pCall_,
897 *pGetVelocity_, *pGiveDelta_;
898 bool pEmission_overloaded_, pIntegrateEmission_overloaded_;
911 virtual double emission(double nu_em, double dsem, state_t const &coord_ph,
912 double const coord_obj[8]=NULL) const ;
914 virtual void emission(double Inu[], double const nu_em[], size_t nbnu,
915 double dsem, state_t const &coord_ph,
916 double const coord_obj[8]=NULL) const ;
918 virtual double integrateEmission(double nu1, double nu2, double dsem,
919 state_t const &c_ph, double const c_obj[8]=NULL) const;
921 virtual void integrateEmission(double * I, double const * boundaries,
922 size_t const * chaninds, size_t nbnu,
923 double dsem, state_t const &cph, double const *co) const;
925 virtual double transmission(double nuem, double dsem, state_t const &cph, double const *co) const ;
928 virtual double operator()(double const coord[4]) ;
929 virtual void getVelocity(double const pos[4], double vel[4]) ;
930 virtual double giveDelta(double coord[8]);
933 virtual std::string
module() const ;
934 virtual void
module(const std::string&);
937 virtual std::string
klass() const ;
938 virtual void
klass(const std::string& c);
946 virtual void
instance(size_t address);
949 virtual std::vector<double>
parameters() const;
950 virtual void
parameters(const std::vector<double>&);
951 virtual double criticalValue() const ;
952 virtual void criticalValue(double) ;
971 PyObject *pEmission_, *pIntegrateEmission_, *pTransmission_, *
pCall_,
972 *pGetVelocity_, *pGiveDelta_;
973 bool pEmission_overloaded_, pIntegrateEmission_overloaded_;
986 virtual double emission(double nu_em, double dsem, state_t const &coord_ph,
987 double const coord_obj[8]=NULL) const ;
989 virtual void emission(double Inu[], double const nu_em[], size_t nbnu,
990 double dsem, state_t const &coord_ph,
991 double const coord_obj[8]=NULL) const ;
993 virtual double integrateEmission(double nu1, double nu2, double dsem,
994 state_t const &c_ph, double const c_obj[8]=NULL) const;
996 virtual void integrateEmission(double * I, double const * boundaries,
997 size_t const * chaninds, size_t nbnu,
998 double dsem, state_t const &cph, double const *co) const;
1000 virtual double transmission(double nuem, double dsem, state_t const &cph ,double const *co) const ;
1003 virtual double operator()(double const coord[4]) ;
1004 virtual void getVelocity(double const pos[4], double vel[4]) ;
1007 virtual std::string
module() const ;
1008 virtual void
module(const std::string&);
1011 virtual std::string
klass() const ;
1012 virtual void
klass(const std::string&);
1020 virtual void
instance(size_t address);
1023 virtual std::vector<double>
parameters() const;
1024 virtual void
parameters(const std::vector<double>&);
virtual void instance(PyObject *pinstance)
Set pInstance_.
virtual size_t instance() const
Retrieve pInstance_.
int getNextParameter(std::string *name, std::string *content, std::string *unit=NULL)
Get name and value of next parameter.
PyObject * pGyotoMetric()
Get reference to the Metric constructor in the gyoto Python extension.
#define GYOTO_OBJECT
Declare class::properties and class::getProperties()
Definition: GyotoObject.h:84
PyObject * pGyotoAstrobj()
Get reference to the Astrobj constructor in the gyoto Python extension.
void track(PyObject *&obj)
Add a PyObject* to the list of tracked objects.
virtual double integrate(double nu1, double nu2)
Integrate optically thick I_nu.
SmartPointer< Astrobj::Generic > astrobj()
Build and get the Astrobj described in this XML file.
PyObject * pGyotoSpectrometer()
Get reference to the Spectrometer constructor in the gyoto Python extension.
double mass() const
Get mass used in unitLength()
PyObject * pModule_
Reference to the python module once it has been loaded.
Definition: GyotoPython.h:293
std::string class_
Name of the Python class that we want to expose.
Definition: GyotoPython.h:279
Type is std::string and holds a file name.
Definition: GyotoProperty.h:638
virtual void attachInstance(PyObject *instance)
Attach pInstance_ and cached method pointers.
PyObject * pGyotoThinDisk()
Get reference to the ThinDisk constructor in the gyoto Python extension.
virtual void attachInstance(PyObject *instance)
Attach pInstance_ and cached method pointers.
void PyInstance_SetThis(PyObject *pInstance, PyObject *pNew, void *ptr)
Set "this" attribute in instance.
#define GYOTO_OBJECT_THREAD_SAFETY
Declare virtual bool isThreadSafe() const.
Definition: GyotoObject.h:99
virtual std::string klass() const
Retrieve class_.
Introspectable properties.
GILGuard()
Constructor: Acquires the GIL.
#define GYOTO_DEBUG
Display debug message (in debug mode)
Definition: GyotoDefs.h:367
#define size_t
If not defined in <sys/types.h>.
Definition: GyotoConfig.h:424
Factory / SmartPointee::Subcontractor_t interface.
PyObject * pInstance_
Reference to the python instance once it has been instantiated.
Definition: GyotoPython.h:298
Type is Gyoto::SmartPointer<Gyoto::Metric::Generic>
Definition: GyotoProperty.h:644
Factory / SmartPointee::Subcontractor_t interface.
Definition: GyotoFactoryMessenger.h:92
PyObject * pGyotoSpectrum()
Get reference to the Spectrum constructor in the gyoto Python extension.
PyObject * pGyotoScreen()
Get reference to the Screen constructor in the gyoto Python extension.
virtual void detachInstance()
Detach pInstance_ and cached method pointers.
SmartPointer< Screen > screen()
Build and get the Screen described in this XML file.
std::string fullPath(const std::string &relpath) const
Transform path into full path specification.
std::string module_
Name of the Python module that holds the class.
Definition: GyotoPython.h:266
#define GYOTO_ERROR(msg)
Throw a Gyoto::Error nicely.
Definition: GyotoError.h:196
Astronomical objects defined bya a potential/distance.
PyObject * instantiateClass(std::string &klass) const
Creates an instance of class klass in module module_.
PyObject * pCall_
Reference to ___call__.
Definition: GyotoPython.h:700
virtual std::string inlineModule() const
Return inline_module_.
#define GYOTO_DEBUG_EXPR(a)
Output expression value in debug mode.
Definition: GyotoDefs.h:280
PyGILState_STATE gstate_
The GIL state guarded by this GILGuard.
Definition: GyotoPython.h:218
std::string inline_module_
Python source code for module that holds the class.
Definition: GyotoPython.h:272
PyObject * pGet_
Reference to the (optional) Get method.
Definition: GyotoPython.h:313
std::vector< PyObject ** > tracked_objects_
Stores pointers to PyObject* variables.
Definition: GyotoPython.h:220
Gyoto::Spectrometer::Subcontractor_t * getSubcontractor(std::string name, std::vector< std::string > &plugins, int errmode=0)
Query the Spectrometer register Spectrometer::Register_.
PyObject * PyImport_Gyoto()
Return reference to the gyoto module, or NULL.
Base class for metric description.
virtual std::string module() const
Return module_.
type_e
Possible type of a Property instance.
Definition: GyotoProperty.h:616
Type is Gyoto::SmartPointer<Gyoto::Spectrum::Generic>
Definition: GyotoProperty.h:650
void checkModuleForSingleClass()
Look for single class in pModule_.
Metric coded in Python.
Definition: GyotoPython.h:785
Coding a Gyoto::Astrobj::Standard in Python.
Definition: GyotoPython.h:890
virtual std::vector< double > parameters() const
Retrieve parameters_.
bool pCall_overloaded_
Whether call is overloaded.
Definition: GyotoPython.h:724
virtual void detachInstance()
Detach pInstance_ and cached method pointers.
Geometrically thin disks and rings.
Spectrum of a simple object (e.g. Star)
Namespace for the Gyoto library.
Definition: GyotoAstrobj.h:46
Type is Gyoto::SmartPointer<Gyoto::Spectrometer::Generic>
Definition: GyotoProperty.h:652
PyObject * pIntegrate_
Reference to the (optional) integrate method.
Definition: GyotoPython.h:705
PyObject * PyInstance_GetMethod(PyObject *pInstance, const char *name)
Return new reference to method, or NULL if method not found.
virtual Python * clone() const
Cloner.
Manage the GIL and free pointers on error.
Definition: GyotoPython.h:198
Pointers performing reference counting.
Definition: GyotoProperty.h:45
static type_e typeFromString(std::string stype)
Get Property::type_e value from name.
Type is Gyoto::SmartPointer<Gyoto::Astrobj::Generic>
Definition: GyotoProperty.h:648
Gyoto::Spectrum::Subcontractor_t * getSubcontractor(std::string name, std::vector< std::string > &plugins, int errmode=0)
Query the Spectrum register Spectrum::Register_.
Coding a Gyoto::Astrobj::ThinDisk in Python.
Definition: GyotoPython.h:965
virtual std::string inlineModule() const
Return inline_module_.
SmartPointer< Metric::Generic > metric()
Build and get the Metric described in this XML file.
Container for the value of a Property.
Definition: GyotoValue.h:60
std::vector< std::string > split(std::string const &src, std::string const &delim)
Split string.
Loader for Python classes implementing the Spectrum interface.
Definition: GyotoPython.h:688
virtual std::vector< double > parameters() const
Retrieve parameters_.
PyObject * pSet_
Reference to the (optional) Set method.
Definition: GyotoPython.h:308
virtual std::string module() const
Return module_.
Object with properties.
Definition: GyotoObject.h:151
Base class for classes in the Python plug-in.
Definition: GyotoPython.h:258
Description of the observer screen.
virtual std::string klass() const
Retrieve class_.
PyObject * PyObject_FromGyotoValue(const Gyoto::Value &)
Convert Gyoto Value to Python Object.
Type is Gyoto::SmartPointer<Gyoto::Screen::Generic>
Definition: GyotoProperty.h:646
Class template to implement parts of the Gyoto::Object API.
Definition: GyotoPython.h:110
PyObject * pGyotoStandardAstrobj()
Get reference to the StandardAstrobj constructor in the gyoto Python extension.
bool PyCallable_HasVarArg(PyObject *pMethod)
Check whether method accepts the varargs argument.
PyObject * pProperties_
Reference to the properties member.
Definition: GyotoPython.h:303
PyObject * PyModule_NewFromPythonCode(const char *code)
Create module from Python source code in a C string.
std::vector< double > parameters_
Parameters that this class needs.
Definition: GyotoPython.h:288