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