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