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