[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 | /** CHardsphereStructure |
---|
| 16 | * |
---|
| 17 | * C extension |
---|
| 18 | * |
---|
| 19 | * WARNING: THIS FILE WAS GENERATED BY WRAPPERGENERATOR.PY |
---|
[9ce41c6] | 20 | * DO NOT MODIFY THIS FILE, MODIFY Hardsphere.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 "Hardsphere.h" |
---|
[25579e8] | 36 | } |
---|
| 37 | |
---|
| 38 | #include "models.hh" |
---|
| 39 | #include "dispersion_visitor.hh" |
---|
| 40 | |
---|
| 41 | /// Error object for raised exceptions |
---|
| 42 | static PyObject * CHardsphereStructureError = 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 | HardsphereStructure * model; |
---|
| 54 | /// Log for unit testing |
---|
| 55 | PyObject * log; |
---|
| 56 | } CHardsphereStructure; |
---|
| 57 | |
---|
| 58 | |
---|
| 59 | static void |
---|
| 60 | CHardsphereStructure_dealloc(CHardsphereStructure* 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); |
---|
[b1c3295] | 67 | |
---|
[25579e8] | 68 | |
---|
| 69 | } |
---|
| 70 | |
---|
| 71 | static PyObject * |
---|
| 72 | CHardsphereStructure_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
---|
| 73 | { |
---|
| 74 | CHardsphereStructure *self; |
---|
| 75 | |
---|
| 76 | self = (CHardsphereStructure *)type->tp_alloc(type, 0); |
---|
| 77 | |
---|
| 78 | return (PyObject *)self; |
---|
| 79 | } |
---|
| 80 | |
---|
| 81 | static int |
---|
| 82 | CHardsphereStructure_init(CHardsphereStructure *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 HardsphereStructure(); |
---|
| 90 | |
---|
[b1c3295] | 91 | // Initialize parameter dictionary |
---|
| 92 | PyDict_SetItemString(self->params,"effect_radius",Py_BuildValue("d",50.000000000000)); |
---|
| 93 | PyDict_SetItemString(self->params,"volfraction",Py_BuildValue("d",0.200000000000)); |
---|
| 94 | // Initialize dispersion / averaging parameter dict |
---|
| 95 | DispersionVisitor* visitor = new DispersionVisitor(); |
---|
| 96 | PyObject * disp_dict; |
---|
| 97 | disp_dict = PyDict_New(); |
---|
| 98 | self->model->effect_radius.dispersion->accept_as_source(visitor, self->model->effect_radius.dispersion, disp_dict); |
---|
| 99 | PyDict_SetItemString(self->dispersion, "effect_radius", disp_dict); |
---|
| 100 | |
---|
[25579e8] | 101 | |
---|
| 102 | |
---|
| 103 | // Create empty log |
---|
| 104 | self->log = PyDict_New(); |
---|
| 105 | |
---|
[b1c3295] | 106 | |
---|
[25579e8] | 107 | |
---|
| 108 | } |
---|
| 109 | return 0; |
---|
| 110 | } |
---|
| 111 | |
---|
[b1c3295] | 112 | static char name_params[] = "params"; |
---|
| 113 | static char def_params[] = "Parameters"; |
---|
| 114 | static char name_dispersion[] = "dispersion"; |
---|
| 115 | static char def_dispersion[] = "Dispersion parameters"; |
---|
| 116 | static char name_log[] = "log"; |
---|
| 117 | static char def_log[] = "Log"; |
---|
| 118 | |
---|
[25579e8] | 119 | static PyMemberDef CHardsphereStructure_members[] = { |
---|
[b1c3295] | 120 | {name_params, T_OBJECT, offsetof(CHardsphereStructure, params), 0, def_params}, |
---|
| 121 | {name_dispersion, T_OBJECT, offsetof(CHardsphereStructure, dispersion), 0, def_dispersion}, |
---|
| 122 | {name_log, T_OBJECT, offsetof(CHardsphereStructure, log), 0, def_log}, |
---|
[25579e8] | 123 | {NULL} /* Sentinel */ |
---|
| 124 | }; |
---|
| 125 | |
---|
| 126 | /** Read double from PyObject |
---|
| 127 | @param p PyObject |
---|
| 128 | @return double |
---|
| 129 | */ |
---|
| 130 | double CHardsphereStructure_readDouble(PyObject *p) { |
---|
| 131 | if (PyFloat_Check(p)==1) { |
---|
| 132 | return (double)(((PyFloatObject *)(p))->ob_fval); |
---|
| 133 | } else if (PyInt_Check(p)==1) { |
---|
| 134 | return (double)(((PyIntObject *)(p))->ob_ival); |
---|
| 135 | } else if (PyLong_Check(p)==1) { |
---|
| 136 | return (double)PyLong_AsLong(p); |
---|
| 137 | } else { |
---|
| 138 | return 0.0; |
---|
| 139 | } |
---|
| 140 | } |
---|
[9bd69098] | 141 | /** |
---|
| 142 | * Function to call to evaluate model |
---|
| 143 | * @param args: input numpy array q[] |
---|
| 144 | * @return: numpy array object |
---|
| 145 | */ |
---|
| 146 | |
---|
| 147 | static PyObject *evaluateOneDim(HardsphereStructure* model, PyArrayObject *q){ |
---|
| 148 | PyArrayObject *result; |
---|
| 149 | |
---|
| 150 | // Check validity of array q , q must be of dimension 1, an array of double |
---|
| 151 | if (q->nd != 1 || q->descr->type_num != PyArray_DOUBLE) |
---|
| 152 | { |
---|
| 153 | //const char * message= "Invalid array: q->nd=%d,type_num=%d\n",q->nd,q->descr->type_num; |
---|
| 154 | //PyErr_SetString(PyExc_ValueError , message); |
---|
| 155 | return NULL; |
---|
| 156 | } |
---|
| 157 | result = (PyArrayObject *)PyArray_FromDims(q->nd, (int *)(q->dimensions), |
---|
| 158 | PyArray_DOUBLE); |
---|
| 159 | if (result == NULL) { |
---|
| 160 | const char * message= "Could not create result "; |
---|
| 161 | PyErr_SetString(PyExc_RuntimeError , message); |
---|
| 162 | return NULL; |
---|
| 163 | } |
---|
| 164 | for (int i = 0; i < q->dimensions[0]; i++){ |
---|
| 165 | double q_value = *(double *)(q->data + i*q->strides[0]); |
---|
| 166 | double *result_value = (double *)(result->data + i*result->strides[0]); |
---|
| 167 | *result_value =(*model)(q_value); |
---|
| 168 | } |
---|
| 169 | return PyArray_Return(result); |
---|
| 170 | } |
---|
[25579e8] | 171 | |
---|
[9bd69098] | 172 | /** |
---|
| 173 | * Function to call to evaluate model |
---|
| 174 | * @param args: input numpy array [x[],y[]] |
---|
| 175 | * @return: numpy array object |
---|
| 176 | */ |
---|
| 177 | static PyObject * evaluateTwoDimXY( HardsphereStructure* model, |
---|
| 178 | PyArrayObject *x, PyArrayObject *y) |
---|
| 179 | { |
---|
| 180 | PyArrayObject *result; |
---|
[00c2141] | 181 | int i, x_len, y_len, dims[1]; |
---|
[9bd69098] | 182 | //check validity of input vectors |
---|
[3080527] | 183 | if (x->nd != 1 || x->descr->type_num != PyArray_DOUBLE |
---|
| 184 | || y->nd != 1 || y->descr->type_num != PyArray_DOUBLE |
---|
| 185 | || y->dimensions[0] != x->dimensions[0]){ |
---|
[9bd69098] | 186 | const char * message= "evaluateTwoDimXY expect 2 numpy arrays"; |
---|
| 187 | PyErr_SetString(PyExc_ValueError , message); |
---|
| 188 | return NULL; |
---|
| 189 | } |
---|
| 190 | |
---|
| 191 | if (PyArray_Check(x) && PyArray_Check(y)) { |
---|
[a8d6888] | 192 | |
---|
[3080527] | 193 | x_len = dims[0]= x->dimensions[0]; |
---|
[b1c3295] | 194 | y_len = dims[0]= y->dimensions[0]; |
---|
[9bd69098] | 195 | |
---|
| 196 | // Make a new double matrix of same dims |
---|
[3080527] | 197 | result=(PyArrayObject *) PyArray_FromDims(1,dims,NPY_DOUBLE); |
---|
[9bd69098] | 198 | if (result == NULL){ |
---|
| 199 | const char * message= "Could not create result "; |
---|
| 200 | PyErr_SetString(PyExc_RuntimeError , message); |
---|
| 201 | return NULL; |
---|
| 202 | } |
---|
| 203 | |
---|
| 204 | /* Do the calculation. */ |
---|
[3080527] | 205 | for ( i=0; i< x_len; i++) { |
---|
| 206 | double x_value = *(double *)(x->data + i*x->strides[0]); |
---|
| 207 | double y_value = *(double *)(y->data + i*y->strides[0]); |
---|
| 208 | double *result_value = (double *)(result->data + |
---|
| 209 | i*result->strides[0]); |
---|
| 210 | *result_value = (*model)(x_value, y_value); |
---|
| 211 | } |
---|
[9bd69098] | 212 | return PyArray_Return(result); |
---|
| 213 | |
---|
| 214 | }else{ |
---|
| 215 | PyErr_SetString(CHardsphereStructureError, |
---|
| 216 | "CHardsphereStructure.evaluateTwoDimXY couldn't run."); |
---|
| 217 | return NULL; |
---|
| 218 | } |
---|
| 219 | } |
---|
| 220 | /** |
---|
| 221 | * evalDistribution function evaluate a model function with input vector |
---|
| 222 | * @param args: input q as vector or [qx, qy] where qx, qy are vectors |
---|
| 223 | * |
---|
| 224 | */ |
---|
| 225 | static PyObject * evalDistribution(CHardsphereStructure *self, PyObject *args){ |
---|
| 226 | PyObject *qx, *qy; |
---|
| 227 | PyArrayObject * pars; |
---|
| 228 | int npars ,mpars; |
---|
| 229 | |
---|
| 230 | // Get parameters |
---|
| 231 | |
---|
[b1c3295] | 232 | // Reader parameter dictionary |
---|
| 233 | self->model->effect_radius = PyFloat_AsDouble( PyDict_GetItemString(self->params, "effect_radius") ); |
---|
| 234 | self->model->volfraction = PyFloat_AsDouble( PyDict_GetItemString(self->params, "volfraction") ); |
---|
| 235 | // Read in dispersion parameters |
---|
| 236 | PyObject* disp_dict; |
---|
| 237 | DispersionVisitor* visitor = new DispersionVisitor(); |
---|
| 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(CHardsphereStructureError, |
---|
| 245 | "CHardsphereStructure.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(CHardsphereStructureError, |
---|
| 261 | "CHardsphereStructure.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(CHardsphereStructureError, |
---|
| 269 | "CHardsphereStructure.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(CHardsphereStructureError, |
---|
| 279 | "CHardsphereStructure.evalDistribution expect 2 numpy arrays in list."); |
---|
| 280 | return NULL; |
---|
| 281 | } |
---|
| 282 | } |
---|
[e0a8a3c] | 283 | PyErr_SetString(CHardsphereStructureError, |
---|
| 284 | "CHardsphereStructure.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(CHardsphereStructure *self, PyObject *args) { |
---|
| 295 | double q_value, phi_value; |
---|
| 296 | PyObject* pars; |
---|
| 297 | int npars; |
---|
| 298 | |
---|
| 299 | // Get parameters |
---|
| 300 | |
---|
[b1c3295] | 301 | // Reader parameter dictionary |
---|
| 302 | self->model->effect_radius = PyFloat_AsDouble( PyDict_GetItemString(self->params, "effect_radius") ); |
---|
| 303 | self->model->volfraction = PyFloat_AsDouble( PyDict_GetItemString(self->params, "volfraction") ); |
---|
| 304 | // Read in dispersion parameters |
---|
| 305 | PyObject* disp_dict; |
---|
| 306 | DispersionVisitor* visitor = new DispersionVisitor(); |
---|
| 307 | disp_dict = PyDict_GetItemString(self->dispersion, "effect_radius"); |
---|
| 308 | self->model->effect_radius.dispersion->accept_as_destination(visitor, self->model->effect_radius.dispersion, disp_dict); |
---|
[25579e8] | 309 | |
---|
| 310 | |
---|
| 311 | // Get input and determine whether we have to supply a 1D or 2D return value. |
---|
| 312 | if ( !PyArg_ParseTuple(args,"O",&pars) ) { |
---|
| 313 | PyErr_SetString(CHardsphereStructureError, |
---|
| 314 | "CHardsphereStructure.run expects a q value."); |
---|
| 315 | return NULL; |
---|
| 316 | } |
---|
| 317 | |
---|
| 318 | // Check params |
---|
| 319 | if( PyList_Check(pars)==1) { |
---|
| 320 | |
---|
| 321 | // Length of list should be 2 for I(q,phi) |
---|
| 322 | npars = PyList_GET_SIZE(pars); |
---|
| 323 | if(npars!=2) { |
---|
| 324 | PyErr_SetString(CHardsphereStructureError, |
---|
| 325 | "CHardsphereStructure.run expects a double or a list of dimension 2."); |
---|
| 326 | return NULL; |
---|
| 327 | } |
---|
| 328 | // We have a vector q, get the q and phi values at which |
---|
| 329 | // to evaluate I(q,phi) |
---|
| 330 | q_value = CHardsphereStructure_readDouble(PyList_GET_ITEM(pars,0)); |
---|
| 331 | phi_value = CHardsphereStructure_readDouble(PyList_GET_ITEM(pars,1)); |
---|
| 332 | // Skip zero |
---|
| 333 | if (q_value==0) { |
---|
| 334 | return Py_BuildValue("d",0.0); |
---|
| 335 | } |
---|
| 336 | return Py_BuildValue("d",(*(self->model)).evaluate_rphi(q_value,phi_value)); |
---|
| 337 | |
---|
| 338 | } else { |
---|
| 339 | |
---|
| 340 | // We have a scalar q, we will evaluate I(q) |
---|
| 341 | q_value = CHardsphereStructure_readDouble(pars); |
---|
| 342 | |
---|
| 343 | return Py_BuildValue("d",(*(self->model))(q_value)); |
---|
| 344 | } |
---|
| 345 | } |
---|
[5eb9154] | 346 | /** |
---|
| 347 | * Function to call to calculate_ER |
---|
| 348 | * @return: effective radius value |
---|
| 349 | */ |
---|
| 350 | static PyObject * calculate_ER(CHardsphereStructure *self) { |
---|
[25579e8] | 351 | |
---|
[5eb9154] | 352 | // Get parameters |
---|
| 353 | |
---|
[b1c3295] | 354 | // Reader parameter dictionary |
---|
| 355 | self->model->effect_radius = PyFloat_AsDouble( PyDict_GetItemString(self->params, "effect_radius") ); |
---|
| 356 | self->model->volfraction = PyFloat_AsDouble( PyDict_GetItemString(self->params, "volfraction") ); |
---|
| 357 | // Read in dispersion parameters |
---|
| 358 | PyObject* disp_dict; |
---|
| 359 | DispersionVisitor* visitor = new DispersionVisitor(); |
---|
| 360 | disp_dict = PyDict_GetItemString(self->dispersion, "effect_radius"); |
---|
| 361 | self->model->effect_radius.dispersion->accept_as_destination(visitor, self->model->effect_radius.dispersion, disp_dict); |
---|
[5eb9154] | 362 | |
---|
| 363 | |
---|
| 364 | return Py_BuildValue("d",(*(self->model)).calculate_ER()); |
---|
| 365 | |
---|
| 366 | } |
---|
[25579e8] | 367 | /** |
---|
| 368 | * Function to call to evaluate model in cartesian coordinates |
---|
| 369 | * @param args: input q or [qx, qy]] |
---|
| 370 | * @return: function value |
---|
| 371 | */ |
---|
| 372 | static PyObject * runXY(CHardsphereStructure *self, PyObject *args) { |
---|
| 373 | double qx_value, qy_value; |
---|
| 374 | PyObject* pars; |
---|
| 375 | int npars; |
---|
| 376 | |
---|
| 377 | // Get parameters |
---|
| 378 | |
---|
[b1c3295] | 379 | // Reader parameter dictionary |
---|
| 380 | self->model->effect_radius = PyFloat_AsDouble( PyDict_GetItemString(self->params, "effect_radius") ); |
---|
| 381 | self->model->volfraction = PyFloat_AsDouble( PyDict_GetItemString(self->params, "volfraction") ); |
---|
| 382 | // Read in dispersion parameters |
---|
| 383 | PyObject* disp_dict; |
---|
| 384 | DispersionVisitor* visitor = new DispersionVisitor(); |
---|
| 385 | disp_dict = PyDict_GetItemString(self->dispersion, "effect_radius"); |
---|
| 386 | self->model->effect_radius.dispersion->accept_as_destination(visitor, self->model->effect_radius.dispersion, disp_dict); |
---|
[25579e8] | 387 | |
---|
| 388 | |
---|
| 389 | // Get input and determine whether we have to supply a 1D or 2D return value. |
---|
| 390 | if ( !PyArg_ParseTuple(args,"O",&pars) ) { |
---|
| 391 | PyErr_SetString(CHardsphereStructureError, |
---|
| 392 | "CHardsphereStructure.run expects a q value."); |
---|
| 393 | return NULL; |
---|
| 394 | } |
---|
| 395 | |
---|
| 396 | // Check params |
---|
| 397 | if( PyList_Check(pars)==1) { |
---|
| 398 | |
---|
| 399 | // Length of list should be 2 for I(qx, qy)) |
---|
| 400 | npars = PyList_GET_SIZE(pars); |
---|
| 401 | if(npars!=2) { |
---|
| 402 | PyErr_SetString(CHardsphereStructureError, |
---|
| 403 | "CHardsphereStructure.run expects a double or a list of dimension 2."); |
---|
| 404 | return NULL; |
---|
| 405 | } |
---|
| 406 | // We have a vector q, get the qx and qy values at which |
---|
| 407 | // to evaluate I(qx,qy) |
---|
| 408 | qx_value = CHardsphereStructure_readDouble(PyList_GET_ITEM(pars,0)); |
---|
| 409 | qy_value = CHardsphereStructure_readDouble(PyList_GET_ITEM(pars,1)); |
---|
| 410 | return Py_BuildValue("d",(*(self->model))(qx_value,qy_value)); |
---|
| 411 | |
---|
| 412 | } else { |
---|
| 413 | |
---|
| 414 | // We have a scalar q, we will evaluate I(q) |
---|
| 415 | qx_value = CHardsphereStructure_readDouble(pars); |
---|
| 416 | |
---|
| 417 | return Py_BuildValue("d",(*(self->model))(qx_value)); |
---|
| 418 | } |
---|
| 419 | } |
---|
| 420 | |
---|
| 421 | static PyObject * reset(CHardsphereStructure *self, PyObject *args) { |
---|
[b1c3295] | 422 | |
---|
[25579e8] | 423 | |
---|
| 424 | return Py_BuildValue("d",0.0); |
---|
| 425 | } |
---|
| 426 | |
---|
| 427 | static PyObject * set_dispersion(CHardsphereStructure *self, PyObject *args) { |
---|
| 428 | PyObject * disp; |
---|
| 429 | const char * par_name; |
---|
| 430 | |
---|
| 431 | if ( !PyArg_ParseTuple(args,"sO", &par_name, &disp) ) { |
---|
| 432 | PyErr_SetString(CHardsphereStructureError, |
---|
| 433 | "CHardsphereStructure.set_dispersion expects a DispersionModel object."); |
---|
| 434 | return NULL; |
---|
| 435 | } |
---|
| 436 | void *temp = PyCObject_AsVoidPtr(disp); |
---|
| 437 | DispersionModel * dispersion = static_cast<DispersionModel *>(temp); |
---|
| 438 | |
---|
| 439 | |
---|
| 440 | // Ugliness necessary to go from python to C |
---|
[b1c3295] | 441 | // TODO: refactor this |
---|
| 442 | if (!strcmp(par_name, "effect_radius")) { |
---|
| 443 | self->model->effect_radius.dispersion = dispersion; |
---|
[25579e8] | 444 | } else { |
---|
| 445 | PyErr_SetString(CHardsphereStructureError, |
---|
| 446 | "CHardsphereStructure.set_dispersion expects a valid parameter name."); |
---|
| 447 | return NULL; |
---|
| 448 | } |
---|
| 449 | |
---|
| 450 | DispersionVisitor* visitor = new DispersionVisitor(); |
---|
| 451 | PyObject * disp_dict = PyDict_New(); |
---|
| 452 | dispersion->accept_as_source(visitor, dispersion, disp_dict); |
---|
| 453 | PyDict_SetItemString(self->dispersion, par_name, disp_dict); |
---|
| 454 | return Py_BuildValue("i",1); |
---|
| 455 | } |
---|
| 456 | |
---|
| 457 | |
---|
| 458 | static PyMethodDef CHardsphereStructure_methods[] = { |
---|
| 459 | {"run", (PyCFunction)run , METH_VARARGS, |
---|
| 460 | "Evaluate the model at a given Q or Q, phi"}, |
---|
| 461 | {"runXY", (PyCFunction)runXY , METH_VARARGS, |
---|
| 462 | "Evaluate the model at a given Q or Qx, Qy"}, |
---|
[5eb9154] | 463 | {"calculate_ER", (PyCFunction)calculate_ER , METH_VARARGS, |
---|
| 464 | "Evaluate the model at a given Q or Q, phi"}, |
---|
[9bd69098] | 465 | |
---|
| 466 | {"evalDistribution", (PyCFunction)evalDistribution , METH_VARARGS, |
---|
| 467 | "Evaluate the model at a given Q or Qx, Qy vector "}, |
---|
[25579e8] | 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 | |
---|
| 475 | static PyTypeObject CHardsphereStructureType = { |
---|
| 476 | PyObject_HEAD_INIT(NULL) |
---|
| 477 | 0, /*ob_size*/ |
---|
| 478 | "CHardsphereStructure", /*tp_name*/ |
---|
| 479 | sizeof(CHardsphereStructure), /*tp_basicsize*/ |
---|
| 480 | 0, /*tp_itemsize*/ |
---|
| 481 | (destructor)CHardsphereStructure_dealloc, /*tp_dealloc*/ |
---|
| 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*/ |
---|
| 497 | "CHardsphereStructure objects", /* tp_doc */ |
---|
| 498 | 0, /* tp_traverse */ |
---|
| 499 | 0, /* tp_clear */ |
---|
| 500 | 0, /* tp_richcompare */ |
---|
| 501 | 0, /* tp_weaklistoffset */ |
---|
| 502 | 0, /* tp_iter */ |
---|
| 503 | 0, /* tp_iternext */ |
---|
| 504 | CHardsphereStructure_methods, /* tp_methods */ |
---|
| 505 | CHardsphereStructure_members, /* tp_members */ |
---|
| 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 */ |
---|
| 512 | (initproc)CHardsphereStructure_init, /* tp_init */ |
---|
| 513 | 0, /* tp_alloc */ |
---|
| 514 | CHardsphereStructure_new, /* tp_new */ |
---|
| 515 | }; |
---|
| 516 | |
---|
| 517 | |
---|
[9bd69098] | 518 | //static PyMethodDef module_methods[] = { |
---|
| 519 | // {NULL} |
---|
| 520 | //}; |
---|
[25579e8] | 521 | |
---|
| 522 | /** |
---|
| 523 | * Function used to add the model class to a module |
---|
| 524 | * @param module: module to add the class to |
---|
| 525 | */ |
---|
| 526 | void addCHardsphereStructure(PyObject *module) { |
---|
| 527 | PyObject *d; |
---|
| 528 | |
---|
| 529 | if (PyType_Ready(&CHardsphereStructureType) < 0) |
---|
| 530 | return; |
---|
| 531 | |
---|
| 532 | Py_INCREF(&CHardsphereStructureType); |
---|
| 533 | PyModule_AddObject(module, "CHardsphereStructure", (PyObject *)&CHardsphereStructureType); |
---|
| 534 | |
---|
| 535 | d = PyModule_GetDict(module); |
---|
[2605da22] | 536 | static char error_name[] = "CHardsphereStructure.error"; |
---|
| 537 | CHardsphereStructureError = PyErr_NewException(error_name, NULL, NULL); |
---|
[25579e8] | 538 | PyDict_SetItemString(d, "CHardsphereStructureError", CHardsphereStructureError); |
---|
| 539 | } |
---|
[b1c3295] | 540 | |
---|