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