[e0a8a3c] | 1 | /** CDiamCylFunc |
---|
| 2 | * |
---|
| 3 | * C extension |
---|
| 4 | * |
---|
| 5 | * WARNING: THIS FILE WAS GENERATED BY WRAPPERGENERATOR.PY |
---|
| 6 | * DO NOT MODIFY THIS FILE, MODIFY DiamCyl.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 "DiamCyl.h" |
---|
| 20 | |
---|
| 21 | /// Error object for raised exceptions |
---|
| 22 | static PyObject * CDiamCylFuncError = 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 | DiamCyldParameters model_pars; |
---|
| 34 | } CDiamCylFunc; |
---|
| 35 | |
---|
| 36 | |
---|
| 37 | static void |
---|
| 38 | CDiamCylFunc_dealloc(CDiamCylFunc* self) |
---|
| 39 | { |
---|
| 40 | self->ob_type->tp_free((PyObject*)self); |
---|
| 41 | |
---|
| 42 | |
---|
| 43 | } |
---|
| 44 | |
---|
| 45 | static PyObject * |
---|
| 46 | CDiamCylFunc_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
---|
| 47 | { |
---|
| 48 | CDiamCylFunc *self; |
---|
| 49 | |
---|
| 50 | self = (CDiamCylFunc *)type->tp_alloc(type, 0); |
---|
| 51 | |
---|
| 52 | return (PyObject *)self; |
---|
| 53 | } |
---|
| 54 | |
---|
| 55 | static int |
---|
| 56 | CDiamCylFunc_init(CDiamCylFunc *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,"length",Py_BuildValue("d",400.000000)); |
---|
| 65 | PyDict_SetItemString(self->params,"radius",Py_BuildValue("d",20.000000)); |
---|
| 66 | |
---|
| 67 | |
---|
| 68 | // Create empty log |
---|
| 69 | self->log = PyDict_New(); |
---|
| 70 | |
---|
| 71 | |
---|
| 72 | |
---|
| 73 | } |
---|
| 74 | return 0; |
---|
| 75 | } |
---|
| 76 | |
---|
| 77 | static PyMemberDef CDiamCylFunc_members[] = { |
---|
| 78 | {"params", T_OBJECT, offsetof(CDiamCylFunc, params), 0, |
---|
| 79 | "Parameters"}, |
---|
| 80 | {"log", T_OBJECT, offsetof(CDiamCylFunc, log), 0, |
---|
| 81 | "Log"}, |
---|
| 82 | {NULL} /* Sentinel */ |
---|
| 83 | }; |
---|
| 84 | |
---|
| 85 | /** Read double from PyObject |
---|
| 86 | @param p PyObject |
---|
| 87 | @return double |
---|
| 88 | */ |
---|
| 89 | double CDiamCylFunc_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(CDiamCylFunc *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.length = PyFloat_AsDouble( PyDict_GetItemString(self->params, "length") ); |
---|
| 116 | self->model_pars.radius = PyFloat_AsDouble( PyDict_GetItemString(self->params, "radius") ); |
---|
| 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(CDiamCylFuncError, |
---|
| 122 | "CDiamCylFunc.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(CDiamCylFuncError, |
---|
| 133 | "CDiamCylFunc.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 = CDiamCylFunc_readDouble(PyList_GET_ITEM(pars,0)); |
---|
| 139 | phi_value = CDiamCylFunc_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",DiamCyl_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 = CDiamCylFunc_readDouble(pars); |
---|
| 150 | |
---|
| 151 | return Py_BuildValue("d",DiamCyl_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(CDiamCylFunc *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.length = PyFloat_AsDouble( PyDict_GetItemString(self->params, "length") ); |
---|
| 169 | self->model_pars.radius = PyFloat_AsDouble( PyDict_GetItemString(self->params, "radius") ); |
---|
| 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(CDiamCylFuncError, |
---|
| 175 | "CDiamCylFunc.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(CDiamCylFuncError, |
---|
| 186 | "CDiamCylFunc.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 = CDiamCylFunc_readDouble(PyList_GET_ITEM(pars,0)); |
---|
| 192 | qy_value = CDiamCylFunc_readDouble(PyList_GET_ITEM(pars,1)); |
---|
| 193 | return Py_BuildValue("d",DiamCyl_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 = CDiamCylFunc_readDouble(pars); |
---|
| 199 | |
---|
| 200 | return Py_BuildValue("d",DiamCyl_analytical_1D(&(self->model_pars),qx_value)); |
---|
| 201 | } |
---|
| 202 | } |
---|
| 203 | |
---|
| 204 | static PyObject * reset(CDiamCylFunc *self, PyObject *args) { |
---|
| 205 | |
---|
| 206 | |
---|
| 207 | return Py_BuildValue("d",0.0); |
---|
| 208 | } |
---|
| 209 | |
---|
| 210 | |
---|
| 211 | static PyMethodDef CDiamCylFunc_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 CDiamCylFuncType = { |
---|
| 224 | PyObject_HEAD_INIT(NULL) |
---|
| 225 | 0, /*ob_size*/ |
---|
| 226 | "CDiamCylFunc", /*tp_name*/ |
---|
| 227 | sizeof(CDiamCylFunc), /*tp_basicsize*/ |
---|
| 228 | 0, /*tp_itemsize*/ |
---|
| 229 | (destructor)CDiamCylFunc_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 | "CDiamCylFunc 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 | CDiamCylFunc_methods, /* tp_methods */ |
---|
| 253 | CDiamCylFunc_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)CDiamCylFunc_init, /* tp_init */ |
---|
| 261 | 0, /* tp_alloc */ |
---|
| 262 | CDiamCylFunc_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 addCDiamCylFunc(PyObject *module) { |
---|
| 275 | PyObject *d; |
---|
| 276 | |
---|
| 277 | if (PyType_Ready(&CDiamCylFuncType) < 0) |
---|
| 278 | return; |
---|
| 279 | |
---|
| 280 | Py_INCREF(&CDiamCylFuncType); |
---|
| 281 | PyModule_AddObject(module, "CDiamCylFunc", (PyObject *)&CDiamCylFuncType); |
---|
| 282 | |
---|
| 283 | d = PyModule_GetDict(module); |
---|
| 284 | CDiamCylFuncError = PyErr_NewException("CDiamCylFunc.error", NULL, NULL); |
---|
| 285 | PyDict_SetItemString(d, "CDiamCylFuncError", CDiamCylFuncError); |
---|
| 286 | } |
---|
| 287 | |
---|