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