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