[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 |
---|
| 20 | * DO NOT MODIFY THIS FILE, MODIFY Hardsphere.h |
---|
| 21 | * AND RE-RUN THE GENERATOR SCRIPT |
---|
| 22 | * |
---|
| 23 | */ |
---|
| 24 | |
---|
| 25 | extern "C" { |
---|
| 26 | #include <Python.h> |
---|
| 27 | #include "structmember.h" |
---|
| 28 | #include <stdio.h> |
---|
| 29 | #include <stdlib.h> |
---|
| 30 | #include <math.h> |
---|
| 31 | #include <time.h> |
---|
| 32 | #include "Hardsphere.h" |
---|
| 33 | } |
---|
| 34 | |
---|
| 35 | #include "models.hh" |
---|
| 36 | #include "dispersion_visitor.hh" |
---|
| 37 | |
---|
| 38 | /// Error object for raised exceptions |
---|
| 39 | static PyObject * CHardsphereStructureError = NULL; |
---|
| 40 | |
---|
| 41 | |
---|
| 42 | // Class definition |
---|
| 43 | typedef struct { |
---|
| 44 | PyObject_HEAD |
---|
| 45 | /// Parameters |
---|
| 46 | PyObject * params; |
---|
| 47 | /// Dispersion parameters |
---|
| 48 | PyObject * dispersion; |
---|
| 49 | /// Underlying model object |
---|
| 50 | HardsphereStructure * model; |
---|
| 51 | /// Log for unit testing |
---|
| 52 | PyObject * log; |
---|
| 53 | } CHardsphereStructure; |
---|
| 54 | |
---|
| 55 | |
---|
| 56 | static void |
---|
| 57 | CHardsphereStructure_dealloc(CHardsphereStructure* self) |
---|
| 58 | { |
---|
| 59 | self->ob_type->tp_free((PyObject*)self); |
---|
| 60 | |
---|
| 61 | |
---|
| 62 | } |
---|
| 63 | |
---|
| 64 | static PyObject * |
---|
| 65 | CHardsphereStructure_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
---|
| 66 | { |
---|
| 67 | CHardsphereStructure *self; |
---|
| 68 | |
---|
| 69 | self = (CHardsphereStructure *)type->tp_alloc(type, 0); |
---|
| 70 | |
---|
| 71 | return (PyObject *)self; |
---|
| 72 | } |
---|
| 73 | |
---|
| 74 | static int |
---|
| 75 | CHardsphereStructure_init(CHardsphereStructure *self, PyObject *args, PyObject *kwds) |
---|
| 76 | { |
---|
| 77 | if (self != NULL) { |
---|
| 78 | |
---|
| 79 | // Create parameters |
---|
| 80 | self->params = PyDict_New(); |
---|
| 81 | self->dispersion = PyDict_New(); |
---|
| 82 | self->model = new HardsphereStructure(); |
---|
| 83 | |
---|
| 84 | // Initialize parameter dictionary |
---|
| 85 | PyDict_SetItemString(self->params,"radius",Py_BuildValue("d",50.000000)); |
---|
| 86 | PyDict_SetItemString(self->params,"volfraction",Py_BuildValue("d",0.200000)); |
---|
| 87 | // Initialize dispersion / averaging parameter dict |
---|
| 88 | DispersionVisitor* visitor = new DispersionVisitor(); |
---|
| 89 | PyObject * disp_dict; |
---|
| 90 | disp_dict = PyDict_New(); |
---|
| 91 | self->model->radius.dispersion->accept_as_source(visitor, self->model->radius.dispersion, disp_dict); |
---|
| 92 | PyDict_SetItemString(self->dispersion, "radius", disp_dict); |
---|
| 93 | |
---|
| 94 | |
---|
| 95 | |
---|
| 96 | // Create empty log |
---|
| 97 | self->log = PyDict_New(); |
---|
| 98 | |
---|
| 99 | |
---|
| 100 | |
---|
| 101 | } |
---|
| 102 | return 0; |
---|
| 103 | } |
---|
| 104 | |
---|
| 105 | static PyMemberDef CHardsphereStructure_members[] = { |
---|
| 106 | {"params", T_OBJECT, offsetof(CHardsphereStructure, params), 0, |
---|
| 107 | "Parameters"}, |
---|
| 108 | {"dispersion", T_OBJECT, offsetof(CHardsphereStructure, dispersion), 0, |
---|
| 109 | "Dispersion parameters"}, |
---|
| 110 | {"log", T_OBJECT, offsetof(CHardsphereStructure, log), 0, |
---|
| 111 | "Log"}, |
---|
| 112 | {NULL} /* Sentinel */ |
---|
| 113 | }; |
---|
| 114 | |
---|
| 115 | /** Read double from PyObject |
---|
| 116 | @param p PyObject |
---|
| 117 | @return double |
---|
| 118 | */ |
---|
| 119 | double CHardsphereStructure_readDouble(PyObject *p) { |
---|
| 120 | if (PyFloat_Check(p)==1) { |
---|
| 121 | return (double)(((PyFloatObject *)(p))->ob_fval); |
---|
| 122 | } else if (PyInt_Check(p)==1) { |
---|
| 123 | return (double)(((PyIntObject *)(p))->ob_ival); |
---|
| 124 | } else if (PyLong_Check(p)==1) { |
---|
| 125 | return (double)PyLong_AsLong(p); |
---|
| 126 | } else { |
---|
| 127 | return 0.0; |
---|
| 128 | } |
---|
| 129 | } |
---|
| 130 | |
---|
| 131 | |
---|
| 132 | /** |
---|
| 133 | * Function to call to evaluate model |
---|
| 134 | * @param args: input q or [q,phi] |
---|
| 135 | * @return: function value |
---|
| 136 | */ |
---|
| 137 | static PyObject * run(CHardsphereStructure *self, PyObject *args) { |
---|
| 138 | double q_value, phi_value; |
---|
| 139 | PyObject* pars; |
---|
| 140 | int npars; |
---|
| 141 | |
---|
| 142 | // Get parameters |
---|
| 143 | |
---|
| 144 | // Reader parameter dictionary |
---|
| 145 | self->model->radius = PyFloat_AsDouble( PyDict_GetItemString(self->params, "radius") ); |
---|
| 146 | self->model->volfraction = PyFloat_AsDouble( PyDict_GetItemString(self->params, "volfraction") ); |
---|
| 147 | // Read in dispersion parameters |
---|
| 148 | PyObject* disp_dict; |
---|
| 149 | DispersionVisitor* visitor = new DispersionVisitor(); |
---|
| 150 | disp_dict = PyDict_GetItemString(self->dispersion, "radius"); |
---|
| 151 | self->model->radius.dispersion->accept_as_destination(visitor, self->model->radius.dispersion, disp_dict); |
---|
| 152 | |
---|
| 153 | |
---|
| 154 | // Get input and determine whether we have to supply a 1D or 2D return value. |
---|
| 155 | if ( !PyArg_ParseTuple(args,"O",&pars) ) { |
---|
| 156 | PyErr_SetString(CHardsphereStructureError, |
---|
| 157 | "CHardsphereStructure.run expects a q value."); |
---|
| 158 | return NULL; |
---|
| 159 | } |
---|
| 160 | |
---|
| 161 | // Check params |
---|
| 162 | if( PyList_Check(pars)==1) { |
---|
| 163 | |
---|
| 164 | // Length of list should be 2 for I(q,phi) |
---|
| 165 | npars = PyList_GET_SIZE(pars); |
---|
| 166 | if(npars!=2) { |
---|
| 167 | PyErr_SetString(CHardsphereStructureError, |
---|
| 168 | "CHardsphereStructure.run expects a double or a list of dimension 2."); |
---|
| 169 | return NULL; |
---|
| 170 | } |
---|
| 171 | // We have a vector q, get the q and phi values at which |
---|
| 172 | // to evaluate I(q,phi) |
---|
| 173 | q_value = CHardsphereStructure_readDouble(PyList_GET_ITEM(pars,0)); |
---|
| 174 | phi_value = CHardsphereStructure_readDouble(PyList_GET_ITEM(pars,1)); |
---|
| 175 | // Skip zero |
---|
| 176 | if (q_value==0) { |
---|
| 177 | return Py_BuildValue("d",0.0); |
---|
| 178 | } |
---|
| 179 | return Py_BuildValue("d",(*(self->model)).evaluate_rphi(q_value,phi_value)); |
---|
| 180 | |
---|
| 181 | } else { |
---|
| 182 | |
---|
| 183 | // We have a scalar q, we will evaluate I(q) |
---|
| 184 | q_value = CHardsphereStructure_readDouble(pars); |
---|
| 185 | |
---|
| 186 | return Py_BuildValue("d",(*(self->model))(q_value)); |
---|
| 187 | } |
---|
| 188 | } |
---|
| 189 | |
---|
| 190 | /** |
---|
| 191 | * Function to call to evaluate model in cartesian coordinates |
---|
| 192 | * @param args: input q or [qx, qy]] |
---|
| 193 | * @return: function value |
---|
| 194 | */ |
---|
| 195 | static PyObject * runXY(CHardsphereStructure *self, PyObject *args) { |
---|
| 196 | double qx_value, qy_value; |
---|
| 197 | PyObject* pars; |
---|
| 198 | int npars; |
---|
| 199 | |
---|
| 200 | // Get parameters |
---|
| 201 | |
---|
| 202 | // Reader parameter dictionary |
---|
| 203 | self->model->radius = PyFloat_AsDouble( PyDict_GetItemString(self->params, "radius") ); |
---|
| 204 | self->model->volfraction = PyFloat_AsDouble( PyDict_GetItemString(self->params, "volfraction") ); |
---|
| 205 | // Read in dispersion parameters |
---|
| 206 | PyObject* disp_dict; |
---|
| 207 | DispersionVisitor* visitor = new DispersionVisitor(); |
---|
| 208 | disp_dict = PyDict_GetItemString(self->dispersion, "radius"); |
---|
| 209 | self->model->radius.dispersion->accept_as_destination(visitor, self->model->radius.dispersion, disp_dict); |
---|
| 210 | |
---|
| 211 | |
---|
| 212 | // Get input and determine whether we have to supply a 1D or 2D return value. |
---|
| 213 | if ( !PyArg_ParseTuple(args,"O",&pars) ) { |
---|
| 214 | PyErr_SetString(CHardsphereStructureError, |
---|
| 215 | "CHardsphereStructure.run expects a q value."); |
---|
| 216 | return NULL; |
---|
| 217 | } |
---|
| 218 | |
---|
| 219 | // Check params |
---|
| 220 | if( PyList_Check(pars)==1) { |
---|
| 221 | |
---|
| 222 | // Length of list should be 2 for I(qx, qy)) |
---|
| 223 | npars = PyList_GET_SIZE(pars); |
---|
| 224 | if(npars!=2) { |
---|
| 225 | PyErr_SetString(CHardsphereStructureError, |
---|
| 226 | "CHardsphereStructure.run expects a double or a list of dimension 2."); |
---|
| 227 | return NULL; |
---|
| 228 | } |
---|
| 229 | // We have a vector q, get the qx and qy values at which |
---|
| 230 | // to evaluate I(qx,qy) |
---|
| 231 | qx_value = CHardsphereStructure_readDouble(PyList_GET_ITEM(pars,0)); |
---|
| 232 | qy_value = CHardsphereStructure_readDouble(PyList_GET_ITEM(pars,1)); |
---|
| 233 | return Py_BuildValue("d",(*(self->model))(qx_value,qy_value)); |
---|
| 234 | |
---|
| 235 | } else { |
---|
| 236 | |
---|
| 237 | // We have a scalar q, we will evaluate I(q) |
---|
| 238 | qx_value = CHardsphereStructure_readDouble(pars); |
---|
| 239 | |
---|
| 240 | return Py_BuildValue("d",(*(self->model))(qx_value)); |
---|
| 241 | } |
---|
| 242 | } |
---|
| 243 | |
---|
| 244 | static PyObject * reset(CHardsphereStructure *self, PyObject *args) { |
---|
| 245 | |
---|
| 246 | |
---|
| 247 | return Py_BuildValue("d",0.0); |
---|
| 248 | } |
---|
| 249 | |
---|
| 250 | static PyObject * set_dispersion(CHardsphereStructure *self, PyObject *args) { |
---|
| 251 | PyObject * disp; |
---|
| 252 | const char * par_name; |
---|
| 253 | |
---|
| 254 | if ( !PyArg_ParseTuple(args,"sO", &par_name, &disp) ) { |
---|
| 255 | PyErr_SetString(CHardsphereStructureError, |
---|
| 256 | "CHardsphereStructure.set_dispersion expects a DispersionModel object."); |
---|
| 257 | return NULL; |
---|
| 258 | } |
---|
| 259 | void *temp = PyCObject_AsVoidPtr(disp); |
---|
| 260 | DispersionModel * dispersion = static_cast<DispersionModel *>(temp); |
---|
| 261 | |
---|
| 262 | |
---|
| 263 | // Ugliness necessary to go from python to C |
---|
| 264 | // TODO: refactor this |
---|
| 265 | if (!strcmp(par_name, "radius")) { |
---|
| 266 | self->model->radius.dispersion = dispersion; |
---|
| 267 | } else { |
---|
| 268 | PyErr_SetString(CHardsphereStructureError, |
---|
| 269 | "CHardsphereStructure.set_dispersion expects a valid parameter name."); |
---|
| 270 | return NULL; |
---|
| 271 | } |
---|
| 272 | |
---|
| 273 | DispersionVisitor* visitor = new DispersionVisitor(); |
---|
| 274 | PyObject * disp_dict = PyDict_New(); |
---|
| 275 | dispersion->accept_as_source(visitor, dispersion, disp_dict); |
---|
| 276 | PyDict_SetItemString(self->dispersion, par_name, disp_dict); |
---|
| 277 | return Py_BuildValue("i",1); |
---|
| 278 | } |
---|
| 279 | |
---|
| 280 | |
---|
| 281 | static PyMethodDef CHardsphereStructure_methods[] = { |
---|
| 282 | {"run", (PyCFunction)run , METH_VARARGS, |
---|
| 283 | "Evaluate the model at a given Q or Q, phi"}, |
---|
| 284 | {"runXY", (PyCFunction)runXY , METH_VARARGS, |
---|
| 285 | "Evaluate the model at a given Q or Qx, Qy"}, |
---|
| 286 | {"reset", (PyCFunction)reset , METH_VARARGS, |
---|
| 287 | "Reset pair correlation"}, |
---|
| 288 | {"set_dispersion", (PyCFunction)set_dispersion , METH_VARARGS, |
---|
| 289 | "Set the dispersion model for a given parameter"}, |
---|
| 290 | {NULL} |
---|
| 291 | }; |
---|
| 292 | |
---|
| 293 | static PyTypeObject CHardsphereStructureType = { |
---|
| 294 | PyObject_HEAD_INIT(NULL) |
---|
| 295 | 0, /*ob_size*/ |
---|
| 296 | "CHardsphereStructure", /*tp_name*/ |
---|
| 297 | sizeof(CHardsphereStructure), /*tp_basicsize*/ |
---|
| 298 | 0, /*tp_itemsize*/ |
---|
| 299 | (destructor)CHardsphereStructure_dealloc, /*tp_dealloc*/ |
---|
| 300 | 0, /*tp_print*/ |
---|
| 301 | 0, /*tp_getattr*/ |
---|
| 302 | 0, /*tp_setattr*/ |
---|
| 303 | 0, /*tp_compare*/ |
---|
| 304 | 0, /*tp_repr*/ |
---|
| 305 | 0, /*tp_as_number*/ |
---|
| 306 | 0, /*tp_as_sequence*/ |
---|
| 307 | 0, /*tp_as_mapping*/ |
---|
| 308 | 0, /*tp_hash */ |
---|
| 309 | 0, /*tp_call*/ |
---|
| 310 | 0, /*tp_str*/ |
---|
| 311 | 0, /*tp_getattro*/ |
---|
| 312 | 0, /*tp_setattro*/ |
---|
| 313 | 0, /*tp_as_buffer*/ |
---|
| 314 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/ |
---|
| 315 | "CHardsphereStructure objects", /* tp_doc */ |
---|
| 316 | 0, /* tp_traverse */ |
---|
| 317 | 0, /* tp_clear */ |
---|
| 318 | 0, /* tp_richcompare */ |
---|
| 319 | 0, /* tp_weaklistoffset */ |
---|
| 320 | 0, /* tp_iter */ |
---|
| 321 | 0, /* tp_iternext */ |
---|
| 322 | CHardsphereStructure_methods, /* tp_methods */ |
---|
| 323 | CHardsphereStructure_members, /* tp_members */ |
---|
| 324 | 0, /* tp_getset */ |
---|
| 325 | 0, /* tp_base */ |
---|
| 326 | 0, /* tp_dict */ |
---|
| 327 | 0, /* tp_descr_get */ |
---|
| 328 | 0, /* tp_descr_set */ |
---|
| 329 | 0, /* tp_dictoffset */ |
---|
| 330 | (initproc)CHardsphereStructure_init, /* tp_init */ |
---|
| 331 | 0, /* tp_alloc */ |
---|
| 332 | CHardsphereStructure_new, /* tp_new */ |
---|
| 333 | }; |
---|
| 334 | |
---|
| 335 | |
---|
| 336 | static PyMethodDef module_methods[] = { |
---|
| 337 | {NULL} |
---|
| 338 | }; |
---|
| 339 | |
---|
| 340 | /** |
---|
| 341 | * Function used to add the model class to a module |
---|
| 342 | * @param module: module to add the class to |
---|
| 343 | */ |
---|
| 344 | void addCHardsphereStructure(PyObject *module) { |
---|
| 345 | PyObject *d; |
---|
| 346 | |
---|
| 347 | if (PyType_Ready(&CHardsphereStructureType) < 0) |
---|
| 348 | return; |
---|
| 349 | |
---|
| 350 | Py_INCREF(&CHardsphereStructureType); |
---|
| 351 | PyModule_AddObject(module, "CHardsphereStructure", (PyObject *)&CHardsphereStructureType); |
---|
| 352 | |
---|
| 353 | d = PyModule_GetDict(module); |
---|
| 354 | CHardsphereStructureError = PyErr_NewException("CHardsphereStructure.error", NULL, NULL); |
---|
| 355 | PyDict_SetItemString(d, "CHardsphereStructureError", CHardsphereStructureError); |
---|
| 356 | } |
---|
| 357 | |
---|