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