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