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