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