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