1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382
|
#include "GyotoPython.h"
#include "GyotoProperty.h"
#include "GyotoError.h"
#include <Python.h>
#define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION
#define NO_IMPORT_ARRAY
#define PY_ARRAY_UNIQUE_SYMBOL GyotoPython_ARRAY_API
#include <numpy/arrayobject.h>
using namespace Gyoto;
using namespace std;
GYOTO_PROPERTY_START(Gyoto::Astrobj::Python::ThinDisk,
"Python-based Astrobj class")
GYOTO_PROPERTY_STRING(Gyoto::Astrobj::Python::ThinDisk, Module, module,
"Python module containing the Astrobj implementation.")
GYOTO_PROPERTY_STRING(Gyoto::Astrobj::Python::ThinDisk, InlineModule, inlineModule,
"Inline code of Python module containing the Spectrum implementation.")
GYOTO_PROPERTY_STRING(Gyoto::Astrobj::Python::ThinDisk, Class, klass,
"Python class (in Module) implementing the Astrobj.")
GYOTO_PROPERTY_VECTOR_DOUBLE(Gyoto::Astrobj::Python::ThinDisk,
Parameters, parameters,
"Parameters for the class instance.")
GYOTO_PROPERTY_END(Astrobj::Python::ThinDisk, Astrobj::ThinDisk::properties)
GYOTO_PROPERTY_THREAD_UNSAFE(Astrobj::Python::ThinDisk)
// Birth and death
Gyoto::Astrobj::Python::ThinDisk::ThinDisk()
: Gyoto::Python::Object<Astrobj::ThinDisk>(),
pEmission_(NULL), pIntegrateEmission_(NULL), pTransmission_(NULL),
pCall_(NULL), pGetVelocity_(NULL),
pEmission_overloaded_(false), pIntegrateEmission_overloaded_(false)
{
kind("Python::ThinDisk");
}
Gyoto::Astrobj::Python::ThinDisk::ThinDisk(const ThinDisk& o)
:
Gyoto::Python::Object<Astrobj::ThinDisk>(o),
pEmission_(o.pEmission_), pIntegrateEmission_(o.pIntegrateEmission_),
pTransmission_(o.pTransmission_), pCall_(o.pCall_),
pGetVelocity_(o.pGetVelocity_),
pEmission_overloaded_(o.pEmission_overloaded_),
pIntegrateEmission_overloaded_(o.pIntegrateEmission_overloaded_)
{
Py_XINCREF(pEmission_);
Py_XINCREF(pIntegrateEmission_);
Py_XINCREF(pTransmission_);
Py_XINCREF(pCall_);
Py_XINCREF(pGetVelocity_);
}
Gyoto::Astrobj::Python::ThinDisk::~ThinDisk() {
Py_XDECREF(pEmission_);
Py_XDECREF(pIntegrateEmission_);
Py_XDECREF(pTransmission_);
Py_XDECREF(pCall_);
Py_XDECREF(pGetVelocity_);
}
Astrobj::Python::ThinDisk* Gyoto::Astrobj::Python::ThinDisk::clone() const
{return new ThinDisk(*this);}
std::vector<double> Astrobj::Python::ThinDisk::parameters() const
{return Gyoto::Python::Base::parameters();}
void Astrobj::Python::ThinDisk::parameters(const std::vector<double>& m)
{Gyoto::Python::Base::parameters(m);}
std::string Astrobj::Python::ThinDisk::module() const
{return Gyoto::Python::Base::module();}
void Astrobj::Python::ThinDisk::module(const std::string& m)
{Gyoto::Python::Base::module(m);}
std::string Astrobj::Python::ThinDisk::inlineModule() const
{return Gyoto::Python::Base::inlineModule();}
void Astrobj::Python::ThinDisk::inlineModule(const std::string& m)
{Gyoto::Python::Base::inlineModule(m);}
std::string Astrobj::Python::ThinDisk::klass() const
{return Gyoto::Python::Base::klass();}
void Gyoto::Astrobj::Python::ThinDisk::klass(const std::string &f) {
PyGILState_STATE gstate = PyGILState_Ensure();
Py_XDECREF(pEmission_);
Py_XDECREF(pIntegrateEmission_);
Py_XDECREF(pTransmission_);
Py_XDECREF(pCall_);
Py_XDECREF(pGetVelocity_);
PyGILState_Release(gstate);
pEmission_overloaded_ = false;
pIntegrateEmission_overloaded_ = false;
Gyoto::Python::Base::klass(f);
if (!pModule_) return;
gstate = PyGILState_Ensure();
GYOTO_DEBUG << "Checking Python class methods" << f << endl;
pEmission_ =
Gyoto::Python::PyInstance_GetMethod(pInstance_, "emission");
pIntegrateEmission_ =
Gyoto::Python::PyInstance_GetMethod(pInstance_, "integrateEmission");
pTransmission_ =
Gyoto::Python::PyInstance_GetMethod(pInstance_, "transmission");
pCall_ =
Gyoto::Python::PyInstance_GetMethod(pInstance_, "__call__");
pGetVelocity_ =
Gyoto::Python::PyInstance_GetMethod(pInstance_, "getVelocity");
if (PyErr_Occurred()) {
PyErr_Print();
PyGILState_Release(gstate);
GYOTO_ERROR("Error while retrieving methods");
}
pEmission_overloaded_ = pEmission_ &&
Gyoto::Python::PyCallable_HasVarArg(pEmission_);
pIntegrateEmission_overloaded_ = pIntegrateEmission_ &&
Gyoto::Python::PyCallable_HasVarArg(pIntegrateEmission_);
Gyoto::Python::PyInstance_SetThis(pInstance_,
Gyoto::Python::pGyotoThinDisk(),
this);
PyGILState_Release(gstate);
if (parameters_.size()) parameters(parameters_);
GYOTO_DEBUG << "Done checking Python class methods" << f << endl;
}
double Gyoto::Astrobj::Python::ThinDisk::operator()(double const coord[4]) {
if (!pCall_) return Gyoto::Astrobj::ThinDisk::operator()(coord);
PyGILState_STATE gstate = PyGILState_Ensure();
npy_intp dims[] = {4};
PyObject * pCoord = PyArray_SimpleNewFromData(1, dims, NPY_DOUBLE,
const_cast<double*>(coord));
PyObject * pR = PyObject_CallFunctionObjArgs(pCall_, pCoord, NULL);
Py_XDECREF(pCoord);
if (PyErr_Occurred()) {
Py_XDECREF(pR);
PyErr_Print();
PyGILState_Release(gstate);
GYOTO_ERROR("Error occurred in ThinDisk::operator()()");
}
double res = PyFloat_AsDouble(pR);
Py_XDECREF(pR);
PyGILState_Release(gstate);
return res;
}
void Gyoto::Astrobj::Python::ThinDisk::getVelocity
(double const coord[4], double vel[4]) {
if (!pGetVelocity_) return Gyoto::Astrobj::ThinDisk::getVelocity(coord, vel);
PyGILState_STATE gstate = PyGILState_Ensure();
npy_intp dims[] = {4};
PyObject * pCoord = PyArray_SimpleNewFromData(1, dims, NPY_DOUBLE,
const_cast<double*>(coord));
PyObject * pVel = PyArray_SimpleNewFromData(1, dims, NPY_DOUBLE, vel);
PyObject * pR =
PyObject_CallFunctionObjArgs(pGetVelocity_, pCoord, pVel, NULL);
Py_XDECREF(pR);
Py_XDECREF(pCoord);
Py_XDECREF(pVel);
if (PyErr_Occurred()) {
PyErr_Print();
PyGILState_Release(gstate);
GYOTO_ERROR("Error occurred in ThinDisk::getVelocity()");
}
PyGILState_Release(gstate);
}
double Gyoto::Astrobj::Python::ThinDisk::emission
(double nu_em, double dsem, state_t const &coord_ph, double const coord_obj[8])
const {
if (!pEmission_)
return Astrobj::ThinDisk::emission(nu_em, dsem, coord_ph, coord_obj);
PyGILState_STATE gstate = PyGILState_Ensure();
npy_intp dims_co[] = {8};
npy_intp dims_cp[] = {npy_intp(coord_ph.size())};
PyObject * pNu = PyFloat_FromDouble(nu_em);
PyObject * pDs = PyFloat_FromDouble(dsem);
PyObject * pCp = PyArray_SimpleNewFromData(1, dims_cp, NPY_DOUBLE, const_cast<double*>(&coord_ph[0]));
PyObject * pCo = PyArray_SimpleNewFromData(1, dims_co, NPY_DOUBLE, const_cast<double*>(coord_obj));
PyObject * pR =
PyObject_CallFunctionObjArgs(pEmission_, pNu, pDs, pCp, pCo, NULL);
Py_XDECREF(pCo);
Py_XDECREF(pCp);
Py_XDECREF(pDs);
Py_XDECREF(pNu);
if (PyErr_Occurred()) {
Py_XDECREF(pR);
PyErr_Print();
PyGILState_Release(gstate);
GYOTO_ERROR("Error occurred in ThinDisk::emission()");
}
double res = PyFloat_AsDouble(pR);
Py_XDECREF(pR);
PyGILState_Release(gstate);
return res;
}
void Gyoto::Astrobj::Python::ThinDisk::emission
(double Inu[], double const nu_em[], size_t nbnu, double dsem, state_t const &coord_ph,
double const coord_obj[8]) const {
if (!pEmission_ || !pEmission_overloaded_) {
Astrobj::ThinDisk::emission(Inu, nu_em, nbnu, dsem,
coord_ph, coord_obj);
return;
}
PyGILState_STATE gstate = PyGILState_Ensure();
npy_intp I_dims[] = {static_cast<npy_intp>(nbnu)};
npy_intp dims_co[] = {8};
npy_intp dims_cp[] = {npy_intp(coord_ph.size())};
PyObject * pIn = PyArray_SimpleNewFromData(1, I_dims, NPY_DOUBLE, Inu);
PyObject * pNu = PyArray_SimpleNewFromData(1, I_dims, NPY_DOUBLE, const_cast<double*>(nu_em));
PyObject * pDs = PyFloat_FromDouble(dsem);
PyObject * pCp = PyArray_SimpleNewFromData(1, dims_cp, NPY_DOUBLE, const_cast<double*>(&coord_ph[0]));
PyObject * pCo = PyArray_SimpleNewFromData(1, dims_co, NPY_DOUBLE, const_cast<double*>(coord_obj));
PyObject * pR =
PyObject_CallFunctionObjArgs(pEmission_, pIn, pNu, pDs, pCp, pCo, NULL);
Py_XDECREF(pR);
Py_XDECREF(pCo);
Py_XDECREF(pCp);
Py_XDECREF(pDs);
Py_XDECREF(pNu);
Py_XDECREF(pIn);
if (PyErr_Occurred()) {
PyErr_Print();
PyGILState_Release(gstate);
GYOTO_ERROR("Error occurred in ThinDisk::emission()");
}
PyGILState_Release(gstate);
}
double Gyoto::Astrobj::Python::ThinDisk::integrateEmission
(double nu1, double nu2, double dsem, state_t const &c_ph, double const c_obj[8])
const {
if (!pIntegrateEmission_)
return Astrobj::ThinDisk::integrateEmission(nu1, nu2, dsem, c_ph,c_obj);
PyGILState_STATE gstate = PyGILState_Ensure();
npy_intp dims_co[] = {8};
npy_intp dims_cp[] = {npy_intp(c_ph.size())};
PyObject * pN1 = PyFloat_FromDouble(nu1);
PyObject * pN2 = PyFloat_FromDouble(nu2);
PyObject * pDs = PyFloat_FromDouble(dsem);
PyObject * pCp = PyArray_SimpleNewFromData(1, dims_cp, NPY_DOUBLE, const_cast<double*>(&c_ph[0]));
PyObject * pCo = PyArray_SimpleNewFromData(1, dims_co, NPY_DOUBLE, const_cast<double*>(c_obj));
PyObject * pR =
PyObject_CallFunctionObjArgs(pIntegrateEmission_,
pN1, pN2, pDs, pCp, pCo, NULL);
Py_XDECREF(pCo);
Py_XDECREF(pCp);
Py_XDECREF(pDs);
Py_XDECREF(pN2);
Py_XDECREF(pN1);
if (PyErr_Occurred()) {
Py_XDECREF(pR);
PyErr_Print();
PyGILState_Release(gstate);
GYOTO_ERROR("Error occurred in ThinDisk::integrateEmission()");
}
double res = PyFloat_AsDouble(pR);
Py_XDECREF(pR);
PyGILState_Release(gstate);
return res;
}
void Gyoto::Astrobj::Python::ThinDisk::integrateEmission
(double * I, double const * boundaries, size_t const * chaninds,
size_t nbnu, double dsem, state_t const &cph, double const *co) const{
if (!pIntegrateEmission_ || !pIntegrateEmission_overloaded_) {
Gyoto::Astrobj::ThinDisk::integrateEmission(I, boundaries, chaninds,
nbnu, dsem, cph, co);
return;
}
PyGILState_STATE gstate = PyGILState_Ensure();
size_t nbo=0;
for (size_t i=0; i<2*nbnu; ++i)
if (nbo < chaninds[i]) nbo=chaninds[i];
npy_intp nNu = nbnu, nBo = nbo, nCh = 2*nbnu, nCo = 8, nCp = npy_intp(cph.size());
PyObject * pI = PyArray_SimpleNewFromData(1, &nNu, NPY_DOUBLE, I);
PyObject * pBo = PyArray_SimpleNewFromData(1, &nBo, NPY_DOUBLE,
const_cast<double*>(boundaries));
PyObject * pCh = PyArray_SimpleNewFromData(1, &nCh, NPY_UINTP,
const_cast<size_t *>(chaninds));
PyObject * pDs = PyFloat_FromDouble(dsem);
PyObject * pCp = PyArray_SimpleNewFromData(1, &nCp, NPY_DOUBLE,
const_cast<double*>(&cph[0]));
PyObject * pCo = PyArray_SimpleNewFromData(1, &nCo, NPY_DOUBLE,
const_cast<double*>(co));
PyObject * pR =
PyObject_CallFunctionObjArgs(pIntegrateEmission_,
pI, pBo, pCh, pDs, pCp, pCo, NULL);
Py_XDECREF(pR);
Py_XDECREF(pCo);
Py_XDECREF(pCp);
Py_XDECREF(pDs);
Py_XDECREF(pCh);
Py_XDECREF(pBo);
Py_XDECREF(pI);
if (PyErr_Occurred()) {
PyErr_Print();
PyGILState_Release(gstate);
GYOTO_ERROR("Error occurred in ThinDisk::integrateEmission()");
}
PyGILState_Release(gstate);
}
double Gyoto::Astrobj::Python::ThinDisk::transmission
(double nuem, double dsem, state_t const &cph, double const * co) const {
if (!pTransmission_)
return Astrobj::ThinDisk::transmission(nuem, dsem, cph, co);
PyGILState_STATE gstate = PyGILState_Ensure();
npy_intp pdims[] = {npy_intp(cph.size())};
npy_intp odims[] = {8};
PyObject * pNu = PyFloat_FromDouble(nuem);
PyObject * pDs = PyFloat_FromDouble(dsem);
PyObject * pCp = PyArray_SimpleNewFromData(1, pdims, NPY_DOUBLE, const_cast<double*>(&cph[0]));
PyObject * pCo = PyArray_SimpleNewFromData(1, odims, NPY_DOUBLE, const_cast<double*>(co));
PyObject * pR =
PyObject_CallFunctionObjArgs(pTransmission_, pNu, pDs, pCp, pCo, NULL);
Py_XDECREF(pCo);
Py_XDECREF(pCp);
Py_XDECREF(pDs);
Py_XDECREF(pNu);
if (PyErr_Occurred()) {
Py_XDECREF(pR);
PyErr_Print();
PyGILState_Release(gstate);
GYOTO_ERROR("Error occurred in ThinDisk::transmission()");
}
double res = PyFloat_AsDouble(pR);
Py_XDECREF(pR);
PyGILState_Release(gstate);
return res;
}
|