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