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