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