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