[af03ddd] | 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 | /** CCylinderModel |
---|
[fca6936] | 16 | * |
---|
[af03ddd] | 17 | * C extension |
---|
| 18 | * |
---|
| 19 | * WARNING: THIS FILE WAS GENERATED BY WRAPPERGENERATOR.PY |
---|
| 20 | * DO NOT MODIFY THIS FILE, MODIFY cylinder.h |
---|
| 21 | * AND RE-RUN THE GENERATOR SCRIPT |
---|
[fca6936] | 22 | * |
---|
| 23 | */ |
---|
[9bd69098] | 24 | #define NO_IMPORT_ARRAY |
---|
| 25 | #define PY_ARRAY_UNIQUE_SYMBOL PyArray_API_sans |
---|
[af03ddd] | 26 | |
---|
[fca6936] | 27 | extern "C" { |
---|
| 28 | #include <Python.h> |
---|
[9bd69098] | 29 | #include <arrayobject.h> |
---|
[fca6936] | 30 | #include "structmember.h" |
---|
| 31 | #include <stdio.h> |
---|
| 32 | #include <stdlib.h> |
---|
| 33 | #include <math.h> |
---|
| 34 | #include <time.h> |
---|
| 35 | #include "cylinder.h" |
---|
| 36 | } |
---|
| 37 | |
---|
| 38 | #include "models.hh" |
---|
| 39 | #include "dispersion_visitor.hh" |
---|
| 40 | |
---|
| 41 | /// Error object for raised exceptions |
---|
| 42 | static PyObject * CCylinderModelError = NULL; |
---|
| 43 | |
---|
| 44 | |
---|
| 45 | // Class definition |
---|
| 46 | typedef struct { |
---|
| 47 | PyObject_HEAD |
---|
| 48 | /// Parameters |
---|
| 49 | PyObject * params; |
---|
[af03ddd] | 50 | /// Dispersion parameters |
---|
[fca6936] | 51 | PyObject * dispersion; |
---|
[af03ddd] | 52 | /// Underlying model object |
---|
| 53 | CylinderModel * model; |
---|
[fca6936] | 54 | /// Log for unit testing |
---|
| 55 | PyObject * log; |
---|
| 56 | } CCylinderModel; |
---|
| 57 | |
---|
| 58 | |
---|
| 59 | static void |
---|
| 60 | CCylinderModel_dealloc(CCylinderModel* self) |
---|
| 61 | { |
---|
[71e2de7] | 62 | Py_DECREF(self->params); |
---|
| 63 | Py_DECREF(self->dispersion); |
---|
| 64 | Py_DECREF(self->log); |
---|
| 65 | delete self->model; |
---|
[fca6936] | 66 | self->ob_type->tp_free((PyObject*)self); |
---|
[af03ddd] | 67 | |
---|
[fca6936] | 68 | |
---|
| 69 | } |
---|
| 70 | |
---|
| 71 | static PyObject * |
---|
| 72 | CCylinderModel_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
---|
| 73 | { |
---|
| 74 | CCylinderModel *self; |
---|
[af03ddd] | 75 | |
---|
[fca6936] | 76 | self = (CCylinderModel *)type->tp_alloc(type, 0); |
---|
[af03ddd] | 77 | |
---|
[fca6936] | 78 | return (PyObject *)self; |
---|
| 79 | } |
---|
| 80 | |
---|
| 81 | static int |
---|
| 82 | CCylinderModel_init(CCylinderModel *self, PyObject *args, PyObject *kwds) |
---|
| 83 | { |
---|
| 84 | if (self != NULL) { |
---|
[af03ddd] | 85 | |
---|
[fca6936] | 86 | // Create parameters |
---|
| 87 | self->params = PyDict_New(); |
---|
| 88 | self->dispersion = PyDict_New(); |
---|
[af03ddd] | 89 | self->model = new CylinderModel(); |
---|
| 90 | |
---|
[fca6936] | 91 | // Initialize parameter dictionary |
---|
[c724ccd] | 92 | PyDict_SetItemString(self->params,"scale",Py_BuildValue("d",1.00000000)); |
---|
| 93 | PyDict_SetItemString(self->params,"sldCyl",Py_BuildValue("d",0.00000400)); |
---|
| 94 | PyDict_SetItemString(self->params,"cyl_theta",Py_BuildValue("d",1.00000000)); |
---|
| 95 | PyDict_SetItemString(self->params,"length",Py_BuildValue("d",400.00000000)); |
---|
| 96 | PyDict_SetItemString(self->params,"sldSolv",Py_BuildValue("d",0.00000100)); |
---|
| 97 | PyDict_SetItemString(self->params,"background",Py_BuildValue("d",0.00000000)); |
---|
| 98 | PyDict_SetItemString(self->params,"radius",Py_BuildValue("d",20.00000000)); |
---|
| 99 | PyDict_SetItemString(self->params,"cyl_phi",Py_BuildValue("d",1.00000000)); |
---|
[fca6936] | 100 | // Initialize dispersion / averaging parameter dict |
---|
| 101 | DispersionVisitor* visitor = new DispersionVisitor(); |
---|
[af03ddd] | 102 | PyObject * disp_dict; |
---|
| 103 | disp_dict = PyDict_New(); |
---|
[fca6936] | 104 | self->model->radius.dispersion->accept_as_source(visitor, self->model->radius.dispersion, disp_dict); |
---|
[af03ddd] | 105 | PyDict_SetItemString(self->dispersion, "radius", disp_dict); |
---|
[fca6936] | 106 | disp_dict = PyDict_New(); |
---|
| 107 | self->model->length.dispersion->accept_as_source(visitor, self->model->length.dispersion, disp_dict); |
---|
[af03ddd] | 108 | PyDict_SetItemString(self->dispersion, "length", disp_dict); |
---|
| 109 | disp_dict = PyDict_New(); |
---|
| 110 | self->model->cyl_theta.dispersion->accept_as_source(visitor, self->model->cyl_theta.dispersion, disp_dict); |
---|
| 111 | PyDict_SetItemString(self->dispersion, "cyl_theta", disp_dict); |
---|
[fca6936] | 112 | disp_dict = PyDict_New(); |
---|
| 113 | self->model->cyl_phi.dispersion->accept_as_source(visitor, self->model->cyl_phi.dispersion, disp_dict); |
---|
[af03ddd] | 114 | PyDict_SetItemString(self->dispersion, "cyl_phi", disp_dict); |
---|
[fca6936] | 115 | |
---|
| 116 | |
---|
[af03ddd] | 117 | |
---|
[fca6936] | 118 | // Create empty log |
---|
| 119 | self->log = PyDict_New(); |
---|
[af03ddd] | 120 | |
---|
| 121 | |
---|
[fca6936] | 122 | |
---|
| 123 | } |
---|
| 124 | return 0; |
---|
| 125 | } |
---|
| 126 | |
---|
| 127 | static PyMemberDef CCylinderModel_members[] = { |
---|
[af03ddd] | 128 | {"params", T_OBJECT, offsetof(CCylinderModel, params), 0, |
---|
| 129 | "Parameters"}, |
---|
[fca6936] | 130 | {"dispersion", T_OBJECT, offsetof(CCylinderModel, dispersion), 0, |
---|
[af03ddd] | 131 | "Dispersion parameters"}, |
---|
[fca6936] | 132 | {"log", T_OBJECT, offsetof(CCylinderModel, log), 0, |
---|
| 133 | "Log"}, |
---|
| 134 | {NULL} /* Sentinel */ |
---|
| 135 | }; |
---|
| 136 | |
---|
| 137 | /** Read double from PyObject |
---|
| 138 | @param p PyObject |
---|
| 139 | @return double |
---|
| 140 | */ |
---|
| 141 | double CCylinderModel_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(CylinderModel* 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( CylinderModel* model, |
---|
| 189 | PyArrayObject *x, PyArrayObject *y) |
---|
| 190 | { |
---|
| 191 | PyArrayObject *result; |
---|
[3080527] | 192 | int i,j, 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(CCylinderModelError, |
---|
| 227 | "CCylinderModel.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(CCylinderModel *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") ); |
---|
[f10063e] | 245 | self->model->sldCyl = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sldCyl") ); |
---|
[9bd69098] | 246 | self->model->cyl_theta = PyFloat_AsDouble( PyDict_GetItemString(self->params, "cyl_theta") ); |
---|
[f10063e] | 247 | self->model->length = PyFloat_AsDouble( PyDict_GetItemString(self->params, "length") ); |
---|
| 248 | self->model->sldSolv = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sldSolv") ); |
---|
[9bd69098] | 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->cyl_phi = PyFloat_AsDouble( PyDict_GetItemString(self->params, "cyl_phi") ); |
---|
| 252 | // Read in dispersion parameters |
---|
| 253 | PyObject* disp_dict; |
---|
| 254 | DispersionVisitor* visitor = new DispersionVisitor(); |
---|
| 255 | disp_dict = PyDict_GetItemString(self->dispersion, "radius"); |
---|
| 256 | self->model->radius.dispersion->accept_as_destination(visitor, self->model->radius.dispersion, disp_dict); |
---|
| 257 | disp_dict = PyDict_GetItemString(self->dispersion, "length"); |
---|
| 258 | self->model->length.dispersion->accept_as_destination(visitor, self->model->length.dispersion, disp_dict); |
---|
| 259 | disp_dict = PyDict_GetItemString(self->dispersion, "cyl_theta"); |
---|
| 260 | self->model->cyl_theta.dispersion->accept_as_destination(visitor, self->model->cyl_theta.dispersion, disp_dict); |
---|
| 261 | disp_dict = PyDict_GetItemString(self->dispersion, "cyl_phi"); |
---|
| 262 | self->model->cyl_phi.dispersion->accept_as_destination(visitor, self->model->cyl_phi.dispersion, disp_dict); |
---|
[fca6936] | 263 | |
---|
[9bd69098] | 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(CCylinderModelError, |
---|
| 268 | "CCylinderModel.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(CCylinderModelError, |
---|
| 284 | "CCylinderModel.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(CCylinderModelError, |
---|
| 292 | "CCylinderModel.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(CCylinderModelError, |
---|
| 302 | "CCylinderModel.evalDistribution expect 2 numpy arrays in list."); |
---|
| 303 | return NULL; |
---|
| 304 | } |
---|
| 305 | } |
---|
[e0a8a3c] | 306 | PyErr_SetString(CCylinderModelError, |
---|
| 307 | "CCylinderModel.evalDistribution couln't be run."); |
---|
| 308 | return NULL; |
---|
| 309 | |
---|
[9bd69098] | 310 | } |
---|
[fca6936] | 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(CCylinderModel *self, PyObject *args) { |
---|
| 318 | double q_value, phi_value; |
---|
| 319 | PyObject* pars; |
---|
| 320 | int npars; |
---|
[af03ddd] | 321 | |
---|
[fca6936] | 322 | // Get parameters |
---|
[af03ddd] | 323 | |
---|
| 324 | // Reader parameter dictionary |
---|
[fca6936] | 325 | self->model->scale = PyFloat_AsDouble( PyDict_GetItemString(self->params, "scale") ); |
---|
[f10063e] | 326 | self->model->sldCyl = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sldCyl") ); |
---|
[fca6936] | 327 | self->model->cyl_theta = PyFloat_AsDouble( PyDict_GetItemString(self->params, "cyl_theta") ); |
---|
[f10063e] | 328 | self->model->length = PyFloat_AsDouble( PyDict_GetItemString(self->params, "length") ); |
---|
| 329 | self->model->sldSolv = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sldSolv") ); |
---|
[fca6936] | 330 | self->model->background = PyFloat_AsDouble( PyDict_GetItemString(self->params, "background") ); |
---|
| 331 | self->model->radius = PyFloat_AsDouble( PyDict_GetItemString(self->params, "radius") ); |
---|
| 332 | self->model->cyl_phi = PyFloat_AsDouble( PyDict_GetItemString(self->params, "cyl_phi") ); |
---|
| 333 | // Read in dispersion parameters |
---|
[af03ddd] | 334 | PyObject* disp_dict; |
---|
[fca6936] | 335 | DispersionVisitor* visitor = new DispersionVisitor(); |
---|
[af03ddd] | 336 | disp_dict = PyDict_GetItemString(self->dispersion, "radius"); |
---|
[fca6936] | 337 | self->model->radius.dispersion->accept_as_destination(visitor, self->model->radius.dispersion, disp_dict); |
---|
[af03ddd] | 338 | disp_dict = PyDict_GetItemString(self->dispersion, "length"); |
---|
[fca6936] | 339 | self->model->length.dispersion->accept_as_destination(visitor, self->model->length.dispersion, disp_dict); |
---|
[af03ddd] | 340 | disp_dict = PyDict_GetItemString(self->dispersion, "cyl_theta"); |
---|
[fca6936] | 341 | self->model->cyl_theta.dispersion->accept_as_destination(visitor, self->model->cyl_theta.dispersion, disp_dict); |
---|
[af03ddd] | 342 | disp_dict = PyDict_GetItemString(self->dispersion, "cyl_phi"); |
---|
[fca6936] | 343 | self->model->cyl_phi.dispersion->accept_as_destination(visitor, self->model->cyl_phi.dispersion, disp_dict); |
---|
| 344 | |
---|
[af03ddd] | 345 | |
---|
[fca6936] | 346 | // Get input and determine whether we have to supply a 1D or 2D return value. |
---|
| 347 | if ( !PyArg_ParseTuple(args,"O",&pars) ) { |
---|
[af03ddd] | 348 | PyErr_SetString(CCylinderModelError, |
---|
[fca6936] | 349 | "CCylinderModel.run expects a q value."); |
---|
| 350 | return NULL; |
---|
| 351 | } |
---|
[af03ddd] | 352 | |
---|
[fca6936] | 353 | // Check params |
---|
| 354 | if( PyList_Check(pars)==1) { |
---|
[af03ddd] | 355 | |
---|
[fca6936] | 356 | // Length of list should be 2 for I(q,phi) |
---|
[af03ddd] | 357 | npars = PyList_GET_SIZE(pars); |
---|
[fca6936] | 358 | if(npars!=2) { |
---|
[af03ddd] | 359 | PyErr_SetString(CCylinderModelError, |
---|
[fca6936] | 360 | "CCylinderModel.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) |
---|
[4176435] | 365 | q_value = CCylinderModel_readDouble(PyList_GET_ITEM(pars,0)); |
---|
| 366 | phi_value = CCylinderModel_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 | |
---|
[fca6936] | 373 | } else { |
---|
[4176435] | 374 | |
---|
| 375 | // We have a scalar q, we will evaluate I(q) |
---|
[af03ddd] | 376 | q_value = CCylinderModel_readDouble(pars); |
---|
[4176435] | 377 | |
---|
| 378 | return Py_BuildValue("d",(*(self->model))(q_value)); |
---|
| 379 | } |
---|
[fca6936] | 380 | } |
---|
[5eb9154] | 381 | /** |
---|
| 382 | * Function to call to calculate_ER |
---|
| 383 | * @return: effective radius value |
---|
| 384 | */ |
---|
| 385 | static PyObject * calculate_ER(CCylinderModel *self) { |
---|
[fca6936] | 386 | |
---|
[5eb9154] | 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") ); |
---|
[f10063e] | 394 | self->model->sldCyl = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sldCyl") ); |
---|
[5eb9154] | 395 | self->model->cyl_theta = PyFloat_AsDouble( PyDict_GetItemString(self->params, "cyl_theta") ); |
---|
[f10063e] | 396 | self->model->length = PyFloat_AsDouble( PyDict_GetItemString(self->params, "length") ); |
---|
| 397 | self->model->sldSolv = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sldSolv") ); |
---|
[5eb9154] | 398 | self->model->background = PyFloat_AsDouble( PyDict_GetItemString(self->params, "background") ); |
---|
| 399 | self->model->radius = PyFloat_AsDouble( PyDict_GetItemString(self->params, "radius") ); |
---|
| 400 | self->model->cyl_phi = PyFloat_AsDouble( PyDict_GetItemString(self->params, "cyl_phi") ); |
---|
| 401 | // Read in dispersion parameters |
---|
| 402 | PyObject* disp_dict; |
---|
| 403 | DispersionVisitor* visitor = new DispersionVisitor(); |
---|
| 404 | disp_dict = PyDict_GetItemString(self->dispersion, "radius"); |
---|
| 405 | self->model->radius.dispersion->accept_as_destination(visitor, self->model->radius.dispersion, disp_dict); |
---|
| 406 | disp_dict = PyDict_GetItemString(self->dispersion, "length"); |
---|
| 407 | self->model->length.dispersion->accept_as_destination(visitor, self->model->length.dispersion, disp_dict); |
---|
| 408 | disp_dict = PyDict_GetItemString(self->dispersion, "cyl_theta"); |
---|
| 409 | self->model->cyl_theta.dispersion->accept_as_destination(visitor, self->model->cyl_theta.dispersion, disp_dict); |
---|
| 410 | disp_dict = PyDict_GetItemString(self->dispersion, "cyl_phi"); |
---|
| 411 | self->model->cyl_phi.dispersion->accept_as_destination(visitor, self->model->cyl_phi.dispersion, disp_dict); |
---|
| 412 | |
---|
| 413 | |
---|
| 414 | return Py_BuildValue("d",(*(self->model)).calculate_ER()); |
---|
| 415 | |
---|
| 416 | } |
---|
[fca6936] | 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(CCylinderModel *self, PyObject *args) { |
---|
| 423 | double qx_value, qy_value; |
---|
| 424 | PyObject* pars; |
---|
| 425 | int npars; |
---|
[af03ddd] | 426 | |
---|
[fca6936] | 427 | // Get parameters |
---|
[af03ddd] | 428 | |
---|
| 429 | // Reader parameter dictionary |
---|
[fca6936] | 430 | self->model->scale = PyFloat_AsDouble( PyDict_GetItemString(self->params, "scale") ); |
---|
[f10063e] | 431 | self->model->sldCyl = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sldCyl") ); |
---|
[fca6936] | 432 | self->model->cyl_theta = PyFloat_AsDouble( PyDict_GetItemString(self->params, "cyl_theta") ); |
---|
[f10063e] | 433 | self->model->length = PyFloat_AsDouble( PyDict_GetItemString(self->params, "length") ); |
---|
| 434 | self->model->sldSolv = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sldSolv") ); |
---|
[fca6936] | 435 | self->model->background = PyFloat_AsDouble( PyDict_GetItemString(self->params, "background") ); |
---|
| 436 | self->model->radius = PyFloat_AsDouble( PyDict_GetItemString(self->params, "radius") ); |
---|
| 437 | self->model->cyl_phi = PyFloat_AsDouble( PyDict_GetItemString(self->params, "cyl_phi") ); |
---|
| 438 | // Read in dispersion parameters |
---|
[af03ddd] | 439 | PyObject* disp_dict; |
---|
[fca6936] | 440 | DispersionVisitor* visitor = new DispersionVisitor(); |
---|
[af03ddd] | 441 | disp_dict = PyDict_GetItemString(self->dispersion, "radius"); |
---|
[fca6936] | 442 | self->model->radius.dispersion->accept_as_destination(visitor, self->model->radius.dispersion, disp_dict); |
---|
[af03ddd] | 443 | disp_dict = PyDict_GetItemString(self->dispersion, "length"); |
---|
[fca6936] | 444 | self->model->length.dispersion->accept_as_destination(visitor, self->model->length.dispersion, disp_dict); |
---|
[af03ddd] | 445 | disp_dict = PyDict_GetItemString(self->dispersion, "cyl_theta"); |
---|
[fca6936] | 446 | self->model->cyl_theta.dispersion->accept_as_destination(visitor, self->model->cyl_theta.dispersion, disp_dict); |
---|
[af03ddd] | 447 | disp_dict = PyDict_GetItemString(self->dispersion, "cyl_phi"); |
---|
[fca6936] | 448 | self->model->cyl_phi.dispersion->accept_as_destination(visitor, self->model->cyl_phi.dispersion, disp_dict); |
---|
| 449 | |
---|
[af03ddd] | 450 | |
---|
[fca6936] | 451 | // Get input and determine whether we have to supply a 1D or 2D return value. |
---|
| 452 | if ( !PyArg_ParseTuple(args,"O",&pars) ) { |
---|
[af03ddd] | 453 | PyErr_SetString(CCylinderModelError, |
---|
[fca6936] | 454 | "CCylinderModel.run expects a q value."); |
---|
| 455 | return NULL; |
---|
| 456 | } |
---|
[af03ddd] | 457 | |
---|
[fca6936] | 458 | // Check params |
---|
| 459 | if( PyList_Check(pars)==1) { |
---|
[af03ddd] | 460 | |
---|
[fca6936] | 461 | // Length of list should be 2 for I(qx, qy)) |
---|
[af03ddd] | 462 | npars = PyList_GET_SIZE(pars); |
---|
[fca6936] | 463 | if(npars!=2) { |
---|
[af03ddd] | 464 | PyErr_SetString(CCylinderModelError, |
---|
[fca6936] | 465 | "CCylinderModel.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) |
---|
[4176435] | 470 | qx_value = CCylinderModel_readDouble(PyList_GET_ITEM(pars,0)); |
---|
| 471 | qy_value = CCylinderModel_readDouble(PyList_GET_ITEM(pars,1)); |
---|
| 472 | return Py_BuildValue("d",(*(self->model))(qx_value,qy_value)); |
---|
[fca6936] | 473 | |
---|
| 474 | } else { |
---|
[4176435] | 475 | |
---|
| 476 | // We have a scalar q, we will evaluate I(q) |
---|
| 477 | qx_value = CCylinderModel_readDouble(pars); |
---|
[af03ddd] | 478 | |
---|
[4176435] | 479 | return Py_BuildValue("d",(*(self->model))(qx_value)); |
---|
[af03ddd] | 480 | } |
---|
[fca6936] | 481 | } |
---|
| 482 | |
---|
| 483 | static PyObject * reset(CCylinderModel *self, PyObject *args) { |
---|
[af03ddd] | 484 | |
---|
[fca6936] | 485 | |
---|
| 486 | return Py_BuildValue("d",0.0); |
---|
| 487 | } |
---|
| 488 | |
---|
[af03ddd] | 489 | static PyObject * set_dispersion(CCylinderModel *self, PyObject *args) { |
---|
| 490 | PyObject * disp; |
---|
| 491 | const char * par_name; |
---|
| 492 | |
---|
| 493 | if ( !PyArg_ParseTuple(args,"sO", &par_name, &disp) ) { |
---|
| 494 | PyErr_SetString(CCylinderModelError, |
---|
| 495 | "CCylinderModel.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, "radius")) { |
---|
| 505 | self->model->radius.dispersion = dispersion; |
---|
| 506 | } else if (!strcmp(par_name, "length")) { |
---|
| 507 | self->model->length.dispersion = dispersion; |
---|
| 508 | } else if (!strcmp(par_name, "cyl_theta")) { |
---|
| 509 | self->model->cyl_theta.dispersion = dispersion; |
---|
| 510 | } else if (!strcmp(par_name, "cyl_phi")) { |
---|
| 511 | self->model->cyl_phi.dispersion = dispersion; |
---|
| 512 | } else { |
---|
| 513 | PyErr_SetString(CCylinderModelError, |
---|
| 514 | "CCylinderModel.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 | |
---|
[fca6936] | 525 | |
---|
| 526 | static PyMethodDef CCylinderModel_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"}, |
---|
[5eb9154] | 531 | {"calculate_ER", (PyCFunction)calculate_ER , METH_VARARGS, |
---|
| 532 | "Evaluate the model at a given Q or Q, phi"}, |
---|
[9bd69098] | 533 | |
---|
| 534 | {"evalDistribution", (PyCFunction)evalDistribution , METH_VARARGS, |
---|
| 535 | "Evaluate the model at a given Q or Qx, Qy vector "}, |
---|
[fca6936] | 536 | {"reset", (PyCFunction)reset , METH_VARARGS, |
---|
| 537 | "Reset pair correlation"}, |
---|
[af03ddd] | 538 | {"set_dispersion", (PyCFunction)set_dispersion , METH_VARARGS, |
---|
| 539 | "Set the dispersion model for a given parameter"}, |
---|
[fca6936] | 540 | {NULL} |
---|
| 541 | }; |
---|
| 542 | |
---|
| 543 | static PyTypeObject CCylinderModelType = { |
---|
| 544 | PyObject_HEAD_INIT(NULL) |
---|
| 545 | 0, /*ob_size*/ |
---|
| 546 | "CCylinderModel", /*tp_name*/ |
---|
| 547 | sizeof(CCylinderModel), /*tp_basicsize*/ |
---|
| 548 | 0, /*tp_itemsize*/ |
---|
| 549 | (destructor)CCylinderModel_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 | "CCylinderModel 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 | CCylinderModel_methods, /* tp_methods */ |
---|
| 573 | CCylinderModel_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)CCylinderModel_init, /* tp_init */ |
---|
| 581 | 0, /* tp_alloc */ |
---|
| 582 | CCylinderModel_new, /* tp_new */ |
---|
| 583 | }; |
---|
| 584 | |
---|
| 585 | |
---|
[9bd69098] | 586 | //static PyMethodDef module_methods[] = { |
---|
| 587 | // {NULL} |
---|
| 588 | //}; |
---|
[4176435] | 589 | |
---|
[fca6936] | 590 | /** |
---|
| 591 | * Function used to add the model class to a module |
---|
| 592 | * @param module: module to add the class to |
---|
[af03ddd] | 593 | */ |
---|
[fca6936] | 594 | void addCCylinderModel(PyObject *module) { |
---|
| 595 | PyObject *d; |
---|
[af03ddd] | 596 | |
---|
[fca6936] | 597 | if (PyType_Ready(&CCylinderModelType) < 0) |
---|
| 598 | return; |
---|
| 599 | |
---|
| 600 | Py_INCREF(&CCylinderModelType); |
---|
| 601 | PyModule_AddObject(module, "CCylinderModel", (PyObject *)&CCylinderModelType); |
---|
[af03ddd] | 602 | |
---|
[fca6936] | 603 | d = PyModule_GetDict(module); |
---|
| 604 | CCylinderModelError = PyErr_NewException("CCylinderModel.error", NULL, NULL); |
---|
| 605 | PyDict_SetItemString(d, "CCylinderModelError", CCylinderModelError); |
---|
| 606 | } |
---|
| 607 | |
---|