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