[c724ccd] | 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 | /** CFuzzySphereModel |
---|
| 16 | * |
---|
| 17 | * C extension |
---|
| 18 | * |
---|
| 19 | * WARNING: THIS FILE WAS GENERATED BY WRAPPERGENERATOR.PY |
---|
| 20 | * DO NOT MODIFY THIS FILE, MODIFY fuzzysphere.h |
---|
| 21 | * AND RE-RUN THE GENERATOR SCRIPT |
---|
| 22 | * |
---|
| 23 | */ |
---|
| 24 | #define NO_IMPORT_ARRAY |
---|
| 25 | #define PY_ARRAY_UNIQUE_SYMBOL PyArray_API_sans |
---|
| 26 | |
---|
| 27 | extern "C" { |
---|
| 28 | #include <Python.h> |
---|
| 29 | #include <arrayobject.h> |
---|
| 30 | #include "structmember.h" |
---|
| 31 | #include <stdio.h> |
---|
| 32 | #include <stdlib.h> |
---|
| 33 | #include <math.h> |
---|
| 34 | #include <time.h> |
---|
| 35 | #include "fuzzysphere.h" |
---|
| 36 | } |
---|
| 37 | |
---|
| 38 | #include "models.hh" |
---|
| 39 | #include "dispersion_visitor.hh" |
---|
| 40 | |
---|
| 41 | /// Error object for raised exceptions |
---|
| 42 | static PyObject * CFuzzySphereModelError = 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 | FuzzySphereModel * model; |
---|
| 54 | /// Log for unit testing |
---|
| 55 | PyObject * log; |
---|
| 56 | } CFuzzySphereModel; |
---|
| 57 | |
---|
| 58 | |
---|
| 59 | static void |
---|
| 60 | CFuzzySphereModel_dealloc(CFuzzySphereModel* self) |
---|
| 61 | { |
---|
| 62 | Py_DECREF(self->params); |
---|
| 63 | Py_DECREF(self->dispersion); |
---|
| 64 | Py_DECREF(self->log); |
---|
| 65 | delete self->model; |
---|
| 66 | self->ob_type->tp_free((PyObject*)self); |
---|
| 67 | |
---|
| 68 | |
---|
| 69 | } |
---|
| 70 | |
---|
| 71 | static PyObject * |
---|
| 72 | CFuzzySphereModel_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
---|
| 73 | { |
---|
| 74 | CFuzzySphereModel *self; |
---|
| 75 | |
---|
| 76 | self = (CFuzzySphereModel *)type->tp_alloc(type, 0); |
---|
| 77 | |
---|
| 78 | return (PyObject *)self; |
---|
| 79 | } |
---|
| 80 | |
---|
| 81 | static int |
---|
| 82 | CFuzzySphereModel_init(CFuzzySphereModel *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 FuzzySphereModel(); |
---|
| 90 | |
---|
| 91 | // Initialize parameter dictionary |
---|
[35aface] | 92 | PyDict_SetItemString(self->params,"scale",Py_BuildValue("d",0.010000000000)); |
---|
| 93 | PyDict_SetItemString(self->params,"sldSph",Py_BuildValue("d",0.000001000000)); |
---|
| 94 | PyDict_SetItemString(self->params,"radius",Py_BuildValue("d",60.000000000000)); |
---|
| 95 | PyDict_SetItemString(self->params,"background",Py_BuildValue("d",0.001000000000)); |
---|
| 96 | PyDict_SetItemString(self->params,"sldSolv",Py_BuildValue("d",0.000003000000)); |
---|
| 97 | PyDict_SetItemString(self->params,"fuzziness",Py_BuildValue("d",10.000000000000)); |
---|
[c724ccd] | 98 | // Initialize dispersion / averaging parameter dict |
---|
| 99 | DispersionVisitor* visitor = new DispersionVisitor(); |
---|
| 100 | PyObject * disp_dict; |
---|
| 101 | disp_dict = PyDict_New(); |
---|
| 102 | self->model->radius.dispersion->accept_as_source(visitor, self->model->radius.dispersion, disp_dict); |
---|
| 103 | PyDict_SetItemString(self->dispersion, "radius", disp_dict); |
---|
| 104 | disp_dict = PyDict_New(); |
---|
| 105 | self->model->fuzziness.dispersion->accept_as_source(visitor, self->model->fuzziness.dispersion, disp_dict); |
---|
| 106 | PyDict_SetItemString(self->dispersion, "fuzziness", disp_dict); |
---|
| 107 | |
---|
| 108 | |
---|
| 109 | |
---|
| 110 | // Create empty log |
---|
| 111 | self->log = PyDict_New(); |
---|
| 112 | |
---|
| 113 | |
---|
| 114 | |
---|
| 115 | } |
---|
| 116 | return 0; |
---|
| 117 | } |
---|
| 118 | |
---|
| 119 | static PyMemberDef CFuzzySphereModel_members[] = { |
---|
| 120 | {"params", T_OBJECT, offsetof(CFuzzySphereModel, params), 0, |
---|
| 121 | "Parameters"}, |
---|
| 122 | {"dispersion", T_OBJECT, offsetof(CFuzzySphereModel, dispersion), 0, |
---|
| 123 | "Dispersion parameters"}, |
---|
| 124 | {"log", T_OBJECT, offsetof(CFuzzySphereModel, log), 0, |
---|
| 125 | "Log"}, |
---|
| 126 | {NULL} /* Sentinel */ |
---|
| 127 | }; |
---|
| 128 | |
---|
| 129 | /** Read double from PyObject |
---|
| 130 | @param p PyObject |
---|
| 131 | @return double |
---|
| 132 | */ |
---|
| 133 | double CFuzzySphereModel_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 | } |
---|
| 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(FuzzySphereModel* 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 | } |
---|
| 174 | |
---|
| 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( FuzzySphereModel* model, |
---|
| 181 | PyArrayObject *x, PyArrayObject *y) |
---|
| 182 | { |
---|
| 183 | PyArrayObject *result; |
---|
| 184 | int i,j, x_len, y_len, dims[1]; |
---|
| 185 | //check validity of input vectors |
---|
| 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]){ |
---|
| 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)) { |
---|
| 195 | |
---|
| 196 | x_len = dims[0]= x->dimensions[0]; |
---|
| 197 | y_len = dims[0]= y->dimensions[0]; |
---|
| 198 | |
---|
| 199 | // Make a new double matrix of same dims |
---|
| 200 | result=(PyArrayObject *) PyArray_FromDims(1,dims,NPY_DOUBLE); |
---|
| 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. */ |
---|
| 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 | } |
---|
| 215 | return PyArray_Return(result); |
---|
| 216 | |
---|
| 217 | }else{ |
---|
| 218 | PyErr_SetString(CFuzzySphereModelError, |
---|
| 219 | "CFuzzySphereModel.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(CFuzzySphereModel *self, PyObject *args){ |
---|
| 229 | PyObject *qx, *qy; |
---|
| 230 | PyArrayObject * pars; |
---|
| 231 | int npars ,mpars; |
---|
| 232 | |
---|
| 233 | // Get parameters |
---|
| 234 | |
---|
| 235 | // Reader parameter dictionary |
---|
| 236 | self->model->scale = PyFloat_AsDouble( PyDict_GetItemString(self->params, "scale") ); |
---|
| 237 | self->model->sldSph = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sldSph") ); |
---|
| 238 | self->model->radius = PyFloat_AsDouble( PyDict_GetItemString(self->params, "radius") ); |
---|
| 239 | self->model->background = PyFloat_AsDouble( PyDict_GetItemString(self->params, "background") ); |
---|
| 240 | self->model->sldSolv = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sldSolv") ); |
---|
| 241 | self->model->fuzziness = PyFloat_AsDouble( PyDict_GetItemString(self->params, "fuzziness") ); |
---|
| 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); |
---|
| 247 | disp_dict = PyDict_GetItemString(self->dispersion, "fuzziness"); |
---|
| 248 | self->model->fuzziness.dispersion->accept_as_destination(visitor, self->model->fuzziness.dispersion, disp_dict); |
---|
| 249 | |
---|
| 250 | |
---|
| 251 | // Get input and determine whether we have to supply a 1D or 2D return value. |
---|
| 252 | if ( !PyArg_ParseTuple(args,"O",&pars) ) { |
---|
| 253 | PyErr_SetString(CFuzzySphereModelError, |
---|
| 254 | "CFuzzySphereModel.evalDistribution expects a q value."); |
---|
| 255 | return NULL; |
---|
| 256 | } |
---|
| 257 | // Check params |
---|
| 258 | |
---|
| 259 | if(PyArray_Check(pars)==1) { |
---|
| 260 | |
---|
| 261 | // Length of list should 1 or 2 |
---|
| 262 | npars = pars->nd; |
---|
| 263 | if(npars==1) { |
---|
| 264 | // input is a numpy array |
---|
| 265 | if (PyArray_Check(pars)) { |
---|
| 266 | return evaluateOneDim(self->model, (PyArrayObject*)pars); |
---|
| 267 | } |
---|
| 268 | }else{ |
---|
| 269 | PyErr_SetString(CFuzzySphereModelError, |
---|
| 270 | "CFuzzySphereModel.evalDistribution expect numpy array of one dimension."); |
---|
| 271 | return NULL; |
---|
| 272 | } |
---|
| 273 | }else if( PyList_Check(pars)==1) { |
---|
| 274 | // Length of list should be 2 for I(qx,qy) |
---|
| 275 | mpars = PyList_GET_SIZE(pars); |
---|
| 276 | if(mpars!=2) { |
---|
| 277 | PyErr_SetString(CFuzzySphereModelError, |
---|
| 278 | "CFuzzySphereModel.evalDistribution expects a list of dimension 2."); |
---|
| 279 | return NULL; |
---|
| 280 | } |
---|
| 281 | qx = PyList_GET_ITEM(pars,0); |
---|
| 282 | qy = PyList_GET_ITEM(pars,1); |
---|
| 283 | if (PyArray_Check(qx) && PyArray_Check(qy)) { |
---|
| 284 | return evaluateTwoDimXY(self->model, (PyArrayObject*)qx, |
---|
| 285 | (PyArrayObject*)qy); |
---|
| 286 | }else{ |
---|
| 287 | PyErr_SetString(CFuzzySphereModelError, |
---|
| 288 | "CFuzzySphereModel.evalDistribution expect 2 numpy arrays in list."); |
---|
| 289 | return NULL; |
---|
| 290 | } |
---|
| 291 | } |
---|
| 292 | PyErr_SetString(CFuzzySphereModelError, |
---|
| 293 | "CFuzzySphereModel.evalDistribution couln't be run."); |
---|
| 294 | return NULL; |
---|
| 295 | |
---|
| 296 | } |
---|
| 297 | |
---|
| 298 | /** |
---|
| 299 | * Function to call to evaluate model |
---|
| 300 | * @param args: input q or [q,phi] |
---|
| 301 | * @return: function value |
---|
| 302 | */ |
---|
| 303 | static PyObject * run(CFuzzySphereModel *self, PyObject *args) { |
---|
| 304 | double q_value, phi_value; |
---|
| 305 | PyObject* pars; |
---|
| 306 | int npars; |
---|
| 307 | |
---|
| 308 | // Get parameters |
---|
| 309 | |
---|
| 310 | // Reader parameter dictionary |
---|
| 311 | self->model->scale = PyFloat_AsDouble( PyDict_GetItemString(self->params, "scale") ); |
---|
| 312 | self->model->sldSph = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sldSph") ); |
---|
| 313 | self->model->radius = PyFloat_AsDouble( PyDict_GetItemString(self->params, "radius") ); |
---|
| 314 | self->model->background = PyFloat_AsDouble( PyDict_GetItemString(self->params, "background") ); |
---|
| 315 | self->model->sldSolv = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sldSolv") ); |
---|
| 316 | self->model->fuzziness = PyFloat_AsDouble( PyDict_GetItemString(self->params, "fuzziness") ); |
---|
| 317 | // Read in dispersion parameters |
---|
| 318 | PyObject* disp_dict; |
---|
| 319 | DispersionVisitor* visitor = new DispersionVisitor(); |
---|
| 320 | disp_dict = PyDict_GetItemString(self->dispersion, "radius"); |
---|
| 321 | self->model->radius.dispersion->accept_as_destination(visitor, self->model->radius.dispersion, disp_dict); |
---|
| 322 | disp_dict = PyDict_GetItemString(self->dispersion, "fuzziness"); |
---|
| 323 | self->model->fuzziness.dispersion->accept_as_destination(visitor, self->model->fuzziness.dispersion, disp_dict); |
---|
| 324 | |
---|
| 325 | |
---|
| 326 | // Get input and determine whether we have to supply a 1D or 2D return value. |
---|
| 327 | if ( !PyArg_ParseTuple(args,"O",&pars) ) { |
---|
| 328 | PyErr_SetString(CFuzzySphereModelError, |
---|
| 329 | "CFuzzySphereModel.run expects a q value."); |
---|
| 330 | return NULL; |
---|
| 331 | } |
---|
| 332 | |
---|
| 333 | // Check params |
---|
| 334 | if( PyList_Check(pars)==1) { |
---|
| 335 | |
---|
| 336 | // Length of list should be 2 for I(q,phi) |
---|
| 337 | npars = PyList_GET_SIZE(pars); |
---|
| 338 | if(npars!=2) { |
---|
| 339 | PyErr_SetString(CFuzzySphereModelError, |
---|
| 340 | "CFuzzySphereModel.run expects a double or a list of dimension 2."); |
---|
| 341 | return NULL; |
---|
| 342 | } |
---|
| 343 | // We have a vector q, get the q and phi values at which |
---|
| 344 | // to evaluate I(q,phi) |
---|
| 345 | q_value = CFuzzySphereModel_readDouble(PyList_GET_ITEM(pars,0)); |
---|
| 346 | phi_value = CFuzzySphereModel_readDouble(PyList_GET_ITEM(pars,1)); |
---|
| 347 | // Skip zero |
---|
| 348 | if (q_value==0) { |
---|
| 349 | return Py_BuildValue("d",0.0); |
---|
| 350 | } |
---|
| 351 | return Py_BuildValue("d",(*(self->model)).evaluate_rphi(q_value,phi_value)); |
---|
| 352 | |
---|
| 353 | } else { |
---|
| 354 | |
---|
| 355 | // We have a scalar q, we will evaluate I(q) |
---|
| 356 | q_value = CFuzzySphereModel_readDouble(pars); |
---|
| 357 | |
---|
| 358 | return Py_BuildValue("d",(*(self->model))(q_value)); |
---|
| 359 | } |
---|
| 360 | } |
---|
| 361 | /** |
---|
| 362 | * Function to call to calculate_ER |
---|
| 363 | * @return: effective radius value |
---|
| 364 | */ |
---|
| 365 | static PyObject * calculate_ER(CFuzzySphereModel *self) { |
---|
| 366 | |
---|
| 367 | PyObject* pars; |
---|
| 368 | int npars; |
---|
| 369 | |
---|
| 370 | // Get parameters |
---|
| 371 | |
---|
| 372 | // Reader parameter dictionary |
---|
| 373 | self->model->scale = PyFloat_AsDouble( PyDict_GetItemString(self->params, "scale") ); |
---|
| 374 | self->model->sldSph = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sldSph") ); |
---|
| 375 | self->model->radius = PyFloat_AsDouble( PyDict_GetItemString(self->params, "radius") ); |
---|
| 376 | self->model->background = PyFloat_AsDouble( PyDict_GetItemString(self->params, "background") ); |
---|
| 377 | self->model->sldSolv = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sldSolv") ); |
---|
| 378 | self->model->fuzziness = PyFloat_AsDouble( PyDict_GetItemString(self->params, "fuzziness") ); |
---|
| 379 | // Read in dispersion parameters |
---|
| 380 | PyObject* disp_dict; |
---|
| 381 | DispersionVisitor* visitor = new DispersionVisitor(); |
---|
| 382 | disp_dict = PyDict_GetItemString(self->dispersion, "radius"); |
---|
| 383 | self->model->radius.dispersion->accept_as_destination(visitor, self->model->radius.dispersion, disp_dict); |
---|
| 384 | disp_dict = PyDict_GetItemString(self->dispersion, "fuzziness"); |
---|
| 385 | self->model->fuzziness.dispersion->accept_as_destination(visitor, self->model->fuzziness.dispersion, disp_dict); |
---|
| 386 | |
---|
| 387 | |
---|
| 388 | return Py_BuildValue("d",(*(self->model)).calculate_ER()); |
---|
| 389 | |
---|
| 390 | } |
---|
| 391 | /** |
---|
| 392 | * Function to call to evaluate model in cartesian coordinates |
---|
| 393 | * @param args: input q or [qx, qy]] |
---|
| 394 | * @return: function value |
---|
| 395 | */ |
---|
| 396 | static PyObject * runXY(CFuzzySphereModel *self, PyObject *args) { |
---|
| 397 | double qx_value, qy_value; |
---|
| 398 | PyObject* pars; |
---|
| 399 | int npars; |
---|
| 400 | |
---|
| 401 | // Get parameters |
---|
| 402 | |
---|
| 403 | // Reader parameter dictionary |
---|
| 404 | self->model->scale = PyFloat_AsDouble( PyDict_GetItemString(self->params, "scale") ); |
---|
| 405 | self->model->sldSph = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sldSph") ); |
---|
| 406 | self->model->radius = PyFloat_AsDouble( PyDict_GetItemString(self->params, "radius") ); |
---|
| 407 | self->model->background = PyFloat_AsDouble( PyDict_GetItemString(self->params, "background") ); |
---|
| 408 | self->model->sldSolv = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sldSolv") ); |
---|
| 409 | self->model->fuzziness = PyFloat_AsDouble( PyDict_GetItemString(self->params, "fuzziness") ); |
---|
| 410 | // Read in dispersion parameters |
---|
| 411 | PyObject* disp_dict; |
---|
| 412 | DispersionVisitor* visitor = new DispersionVisitor(); |
---|
| 413 | disp_dict = PyDict_GetItemString(self->dispersion, "radius"); |
---|
| 414 | self->model->radius.dispersion->accept_as_destination(visitor, self->model->radius.dispersion, disp_dict); |
---|
| 415 | disp_dict = PyDict_GetItemString(self->dispersion, "fuzziness"); |
---|
| 416 | self->model->fuzziness.dispersion->accept_as_destination(visitor, self->model->fuzziness.dispersion, disp_dict); |
---|
| 417 | |
---|
| 418 | |
---|
| 419 | // Get input and determine whether we have to supply a 1D or 2D return value. |
---|
| 420 | if ( !PyArg_ParseTuple(args,"O",&pars) ) { |
---|
| 421 | PyErr_SetString(CFuzzySphereModelError, |
---|
| 422 | "CFuzzySphereModel.run expects a q value."); |
---|
| 423 | return NULL; |
---|
| 424 | } |
---|
| 425 | |
---|
| 426 | // Check params |
---|
| 427 | if( PyList_Check(pars)==1) { |
---|
| 428 | |
---|
| 429 | // Length of list should be 2 for I(qx, qy)) |
---|
| 430 | npars = PyList_GET_SIZE(pars); |
---|
| 431 | if(npars!=2) { |
---|
| 432 | PyErr_SetString(CFuzzySphereModelError, |
---|
| 433 | "CFuzzySphereModel.run expects a double or a list of dimension 2."); |
---|
| 434 | return NULL; |
---|
| 435 | } |
---|
| 436 | // We have a vector q, get the qx and qy values at which |
---|
| 437 | // to evaluate I(qx,qy) |
---|
| 438 | qx_value = CFuzzySphereModel_readDouble(PyList_GET_ITEM(pars,0)); |
---|
| 439 | qy_value = CFuzzySphereModel_readDouble(PyList_GET_ITEM(pars,1)); |
---|
| 440 | return Py_BuildValue("d",(*(self->model))(qx_value,qy_value)); |
---|
| 441 | |
---|
| 442 | } else { |
---|
| 443 | |
---|
| 444 | // We have a scalar q, we will evaluate I(q) |
---|
| 445 | qx_value = CFuzzySphereModel_readDouble(pars); |
---|
| 446 | |
---|
| 447 | return Py_BuildValue("d",(*(self->model))(qx_value)); |
---|
| 448 | } |
---|
| 449 | } |
---|
| 450 | |
---|
| 451 | static PyObject * reset(CFuzzySphereModel *self, PyObject *args) { |
---|
| 452 | |
---|
| 453 | |
---|
| 454 | return Py_BuildValue("d",0.0); |
---|
| 455 | } |
---|
| 456 | |
---|
| 457 | static PyObject * set_dispersion(CFuzzySphereModel *self, PyObject *args) { |
---|
| 458 | PyObject * disp; |
---|
| 459 | const char * par_name; |
---|
| 460 | |
---|
| 461 | if ( !PyArg_ParseTuple(args,"sO", &par_name, &disp) ) { |
---|
| 462 | PyErr_SetString(CFuzzySphereModelError, |
---|
| 463 | "CFuzzySphereModel.set_dispersion expects a DispersionModel object."); |
---|
| 464 | return NULL; |
---|
| 465 | } |
---|
| 466 | void *temp = PyCObject_AsVoidPtr(disp); |
---|
| 467 | DispersionModel * dispersion = static_cast<DispersionModel *>(temp); |
---|
| 468 | |
---|
| 469 | |
---|
| 470 | // Ugliness necessary to go from python to C |
---|
| 471 | // TODO: refactor this |
---|
| 472 | if (!strcmp(par_name, "radius")) { |
---|
| 473 | self->model->radius.dispersion = dispersion; |
---|
| 474 | } else if (!strcmp(par_name, "fuzziness")) { |
---|
| 475 | self->model->fuzziness.dispersion = dispersion; |
---|
| 476 | } else { |
---|
| 477 | PyErr_SetString(CFuzzySphereModelError, |
---|
| 478 | "CFuzzySphereModel.set_dispersion expects a valid parameter name."); |
---|
| 479 | return NULL; |
---|
| 480 | } |
---|
| 481 | |
---|
| 482 | DispersionVisitor* visitor = new DispersionVisitor(); |
---|
| 483 | PyObject * disp_dict = PyDict_New(); |
---|
| 484 | dispersion->accept_as_source(visitor, dispersion, disp_dict); |
---|
| 485 | PyDict_SetItemString(self->dispersion, par_name, disp_dict); |
---|
| 486 | return Py_BuildValue("i",1); |
---|
| 487 | } |
---|
| 488 | |
---|
| 489 | |
---|
| 490 | static PyMethodDef CFuzzySphereModel_methods[] = { |
---|
| 491 | {"run", (PyCFunction)run , METH_VARARGS, |
---|
| 492 | "Evaluate the model at a given Q or Q, phi"}, |
---|
| 493 | {"runXY", (PyCFunction)runXY , METH_VARARGS, |
---|
| 494 | "Evaluate the model at a given Q or Qx, Qy"}, |
---|
| 495 | {"calculate_ER", (PyCFunction)calculate_ER , METH_VARARGS, |
---|
| 496 | "Evaluate the model at a given Q or Q, phi"}, |
---|
| 497 | |
---|
| 498 | {"evalDistribution", (PyCFunction)evalDistribution , METH_VARARGS, |
---|
| 499 | "Evaluate the model at a given Q or Qx, Qy vector "}, |
---|
| 500 | {"reset", (PyCFunction)reset , METH_VARARGS, |
---|
| 501 | "Reset pair correlation"}, |
---|
| 502 | {"set_dispersion", (PyCFunction)set_dispersion , METH_VARARGS, |
---|
| 503 | "Set the dispersion model for a given parameter"}, |
---|
| 504 | {NULL} |
---|
| 505 | }; |
---|
| 506 | |
---|
| 507 | static PyTypeObject CFuzzySphereModelType = { |
---|
| 508 | PyObject_HEAD_INIT(NULL) |
---|
| 509 | 0, /*ob_size*/ |
---|
| 510 | "CFuzzySphereModel", /*tp_name*/ |
---|
| 511 | sizeof(CFuzzySphereModel), /*tp_basicsize*/ |
---|
| 512 | 0, /*tp_itemsize*/ |
---|
| 513 | (destructor)CFuzzySphereModel_dealloc, /*tp_dealloc*/ |
---|
| 514 | 0, /*tp_print*/ |
---|
| 515 | 0, /*tp_getattr*/ |
---|
| 516 | 0, /*tp_setattr*/ |
---|
| 517 | 0, /*tp_compare*/ |
---|
| 518 | 0, /*tp_repr*/ |
---|
| 519 | 0, /*tp_as_number*/ |
---|
| 520 | 0, /*tp_as_sequence*/ |
---|
| 521 | 0, /*tp_as_mapping*/ |
---|
| 522 | 0, /*tp_hash */ |
---|
| 523 | 0, /*tp_call*/ |
---|
| 524 | 0, /*tp_str*/ |
---|
| 525 | 0, /*tp_getattro*/ |
---|
| 526 | 0, /*tp_setattro*/ |
---|
| 527 | 0, /*tp_as_buffer*/ |
---|
| 528 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/ |
---|
| 529 | "CFuzzySphereModel objects", /* tp_doc */ |
---|
| 530 | 0, /* tp_traverse */ |
---|
| 531 | 0, /* tp_clear */ |
---|
| 532 | 0, /* tp_richcompare */ |
---|
| 533 | 0, /* tp_weaklistoffset */ |
---|
| 534 | 0, /* tp_iter */ |
---|
| 535 | 0, /* tp_iternext */ |
---|
| 536 | CFuzzySphereModel_methods, /* tp_methods */ |
---|
| 537 | CFuzzySphereModel_members, /* tp_members */ |
---|
| 538 | 0, /* tp_getset */ |
---|
| 539 | 0, /* tp_base */ |
---|
| 540 | 0, /* tp_dict */ |
---|
| 541 | 0, /* tp_descr_get */ |
---|
| 542 | 0, /* tp_descr_set */ |
---|
| 543 | 0, /* tp_dictoffset */ |
---|
| 544 | (initproc)CFuzzySphereModel_init, /* tp_init */ |
---|
| 545 | 0, /* tp_alloc */ |
---|
| 546 | CFuzzySphereModel_new, /* tp_new */ |
---|
| 547 | }; |
---|
| 548 | |
---|
| 549 | |
---|
| 550 | //static PyMethodDef module_methods[] = { |
---|
| 551 | // {NULL} |
---|
| 552 | //}; |
---|
| 553 | |
---|
| 554 | /** |
---|
| 555 | * Function used to add the model class to a module |
---|
| 556 | * @param module: module to add the class to |
---|
| 557 | */ |
---|
| 558 | void addCFuzzySphereModel(PyObject *module) { |
---|
| 559 | PyObject *d; |
---|
| 560 | |
---|
| 561 | if (PyType_Ready(&CFuzzySphereModelType) < 0) |
---|
| 562 | return; |
---|
| 563 | |
---|
| 564 | Py_INCREF(&CFuzzySphereModelType); |
---|
| 565 | PyModule_AddObject(module, "CFuzzySphereModel", (PyObject *)&CFuzzySphereModelType); |
---|
| 566 | |
---|
| 567 | d = PyModule_GetDict(module); |
---|
| 568 | CFuzzySphereModelError = PyErr_NewException("CFuzzySphereModel.error", NULL, NULL); |
---|
| 569 | PyDict_SetItemString(d, "CFuzzySphereModelError", CFuzzySphereModelError); |
---|
| 570 | } |
---|
| 571 | |
---|