[9316609] | 1 | /** [PYTHONCLASS] |
---|
[ae3ce4e] | 2 | * |
---|
| 3 | * C extension |
---|
| 4 | * |
---|
| 5 | * WARNING: THIS FILE WAS GENERATED BY WRAPPERGENERATOR.PY |
---|
| 6 | * DO NOT MODIFY THIS FILE, MODIFY elliptical_cylinder.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 "elliptical_cylinder.h" |
---|
| 20 | |
---|
| 21 | /// Error object for raised exceptions |
---|
[9316609] | 22 | static PyObject * [PYTHONCLASS]Error = NULL; |
---|
[ae3ce4e] | 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 | EllipticalCylinderParameters model_pars; |
---|
[9316609] | 34 | } [PYTHONCLASS]; |
---|
[ae3ce4e] | 35 | |
---|
| 36 | |
---|
| 37 | static void |
---|
[9316609] | 38 | [PYTHONCLASS]_dealloc([PYTHONCLASS]* self) |
---|
[ae3ce4e] | 39 | { |
---|
| 40 | self->ob_type->tp_free((PyObject*)self); |
---|
| 41 | |
---|
| 42 | |
---|
| 43 | } |
---|
| 44 | |
---|
| 45 | static PyObject * |
---|
[9316609] | 46 | [PYTHONCLASS]_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
---|
[ae3ce4e] | 47 | { |
---|
[9316609] | 48 | [PYTHONCLASS] *self; |
---|
[ae3ce4e] | 49 | |
---|
[9316609] | 50 | self = ([PYTHONCLASS] *)type->tp_alloc(type, 0); |
---|
[ae3ce4e] | 51 | |
---|
| 52 | return (PyObject *)self; |
---|
| 53 | } |
---|
| 54 | |
---|
| 55 | static int |
---|
[9316609] | 56 | [PYTHONCLASS]_init([PYTHONCLASS] *self, PyObject *args, PyObject *kwds) |
---|
[ae3ce4e] | 57 | { |
---|
| 58 | if (self != NULL) { |
---|
| 59 | |
---|
| 60 | // Create parameters |
---|
| 61 | self->params = PyDict_New(); |
---|
| 62 | |
---|
| 63 | // Initialize parameter dictionary |
---|
| 64 | PyDict_SetItemString(self->params,"scale",Py_BuildValue("d",1.000000)); |
---|
| 65 | PyDict_SetItemString(self->params,"cyl_psi",Py_BuildValue("d",0.000000)); |
---|
| 66 | PyDict_SetItemString(self->params,"length",Py_BuildValue("d",400.000000)); |
---|
| 67 | PyDict_SetItemString(self->params,"r_minor",Py_BuildValue("d",20.000000)); |
---|
| 68 | PyDict_SetItemString(self->params,"cyl_theta",Py_BuildValue("d",1.570000)); |
---|
| 69 | PyDict_SetItemString(self->params,"background",Py_BuildValue("d",0.000000)); |
---|
| 70 | PyDict_SetItemString(self->params,"r_ratio",Py_BuildValue("d",1.500000)); |
---|
| 71 | PyDict_SetItemString(self->params,"contrast",Py_BuildValue("d",0.000003)); |
---|
| 72 | PyDict_SetItemString(self->params,"cyl_phi",Py_BuildValue("d",0.000000)); |
---|
| 73 | |
---|
| 74 | |
---|
| 75 | // Create empty log |
---|
| 76 | self->log = PyDict_New(); |
---|
| 77 | |
---|
| 78 | |
---|
| 79 | |
---|
| 80 | } |
---|
| 81 | return 0; |
---|
| 82 | } |
---|
| 83 | |
---|
[9316609] | 84 | static PyMemberDef [PYTHONCLASS]_members[] = { |
---|
| 85 | {"params", T_OBJECT, offsetof([PYTHONCLASS], params), 0, |
---|
[ae3ce4e] | 86 | "Parameters"}, |
---|
[9316609] | 87 | {"log", T_OBJECT, offsetof([PYTHONCLASS], log), 0, |
---|
[ae3ce4e] | 88 | "Log"}, |
---|
| 89 | {NULL} /* Sentinel */ |
---|
| 90 | }; |
---|
| 91 | |
---|
| 92 | /** Read double from PyObject |
---|
| 93 | @param p PyObject |
---|
| 94 | @return double |
---|
| 95 | */ |
---|
[9316609] | 96 | double [PYTHONCLASS]_readDouble(PyObject *p) { |
---|
[ae3ce4e] | 97 | if (PyFloat_Check(p)==1) { |
---|
| 98 | return (double)(((PyFloatObject *)(p))->ob_fval); |
---|
| 99 | } else if (PyInt_Check(p)==1) { |
---|
| 100 | return (double)(((PyIntObject *)(p))->ob_ival); |
---|
| 101 | } else if (PyLong_Check(p)==1) { |
---|
| 102 | return (double)PyLong_AsLong(p); |
---|
| 103 | } else { |
---|
| 104 | return 0.0; |
---|
| 105 | } |
---|
| 106 | } |
---|
| 107 | |
---|
| 108 | |
---|
| 109 | /** |
---|
| 110 | * Function to call to evaluate model |
---|
| 111 | * @param args: input q or [q,phi] |
---|
| 112 | * @return: function value |
---|
| 113 | */ |
---|
[9316609] | 114 | static PyObject * run([PYTHONCLASS] *self, PyObject *args) { |
---|
[ae3ce4e] | 115 | double q_value, phi_value; |
---|
| 116 | PyObject* pars; |
---|
| 117 | int npars; |
---|
| 118 | |
---|
| 119 | // Get parameters |
---|
| 120 | |
---|
| 121 | // Reader parameter dictionary |
---|
| 122 | self->model_pars.scale = PyFloat_AsDouble( PyDict_GetItemString(self->params, "scale") ); |
---|
| 123 | self->model_pars.cyl_psi = PyFloat_AsDouble( PyDict_GetItemString(self->params, "cyl_psi") ); |
---|
| 124 | self->model_pars.length = PyFloat_AsDouble( PyDict_GetItemString(self->params, "length") ); |
---|
| 125 | self->model_pars.r_minor = PyFloat_AsDouble( PyDict_GetItemString(self->params, "r_minor") ); |
---|
| 126 | self->model_pars.cyl_theta = PyFloat_AsDouble( PyDict_GetItemString(self->params, "cyl_theta") ); |
---|
| 127 | self->model_pars.background = PyFloat_AsDouble( PyDict_GetItemString(self->params, "background") ); |
---|
| 128 | self->model_pars.r_ratio = PyFloat_AsDouble( PyDict_GetItemString(self->params, "r_ratio") ); |
---|
| 129 | self->model_pars.contrast = PyFloat_AsDouble( PyDict_GetItemString(self->params, "contrast") ); |
---|
| 130 | self->model_pars.cyl_phi = PyFloat_AsDouble( PyDict_GetItemString(self->params, "cyl_phi") ); |
---|
| 131 | |
---|
| 132 | |
---|
| 133 | // Get input and determine whether we have to supply a 1D or 2D return value. |
---|
| 134 | if ( !PyArg_ParseTuple(args,"O",&pars) ) { |
---|
[9316609] | 135 | PyErr_SetString([PYTHONCLASS]Error, |
---|
| 136 | "[PYTHONCLASS].run expects a q value."); |
---|
[ae3ce4e] | 137 | return NULL; |
---|
| 138 | } |
---|
| 139 | |
---|
| 140 | // Check params |
---|
| 141 | if( PyList_Check(pars)==1) { |
---|
| 142 | |
---|
| 143 | // Length of list should be 2 for I(q,phi) |
---|
| 144 | npars = PyList_GET_SIZE(pars); |
---|
| 145 | if(npars!=2) { |
---|
[9316609] | 146 | PyErr_SetString([PYTHONCLASS]Error, |
---|
| 147 | "[PYTHONCLASS].run expects a double or a list of dimension 2."); |
---|
[ae3ce4e] | 148 | return NULL; |
---|
| 149 | } |
---|
| 150 | // We have a vector q, get the q and phi values at which |
---|
| 151 | // to evaluate I(q,phi) |
---|
[9316609] | 152 | q_value = [PYTHONCLASS]_readDouble(PyList_GET_ITEM(pars,0)); |
---|
| 153 | phi_value = [PYTHONCLASS]_readDouble(PyList_GET_ITEM(pars,1)); |
---|
[ae3ce4e] | 154 | // Skip zero |
---|
| 155 | if (q_value==0) { |
---|
| 156 | return Py_BuildValue("d",0.0); |
---|
| 157 | } |
---|
| 158 | return Py_BuildValue("d",elliptical_cylinder_analytical_2D(&(self->model_pars),q_value,phi_value)); |
---|
| 159 | |
---|
| 160 | } else { |
---|
| 161 | |
---|
| 162 | // We have a scalar q, we will evaluate I(q) |
---|
[9316609] | 163 | q_value = [PYTHONCLASS]_readDouble(pars); |
---|
[ae3ce4e] | 164 | |
---|
| 165 | return Py_BuildValue("d",elliptical_cylinder_analytical_1D(&(self->model_pars),q_value)); |
---|
| 166 | } |
---|
| 167 | } |
---|
| 168 | |
---|
| 169 | /** |
---|
| 170 | * Function to call to evaluate model in cartesian coordinates |
---|
| 171 | * @param args: input q or [qx, qy]] |
---|
| 172 | * @return: function value |
---|
| 173 | */ |
---|
[9316609] | 174 | static PyObject * runXY([PYTHONCLASS] *self, PyObject *args) { |
---|
[ae3ce4e] | 175 | double qx_value, qy_value; |
---|
| 176 | PyObject* pars; |
---|
| 177 | int npars; |
---|
| 178 | |
---|
| 179 | // Get parameters |
---|
| 180 | |
---|
| 181 | // Reader parameter dictionary |
---|
| 182 | self->model_pars.scale = PyFloat_AsDouble( PyDict_GetItemString(self->params, "scale") ); |
---|
| 183 | self->model_pars.cyl_psi = PyFloat_AsDouble( PyDict_GetItemString(self->params, "cyl_psi") ); |
---|
| 184 | self->model_pars.length = PyFloat_AsDouble( PyDict_GetItemString(self->params, "length") ); |
---|
| 185 | self->model_pars.r_minor = PyFloat_AsDouble( PyDict_GetItemString(self->params, "r_minor") ); |
---|
| 186 | self->model_pars.cyl_theta = PyFloat_AsDouble( PyDict_GetItemString(self->params, "cyl_theta") ); |
---|
| 187 | self->model_pars.background = PyFloat_AsDouble( PyDict_GetItemString(self->params, "background") ); |
---|
| 188 | self->model_pars.r_ratio = PyFloat_AsDouble( PyDict_GetItemString(self->params, "r_ratio") ); |
---|
| 189 | self->model_pars.contrast = PyFloat_AsDouble( PyDict_GetItemString(self->params, "contrast") ); |
---|
| 190 | self->model_pars.cyl_phi = PyFloat_AsDouble( PyDict_GetItemString(self->params, "cyl_phi") ); |
---|
| 191 | |
---|
| 192 | |
---|
| 193 | // Get input and determine whether we have to supply a 1D or 2D return value. |
---|
| 194 | if ( !PyArg_ParseTuple(args,"O",&pars) ) { |
---|
[9316609] | 195 | PyErr_SetString([PYTHONCLASS]Error, |
---|
| 196 | "[PYTHONCLASS].run expects a q value."); |
---|
[ae3ce4e] | 197 | return NULL; |
---|
| 198 | } |
---|
| 199 | |
---|
| 200 | // Check params |
---|
| 201 | if( PyList_Check(pars)==1) { |
---|
| 202 | |
---|
| 203 | // Length of list should be 2 for I(qx, qy)) |
---|
| 204 | npars = PyList_GET_SIZE(pars); |
---|
| 205 | if(npars!=2) { |
---|
[9316609] | 206 | PyErr_SetString([PYTHONCLASS]Error, |
---|
| 207 | "[PYTHONCLASS].run expects a double or a list of dimension 2."); |
---|
[ae3ce4e] | 208 | return NULL; |
---|
| 209 | } |
---|
| 210 | // We have a vector q, get the qx and qy values at which |
---|
| 211 | // to evaluate I(qx,qy) |
---|
[9316609] | 212 | qx_value = [PYTHONCLASS]_readDouble(PyList_GET_ITEM(pars,0)); |
---|
| 213 | qy_value = [PYTHONCLASS]_readDouble(PyList_GET_ITEM(pars,1)); |
---|
[ae3ce4e] | 214 | return Py_BuildValue("d",elliptical_cylinder_analytical_2DXY(&(self->model_pars),qx_value,qy_value)); |
---|
| 215 | |
---|
| 216 | } else { |
---|
| 217 | |
---|
| 218 | // We have a scalar q, we will evaluate I(q) |
---|
[9316609] | 219 | qx_value = [PYTHONCLASS]_readDouble(pars); |
---|
[ae3ce4e] | 220 | |
---|
| 221 | return Py_BuildValue("d",elliptical_cylinder_analytical_1D(&(self->model_pars),qx_value)); |
---|
| 222 | } |
---|
| 223 | } |
---|
| 224 | |
---|
[9316609] | 225 | static PyObject * reset([PYTHONCLASS] *self, PyObject *args) { |
---|
[ae3ce4e] | 226 | |
---|
| 227 | |
---|
| 228 | return Py_BuildValue("d",0.0); |
---|
| 229 | } |
---|
| 230 | |
---|
| 231 | |
---|
[9316609] | 232 | static PyMethodDef [PYTHONCLASS]_methods[] = { |
---|
[ae3ce4e] | 233 | {"run", (PyCFunction)run , METH_VARARGS, |
---|
| 234 | "Evaluate the model at a given Q or Q, phi"}, |
---|
| 235 | {"runXY", (PyCFunction)runXY , METH_VARARGS, |
---|
| 236 | "Evaluate the model at a given Q or Qx, Qy"}, |
---|
| 237 | {"reset", (PyCFunction)reset , METH_VARARGS, |
---|
| 238 | "Reset pair correlation"}, |
---|
| 239 | //{"numerical_1D", (PyCFunction)numerical_1D , METH_VARARGS, |
---|
| 240 | // "Evaluate the 1D model at a given Q"}, |
---|
| 241 | {NULL} |
---|
| 242 | }; |
---|
| 243 | |
---|
[9316609] | 244 | static PyTypeObject [PYTHONCLASS]Type = { |
---|
[ae3ce4e] | 245 | PyObject_HEAD_INIT(NULL) |
---|
| 246 | 0, /*ob_size*/ |
---|
[9316609] | 247 | "[PYTHONCLASS]", /*tp_name*/ |
---|
| 248 | sizeof([PYTHONCLASS]), /*tp_basicsize*/ |
---|
[ae3ce4e] | 249 | 0, /*tp_itemsize*/ |
---|
[9316609] | 250 | (destructor)[PYTHONCLASS]_dealloc, /*tp_dealloc*/ |
---|
[ae3ce4e] | 251 | 0, /*tp_print*/ |
---|
| 252 | 0, /*tp_getattr*/ |
---|
| 253 | 0, /*tp_setattr*/ |
---|
| 254 | 0, /*tp_compare*/ |
---|
| 255 | 0, /*tp_repr*/ |
---|
| 256 | 0, /*tp_as_number*/ |
---|
| 257 | 0, /*tp_as_sequence*/ |
---|
| 258 | 0, /*tp_as_mapping*/ |
---|
| 259 | 0, /*tp_hash */ |
---|
| 260 | 0, /*tp_call*/ |
---|
| 261 | 0, /*tp_str*/ |
---|
| 262 | 0, /*tp_getattro*/ |
---|
| 263 | 0, /*tp_setattro*/ |
---|
| 264 | 0, /*tp_as_buffer*/ |
---|
| 265 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/ |
---|
[9316609] | 266 | "[PYTHONCLASS] objects", /* tp_doc */ |
---|
[ae3ce4e] | 267 | 0, /* tp_traverse */ |
---|
| 268 | 0, /* tp_clear */ |
---|
| 269 | 0, /* tp_richcompare */ |
---|
| 270 | 0, /* tp_weaklistoffset */ |
---|
| 271 | 0, /* tp_iter */ |
---|
| 272 | 0, /* tp_iternext */ |
---|
[9316609] | 273 | [PYTHONCLASS]_methods, /* tp_methods */ |
---|
| 274 | [PYTHONCLASS]_members, /* tp_members */ |
---|
[ae3ce4e] | 275 | 0, /* tp_getset */ |
---|
| 276 | 0, /* tp_base */ |
---|
| 277 | 0, /* tp_dict */ |
---|
| 278 | 0, /* tp_descr_get */ |
---|
| 279 | 0, /* tp_descr_set */ |
---|
| 280 | 0, /* tp_dictoffset */ |
---|
[9316609] | 281 | (initproc)[PYTHONCLASS]_init, /* tp_init */ |
---|
[ae3ce4e] | 282 | 0, /* tp_alloc */ |
---|
[9316609] | 283 | [PYTHONCLASS]_new, /* tp_new */ |
---|
[ae3ce4e] | 284 | }; |
---|
| 285 | |
---|
| 286 | |
---|
| 287 | static PyMethodDef module_methods[] = { |
---|
| 288 | {NULL} |
---|
| 289 | }; |
---|
| 290 | |
---|
| 291 | /** |
---|
| 292 | * Function used to add the model class to a module |
---|
| 293 | * @param module: module to add the class to |
---|
| 294 | */ |
---|
[9316609] | 295 | void add[PYTHONCLASS](PyObject *module) { |
---|
[ae3ce4e] | 296 | PyObject *d; |
---|
| 297 | |
---|
[9316609] | 298 | if (PyType_Ready(&[PYTHONCLASS]Type) < 0) |
---|
[ae3ce4e] | 299 | return; |
---|
| 300 | |
---|
[9316609] | 301 | Py_INCREF(&[PYTHONCLASS]Type); |
---|
| 302 | PyModule_AddObject(module, "[PYTHONCLASS]", (PyObject *)&[PYTHONCLASS]Type); |
---|
[ae3ce4e] | 303 | |
---|
| 304 | d = PyModule_GetDict(module); |
---|
[9316609] | 305 | [PYTHONCLASS]Error = PyErr_NewException("[PYTHONCLASS].error", NULL, NULL); |
---|
| 306 | PyDict_SetItemString(d, "[PYTHONCLASS]Error", [PYTHONCLASS]Error); |
---|
[ae3ce4e] | 307 | } |
---|
| 308 | |
---|