[5068697] | 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 | /** CTriaxialEllipsoidModel |
---|
| 16 | * |
---|
| 17 | * C extension |
---|
| 18 | * |
---|
| 19 | * WARNING: THIS FILE WAS GENERATED BY WRAPPERGENERATOR.PY |
---|
| 20 | * DO NOT MODIFY THIS FILE, MODIFY triaxial_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 |
---|
[5068697] | 26 | |
---|
| 27 | extern "C" { |
---|
| 28 | #include <Python.h> |
---|
[9bd69098] | 29 | #include <arrayobject.h> |
---|
[5068697] | 30 | #include "structmember.h" |
---|
| 31 | #include <stdio.h> |
---|
| 32 | #include <stdlib.h> |
---|
| 33 | #include <math.h> |
---|
| 34 | #include <time.h> |
---|
| 35 | #include "triaxial_ellipsoid.h" |
---|
| 36 | } |
---|
| 37 | |
---|
| 38 | #include "models.hh" |
---|
| 39 | #include "dispersion_visitor.hh" |
---|
| 40 | |
---|
| 41 | /// Error object for raised exceptions |
---|
| 42 | static PyObject * CTriaxialEllipsoidModelError = 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 | TriaxialEllipsoidModel * model; |
---|
| 54 | /// Log for unit testing |
---|
| 55 | PyObject * log; |
---|
| 56 | } CTriaxialEllipsoidModel; |
---|
| 57 | |
---|
| 58 | |
---|
| 59 | static void |
---|
| 60 | CTriaxialEllipsoidModel_dealloc(CTriaxialEllipsoidModel* self) |
---|
| 61 | { |
---|
[71e2de7] | 62 | Py_DECREF(self->params); |
---|
| 63 | Py_DECREF(self->dispersion); |
---|
| 64 | Py_DECREF(self->log); |
---|
| 65 | delete self->model; |
---|
[5068697] | 66 | self->ob_type->tp_free((PyObject*)self); |
---|
| 67 | |
---|
| 68 | |
---|
| 69 | } |
---|
| 70 | |
---|
| 71 | static PyObject * |
---|
| 72 | CTriaxialEllipsoidModel_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
---|
| 73 | { |
---|
| 74 | CTriaxialEllipsoidModel *self; |
---|
| 75 | |
---|
| 76 | self = (CTriaxialEllipsoidModel *)type->tp_alloc(type, 0); |
---|
| 77 | |
---|
| 78 | return (PyObject *)self; |
---|
| 79 | } |
---|
| 80 | |
---|
| 81 | static int |
---|
| 82 | CTriaxialEllipsoidModel_init(CTriaxialEllipsoidModel *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 TriaxialEllipsoidModel(); |
---|
| 90 | |
---|
| 91 | // Initialize parameter dictionary |
---|
[c724ccd] | 92 | PyDict_SetItemString(self->params,"scale",Py_BuildValue("d",1.00000000)); |
---|
| 93 | PyDict_SetItemString(self->params,"axis_psi",Py_BuildValue("d",0.00000000)); |
---|
| 94 | PyDict_SetItemString(self->params,"axis_theta",Py_BuildValue("d",1.00000000)); |
---|
| 95 | PyDict_SetItemString(self->params,"semi_axisA",Py_BuildValue("d",35.00000000)); |
---|
| 96 | PyDict_SetItemString(self->params,"semi_axisB",Py_BuildValue("d",100.00000000)); |
---|
| 97 | PyDict_SetItemString(self->params,"semi_axisC",Py_BuildValue("d",400.00000000)); |
---|
| 98 | PyDict_SetItemString(self->params,"axis_phi",Py_BuildValue("d",1.00000000)); |
---|
[13eb1c4] | 99 | PyDict_SetItemString(self->params,"sldSolv",Py_BuildValue("d",0.00000630)); |
---|
[c724ccd] | 100 | PyDict_SetItemString(self->params,"background",Py_BuildValue("d",0.00000000)); |
---|
[13eb1c4] | 101 | PyDict_SetItemString(self->params,"sldEll",Py_BuildValue("d",0.00000100)); |
---|
[5068697] | 102 | // Initialize dispersion / averaging parameter dict |
---|
| 103 | DispersionVisitor* visitor = new DispersionVisitor(); |
---|
| 104 | PyObject * disp_dict; |
---|
| 105 | disp_dict = PyDict_New(); |
---|
| 106 | self->model->axis_theta.dispersion->accept_as_source(visitor, self->model->axis_theta.dispersion, disp_dict); |
---|
| 107 | PyDict_SetItemString(self->dispersion, "axis_theta", disp_dict); |
---|
| 108 | disp_dict = PyDict_New(); |
---|
| 109 | self->model->axis_phi.dispersion->accept_as_source(visitor, self->model->axis_phi.dispersion, disp_dict); |
---|
| 110 | PyDict_SetItemString(self->dispersion, "axis_phi", disp_dict); |
---|
[975ec8e] | 111 | disp_dict = PyDict_New(); |
---|
| 112 | self->model->axis_psi.dispersion->accept_as_source(visitor, self->model->axis_psi.dispersion, disp_dict); |
---|
| 113 | PyDict_SetItemString(self->dispersion, "axis_psi", disp_dict); |
---|
[5068697] | 114 | |
---|
| 115 | |
---|
| 116 | |
---|
| 117 | // Create empty log |
---|
| 118 | self->log = PyDict_New(); |
---|
| 119 | |
---|
| 120 | |
---|
| 121 | |
---|
| 122 | } |
---|
| 123 | return 0; |
---|
| 124 | } |
---|
| 125 | |
---|
| 126 | static PyMemberDef CTriaxialEllipsoidModel_members[] = { |
---|
| 127 | {"params", T_OBJECT, offsetof(CTriaxialEllipsoidModel, params), 0, |
---|
| 128 | "Parameters"}, |
---|
| 129 | {"dispersion", T_OBJECT, offsetof(CTriaxialEllipsoidModel, dispersion), 0, |
---|
| 130 | "Dispersion parameters"}, |
---|
| 131 | {"log", T_OBJECT, offsetof(CTriaxialEllipsoidModel, log), 0, |
---|
| 132 | "Log"}, |
---|
| 133 | {NULL} /* Sentinel */ |
---|
| 134 | }; |
---|
| 135 | |
---|
| 136 | /** Read double from PyObject |
---|
| 137 | @param p PyObject |
---|
| 138 | @return double |
---|
| 139 | */ |
---|
| 140 | double CTriaxialEllipsoidModel_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(TriaxialEllipsoidModel* 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 | } |
---|
[5068697] | 181 | |
---|
[9bd69098] | 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( TriaxialEllipsoidModel* model, |
---|
| 188 | PyArrayObject *x, PyArrayObject *y) |
---|
| 189 | { |
---|
| 190 | PyArrayObject *result; |
---|
[3080527] | 191 | int i,j, x_len, y_len, dims[1]; |
---|
[9bd69098] | 192 | //check validity of input vectors |
---|
[3080527] | 193 | if (x->nd != 1 || x->descr->type_num != PyArray_DOUBLE |
---|
| 194 | || y->nd != 1 || y->descr->type_num != PyArray_DOUBLE |
---|
| 195 | || y->dimensions[0] != x->dimensions[0]){ |
---|
[9bd69098] | 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 | |
---|
[3080527] | 203 | x_len = dims[0]= x->dimensions[0]; |
---|
[9ce41c6] | 204 | y_len = dims[0]= y->dimensions[0]; |
---|
[9bd69098] | 205 | |
---|
| 206 | // Make a new double matrix of same dims |
---|
[3080527] | 207 | result=(PyArrayObject *) PyArray_FromDims(1,dims,NPY_DOUBLE); |
---|
[9bd69098] | 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. */ |
---|
[3080527] | 215 | for ( i=0; i< x_len; i++) { |
---|
| 216 | double x_value = *(double *)(x->data + i*x->strides[0]); |
---|
| 217 | double y_value = *(double *)(y->data + i*y->strides[0]); |
---|
| 218 | double *result_value = (double *)(result->data + |
---|
| 219 | i*result->strides[0]); |
---|
| 220 | *result_value = (*model)(x_value, y_value); |
---|
| 221 | } |
---|
[9bd69098] | 222 | return PyArray_Return(result); |
---|
| 223 | |
---|
| 224 | }else{ |
---|
| 225 | PyErr_SetString(CTriaxialEllipsoidModelError, |
---|
| 226 | "CTriaxialEllipsoidModel.evaluateTwoDimXY couldn't run."); |
---|
| 227 | return NULL; |
---|
| 228 | } |
---|
| 229 | } |
---|
| 230 | /** |
---|
| 231 | * evalDistribution function evaluate a model function with input vector |
---|
| 232 | * @param args: input q as vector or [qx, qy] where qx, qy are vectors |
---|
| 233 | * |
---|
| 234 | */ |
---|
| 235 | static PyObject * evalDistribution(CTriaxialEllipsoidModel *self, PyObject *args){ |
---|
| 236 | PyObject *qx, *qy; |
---|
| 237 | PyArrayObject * pars; |
---|
| 238 | int npars ,mpars; |
---|
| 239 | |
---|
| 240 | // Get parameters |
---|
| 241 | |
---|
| 242 | // Reader parameter dictionary |
---|
| 243 | self->model->scale = PyFloat_AsDouble( PyDict_GetItemString(self->params, "scale") ); |
---|
[975ec8e] | 244 | self->model->axis_psi = PyFloat_AsDouble( PyDict_GetItemString(self->params, "axis_psi") ); |
---|
[9bd69098] | 245 | self->model->axis_theta = PyFloat_AsDouble( PyDict_GetItemString(self->params, "axis_theta") ); |
---|
| 246 | self->model->semi_axisA = PyFloat_AsDouble( PyDict_GetItemString(self->params, "semi_axisA") ); |
---|
| 247 | self->model->semi_axisB = PyFloat_AsDouble( PyDict_GetItemString(self->params, "semi_axisB") ); |
---|
| 248 | self->model->semi_axisC = PyFloat_AsDouble( PyDict_GetItemString(self->params, "semi_axisC") ); |
---|
| 249 | self->model->axis_phi = PyFloat_AsDouble( PyDict_GetItemString(self->params, "axis_phi") ); |
---|
[13eb1c4] | 250 | self->model->sldSolv = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sldSolv") ); |
---|
[9bd69098] | 251 | self->model->background = PyFloat_AsDouble( PyDict_GetItemString(self->params, "background") ); |
---|
[13eb1c4] | 252 | self->model->sldEll = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sldEll") ); |
---|
[9bd69098] | 253 | // Read in dispersion parameters |
---|
| 254 | PyObject* disp_dict; |
---|
| 255 | DispersionVisitor* visitor = new DispersionVisitor(); |
---|
| 256 | disp_dict = PyDict_GetItemString(self->dispersion, "axis_theta"); |
---|
| 257 | self->model->axis_theta.dispersion->accept_as_destination(visitor, self->model->axis_theta.dispersion, disp_dict); |
---|
| 258 | disp_dict = PyDict_GetItemString(self->dispersion, "axis_phi"); |
---|
| 259 | self->model->axis_phi.dispersion->accept_as_destination(visitor, self->model->axis_phi.dispersion, disp_dict); |
---|
[975ec8e] | 260 | disp_dict = PyDict_GetItemString(self->dispersion, "axis_psi"); |
---|
| 261 | self->model->axis_psi.dispersion->accept_as_destination(visitor, self->model->axis_psi.dispersion, disp_dict); |
---|
[9bd69098] | 262 | |
---|
| 263 | |
---|
| 264 | // Get input and determine whether we have to supply a 1D or 2D return value. |
---|
| 265 | if ( !PyArg_ParseTuple(args,"O",&pars) ) { |
---|
| 266 | PyErr_SetString(CTriaxialEllipsoidModelError, |
---|
| 267 | "CTriaxialEllipsoidModel.evalDistribution expects a q value."); |
---|
| 268 | return NULL; |
---|
| 269 | } |
---|
| 270 | // Check params |
---|
| 271 | |
---|
| 272 | if(PyArray_Check(pars)==1) { |
---|
| 273 | |
---|
| 274 | // Length of list should 1 or 2 |
---|
| 275 | npars = pars->nd; |
---|
| 276 | if(npars==1) { |
---|
| 277 | // input is a numpy array |
---|
| 278 | if (PyArray_Check(pars)) { |
---|
| 279 | return evaluateOneDim(self->model, (PyArrayObject*)pars); |
---|
| 280 | } |
---|
| 281 | }else{ |
---|
| 282 | PyErr_SetString(CTriaxialEllipsoidModelError, |
---|
| 283 | "CTriaxialEllipsoidModel.evalDistribution expect numpy array of one dimension."); |
---|
| 284 | return NULL; |
---|
| 285 | } |
---|
| 286 | }else if( PyList_Check(pars)==1) { |
---|
| 287 | // Length of list should be 2 for I(qx,qy) |
---|
| 288 | mpars = PyList_GET_SIZE(pars); |
---|
| 289 | if(mpars!=2) { |
---|
| 290 | PyErr_SetString(CTriaxialEllipsoidModelError, |
---|
| 291 | "CTriaxialEllipsoidModel.evalDistribution expects a list of dimension 2."); |
---|
| 292 | return NULL; |
---|
| 293 | } |
---|
| 294 | qx = PyList_GET_ITEM(pars,0); |
---|
| 295 | qy = PyList_GET_ITEM(pars,1); |
---|
| 296 | if (PyArray_Check(qx) && PyArray_Check(qy)) { |
---|
| 297 | return evaluateTwoDimXY(self->model, (PyArrayObject*)qx, |
---|
| 298 | (PyArrayObject*)qy); |
---|
| 299 | }else{ |
---|
| 300 | PyErr_SetString(CTriaxialEllipsoidModelError, |
---|
| 301 | "CTriaxialEllipsoidModel.evalDistribution expect 2 numpy arrays in list."); |
---|
| 302 | return NULL; |
---|
| 303 | } |
---|
| 304 | } |
---|
[e0a8a3c] | 305 | PyErr_SetString(CTriaxialEllipsoidModelError, |
---|
| 306 | "CTriaxialEllipsoidModel.evalDistribution couln't be run."); |
---|
| 307 | return NULL; |
---|
| 308 | |
---|
[9bd69098] | 309 | } |
---|
[5068697] | 310 | |
---|
| 311 | /** |
---|
| 312 | * Function to call to evaluate model |
---|
| 313 | * @param args: input q or [q,phi] |
---|
| 314 | * @return: function value |
---|
| 315 | */ |
---|
| 316 | static PyObject * run(CTriaxialEllipsoidModel *self, PyObject *args) { |
---|
| 317 | double q_value, phi_value; |
---|
| 318 | PyObject* pars; |
---|
| 319 | int npars; |
---|
| 320 | |
---|
| 321 | // Get parameters |
---|
| 322 | |
---|
| 323 | // Reader parameter dictionary |
---|
| 324 | self->model->scale = PyFloat_AsDouble( PyDict_GetItemString(self->params, "scale") ); |
---|
[975ec8e] | 325 | self->model->axis_psi = PyFloat_AsDouble( PyDict_GetItemString(self->params, "axis_psi") ); |
---|
[5068697] | 326 | self->model->axis_theta = PyFloat_AsDouble( PyDict_GetItemString(self->params, "axis_theta") ); |
---|
| 327 | self->model->semi_axisA = PyFloat_AsDouble( PyDict_GetItemString(self->params, "semi_axisA") ); |
---|
| 328 | self->model->semi_axisB = PyFloat_AsDouble( PyDict_GetItemString(self->params, "semi_axisB") ); |
---|
| 329 | self->model->semi_axisC = PyFloat_AsDouble( PyDict_GetItemString(self->params, "semi_axisC") ); |
---|
| 330 | self->model->axis_phi = PyFloat_AsDouble( PyDict_GetItemString(self->params, "axis_phi") ); |
---|
[13eb1c4] | 331 | self->model->sldSolv = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sldSolv") ); |
---|
[5068697] | 332 | self->model->background = PyFloat_AsDouble( PyDict_GetItemString(self->params, "background") ); |
---|
[13eb1c4] | 333 | self->model->sldEll = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sldEll") ); |
---|
[5068697] | 334 | // Read in dispersion parameters |
---|
| 335 | PyObject* disp_dict; |
---|
| 336 | DispersionVisitor* visitor = new DispersionVisitor(); |
---|
| 337 | disp_dict = PyDict_GetItemString(self->dispersion, "axis_theta"); |
---|
| 338 | self->model->axis_theta.dispersion->accept_as_destination(visitor, self->model->axis_theta.dispersion, disp_dict); |
---|
| 339 | disp_dict = PyDict_GetItemString(self->dispersion, "axis_phi"); |
---|
| 340 | self->model->axis_phi.dispersion->accept_as_destination(visitor, self->model->axis_phi.dispersion, disp_dict); |
---|
[975ec8e] | 341 | disp_dict = PyDict_GetItemString(self->dispersion, "axis_psi"); |
---|
| 342 | self->model->axis_psi.dispersion->accept_as_destination(visitor, self->model->axis_psi.dispersion, disp_dict); |
---|
[5068697] | 343 | |
---|
| 344 | |
---|
| 345 | // Get input and determine whether we have to supply a 1D or 2D return value. |
---|
| 346 | if ( !PyArg_ParseTuple(args,"O",&pars) ) { |
---|
| 347 | PyErr_SetString(CTriaxialEllipsoidModelError, |
---|
| 348 | "CTriaxialEllipsoidModel.run expects a q value."); |
---|
| 349 | return NULL; |
---|
| 350 | } |
---|
| 351 | |
---|
| 352 | // Check params |
---|
| 353 | if( PyList_Check(pars)==1) { |
---|
| 354 | |
---|
| 355 | // Length of list should be 2 for I(q,phi) |
---|
| 356 | npars = PyList_GET_SIZE(pars); |
---|
| 357 | if(npars!=2) { |
---|
| 358 | PyErr_SetString(CTriaxialEllipsoidModelError, |
---|
| 359 | "CTriaxialEllipsoidModel.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) |
---|
| 364 | q_value = CTriaxialEllipsoidModel_readDouble(PyList_GET_ITEM(pars,0)); |
---|
| 365 | phi_value = CTriaxialEllipsoidModel_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 | |
---|
| 372 | } else { |
---|
| 373 | |
---|
| 374 | // We have a scalar q, we will evaluate I(q) |
---|
| 375 | q_value = CTriaxialEllipsoidModel_readDouble(pars); |
---|
| 376 | |
---|
| 377 | return Py_BuildValue("d",(*(self->model))(q_value)); |
---|
| 378 | } |
---|
| 379 | } |
---|
[5eb9154] | 380 | /** |
---|
| 381 | * Function to call to calculate_ER |
---|
| 382 | * @return: effective radius value |
---|
| 383 | */ |
---|
| 384 | static PyObject * calculate_ER(CTriaxialEllipsoidModel *self) { |
---|
[5068697] | 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->axis_psi = PyFloat_AsDouble( PyDict_GetItemString(self->params, "axis_psi") ); |
---|
| 394 | self->model->axis_theta = PyFloat_AsDouble( PyDict_GetItemString(self->params, "axis_theta") ); |
---|
| 395 | self->model->semi_axisA = PyFloat_AsDouble( PyDict_GetItemString(self->params, "semi_axisA") ); |
---|
| 396 | self->model->semi_axisB = PyFloat_AsDouble( PyDict_GetItemString(self->params, "semi_axisB") ); |
---|
| 397 | self->model->semi_axisC = PyFloat_AsDouble( PyDict_GetItemString(self->params, "semi_axisC") ); |
---|
| 398 | self->model->axis_phi = PyFloat_AsDouble( PyDict_GetItemString(self->params, "axis_phi") ); |
---|
[13eb1c4] | 399 | self->model->sldSolv = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sldSolv") ); |
---|
[5eb9154] | 400 | self->model->background = PyFloat_AsDouble( PyDict_GetItemString(self->params, "background") ); |
---|
[13eb1c4] | 401 | self->model->sldEll = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sldEll") ); |
---|
[5eb9154] | 402 | // Read in dispersion parameters |
---|
| 403 | PyObject* disp_dict; |
---|
| 404 | DispersionVisitor* visitor = new DispersionVisitor(); |
---|
| 405 | disp_dict = PyDict_GetItemString(self->dispersion, "axis_theta"); |
---|
| 406 | self->model->axis_theta.dispersion->accept_as_destination(visitor, self->model->axis_theta.dispersion, disp_dict); |
---|
| 407 | disp_dict = PyDict_GetItemString(self->dispersion, "axis_phi"); |
---|
| 408 | self->model->axis_phi.dispersion->accept_as_destination(visitor, self->model->axis_phi.dispersion, disp_dict); |
---|
| 409 | disp_dict = PyDict_GetItemString(self->dispersion, "axis_psi"); |
---|
| 410 | self->model->axis_psi.dispersion->accept_as_destination(visitor, self->model->axis_psi.dispersion, disp_dict); |
---|
| 411 | |
---|
| 412 | |
---|
| 413 | return Py_BuildValue("d",(*(self->model)).calculate_ER()); |
---|
| 414 | |
---|
| 415 | } |
---|
[5068697] | 416 | /** |
---|
| 417 | * Function to call to evaluate model in cartesian coordinates |
---|
| 418 | * @param args: input q or [qx, qy]] |
---|
| 419 | * @return: function value |
---|
| 420 | */ |
---|
| 421 | static PyObject * runXY(CTriaxialEllipsoidModel *self, PyObject *args) { |
---|
| 422 | double qx_value, qy_value; |
---|
| 423 | PyObject* pars; |
---|
| 424 | int npars; |
---|
| 425 | |
---|
| 426 | // Get parameters |
---|
| 427 | |
---|
| 428 | // Reader parameter dictionary |
---|
| 429 | self->model->scale = PyFloat_AsDouble( PyDict_GetItemString(self->params, "scale") ); |
---|
[975ec8e] | 430 | self->model->axis_psi = PyFloat_AsDouble( PyDict_GetItemString(self->params, "axis_psi") ); |
---|
[5068697] | 431 | self->model->axis_theta = PyFloat_AsDouble( PyDict_GetItemString(self->params, "axis_theta") ); |
---|
| 432 | self->model->semi_axisA = PyFloat_AsDouble( PyDict_GetItemString(self->params, "semi_axisA") ); |
---|
| 433 | self->model->semi_axisB = PyFloat_AsDouble( PyDict_GetItemString(self->params, "semi_axisB") ); |
---|
| 434 | self->model->semi_axisC = PyFloat_AsDouble( PyDict_GetItemString(self->params, "semi_axisC") ); |
---|
| 435 | self->model->axis_phi = PyFloat_AsDouble( PyDict_GetItemString(self->params, "axis_phi") ); |
---|
[13eb1c4] | 436 | self->model->sldSolv = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sldSolv") ); |
---|
[5068697] | 437 | self->model->background = PyFloat_AsDouble( PyDict_GetItemString(self->params, "background") ); |
---|
[13eb1c4] | 438 | self->model->sldEll = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sldEll") ); |
---|
[5068697] | 439 | // Read in dispersion parameters |
---|
| 440 | PyObject* disp_dict; |
---|
| 441 | DispersionVisitor* visitor = new DispersionVisitor(); |
---|
| 442 | disp_dict = PyDict_GetItemString(self->dispersion, "axis_theta"); |
---|
| 443 | self->model->axis_theta.dispersion->accept_as_destination(visitor, self->model->axis_theta.dispersion, disp_dict); |
---|
| 444 | disp_dict = PyDict_GetItemString(self->dispersion, "axis_phi"); |
---|
| 445 | self->model->axis_phi.dispersion->accept_as_destination(visitor, self->model->axis_phi.dispersion, disp_dict); |
---|
[975ec8e] | 446 | disp_dict = PyDict_GetItemString(self->dispersion, "axis_psi"); |
---|
| 447 | self->model->axis_psi.dispersion->accept_as_destination(visitor, self->model->axis_psi.dispersion, disp_dict); |
---|
[5068697] | 448 | |
---|
| 449 | |
---|
| 450 | // Get input and determine whether we have to supply a 1D or 2D return value. |
---|
| 451 | if ( !PyArg_ParseTuple(args,"O",&pars) ) { |
---|
| 452 | PyErr_SetString(CTriaxialEllipsoidModelError, |
---|
| 453 | "CTriaxialEllipsoidModel.run expects a q value."); |
---|
| 454 | return NULL; |
---|
| 455 | } |
---|
| 456 | |
---|
| 457 | // Check params |
---|
| 458 | if( PyList_Check(pars)==1) { |
---|
| 459 | |
---|
| 460 | // Length of list should be 2 for I(qx, qy)) |
---|
| 461 | npars = PyList_GET_SIZE(pars); |
---|
| 462 | if(npars!=2) { |
---|
| 463 | PyErr_SetString(CTriaxialEllipsoidModelError, |
---|
| 464 | "CTriaxialEllipsoidModel.run expects a double or a list of dimension 2."); |
---|
| 465 | return NULL; |
---|
| 466 | } |
---|
| 467 | // We have a vector q, get the qx and qy values at which |
---|
| 468 | // to evaluate I(qx,qy) |
---|
| 469 | qx_value = CTriaxialEllipsoidModel_readDouble(PyList_GET_ITEM(pars,0)); |
---|
| 470 | qy_value = CTriaxialEllipsoidModel_readDouble(PyList_GET_ITEM(pars,1)); |
---|
| 471 | return Py_BuildValue("d",(*(self->model))(qx_value,qy_value)); |
---|
| 472 | |
---|
| 473 | } else { |
---|
| 474 | |
---|
| 475 | // We have a scalar q, we will evaluate I(q) |
---|
| 476 | qx_value = CTriaxialEllipsoidModel_readDouble(pars); |
---|
| 477 | |
---|
| 478 | return Py_BuildValue("d",(*(self->model))(qx_value)); |
---|
| 479 | } |
---|
| 480 | } |
---|
| 481 | |
---|
| 482 | static PyObject * reset(CTriaxialEllipsoidModel *self, PyObject *args) { |
---|
| 483 | |
---|
| 484 | |
---|
| 485 | return Py_BuildValue("d",0.0); |
---|
| 486 | } |
---|
| 487 | |
---|
| 488 | static PyObject * set_dispersion(CTriaxialEllipsoidModel *self, PyObject *args) { |
---|
| 489 | PyObject * disp; |
---|
| 490 | const char * par_name; |
---|
| 491 | |
---|
| 492 | if ( !PyArg_ParseTuple(args,"sO", &par_name, &disp) ) { |
---|
| 493 | PyErr_SetString(CTriaxialEllipsoidModelError, |
---|
| 494 | "CTriaxialEllipsoidModel.set_dispersion expects a DispersionModel object."); |
---|
| 495 | return NULL; |
---|
| 496 | } |
---|
| 497 | void *temp = PyCObject_AsVoidPtr(disp); |
---|
| 498 | DispersionModel * dispersion = static_cast<DispersionModel *>(temp); |
---|
| 499 | |
---|
| 500 | |
---|
| 501 | // Ugliness necessary to go from python to C |
---|
| 502 | // TODO: refactor this |
---|
| 503 | if (!strcmp(par_name, "axis_theta")) { |
---|
| 504 | self->model->axis_theta.dispersion = dispersion; |
---|
| 505 | } else if (!strcmp(par_name, "axis_phi")) { |
---|
| 506 | self->model->axis_phi.dispersion = dispersion; |
---|
[975ec8e] | 507 | } else if (!strcmp(par_name, "axis_psi")) { |
---|
| 508 | self->model->axis_psi.dispersion = dispersion; |
---|
[5068697] | 509 | } else { |
---|
| 510 | PyErr_SetString(CTriaxialEllipsoidModelError, |
---|
| 511 | "CTriaxialEllipsoidModel.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 | |
---|
| 522 | |
---|
| 523 | static PyMethodDef CTriaxialEllipsoidModel_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 "}, |
---|
[5068697] | 533 | {"reset", (PyCFunction)reset , METH_VARARGS, |
---|
| 534 | "Reset pair correlation"}, |
---|
| 535 | {"set_dispersion", (PyCFunction)set_dispersion , METH_VARARGS, |
---|
| 536 | "Set the dispersion model for a given parameter"}, |
---|
| 537 | {NULL} |
---|
| 538 | }; |
---|
| 539 | |
---|
| 540 | static PyTypeObject CTriaxialEllipsoidModelType = { |
---|
| 541 | PyObject_HEAD_INIT(NULL) |
---|
| 542 | 0, /*ob_size*/ |
---|
| 543 | "CTriaxialEllipsoidModel", /*tp_name*/ |
---|
| 544 | sizeof(CTriaxialEllipsoidModel), /*tp_basicsize*/ |
---|
| 545 | 0, /*tp_itemsize*/ |
---|
| 546 | (destructor)CTriaxialEllipsoidModel_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 | "CTriaxialEllipsoidModel 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 | CTriaxialEllipsoidModel_methods, /* tp_methods */ |
---|
| 570 | CTriaxialEllipsoidModel_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)CTriaxialEllipsoidModel_init, /* tp_init */ |
---|
| 578 | 0, /* tp_alloc */ |
---|
| 579 | CTriaxialEllipsoidModel_new, /* tp_new */ |
---|
| 580 | }; |
---|
| 581 | |
---|
| 582 | |
---|
[9bd69098] | 583 | //static PyMethodDef module_methods[] = { |
---|
| 584 | // {NULL} |
---|
| 585 | //}; |
---|
[5068697] | 586 | |
---|
| 587 | /** |
---|
| 588 | * Function used to add the model class to a module |
---|
| 589 | * @param module: module to add the class to |
---|
| 590 | */ |
---|
| 591 | void addCTriaxialEllipsoidModel(PyObject *module) { |
---|
| 592 | PyObject *d; |
---|
| 593 | |
---|
| 594 | if (PyType_Ready(&CTriaxialEllipsoidModelType) < 0) |
---|
| 595 | return; |
---|
| 596 | |
---|
| 597 | Py_INCREF(&CTriaxialEllipsoidModelType); |
---|
| 598 | PyModule_AddObject(module, "CTriaxialEllipsoidModel", (PyObject *)&CTriaxialEllipsoidModelType); |
---|
| 599 | |
---|
| 600 | d = PyModule_GetDict(module); |
---|
| 601 | CTriaxialEllipsoidModelError = PyErr_NewException("CTriaxialEllipsoidModel.error", NULL, NULL); |
---|
| 602 | PyDict_SetItemString(d, "CTriaxialEllipsoidModelError", CTriaxialEllipsoidModelError); |
---|
| 603 | } |
---|
| 604 | |
---|