[0f5bc9f] | 1 | /** |
---|
| 2 | This software was developed by the University of Tennessee as part of the |
---|
| 3 | Distributed Data Analysis of Neutron Scattering Experiments (DANSE) |
---|
| 4 | project funded by the US National Science Foundation. |
---|
| 5 | |
---|
| 6 | If you use DANSE applications to do scientific research that leads to |
---|
| 7 | publication, we ask that you acknowledge the use of the software with the |
---|
| 8 | following sentence: |
---|
| 9 | |
---|
| 10 | "This work benefited from DANSE software developed under NSF award DMR-0520547." |
---|
| 11 | |
---|
| 12 | copyright 2008, University of Tennessee |
---|
| 13 | */ |
---|
| 14 | |
---|
| 15 | /** CSphereModel |
---|
| 16 | * |
---|
| 17 | * C extension |
---|
| 18 | * |
---|
| 19 | * WARNING: THIS FILE WAS GENERATED BY WRAPPERGENERATOR.PY |
---|
| 20 | * DO NOT MODIFY THIS FILE, MODIFY sphere.h |
---|
| 21 | * AND RE-RUN THE GENERATOR SCRIPT |
---|
| 22 | * |
---|
| 23 | */ |
---|
| 24 | |
---|
| 25 | extern "C" { |
---|
| 26 | #include <Python.h> |
---|
| 27 | #include "structmember.h" |
---|
| 28 | #include <stdio.h> |
---|
| 29 | #include <stdlib.h> |
---|
| 30 | #include <math.h> |
---|
| 31 | #include <time.h> |
---|
| 32 | #include "sphere.h" |
---|
| 33 | } |
---|
| 34 | |
---|
| 35 | #include "models.hh" |
---|
| 36 | #include "dispersion_visitor.hh" |
---|
| 37 | |
---|
| 38 | /// Error object for raised exceptions |
---|
| 39 | static PyObject * CSphereModelError = NULL; |
---|
| 40 | |
---|
| 41 | |
---|
| 42 | // Class definition |
---|
| 43 | typedef struct { |
---|
| 44 | PyObject_HEAD |
---|
| 45 | /// Parameters |
---|
| 46 | PyObject * params; |
---|
| 47 | /// Dispersion parameters |
---|
| 48 | PyObject * dispersion; |
---|
| 49 | /// Underlying model object |
---|
| 50 | SphereModel * model; |
---|
| 51 | /// Log for unit testing |
---|
| 52 | PyObject * log; |
---|
| 53 | } CSphereModel; |
---|
| 54 | |
---|
| 55 | |
---|
| 56 | static void |
---|
| 57 | CSphereModel_dealloc(CSphereModel* self) |
---|
| 58 | { |
---|
| 59 | self->ob_type->tp_free((PyObject*)self); |
---|
| 60 | |
---|
| 61 | |
---|
| 62 | } |
---|
| 63 | |
---|
| 64 | static PyObject * |
---|
| 65 | CSphereModel_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
---|
| 66 | { |
---|
| 67 | CSphereModel *self; |
---|
| 68 | |
---|
| 69 | self = (CSphereModel *)type->tp_alloc(type, 0); |
---|
| 70 | |
---|
| 71 | return (PyObject *)self; |
---|
| 72 | } |
---|
| 73 | |
---|
| 74 | static int |
---|
| 75 | CSphereModel_init(CSphereModel *self, PyObject *args, PyObject *kwds) |
---|
| 76 | { |
---|
| 77 | if (self != NULL) { |
---|
| 78 | |
---|
| 79 | // Create parameters |
---|
| 80 | self->params = PyDict_New(); |
---|
| 81 | self->dispersion = PyDict_New(); |
---|
| 82 | self->model = new SphereModel(); |
---|
| 83 | |
---|
| 84 | // Initialize parameter dictionary |
---|
[836fe6e] | 85 | PyDict_SetItemString(self->params,"scale",Py_BuildValue("d",1.000000)); |
---|
[0f5bc9f] | 86 | PyDict_SetItemString(self->params,"radius",Py_BuildValue("d",60.000000)); |
---|
| 87 | PyDict_SetItemString(self->params,"background",Py_BuildValue("d",0.000000)); |
---|
[836fe6e] | 88 | PyDict_SetItemString(self->params,"contrast",Py_BuildValue("d",0.000001)); |
---|
[0f5bc9f] | 89 | // Initialize dispersion / averaging parameter dict |
---|
| 90 | DispersionVisitor* visitor = new DispersionVisitor(); |
---|
| 91 | PyObject * disp_dict; |
---|
| 92 | disp_dict = PyDict_New(); |
---|
| 93 | self->model->radius.dispersion->accept_as_source(visitor, self->model->radius.dispersion, disp_dict); |
---|
| 94 | PyDict_SetItemString(self->dispersion, "radius", disp_dict); |
---|
| 95 | |
---|
| 96 | |
---|
| 97 | |
---|
| 98 | // Create empty log |
---|
| 99 | self->log = PyDict_New(); |
---|
| 100 | |
---|
| 101 | |
---|
| 102 | |
---|
| 103 | } |
---|
| 104 | return 0; |
---|
| 105 | } |
---|
| 106 | |
---|
| 107 | static PyMemberDef CSphereModel_members[] = { |
---|
| 108 | {"params", T_OBJECT, offsetof(CSphereModel, params), 0, |
---|
| 109 | "Parameters"}, |
---|
| 110 | {"dispersion", T_OBJECT, offsetof(CSphereModel, dispersion), 0, |
---|
| 111 | "Dispersion parameters"}, |
---|
| 112 | {"log", T_OBJECT, offsetof(CSphereModel, log), 0, |
---|
| 113 | "Log"}, |
---|
| 114 | {NULL} /* Sentinel */ |
---|
| 115 | }; |
---|
| 116 | |
---|
| 117 | /** Read double from PyObject |
---|
| 118 | @param p PyObject |
---|
| 119 | @return double |
---|
| 120 | */ |
---|
| 121 | double CSphereModel_readDouble(PyObject *p) { |
---|
| 122 | if (PyFloat_Check(p)==1) { |
---|
| 123 | return (double)(((PyFloatObject *)(p))->ob_fval); |
---|
| 124 | } else if (PyInt_Check(p)==1) { |
---|
| 125 | return (double)(((PyIntObject *)(p))->ob_ival); |
---|
| 126 | } else if (PyLong_Check(p)==1) { |
---|
| 127 | return (double)PyLong_AsLong(p); |
---|
| 128 | } else { |
---|
| 129 | return 0.0; |
---|
| 130 | } |
---|
| 131 | } |
---|
| 132 | |
---|
| 133 | |
---|
| 134 | /** |
---|
| 135 | * Function to call to evaluate model |
---|
| 136 | * @param args: input q or [q,phi] |
---|
| 137 | * @return: function value |
---|
| 138 | */ |
---|
| 139 | static PyObject * run(CSphereModel *self, PyObject *args) { |
---|
| 140 | double q_value, phi_value; |
---|
| 141 | PyObject* pars; |
---|
| 142 | int npars; |
---|
| 143 | |
---|
| 144 | // Get parameters |
---|
| 145 | |
---|
| 146 | // Reader parameter dictionary |
---|
| 147 | self->model->scale = PyFloat_AsDouble( PyDict_GetItemString(self->params, "scale") ); |
---|
| 148 | self->model->radius = PyFloat_AsDouble( PyDict_GetItemString(self->params, "radius") ); |
---|
| 149 | self->model->background = PyFloat_AsDouble( PyDict_GetItemString(self->params, "background") ); |
---|
| 150 | self->model->contrast = PyFloat_AsDouble( PyDict_GetItemString(self->params, "contrast") ); |
---|
| 151 | // Read in dispersion parameters |
---|
| 152 | PyObject* disp_dict; |
---|
| 153 | DispersionVisitor* visitor = new DispersionVisitor(); |
---|
| 154 | disp_dict = PyDict_GetItemString(self->dispersion, "radius"); |
---|
| 155 | self->model->radius.dispersion->accept_as_destination(visitor, self->model->radius.dispersion, disp_dict); |
---|
| 156 | |
---|
| 157 | |
---|
| 158 | // Get input and determine whether we have to supply a 1D or 2D return value. |
---|
| 159 | if ( !PyArg_ParseTuple(args,"O",&pars) ) { |
---|
| 160 | PyErr_SetString(CSphereModelError, |
---|
| 161 | "CSphereModel.run expects a q value."); |
---|
| 162 | return NULL; |
---|
| 163 | } |
---|
| 164 | |
---|
| 165 | // Check params |
---|
| 166 | if( PyList_Check(pars)==1) { |
---|
| 167 | |
---|
| 168 | // Length of list should be 2 for I(q,phi) |
---|
| 169 | npars = PyList_GET_SIZE(pars); |
---|
| 170 | if(npars!=2) { |
---|
| 171 | PyErr_SetString(CSphereModelError, |
---|
| 172 | "CSphereModel.run expects a double or a list of dimension 2."); |
---|
| 173 | return NULL; |
---|
| 174 | } |
---|
| 175 | // We have a vector q, get the q and phi values at which |
---|
| 176 | // to evaluate I(q,phi) |
---|
[4176435] | 177 | q_value = CSphereModel_readDouble(PyList_GET_ITEM(pars,0)); |
---|
| 178 | phi_value = CSphereModel_readDouble(PyList_GET_ITEM(pars,1)); |
---|
| 179 | // Skip zero |
---|
| 180 | if (q_value==0) { |
---|
| 181 | return Py_BuildValue("d",0.0); |
---|
| 182 | } |
---|
| 183 | return Py_BuildValue("d",(*(self->model)).evaluate_rphi(q_value,phi_value)); |
---|
| 184 | |
---|
[0f5bc9f] | 185 | } else { |
---|
[4176435] | 186 | |
---|
| 187 | // We have a scalar q, we will evaluate I(q) |
---|
[0f5bc9f] | 188 | q_value = CSphereModel_readDouble(pars); |
---|
[4176435] | 189 | |
---|
| 190 | return Py_BuildValue("d",(*(self->model))(q_value)); |
---|
| 191 | } |
---|
[0f5bc9f] | 192 | } |
---|
| 193 | |
---|
| 194 | /** |
---|
| 195 | * Function to call to evaluate model in cartesian coordinates |
---|
| 196 | * @param args: input q or [qx, qy]] |
---|
| 197 | * @return: function value |
---|
| 198 | */ |
---|
| 199 | static PyObject * runXY(CSphereModel *self, PyObject *args) { |
---|
| 200 | double qx_value, qy_value; |
---|
| 201 | PyObject* pars; |
---|
| 202 | int npars; |
---|
| 203 | |
---|
| 204 | // Get parameters |
---|
| 205 | |
---|
| 206 | // Reader parameter dictionary |
---|
| 207 | self->model->scale = PyFloat_AsDouble( PyDict_GetItemString(self->params, "scale") ); |
---|
| 208 | self->model->radius = PyFloat_AsDouble( PyDict_GetItemString(self->params, "radius") ); |
---|
| 209 | self->model->background = PyFloat_AsDouble( PyDict_GetItemString(self->params, "background") ); |
---|
| 210 | self->model->contrast = PyFloat_AsDouble( PyDict_GetItemString(self->params, "contrast") ); |
---|
| 211 | // Read in dispersion parameters |
---|
| 212 | PyObject* disp_dict; |
---|
| 213 | DispersionVisitor* visitor = new DispersionVisitor(); |
---|
| 214 | disp_dict = PyDict_GetItemString(self->dispersion, "radius"); |
---|
| 215 | self->model->radius.dispersion->accept_as_destination(visitor, self->model->radius.dispersion, disp_dict); |
---|
| 216 | |
---|
| 217 | |
---|
| 218 | // Get input and determine whether we have to supply a 1D or 2D return value. |
---|
| 219 | if ( !PyArg_ParseTuple(args,"O",&pars) ) { |
---|
| 220 | PyErr_SetString(CSphereModelError, |
---|
| 221 | "CSphereModel.run expects a q value."); |
---|
| 222 | return NULL; |
---|
| 223 | } |
---|
| 224 | |
---|
| 225 | // Check params |
---|
| 226 | if( PyList_Check(pars)==1) { |
---|
| 227 | |
---|
| 228 | // Length of list should be 2 for I(qx, qy)) |
---|
| 229 | npars = PyList_GET_SIZE(pars); |
---|
| 230 | if(npars!=2) { |
---|
| 231 | PyErr_SetString(CSphereModelError, |
---|
| 232 | "CSphereModel.run expects a double or a list of dimension 2."); |
---|
| 233 | return NULL; |
---|
| 234 | } |
---|
| 235 | // We have a vector q, get the qx and qy values at which |
---|
| 236 | // to evaluate I(qx,qy) |
---|
[4176435] | 237 | qx_value = CSphereModel_readDouble(PyList_GET_ITEM(pars,0)); |
---|
| 238 | qy_value = CSphereModel_readDouble(PyList_GET_ITEM(pars,1)); |
---|
| 239 | return Py_BuildValue("d",(*(self->model))(qx_value,qy_value)); |
---|
[0f5bc9f] | 240 | |
---|
| 241 | } else { |
---|
[4176435] | 242 | |
---|
| 243 | // We have a scalar q, we will evaluate I(q) |
---|
| 244 | qx_value = CSphereModel_readDouble(pars); |
---|
[0f5bc9f] | 245 | |
---|
[4176435] | 246 | return Py_BuildValue("d",(*(self->model))(qx_value)); |
---|
[0f5bc9f] | 247 | } |
---|
| 248 | } |
---|
| 249 | |
---|
| 250 | static PyObject * reset(CSphereModel *self, PyObject *args) { |
---|
| 251 | |
---|
| 252 | |
---|
| 253 | return Py_BuildValue("d",0.0); |
---|
| 254 | } |
---|
| 255 | |
---|
| 256 | static PyObject * set_dispersion(CSphereModel *self, PyObject *args) { |
---|
| 257 | PyObject * disp; |
---|
| 258 | const char * par_name; |
---|
| 259 | |
---|
| 260 | if ( !PyArg_ParseTuple(args,"sO", &par_name, &disp) ) { |
---|
| 261 | PyErr_SetString(CSphereModelError, |
---|
| 262 | "CSphereModel.set_dispersion expects a DispersionModel object."); |
---|
| 263 | return NULL; |
---|
| 264 | } |
---|
| 265 | void *temp = PyCObject_AsVoidPtr(disp); |
---|
| 266 | DispersionModel * dispersion = static_cast<DispersionModel *>(temp); |
---|
| 267 | |
---|
| 268 | |
---|
| 269 | // Ugliness necessary to go from python to C |
---|
| 270 | // TODO: refactor this |
---|
| 271 | if (!strcmp(par_name, "radius")) { |
---|
| 272 | self->model->radius.dispersion = dispersion; |
---|
| 273 | } else { |
---|
| 274 | PyErr_SetString(CSphereModelError, |
---|
| 275 | "CSphereModel.set_dispersion expects a valid parameter name."); |
---|
| 276 | return NULL; |
---|
| 277 | } |
---|
| 278 | |
---|
| 279 | DispersionVisitor* visitor = new DispersionVisitor(); |
---|
| 280 | PyObject * disp_dict = PyDict_New(); |
---|
| 281 | dispersion->accept_as_source(visitor, dispersion, disp_dict); |
---|
| 282 | PyDict_SetItemString(self->dispersion, par_name, disp_dict); |
---|
| 283 | return Py_BuildValue("i",1); |
---|
| 284 | } |
---|
| 285 | |
---|
| 286 | |
---|
| 287 | static PyMethodDef CSphereModel_methods[] = { |
---|
| 288 | {"run", (PyCFunction)run , METH_VARARGS, |
---|
| 289 | "Evaluate the model at a given Q or Q, phi"}, |
---|
| 290 | {"runXY", (PyCFunction)runXY , METH_VARARGS, |
---|
| 291 | "Evaluate the model at a given Q or Qx, Qy"}, |
---|
| 292 | {"reset", (PyCFunction)reset , METH_VARARGS, |
---|
| 293 | "Reset pair correlation"}, |
---|
| 294 | {"set_dispersion", (PyCFunction)set_dispersion , METH_VARARGS, |
---|
| 295 | "Set the dispersion model for a given parameter"}, |
---|
| 296 | {NULL} |
---|
| 297 | }; |
---|
| 298 | |
---|
| 299 | static PyTypeObject CSphereModelType = { |
---|
| 300 | PyObject_HEAD_INIT(NULL) |
---|
| 301 | 0, /*ob_size*/ |
---|
| 302 | "CSphereModel", /*tp_name*/ |
---|
| 303 | sizeof(CSphereModel), /*tp_basicsize*/ |
---|
| 304 | 0, /*tp_itemsize*/ |
---|
| 305 | (destructor)CSphereModel_dealloc, /*tp_dealloc*/ |
---|
| 306 | 0, /*tp_print*/ |
---|
| 307 | 0, /*tp_getattr*/ |
---|
| 308 | 0, /*tp_setattr*/ |
---|
| 309 | 0, /*tp_compare*/ |
---|
| 310 | 0, /*tp_repr*/ |
---|
| 311 | 0, /*tp_as_number*/ |
---|
| 312 | 0, /*tp_as_sequence*/ |
---|
| 313 | 0, /*tp_as_mapping*/ |
---|
| 314 | 0, /*tp_hash */ |
---|
| 315 | 0, /*tp_call*/ |
---|
| 316 | 0, /*tp_str*/ |
---|
| 317 | 0, /*tp_getattro*/ |
---|
| 318 | 0, /*tp_setattro*/ |
---|
| 319 | 0, /*tp_as_buffer*/ |
---|
| 320 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/ |
---|
| 321 | "CSphereModel objects", /* tp_doc */ |
---|
| 322 | 0, /* tp_traverse */ |
---|
| 323 | 0, /* tp_clear */ |
---|
| 324 | 0, /* tp_richcompare */ |
---|
| 325 | 0, /* tp_weaklistoffset */ |
---|
| 326 | 0, /* tp_iter */ |
---|
| 327 | 0, /* tp_iternext */ |
---|
| 328 | CSphereModel_methods, /* tp_methods */ |
---|
| 329 | CSphereModel_members, /* tp_members */ |
---|
| 330 | 0, /* tp_getset */ |
---|
| 331 | 0, /* tp_base */ |
---|
| 332 | 0, /* tp_dict */ |
---|
| 333 | 0, /* tp_descr_get */ |
---|
| 334 | 0, /* tp_descr_set */ |
---|
| 335 | 0, /* tp_dictoffset */ |
---|
| 336 | (initproc)CSphereModel_init, /* tp_init */ |
---|
| 337 | 0, /* tp_alloc */ |
---|
| 338 | CSphereModel_new, /* tp_new */ |
---|
| 339 | }; |
---|
| 340 | |
---|
| 341 | |
---|
[4176435] | 342 | static PyMethodDef module_methods[] = { |
---|
| 343 | {NULL} |
---|
| 344 | }; |
---|
| 345 | |
---|
[0f5bc9f] | 346 | /** |
---|
| 347 | * Function used to add the model class to a module |
---|
| 348 | * @param module: module to add the class to |
---|
| 349 | */ |
---|
| 350 | void addCSphereModel(PyObject *module) { |
---|
| 351 | PyObject *d; |
---|
| 352 | |
---|
| 353 | if (PyType_Ready(&CSphereModelType) < 0) |
---|
| 354 | return; |
---|
| 355 | |
---|
| 356 | Py_INCREF(&CSphereModelType); |
---|
| 357 | PyModule_AddObject(module, "CSphereModel", (PyObject *)&CSphereModelType); |
---|
| 358 | |
---|
| 359 | d = PyModule_GetDict(module); |
---|
| 360 | CSphereModelError = PyErr_NewException("CSphereModel.error", NULL, NULL); |
---|
| 361 | PyDict_SetItemString(d, "CSphereModelError", CSphereModelError); |
---|
| 362 | } |
---|
| 363 | |
---|