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