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