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