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