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