[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 | */ |
---|
[9bd69098] | 24 | #define NO_IMPORT_ARRAY |
---|
| 25 | #define PY_ARRAY_UNIQUE_SYMBOL PyArray_API_sans |
---|
[0f5bc9f] | 26 | |
---|
| 27 | extern "C" { |
---|
| 28 | #include <Python.h> |
---|
[9bd69098] | 29 | #include <arrayobject.h> |
---|
[0f5bc9f] | 30 | #include "structmember.h" |
---|
| 31 | #include <stdio.h> |
---|
| 32 | #include <stdlib.h> |
---|
| 33 | #include <math.h> |
---|
| 34 | #include <time.h> |
---|
| 35 | #include "sphere.h" |
---|
| 36 | } |
---|
| 37 | |
---|
| 38 | #include "models.hh" |
---|
| 39 | #include "dispersion_visitor.hh" |
---|
| 40 | |
---|
| 41 | /// Error object for raised exceptions |
---|
| 42 | static PyObject * CSphereModelError = NULL; |
---|
| 43 | |
---|
| 44 | |
---|
| 45 | // Class definition |
---|
| 46 | typedef struct { |
---|
| 47 | PyObject_HEAD |
---|
| 48 | /// Parameters |
---|
| 49 | PyObject * params; |
---|
| 50 | /// Dispersion parameters |
---|
| 51 | PyObject * dispersion; |
---|
| 52 | /// Underlying model object |
---|
| 53 | SphereModel * model; |
---|
| 54 | /// Log for unit testing |
---|
| 55 | PyObject * log; |
---|
| 56 | } CSphereModel; |
---|
| 57 | |
---|
| 58 | |
---|
| 59 | static void |
---|
| 60 | CSphereModel_dealloc(CSphereModel* self) |
---|
| 61 | { |
---|
[71e2de7] | 62 | Py_DECREF(self->params); |
---|
| 63 | Py_DECREF(self->dispersion); |
---|
| 64 | Py_DECREF(self->log); |
---|
| 65 | delete self->model; |
---|
[0f5bc9f] | 66 | self->ob_type->tp_free((PyObject*)self); |
---|
| 67 | |
---|
| 68 | |
---|
| 69 | } |
---|
| 70 | |
---|
| 71 | static PyObject * |
---|
| 72 | CSphereModel_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
---|
| 73 | { |
---|
| 74 | CSphereModel *self; |
---|
| 75 | |
---|
| 76 | self = (CSphereModel *)type->tp_alloc(type, 0); |
---|
| 77 | |
---|
| 78 | return (PyObject *)self; |
---|
| 79 | } |
---|
| 80 | |
---|
| 81 | static int |
---|
| 82 | CSphereModel_init(CSphereModel *self, PyObject *args, PyObject *kwds) |
---|
| 83 | { |
---|
| 84 | if (self != NULL) { |
---|
| 85 | |
---|
| 86 | // Create parameters |
---|
| 87 | self->params = PyDict_New(); |
---|
| 88 | self->dispersion = PyDict_New(); |
---|
| 89 | self->model = new SphereModel(); |
---|
| 90 | |
---|
| 91 | // Initialize parameter dictionary |
---|
[35aface] | 92 | PyDict_SetItemString(self->params,"sldSolv",Py_BuildValue("d",0.000001000000)); |
---|
| 93 | PyDict_SetItemString(self->params,"sldSph",Py_BuildValue("d",0.000002000000)); |
---|
| 94 | PyDict_SetItemString(self->params,"scale",Py_BuildValue("d",1.000000000000)); |
---|
| 95 | PyDict_SetItemString(self->params,"radius",Py_BuildValue("d",60.000000000000)); |
---|
| 96 | PyDict_SetItemString(self->params,"background",Py_BuildValue("d",0.000000000000)); |
---|
[0f5bc9f] | 97 | // Initialize dispersion / averaging parameter dict |
---|
| 98 | DispersionVisitor* visitor = new DispersionVisitor(); |
---|
| 99 | PyObject * disp_dict; |
---|
| 100 | disp_dict = PyDict_New(); |
---|
| 101 | self->model->radius.dispersion->accept_as_source(visitor, self->model->radius.dispersion, disp_dict); |
---|
| 102 | PyDict_SetItemString(self->dispersion, "radius", disp_dict); |
---|
| 103 | |
---|
| 104 | |
---|
| 105 | |
---|
| 106 | // Create empty log |
---|
| 107 | self->log = PyDict_New(); |
---|
| 108 | |
---|
| 109 | |
---|
| 110 | |
---|
| 111 | } |
---|
| 112 | return 0; |
---|
| 113 | } |
---|
| 114 | |
---|
| 115 | static PyMemberDef CSphereModel_members[] = { |
---|
| 116 | {"params", T_OBJECT, offsetof(CSphereModel, params), 0, |
---|
| 117 | "Parameters"}, |
---|
| 118 | {"dispersion", T_OBJECT, offsetof(CSphereModel, dispersion), 0, |
---|
| 119 | "Dispersion parameters"}, |
---|
| 120 | {"log", T_OBJECT, offsetof(CSphereModel, log), 0, |
---|
| 121 | "Log"}, |
---|
| 122 | {NULL} /* Sentinel */ |
---|
| 123 | }; |
---|
| 124 | |
---|
| 125 | /** Read double from PyObject |
---|
| 126 | @param p PyObject |
---|
| 127 | @return double |
---|
| 128 | */ |
---|
| 129 | double CSphereModel_readDouble(PyObject *p) { |
---|
| 130 | if (PyFloat_Check(p)==1) { |
---|
| 131 | return (double)(((PyFloatObject *)(p))->ob_fval); |
---|
| 132 | } else if (PyInt_Check(p)==1) { |
---|
| 133 | return (double)(((PyIntObject *)(p))->ob_ival); |
---|
| 134 | } else if (PyLong_Check(p)==1) { |
---|
| 135 | return (double)PyLong_AsLong(p); |
---|
| 136 | } else { |
---|
| 137 | return 0.0; |
---|
| 138 | } |
---|
| 139 | } |
---|
[9bd69098] | 140 | /** |
---|
| 141 | * Function to call to evaluate model |
---|
| 142 | * @param args: input numpy array q[] |
---|
| 143 | * @return: numpy array object |
---|
| 144 | */ |
---|
| 145 | |
---|
| 146 | static PyObject *evaluateOneDim(SphereModel* model, PyArrayObject *q){ |
---|
| 147 | PyArrayObject *result; |
---|
| 148 | |
---|
| 149 | // Check validity of array q , q must be of dimension 1, an array of double |
---|
| 150 | if (q->nd != 1 || q->descr->type_num != PyArray_DOUBLE) |
---|
| 151 | { |
---|
| 152 | //const char * message= "Invalid array: q->nd=%d,type_num=%d\n",q->nd,q->descr->type_num; |
---|
| 153 | //PyErr_SetString(PyExc_ValueError , message); |
---|
| 154 | return NULL; |
---|
| 155 | } |
---|
| 156 | result = (PyArrayObject *)PyArray_FromDims(q->nd, (int *)(q->dimensions), |
---|
| 157 | PyArray_DOUBLE); |
---|
| 158 | if (result == NULL) { |
---|
| 159 | const char * message= "Could not create result "; |
---|
| 160 | PyErr_SetString(PyExc_RuntimeError , message); |
---|
| 161 | return NULL; |
---|
| 162 | } |
---|
| 163 | for (int i = 0; i < q->dimensions[0]; i++){ |
---|
| 164 | double q_value = *(double *)(q->data + i*q->strides[0]); |
---|
| 165 | double *result_value = (double *)(result->data + i*result->strides[0]); |
---|
| 166 | *result_value =(*model)(q_value); |
---|
| 167 | } |
---|
| 168 | return PyArray_Return(result); |
---|
| 169 | } |
---|
[0f5bc9f] | 170 | |
---|
[9bd69098] | 171 | /** |
---|
| 172 | * Function to call to evaluate model |
---|
| 173 | * @param args: input numpy array [x[],y[]] |
---|
| 174 | * @return: numpy array object |
---|
| 175 | */ |
---|
| 176 | static PyObject * evaluateTwoDimXY( SphereModel* model, |
---|
| 177 | PyArrayObject *x, PyArrayObject *y) |
---|
| 178 | { |
---|
| 179 | PyArrayObject *result; |
---|
[3080527] | 180 | int i,j, x_len, y_len, dims[1]; |
---|
[9bd69098] | 181 | //check validity of input vectors |
---|
[3080527] | 182 | if (x->nd != 1 || x->descr->type_num != PyArray_DOUBLE |
---|
| 183 | || y->nd != 1 || y->descr->type_num != PyArray_DOUBLE |
---|
| 184 | || y->dimensions[0] != x->dimensions[0]){ |
---|
[9bd69098] | 185 | const char * message= "evaluateTwoDimXY expect 2 numpy arrays"; |
---|
| 186 | PyErr_SetString(PyExc_ValueError , message); |
---|
| 187 | return NULL; |
---|
| 188 | } |
---|
| 189 | |
---|
| 190 | if (PyArray_Check(x) && PyArray_Check(y)) { |
---|
[a8d6888] | 191 | |
---|
[3080527] | 192 | x_len = dims[0]= x->dimensions[0]; |
---|
[9ce41c6] | 193 | y_len = dims[0]= y->dimensions[0]; |
---|
[9bd69098] | 194 | |
---|
| 195 | // Make a new double matrix of same dims |
---|
[3080527] | 196 | result=(PyArrayObject *) PyArray_FromDims(1,dims,NPY_DOUBLE); |
---|
[9bd69098] | 197 | if (result == NULL){ |
---|
| 198 | const char * message= "Could not create result "; |
---|
| 199 | PyErr_SetString(PyExc_RuntimeError , message); |
---|
| 200 | return NULL; |
---|
| 201 | } |
---|
| 202 | |
---|
| 203 | /* Do the calculation. */ |
---|
[3080527] | 204 | for ( i=0; i< x_len; i++) { |
---|
| 205 | double x_value = *(double *)(x->data + i*x->strides[0]); |
---|
| 206 | double y_value = *(double *)(y->data + i*y->strides[0]); |
---|
| 207 | double *result_value = (double *)(result->data + |
---|
| 208 | i*result->strides[0]); |
---|
| 209 | *result_value = (*model)(x_value, y_value); |
---|
| 210 | } |
---|
[9bd69098] | 211 | return PyArray_Return(result); |
---|
| 212 | |
---|
| 213 | }else{ |
---|
| 214 | PyErr_SetString(CSphereModelError, |
---|
| 215 | "CSphereModel.evaluateTwoDimXY couldn't run."); |
---|
| 216 | return NULL; |
---|
| 217 | } |
---|
| 218 | } |
---|
| 219 | /** |
---|
| 220 | * evalDistribution function evaluate a model function with input vector |
---|
| 221 | * @param args: input q as vector or [qx, qy] where qx, qy are vectors |
---|
| 222 | * |
---|
| 223 | */ |
---|
| 224 | static PyObject * evalDistribution(CSphereModel *self, PyObject *args){ |
---|
| 225 | PyObject *qx, *qy; |
---|
| 226 | PyArrayObject * pars; |
---|
| 227 | int npars ,mpars; |
---|
| 228 | |
---|
| 229 | // Get parameters |
---|
| 230 | |
---|
| 231 | // Reader parameter dictionary |
---|
[f10063e] | 232 | self->model->sldSolv = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sldSolv") ); |
---|
| 233 | self->model->sldSph = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sldSph") ); |
---|
[9bd69098] | 234 | self->model->scale = PyFloat_AsDouble( PyDict_GetItemString(self->params, "scale") ); |
---|
| 235 | self->model->radius = PyFloat_AsDouble( PyDict_GetItemString(self->params, "radius") ); |
---|
| 236 | self->model->background = PyFloat_AsDouble( PyDict_GetItemString(self->params, "background") ); |
---|
| 237 | // Read in dispersion parameters |
---|
| 238 | PyObject* disp_dict; |
---|
| 239 | DispersionVisitor* visitor = new DispersionVisitor(); |
---|
| 240 | disp_dict = PyDict_GetItemString(self->dispersion, "radius"); |
---|
| 241 | self->model->radius.dispersion->accept_as_destination(visitor, self->model->radius.dispersion, disp_dict); |
---|
| 242 | |
---|
| 243 | |
---|
| 244 | // Get input and determine whether we have to supply a 1D or 2D return value. |
---|
| 245 | if ( !PyArg_ParseTuple(args,"O",&pars) ) { |
---|
| 246 | PyErr_SetString(CSphereModelError, |
---|
| 247 | "CSphereModel.evalDistribution expects a q value."); |
---|
| 248 | return NULL; |
---|
| 249 | } |
---|
| 250 | // Check params |
---|
| 251 | |
---|
| 252 | if(PyArray_Check(pars)==1) { |
---|
| 253 | |
---|
| 254 | // Length of list should 1 or 2 |
---|
| 255 | npars = pars->nd; |
---|
| 256 | if(npars==1) { |
---|
| 257 | // input is a numpy array |
---|
| 258 | if (PyArray_Check(pars)) { |
---|
| 259 | return evaluateOneDim(self->model, (PyArrayObject*)pars); |
---|
| 260 | } |
---|
| 261 | }else{ |
---|
| 262 | PyErr_SetString(CSphereModelError, |
---|
| 263 | "CSphereModel.evalDistribution expect numpy array of one dimension."); |
---|
| 264 | return NULL; |
---|
| 265 | } |
---|
| 266 | }else if( PyList_Check(pars)==1) { |
---|
| 267 | // Length of list should be 2 for I(qx,qy) |
---|
| 268 | mpars = PyList_GET_SIZE(pars); |
---|
| 269 | if(mpars!=2) { |
---|
| 270 | PyErr_SetString(CSphereModelError, |
---|
| 271 | "CSphereModel.evalDistribution expects a list of dimension 2."); |
---|
| 272 | return NULL; |
---|
| 273 | } |
---|
| 274 | qx = PyList_GET_ITEM(pars,0); |
---|
| 275 | qy = PyList_GET_ITEM(pars,1); |
---|
| 276 | if (PyArray_Check(qx) && PyArray_Check(qy)) { |
---|
| 277 | return evaluateTwoDimXY(self->model, (PyArrayObject*)qx, |
---|
| 278 | (PyArrayObject*)qy); |
---|
| 279 | }else{ |
---|
| 280 | PyErr_SetString(CSphereModelError, |
---|
| 281 | "CSphereModel.evalDistribution expect 2 numpy arrays in list."); |
---|
| 282 | return NULL; |
---|
| 283 | } |
---|
| 284 | } |
---|
[e0a8a3c] | 285 | PyErr_SetString(CSphereModelError, |
---|
| 286 | "CSphereModel.evalDistribution couln't be run."); |
---|
| 287 | return NULL; |
---|
| 288 | |
---|
[9bd69098] | 289 | } |
---|
[0f5bc9f] | 290 | |
---|
| 291 | /** |
---|
| 292 | * Function to call to evaluate model |
---|
| 293 | * @param args: input q or [q,phi] |
---|
| 294 | * @return: function value |
---|
| 295 | */ |
---|
| 296 | static PyObject * run(CSphereModel *self, PyObject *args) { |
---|
| 297 | double q_value, phi_value; |
---|
| 298 | PyObject* pars; |
---|
| 299 | int npars; |
---|
| 300 | |
---|
| 301 | // Get parameters |
---|
| 302 | |
---|
| 303 | // Reader parameter dictionary |
---|
[f10063e] | 304 | self->model->sldSolv = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sldSolv") ); |
---|
| 305 | self->model->sldSph = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sldSph") ); |
---|
[0f5bc9f] | 306 | self->model->scale = PyFloat_AsDouble( PyDict_GetItemString(self->params, "scale") ); |
---|
| 307 | self->model->radius = PyFloat_AsDouble( PyDict_GetItemString(self->params, "radius") ); |
---|
| 308 | self->model->background = PyFloat_AsDouble( PyDict_GetItemString(self->params, "background") ); |
---|
| 309 | // Read in dispersion parameters |
---|
| 310 | PyObject* disp_dict; |
---|
| 311 | DispersionVisitor* visitor = new DispersionVisitor(); |
---|
| 312 | disp_dict = PyDict_GetItemString(self->dispersion, "radius"); |
---|
| 313 | self->model->radius.dispersion->accept_as_destination(visitor, self->model->radius.dispersion, disp_dict); |
---|
| 314 | |
---|
| 315 | |
---|
| 316 | // Get input and determine whether we have to supply a 1D or 2D return value. |
---|
| 317 | if ( !PyArg_ParseTuple(args,"O",&pars) ) { |
---|
| 318 | PyErr_SetString(CSphereModelError, |
---|
| 319 | "CSphereModel.run expects a q value."); |
---|
| 320 | return NULL; |
---|
| 321 | } |
---|
| 322 | |
---|
| 323 | // Check params |
---|
| 324 | if( PyList_Check(pars)==1) { |
---|
| 325 | |
---|
| 326 | // Length of list should be 2 for I(q,phi) |
---|
| 327 | npars = PyList_GET_SIZE(pars); |
---|
| 328 | if(npars!=2) { |
---|
| 329 | PyErr_SetString(CSphereModelError, |
---|
| 330 | "CSphereModel.run expects a double or a list of dimension 2."); |
---|
| 331 | return NULL; |
---|
| 332 | } |
---|
| 333 | // We have a vector q, get the q and phi values at which |
---|
| 334 | // to evaluate I(q,phi) |
---|
[4176435] | 335 | q_value = CSphereModel_readDouble(PyList_GET_ITEM(pars,0)); |
---|
| 336 | phi_value = CSphereModel_readDouble(PyList_GET_ITEM(pars,1)); |
---|
| 337 | // Skip zero |
---|
| 338 | if (q_value==0) { |
---|
| 339 | return Py_BuildValue("d",0.0); |
---|
| 340 | } |
---|
| 341 | return Py_BuildValue("d",(*(self->model)).evaluate_rphi(q_value,phi_value)); |
---|
| 342 | |
---|
[0f5bc9f] | 343 | } else { |
---|
[4176435] | 344 | |
---|
| 345 | // We have a scalar q, we will evaluate I(q) |
---|
[0f5bc9f] | 346 | q_value = CSphereModel_readDouble(pars); |
---|
[4176435] | 347 | |
---|
| 348 | return Py_BuildValue("d",(*(self->model))(q_value)); |
---|
| 349 | } |
---|
[0f5bc9f] | 350 | } |
---|
[5eb9154] | 351 | /** |
---|
| 352 | * Function to call to calculate_ER |
---|
| 353 | * @return: effective radius value |
---|
| 354 | */ |
---|
| 355 | static PyObject * calculate_ER(CSphereModel *self) { |
---|
| 356 | |
---|
| 357 | PyObject* pars; |
---|
| 358 | int npars; |
---|
| 359 | |
---|
| 360 | // Get parameters |
---|
| 361 | |
---|
| 362 | // Reader parameter dictionary |
---|
[f10063e] | 363 | self->model->sldSolv = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sldSolv") ); |
---|
| 364 | self->model->sldSph = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sldSph") ); |
---|
[5eb9154] | 365 | self->model->scale = PyFloat_AsDouble( PyDict_GetItemString(self->params, "scale") ); |
---|
| 366 | self->model->radius = PyFloat_AsDouble( PyDict_GetItemString(self->params, "radius") ); |
---|
| 367 | self->model->background = PyFloat_AsDouble( PyDict_GetItemString(self->params, "background") ); |
---|
| 368 | // Read in dispersion parameters |
---|
| 369 | PyObject* disp_dict; |
---|
| 370 | DispersionVisitor* visitor = new DispersionVisitor(); |
---|
| 371 | disp_dict = PyDict_GetItemString(self->dispersion, "radius"); |
---|
| 372 | self->model->radius.dispersion->accept_as_destination(visitor, self->model->radius.dispersion, disp_dict); |
---|
| 373 | |
---|
| 374 | |
---|
| 375 | return Py_BuildValue("d",(*(self->model)).calculate_ER()); |
---|
[0f5bc9f] | 376 | |
---|
[5eb9154] | 377 | } |
---|
[0f5bc9f] | 378 | /** |
---|
| 379 | * Function to call to evaluate model in cartesian coordinates |
---|
| 380 | * @param args: input q or [qx, qy]] |
---|
| 381 | * @return: function value |
---|
| 382 | */ |
---|
| 383 | static PyObject * runXY(CSphereModel *self, PyObject *args) { |
---|
| 384 | double qx_value, qy_value; |
---|
| 385 | PyObject* pars; |
---|
| 386 | int npars; |
---|
| 387 | |
---|
| 388 | // Get parameters |
---|
| 389 | |
---|
| 390 | // Reader parameter dictionary |
---|
[f10063e] | 391 | self->model->sldSolv = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sldSolv") ); |
---|
| 392 | self->model->sldSph = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sldSph") ); |
---|
[0f5bc9f] | 393 | self->model->scale = PyFloat_AsDouble( PyDict_GetItemString(self->params, "scale") ); |
---|
| 394 | self->model->radius = PyFloat_AsDouble( PyDict_GetItemString(self->params, "radius") ); |
---|
| 395 | self->model->background = PyFloat_AsDouble( PyDict_GetItemString(self->params, "background") ); |
---|
| 396 | // Read in dispersion parameters |
---|
| 397 | PyObject* disp_dict; |
---|
| 398 | DispersionVisitor* visitor = new DispersionVisitor(); |
---|
| 399 | disp_dict = PyDict_GetItemString(self->dispersion, "radius"); |
---|
| 400 | self->model->radius.dispersion->accept_as_destination(visitor, self->model->radius.dispersion, disp_dict); |
---|
| 401 | |
---|
| 402 | |
---|
| 403 | // Get input and determine whether we have to supply a 1D or 2D return value. |
---|
| 404 | if ( !PyArg_ParseTuple(args,"O",&pars) ) { |
---|
| 405 | PyErr_SetString(CSphereModelError, |
---|
| 406 | "CSphereModel.run expects a q value."); |
---|
| 407 | return NULL; |
---|
| 408 | } |
---|
| 409 | |
---|
| 410 | // Check params |
---|
| 411 | if( PyList_Check(pars)==1) { |
---|
| 412 | |
---|
| 413 | // Length of list should be 2 for I(qx, qy)) |
---|
| 414 | npars = PyList_GET_SIZE(pars); |
---|
| 415 | if(npars!=2) { |
---|
| 416 | PyErr_SetString(CSphereModelError, |
---|
| 417 | "CSphereModel.run expects a double or a list of dimension 2."); |
---|
| 418 | return NULL; |
---|
| 419 | } |
---|
| 420 | // We have a vector q, get the qx and qy values at which |
---|
| 421 | // to evaluate I(qx,qy) |
---|
[4176435] | 422 | qx_value = CSphereModel_readDouble(PyList_GET_ITEM(pars,0)); |
---|
| 423 | qy_value = CSphereModel_readDouble(PyList_GET_ITEM(pars,1)); |
---|
| 424 | return Py_BuildValue("d",(*(self->model))(qx_value,qy_value)); |
---|
[0f5bc9f] | 425 | |
---|
| 426 | } else { |
---|
[4176435] | 427 | |
---|
| 428 | // We have a scalar q, we will evaluate I(q) |
---|
| 429 | qx_value = CSphereModel_readDouble(pars); |
---|
[0f5bc9f] | 430 | |
---|
[4176435] | 431 | return Py_BuildValue("d",(*(self->model))(qx_value)); |
---|
[0f5bc9f] | 432 | } |
---|
| 433 | } |
---|
| 434 | |
---|
| 435 | static PyObject * reset(CSphereModel *self, PyObject *args) { |
---|
| 436 | |
---|
| 437 | |
---|
| 438 | return Py_BuildValue("d",0.0); |
---|
| 439 | } |
---|
| 440 | |
---|
| 441 | static PyObject * set_dispersion(CSphereModel *self, PyObject *args) { |
---|
| 442 | PyObject * disp; |
---|
| 443 | const char * par_name; |
---|
| 444 | |
---|
| 445 | if ( !PyArg_ParseTuple(args,"sO", &par_name, &disp) ) { |
---|
| 446 | PyErr_SetString(CSphereModelError, |
---|
| 447 | "CSphereModel.set_dispersion expects a DispersionModel object."); |
---|
| 448 | return NULL; |
---|
| 449 | } |
---|
| 450 | void *temp = PyCObject_AsVoidPtr(disp); |
---|
| 451 | DispersionModel * dispersion = static_cast<DispersionModel *>(temp); |
---|
| 452 | |
---|
| 453 | |
---|
| 454 | // Ugliness necessary to go from python to C |
---|
| 455 | // TODO: refactor this |
---|
| 456 | if (!strcmp(par_name, "radius")) { |
---|
| 457 | self->model->radius.dispersion = dispersion; |
---|
| 458 | } else { |
---|
| 459 | PyErr_SetString(CSphereModelError, |
---|
| 460 | "CSphereModel.set_dispersion expects a valid parameter name."); |
---|
| 461 | return NULL; |
---|
| 462 | } |
---|
| 463 | |
---|
| 464 | DispersionVisitor* visitor = new DispersionVisitor(); |
---|
| 465 | PyObject * disp_dict = PyDict_New(); |
---|
| 466 | dispersion->accept_as_source(visitor, dispersion, disp_dict); |
---|
| 467 | PyDict_SetItemString(self->dispersion, par_name, disp_dict); |
---|
| 468 | return Py_BuildValue("i",1); |
---|
| 469 | } |
---|
| 470 | |
---|
| 471 | |
---|
| 472 | static PyMethodDef CSphereModel_methods[] = { |
---|
| 473 | {"run", (PyCFunction)run , METH_VARARGS, |
---|
| 474 | "Evaluate the model at a given Q or Q, phi"}, |
---|
| 475 | {"runXY", (PyCFunction)runXY , METH_VARARGS, |
---|
| 476 | "Evaluate the model at a given Q or Qx, Qy"}, |
---|
[5eb9154] | 477 | {"calculate_ER", (PyCFunction)calculate_ER , METH_VARARGS, |
---|
| 478 | "Evaluate the model at a given Q or Q, phi"}, |
---|
[9bd69098] | 479 | |
---|
| 480 | {"evalDistribution", (PyCFunction)evalDistribution , METH_VARARGS, |
---|
| 481 | "Evaluate the model at a given Q or Qx, Qy vector "}, |
---|
[0f5bc9f] | 482 | {"reset", (PyCFunction)reset , METH_VARARGS, |
---|
| 483 | "Reset pair correlation"}, |
---|
| 484 | {"set_dispersion", (PyCFunction)set_dispersion , METH_VARARGS, |
---|
| 485 | "Set the dispersion model for a given parameter"}, |
---|
| 486 | {NULL} |
---|
| 487 | }; |
---|
| 488 | |
---|
| 489 | static PyTypeObject CSphereModelType = { |
---|
| 490 | PyObject_HEAD_INIT(NULL) |
---|
| 491 | 0, /*ob_size*/ |
---|
| 492 | "CSphereModel", /*tp_name*/ |
---|
| 493 | sizeof(CSphereModel), /*tp_basicsize*/ |
---|
| 494 | 0, /*tp_itemsize*/ |
---|
| 495 | (destructor)CSphereModel_dealloc, /*tp_dealloc*/ |
---|
| 496 | 0, /*tp_print*/ |
---|
| 497 | 0, /*tp_getattr*/ |
---|
| 498 | 0, /*tp_setattr*/ |
---|
| 499 | 0, /*tp_compare*/ |
---|
| 500 | 0, /*tp_repr*/ |
---|
| 501 | 0, /*tp_as_number*/ |
---|
| 502 | 0, /*tp_as_sequence*/ |
---|
| 503 | 0, /*tp_as_mapping*/ |
---|
| 504 | 0, /*tp_hash */ |
---|
| 505 | 0, /*tp_call*/ |
---|
| 506 | 0, /*tp_str*/ |
---|
| 507 | 0, /*tp_getattro*/ |
---|
| 508 | 0, /*tp_setattro*/ |
---|
| 509 | 0, /*tp_as_buffer*/ |
---|
| 510 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/ |
---|
| 511 | "CSphereModel objects", /* tp_doc */ |
---|
| 512 | 0, /* tp_traverse */ |
---|
| 513 | 0, /* tp_clear */ |
---|
| 514 | 0, /* tp_richcompare */ |
---|
| 515 | 0, /* tp_weaklistoffset */ |
---|
| 516 | 0, /* tp_iter */ |
---|
| 517 | 0, /* tp_iternext */ |
---|
| 518 | CSphereModel_methods, /* tp_methods */ |
---|
| 519 | CSphereModel_members, /* tp_members */ |
---|
| 520 | 0, /* tp_getset */ |
---|
| 521 | 0, /* tp_base */ |
---|
| 522 | 0, /* tp_dict */ |
---|
| 523 | 0, /* tp_descr_get */ |
---|
| 524 | 0, /* tp_descr_set */ |
---|
| 525 | 0, /* tp_dictoffset */ |
---|
| 526 | (initproc)CSphereModel_init, /* tp_init */ |
---|
| 527 | 0, /* tp_alloc */ |
---|
| 528 | CSphereModel_new, /* tp_new */ |
---|
| 529 | }; |
---|
| 530 | |
---|
| 531 | |
---|
[9bd69098] | 532 | //static PyMethodDef module_methods[] = { |
---|
| 533 | // {NULL} |
---|
| 534 | //}; |
---|
[4176435] | 535 | |
---|
[0f5bc9f] | 536 | /** |
---|
| 537 | * Function used to add the model class to a module |
---|
| 538 | * @param module: module to add the class to |
---|
| 539 | */ |
---|
| 540 | void addCSphereModel(PyObject *module) { |
---|
| 541 | PyObject *d; |
---|
| 542 | |
---|
| 543 | if (PyType_Ready(&CSphereModelType) < 0) |
---|
| 544 | return; |
---|
| 545 | |
---|
| 546 | Py_INCREF(&CSphereModelType); |
---|
| 547 | PyModule_AddObject(module, "CSphereModel", (PyObject *)&CSphereModelType); |
---|
| 548 | |
---|
| 549 | d = PyModule_GetDict(module); |
---|
| 550 | CSphereModelError = PyErr_NewException("CSphereModel.error", NULL, NULL); |
---|
| 551 | PyDict_SetItemString(d, "CSphereModelError", CSphereModelError); |
---|
| 552 | } |
---|
| 553 | |
---|