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