[e0a8a3c] | 1 | /** CHardsphereStructure |
---|
| 2 | * |
---|
| 3 | * C extension |
---|
| 4 | * |
---|
| 5 | * WARNING: THIS FILE WAS GENERATED BY WRAPPERGENERATOR.PY |
---|
| 6 | * DO NOT MODIFY THIS FILE, MODIFY Hardsphere.h |
---|
| 7 | * AND RE-RUN THE GENERATOR SCRIPT |
---|
| 8 | * |
---|
| 9 | * @author M.Doucet / UTK |
---|
| 10 | */ |
---|
| 11 | |
---|
| 12 | #include <Python.h> |
---|
| 13 | #include "structmember.h" |
---|
| 14 | #include <stdio.h> |
---|
| 15 | #include <stdlib.h> |
---|
| 16 | #include <math.h> |
---|
| 17 | #include <time.h> |
---|
| 18 | |
---|
| 19 | #include "Hardsphere.h" |
---|
| 20 | |
---|
| 21 | /// Error object for raised exceptions |
---|
| 22 | static PyObject * CHardsphereStructureError = NULL; |
---|
| 23 | |
---|
| 24 | |
---|
| 25 | // Class definition |
---|
| 26 | typedef struct { |
---|
| 27 | PyObject_HEAD |
---|
| 28 | /// Parameters |
---|
| 29 | PyObject * params; |
---|
| 30 | /// Log for unit testing |
---|
| 31 | PyObject * log; |
---|
| 32 | /// Model parameters |
---|
| 33 | HardsphereParameters model_pars; |
---|
| 34 | } CHardsphereStructure; |
---|
| 35 | |
---|
| 36 | |
---|
| 37 | static void |
---|
| 38 | CHardsphereStructure_dealloc(CHardsphereStructure* self) |
---|
| 39 | { |
---|
| 40 | self->ob_type->tp_free((PyObject*)self); |
---|
| 41 | |
---|
| 42 | |
---|
| 43 | } |
---|
| 44 | |
---|
| 45 | static PyObject * |
---|
| 46 | CHardsphereStructure_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
---|
| 47 | { |
---|
| 48 | CHardsphereStructure *self; |
---|
| 49 | |
---|
| 50 | self = (CHardsphereStructure *)type->tp_alloc(type, 0); |
---|
| 51 | |
---|
| 52 | return (PyObject *)self; |
---|
| 53 | } |
---|
| 54 | |
---|
| 55 | static int |
---|
| 56 | CHardsphereStructure_init(CHardsphereStructure *self, PyObject *args, PyObject *kwds) |
---|
| 57 | { |
---|
| 58 | if (self != NULL) { |
---|
| 59 | |
---|
| 60 | // Create parameters |
---|
| 61 | self->params = PyDict_New(); |
---|
| 62 | |
---|
| 63 | // Initialize parameter dictionary |
---|
| 64 | PyDict_SetItemString(self->params,"radius",Py_BuildValue("d",50.000000)); |
---|
| 65 | PyDict_SetItemString(self->params,"volfraction",Py_BuildValue("d",0.200000)); |
---|
| 66 | |
---|
| 67 | |
---|
| 68 | // Create empty log |
---|
| 69 | self->log = PyDict_New(); |
---|
| 70 | |
---|
| 71 | |
---|
| 72 | |
---|
| 73 | } |
---|
| 74 | return 0; |
---|
| 75 | } |
---|
| 76 | |
---|
| 77 | static PyMemberDef CHardsphereStructure_members[] = { |
---|
| 78 | {"params", T_OBJECT, offsetof(CHardsphereStructure, params), 0, |
---|
| 79 | "Parameters"}, |
---|
| 80 | {"log", T_OBJECT, offsetof(CHardsphereStructure, log), 0, |
---|
| 81 | "Log"}, |
---|
| 82 | {NULL} /* Sentinel */ |
---|
| 83 | }; |
---|
| 84 | |
---|
| 85 | /** Read double from PyObject |
---|
| 86 | @param p PyObject |
---|
| 87 | @return double |
---|
| 88 | */ |
---|
| 89 | double CHardsphereStructure_readDouble(PyObject *p) { |
---|
| 90 | if (PyFloat_Check(p)==1) { |
---|
| 91 | return (double)(((PyFloatObject *)(p))->ob_fval); |
---|
| 92 | } else if (PyInt_Check(p)==1) { |
---|
| 93 | return (double)(((PyIntObject *)(p))->ob_ival); |
---|
| 94 | } else if (PyLong_Check(p)==1) { |
---|
| 95 | return (double)PyLong_AsLong(p); |
---|
| 96 | } else { |
---|
| 97 | return 0.0; |
---|
| 98 | } |
---|
| 99 | } |
---|
| 100 | |
---|
| 101 | |
---|
| 102 | /** |
---|
| 103 | * Function to call to evaluate model |
---|
| 104 | * @param args: input q or [q,phi] |
---|
| 105 | * @return: function value |
---|
| 106 | */ |
---|
| 107 | static PyObject * run(CHardsphereStructure *self, PyObject *args) { |
---|
| 108 | double q_value, phi_value; |
---|
| 109 | PyObject* pars; |
---|
| 110 | int npars; |
---|
| 111 | |
---|
| 112 | // Get parameters |
---|
| 113 | |
---|
| 114 | // Reader parameter dictionary |
---|
| 115 | self->model_pars.radius = PyFloat_AsDouble( PyDict_GetItemString(self->params, "radius") ); |
---|
| 116 | self->model_pars.volfraction = PyFloat_AsDouble( PyDict_GetItemString(self->params, "volfraction") ); |
---|
| 117 | |
---|
| 118 | |
---|
| 119 | // Get input and determine whether we have to supply a 1D or 2D return value. |
---|
| 120 | if ( !PyArg_ParseTuple(args,"O",&pars) ) { |
---|
| 121 | PyErr_SetString(CHardsphereStructureError, |
---|
| 122 | "CHardsphereStructure.run expects a q value."); |
---|
| 123 | return NULL; |
---|
| 124 | } |
---|
| 125 | |
---|
| 126 | // Check params |
---|
| 127 | if( PyList_Check(pars)==1) { |
---|
| 128 | |
---|
| 129 | // Length of list should be 2 for I(q,phi) |
---|
| 130 | npars = PyList_GET_SIZE(pars); |
---|
| 131 | if(npars!=2) { |
---|
| 132 | PyErr_SetString(CHardsphereStructureError, |
---|
| 133 | "CHardsphereStructure.run expects a double or a list of dimension 2."); |
---|
| 134 | return NULL; |
---|
| 135 | } |
---|
| 136 | // We have a vector q, get the q and phi values at which |
---|
| 137 | // to evaluate I(q,phi) |
---|
| 138 | q_value = CHardsphereStructure_readDouble(PyList_GET_ITEM(pars,0)); |
---|
| 139 | phi_value = CHardsphereStructure_readDouble(PyList_GET_ITEM(pars,1)); |
---|
| 140 | // Skip zero |
---|
| 141 | if (q_value==0) { |
---|
| 142 | return Py_BuildValue("d",0.0); |
---|
| 143 | } |
---|
| 144 | return Py_BuildValue("d",Hardsphere_analytical_2D(&(self->model_pars),q_value,phi_value)); |
---|
| 145 | |
---|
| 146 | } else { |
---|
| 147 | |
---|
| 148 | // We have a scalar q, we will evaluate I(q) |
---|
| 149 | q_value = CHardsphereStructure_readDouble(pars); |
---|
| 150 | |
---|
| 151 | return Py_BuildValue("d",Hardsphere_analytical_1D(&(self->model_pars),q_value)); |
---|
| 152 | } |
---|
| 153 | } |
---|
| 154 | |
---|
| 155 | /** |
---|
| 156 | * Function to call to evaluate model in cartesian coordinates |
---|
| 157 | * @param args: input q or [qx, qy]] |
---|
| 158 | * @return: function value |
---|
| 159 | */ |
---|
| 160 | static PyObject * runXY(CHardsphereStructure *self, PyObject *args) { |
---|
| 161 | double qx_value, qy_value; |
---|
| 162 | PyObject* pars; |
---|
| 163 | int npars; |
---|
| 164 | |
---|
| 165 | // Get parameters |
---|
| 166 | |
---|
| 167 | // Reader parameter dictionary |
---|
| 168 | self->model_pars.radius = PyFloat_AsDouble( PyDict_GetItemString(self->params, "radius") ); |
---|
| 169 | self->model_pars.volfraction = PyFloat_AsDouble( PyDict_GetItemString(self->params, "volfraction") ); |
---|
| 170 | |
---|
| 171 | |
---|
| 172 | // Get input and determine whether we have to supply a 1D or 2D return value. |
---|
| 173 | if ( !PyArg_ParseTuple(args,"O",&pars) ) { |
---|
| 174 | PyErr_SetString(CHardsphereStructureError, |
---|
| 175 | "CHardsphereStructure.run expects a q value."); |
---|
| 176 | return NULL; |
---|
| 177 | } |
---|
| 178 | |
---|
| 179 | // Check params |
---|
| 180 | if( PyList_Check(pars)==1) { |
---|
| 181 | |
---|
| 182 | // Length of list should be 2 for I(qx, qy)) |
---|
| 183 | npars = PyList_GET_SIZE(pars); |
---|
| 184 | if(npars!=2) { |
---|
| 185 | PyErr_SetString(CHardsphereStructureError, |
---|
| 186 | "CHardsphereStructure.run expects a double or a list of dimension 2."); |
---|
| 187 | return NULL; |
---|
| 188 | } |
---|
| 189 | // We have a vector q, get the qx and qy values at which |
---|
| 190 | // to evaluate I(qx,qy) |
---|
| 191 | qx_value = CHardsphereStructure_readDouble(PyList_GET_ITEM(pars,0)); |
---|
| 192 | qy_value = CHardsphereStructure_readDouble(PyList_GET_ITEM(pars,1)); |
---|
| 193 | return Py_BuildValue("d",Hardsphere_analytical_2DXY(&(self->model_pars),qx_value,qy_value)); |
---|
| 194 | |
---|
| 195 | } else { |
---|
| 196 | |
---|
| 197 | // We have a scalar q, we will evaluate I(q) |
---|
| 198 | qx_value = CHardsphereStructure_readDouble(pars); |
---|
| 199 | |
---|
| 200 | return Py_BuildValue("d",Hardsphere_analytical_1D(&(self->model_pars),qx_value)); |
---|
| 201 | } |
---|
| 202 | } |
---|
| 203 | |
---|
| 204 | static PyObject * reset(CHardsphereStructure *self, PyObject *args) { |
---|
| 205 | |
---|
| 206 | |
---|
| 207 | return Py_BuildValue("d",0.0); |
---|
| 208 | } |
---|
| 209 | |
---|
| 210 | |
---|
| 211 | static PyMethodDef CHardsphereStructure_methods[] = { |
---|
| 212 | {"run", (PyCFunction)run , METH_VARARGS, |
---|
| 213 | "Evaluate the model at a given Q or Q, phi"}, |
---|
| 214 | {"runXY", (PyCFunction)runXY , METH_VARARGS, |
---|
| 215 | "Evaluate the model at a given Q or Qx, Qy"}, |
---|
| 216 | {"reset", (PyCFunction)reset , METH_VARARGS, |
---|
| 217 | "Reset pair correlation"}, |
---|
| 218 | //{"numerical_1D", (PyCFunction)numerical_1D , METH_VARARGS, |
---|
| 219 | // "Evaluate the 1D model at a given Q"}, |
---|
| 220 | {NULL} |
---|
| 221 | }; |
---|
| 222 | |
---|
| 223 | static PyTypeObject CHardsphereStructureType = { |
---|
| 224 | PyObject_HEAD_INIT(NULL) |
---|
| 225 | 0, /*ob_size*/ |
---|
| 226 | "CHardsphereStructure", /*tp_name*/ |
---|
| 227 | sizeof(CHardsphereStructure), /*tp_basicsize*/ |
---|
| 228 | 0, /*tp_itemsize*/ |
---|
| 229 | (destructor)CHardsphereStructure_dealloc, /*tp_dealloc*/ |
---|
| 230 | 0, /*tp_print*/ |
---|
| 231 | 0, /*tp_getattr*/ |
---|
| 232 | 0, /*tp_setattr*/ |
---|
| 233 | 0, /*tp_compare*/ |
---|
| 234 | 0, /*tp_repr*/ |
---|
| 235 | 0, /*tp_as_number*/ |
---|
| 236 | 0, /*tp_as_sequence*/ |
---|
| 237 | 0, /*tp_as_mapping*/ |
---|
| 238 | 0, /*tp_hash */ |
---|
| 239 | 0, /*tp_call*/ |
---|
| 240 | 0, /*tp_str*/ |
---|
| 241 | 0, /*tp_getattro*/ |
---|
| 242 | 0, /*tp_setattro*/ |
---|
| 243 | 0, /*tp_as_buffer*/ |
---|
| 244 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/ |
---|
| 245 | "CHardsphereStructure objects", /* tp_doc */ |
---|
| 246 | 0, /* tp_traverse */ |
---|
| 247 | 0, /* tp_clear */ |
---|
| 248 | 0, /* tp_richcompare */ |
---|
| 249 | 0, /* tp_weaklistoffset */ |
---|
| 250 | 0, /* tp_iter */ |
---|
| 251 | 0, /* tp_iternext */ |
---|
| 252 | CHardsphereStructure_methods, /* tp_methods */ |
---|
| 253 | CHardsphereStructure_members, /* tp_members */ |
---|
| 254 | 0, /* tp_getset */ |
---|
| 255 | 0, /* tp_base */ |
---|
| 256 | 0, /* tp_dict */ |
---|
| 257 | 0, /* tp_descr_get */ |
---|
| 258 | 0, /* tp_descr_set */ |
---|
| 259 | 0, /* tp_dictoffset */ |
---|
| 260 | (initproc)CHardsphereStructure_init, /* tp_init */ |
---|
| 261 | 0, /* tp_alloc */ |
---|
| 262 | CHardsphereStructure_new, /* tp_new */ |
---|
| 263 | }; |
---|
| 264 | |
---|
| 265 | |
---|
| 266 | static PyMethodDef module_methods[] = { |
---|
| 267 | {NULL} |
---|
| 268 | }; |
---|
| 269 | |
---|
| 270 | /** |
---|
| 271 | * Function used to add the model class to a module |
---|
| 272 | * @param module: module to add the class to |
---|
| 273 | */ |
---|
| 274 | void addCHardsphereStructure(PyObject *module) { |
---|
| 275 | PyObject *d; |
---|
| 276 | |
---|
| 277 | if (PyType_Ready(&CHardsphereStructureType) < 0) |
---|
| 278 | return; |
---|
| 279 | |
---|
| 280 | Py_INCREF(&CHardsphereStructureType); |
---|
| 281 | PyModule_AddObject(module, "CHardsphereStructure", (PyObject *)&CHardsphereStructureType); |
---|
| 282 | |
---|
| 283 | d = PyModule_GetDict(module); |
---|
| 284 | CHardsphereStructureError = PyErr_NewException("CHardsphereStructure.error", NULL, NULL); |
---|
| 285 | PyDict_SetItemString(d, "CHardsphereStructureError", CHardsphereStructureError); |
---|
| 286 | } |
---|
| 287 | |
---|