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