[a8d6888] | 1 | /** CSphereModel |
---|
[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 |
---|
[a8d6888] | 22 | static PyObject * CSphereModelError = 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; |
---|
[a8d6888] | 34 | } CSphereModel; |
---|
[ae3ce4e] | 35 | |
---|
| 36 | |
---|
| 37 | static void |
---|
[a8d6888] | 38 | CSphereModel_dealloc(CSphereModel* self) |
---|
[ae3ce4e] | 39 | { |
---|
| 40 | self->ob_type->tp_free((PyObject*)self); |
---|
| 41 | |
---|
| 42 | |
---|
| 43 | } |
---|
| 44 | |
---|
| 45 | static PyObject * |
---|
[a8d6888] | 46 | CSphereModel_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
---|
[ae3ce4e] | 47 | { |
---|
[a8d6888] | 48 | CSphereModel *self; |
---|
[ae3ce4e] | 49 | |
---|
[a8d6888] | 50 | self = (CSphereModel *)type->tp_alloc(type, 0); |
---|
[ae3ce4e] | 51 | |
---|
| 52 | return (PyObject *)self; |
---|
| 53 | } |
---|
| 54 | |
---|
| 55 | static int |
---|
[a8d6888] | 56 | CSphereModel_init(CSphereModel *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 |
---|
[a8d6888] | 64 | PyDict_SetItemString(self->params,"scale",Py_BuildValue("d",1.000000)); |
---|
[ae3ce4e] | 65 | PyDict_SetItemString(self->params,"radius",Py_BuildValue("d",60.000000)); |
---|
| 66 | PyDict_SetItemString(self->params,"background",Py_BuildValue("d",0.000000)); |
---|
[a8d6888] | 67 | PyDict_SetItemString(self->params,"contrast",Py_BuildValue("d",0.000001)); |
---|
[ae3ce4e] | 68 | |
---|
| 69 | |
---|
| 70 | // Create empty log |
---|
| 71 | self->log = PyDict_New(); |
---|
| 72 | |
---|
| 73 | |
---|
| 74 | |
---|
| 75 | } |
---|
| 76 | return 0; |
---|
| 77 | } |
---|
| 78 | |
---|
[a8d6888] | 79 | static PyMemberDef CSphereModel_members[] = { |
---|
| 80 | {"params", T_OBJECT, offsetof(CSphereModel, params), 0, |
---|
[ae3ce4e] | 81 | "Parameters"}, |
---|
[a8d6888] | 82 | {"log", T_OBJECT, offsetof(CSphereModel, log), 0, |
---|
[ae3ce4e] | 83 | "Log"}, |
---|
| 84 | {NULL} /* Sentinel */ |
---|
| 85 | }; |
---|
| 86 | |
---|
| 87 | /** Read double from PyObject |
---|
| 88 | @param p PyObject |
---|
| 89 | @return double |
---|
| 90 | */ |
---|
[a8d6888] | 91 | double CSphereModel_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 | */ |
---|
[a8d6888] | 109 | static PyObject * run(CSphereModel *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) ) { |
---|
[a8d6888] | 125 | PyErr_SetString(CSphereModelError, |
---|
| 126 | "CSphereModel.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) { |
---|
[a8d6888] | 136 | PyErr_SetString(CSphereModelError, |
---|
| 137 | "CSphereModel.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) |
---|
[a8d6888] | 142 | q_value = CSphereModel_readDouble(PyList_GET_ITEM(pars,0)); |
---|
| 143 | phi_value = CSphereModel_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) |
---|
[a8d6888] | 153 | q_value = CSphereModel_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 | */ |
---|
[a8d6888] | 164 | static PyObject * runXY(CSphereModel *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) ) { |
---|
[a8d6888] | 180 | PyErr_SetString(CSphereModelError, |
---|
| 181 | "CSphereModel.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) { |
---|
[a8d6888] | 191 | PyErr_SetString(CSphereModelError, |
---|
| 192 | "CSphereModel.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) |
---|
[a8d6888] | 197 | qx_value = CSphereModel_readDouble(PyList_GET_ITEM(pars,0)); |
---|
| 198 | qy_value = CSphereModel_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) |
---|
[a8d6888] | 204 | qx_value = CSphereModel_readDouble(pars); |
---|
[ae3ce4e] | 205 | |
---|
| 206 | return Py_BuildValue("d",sphere_analytical_1D(&(self->model_pars),qx_value)); |
---|
| 207 | } |
---|
| 208 | } |
---|
| 209 | |
---|
[a8d6888] | 210 | static PyObject * reset(CSphereModel *self, PyObject *args) { |
---|
[ae3ce4e] | 211 | |
---|
| 212 | |
---|
| 213 | return Py_BuildValue("d",0.0); |
---|
| 214 | } |
---|
| 215 | |
---|
| 216 | |
---|
[a8d6888] | 217 | static PyMethodDef CSphereModel_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 | |
---|
[a8d6888] | 229 | static PyTypeObject CSphereModelType = { |
---|
[ae3ce4e] | 230 | PyObject_HEAD_INIT(NULL) |
---|
| 231 | 0, /*ob_size*/ |
---|
[a8d6888] | 232 | "CSphereModel", /*tp_name*/ |
---|
| 233 | sizeof(CSphereModel), /*tp_basicsize*/ |
---|
[ae3ce4e] | 234 | 0, /*tp_itemsize*/ |
---|
[a8d6888] | 235 | (destructor)CSphereModel_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*/ |
---|
[a8d6888] | 251 | "CSphereModel 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 */ |
---|
[a8d6888] | 258 | CSphereModel_methods, /* tp_methods */ |
---|
| 259 | CSphereModel_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 */ |
---|
[a8d6888] | 266 | (initproc)CSphereModel_init, /* tp_init */ |
---|
[ae3ce4e] | 267 | 0, /* tp_alloc */ |
---|
[a8d6888] | 268 | CSphereModel_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 | */ |
---|
[a8d6888] | 280 | void addCSphereModel(PyObject *module) { |
---|
[ae3ce4e] | 281 | PyObject *d; |
---|
| 282 | |
---|
[a8d6888] | 283 | if (PyType_Ready(&CSphereModelType) < 0) |
---|
[ae3ce4e] | 284 | return; |
---|
| 285 | |
---|
[a8d6888] | 286 | Py_INCREF(&CSphereModelType); |
---|
| 287 | PyModule_AddObject(module, "CSphereModel", (PyObject *)&CSphereModelType); |
---|
[ae3ce4e] | 288 | |
---|
| 289 | d = PyModule_GetDict(module); |
---|
[a8d6888] | 290 | CSphereModelError = PyErr_NewException("CSphereModel.error", NULL, NULL); |
---|
| 291 | PyDict_SetItemString(d, "CSphereModelError", CSphereModelError); |
---|
[ae3ce4e] | 292 | } |
---|
| 293 | |
---|