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