[8a48713] | 1 | /** CParallelepipedModel |
---|
| 2 | * |
---|
| 3 | * C extension |
---|
| 4 | * |
---|
| 5 | * WARNING: THIS FILE WAS GENERATED BY WRAPPERGENERATOR.PY |
---|
[a8d6888] | 6 | * DO NOT MODIFY THIS FILE, MODIFY parallelepiped.h |
---|
[8a48713] | 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 | |
---|
[a8d6888] | 19 | #include "parallelepiped.h" |
---|
[8a48713] | 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 |
---|
[a8d6888] | 64 | PyDict_SetItemString(self->params,"short_a",Py_BuildValue("d",35.000000)); |
---|
| 65 | PyDict_SetItemString(self->params,"short_b",Py_BuildValue("d",75.000000)); |
---|
[8a48713] | 66 | PyDict_SetItemString(self->params,"scale",Py_BuildValue("d",1.000000)); |
---|
[a8d6888] | 67 | PyDict_SetItemString(self->params,"long_c",Py_BuildValue("d",400.000000)); |
---|
| 68 | PyDict_SetItemString(self->params,"parallel_psi",Py_BuildValue("d",0.000000)); |
---|
| 69 | PyDict_SetItemString(self->params,"parallel_phi",Py_BuildValue("d",0.000000)); |
---|
| 70 | PyDict_SetItemString(self->params,"parallel_theta",Py_BuildValue("d",0.000000)); |
---|
[8a48713] | 71 | PyDict_SetItemString(self->params,"background",Py_BuildValue("d",0.000000)); |
---|
| 72 | PyDict_SetItemString(self->params,"contrast",Py_BuildValue("d",0.000005)); |
---|
| 73 | |
---|
| 74 | |
---|
| 75 | // Create empty log |
---|
| 76 | self->log = PyDict_New(); |
---|
| 77 | |
---|
| 78 | |
---|
| 79 | |
---|
| 80 | } |
---|
| 81 | return 0; |
---|
| 82 | } |
---|
| 83 | |
---|
| 84 | static PyMemberDef CParallelepipedModel_members[] = { |
---|
| 85 | {"params", T_OBJECT, offsetof(CParallelepipedModel, params), 0, |
---|
| 86 | "Parameters"}, |
---|
| 87 | {"log", T_OBJECT, offsetof(CParallelepipedModel, log), 0, |
---|
| 88 | "Log"}, |
---|
| 89 | {NULL} /* Sentinel */ |
---|
| 90 | }; |
---|
| 91 | |
---|
| 92 | /** Read double from PyObject |
---|
| 93 | @param p PyObject |
---|
| 94 | @return double |
---|
| 95 | */ |
---|
| 96 | double CParallelepipedModel_readDouble(PyObject *p) { |
---|
| 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 | */ |
---|
| 114 | static PyObject * run(CParallelepipedModel *self, PyObject *args) { |
---|
| 115 | double q_value, phi_value; |
---|
| 116 | PyObject* pars; |
---|
| 117 | int npars; |
---|
| 118 | |
---|
| 119 | // Get parameters |
---|
| 120 | |
---|
| 121 | // Reader parameter dictionary |
---|
[a8d6888] | 122 | self->model_pars.short_a = PyFloat_AsDouble( PyDict_GetItemString(self->params, "short_a") ); |
---|
| 123 | self->model_pars.short_b = PyFloat_AsDouble( PyDict_GetItemString(self->params, "short_b") ); |
---|
[8a48713] | 124 | self->model_pars.scale = PyFloat_AsDouble( PyDict_GetItemString(self->params, "scale") ); |
---|
[a8d6888] | 125 | self->model_pars.long_c = PyFloat_AsDouble( PyDict_GetItemString(self->params, "long_c") ); |
---|
| 126 | self->model_pars.parallel_psi = PyFloat_AsDouble( PyDict_GetItemString(self->params, "parallel_psi") ); |
---|
[8a48713] | 127 | self->model_pars.parallel_phi = PyFloat_AsDouble( PyDict_GetItemString(self->params, "parallel_phi") ); |
---|
| 128 | self->model_pars.parallel_theta = PyFloat_AsDouble( PyDict_GetItemString(self->params, "parallel_theta") ); |
---|
| 129 | self->model_pars.background = PyFloat_AsDouble( PyDict_GetItemString(self->params, "background") ); |
---|
| 130 | self->model_pars.contrast = PyFloat_AsDouble( PyDict_GetItemString(self->params, "contrast") ); |
---|
| 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) ) { |
---|
| 135 | PyErr_SetString(CParallelepipedModelError, |
---|
| 136 | "CParallelepipedModel.run expects a q value."); |
---|
| 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) { |
---|
| 146 | PyErr_SetString(CParallelepipedModelError, |
---|
| 147 | "CParallelepipedModel.run expects a double or a list of dimension 2."); |
---|
| 148 | return NULL; |
---|
| 149 | } |
---|
| 150 | // We have a vector q, get the q and phi values at which |
---|
| 151 | // to evaluate I(q,phi) |
---|
| 152 | q_value = CParallelepipedModel_readDouble(PyList_GET_ITEM(pars,0)); |
---|
| 153 | phi_value = CParallelepipedModel_readDouble(PyList_GET_ITEM(pars,1)); |
---|
| 154 | // Skip zero |
---|
| 155 | if (q_value==0) { |
---|
| 156 | return Py_BuildValue("d",0.0); |
---|
| 157 | } |
---|
[a8d6888] | 158 | return Py_BuildValue("d",parallelepiped_analytical_2D(&(self->model_pars),q_value,phi_value)); |
---|
[8a48713] | 159 | |
---|
| 160 | } else { |
---|
| 161 | |
---|
| 162 | // We have a scalar q, we will evaluate I(q) |
---|
| 163 | q_value = CParallelepipedModel_readDouble(pars); |
---|
| 164 | |
---|
[a8d6888] | 165 | return Py_BuildValue("d",parallelepiped_analytical_1D(&(self->model_pars),q_value)); |
---|
[8a48713] | 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 | */ |
---|
| 174 | static PyObject * runXY(CParallelepipedModel *self, PyObject *args) { |
---|
| 175 | double qx_value, qy_value; |
---|
| 176 | PyObject* pars; |
---|
| 177 | int npars; |
---|
| 178 | |
---|
| 179 | // Get parameters |
---|
| 180 | |
---|
| 181 | // Reader parameter dictionary |
---|
[a8d6888] | 182 | self->model_pars.short_a = PyFloat_AsDouble( PyDict_GetItemString(self->params, "short_a") ); |
---|
| 183 | self->model_pars.short_b = PyFloat_AsDouble( PyDict_GetItemString(self->params, "short_b") ); |
---|
[8a48713] | 184 | self->model_pars.scale = PyFloat_AsDouble( PyDict_GetItemString(self->params, "scale") ); |
---|
[a8d6888] | 185 | self->model_pars.long_c = PyFloat_AsDouble( PyDict_GetItemString(self->params, "long_c") ); |
---|
| 186 | self->model_pars.parallel_psi = PyFloat_AsDouble( PyDict_GetItemString(self->params, "parallel_psi") ); |
---|
[8a48713] | 187 | self->model_pars.parallel_phi = PyFloat_AsDouble( PyDict_GetItemString(self->params, "parallel_phi") ); |
---|
| 188 | self->model_pars.parallel_theta = PyFloat_AsDouble( PyDict_GetItemString(self->params, "parallel_theta") ); |
---|
| 189 | self->model_pars.background = PyFloat_AsDouble( PyDict_GetItemString(self->params, "background") ); |
---|
| 190 | self->model_pars.contrast = PyFloat_AsDouble( PyDict_GetItemString(self->params, "contrast") ); |
---|
| 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) ) { |
---|
| 195 | PyErr_SetString(CParallelepipedModelError, |
---|
| 196 | "CParallelepipedModel.run expects a q value."); |
---|
| 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) { |
---|
| 206 | PyErr_SetString(CParallelepipedModelError, |
---|
| 207 | "CParallelepipedModel.run expects a double or a list of dimension 2."); |
---|
| 208 | return NULL; |
---|
| 209 | } |
---|
| 210 | // We have a vector q, get the qx and qy values at which |
---|
| 211 | // to evaluate I(qx,qy) |
---|
| 212 | qx_value = CParallelepipedModel_readDouble(PyList_GET_ITEM(pars,0)); |
---|
| 213 | qy_value = CParallelepipedModel_readDouble(PyList_GET_ITEM(pars,1)); |
---|
[a8d6888] | 214 | return Py_BuildValue("d",parallelepiped_analytical_2DXY(&(self->model_pars),qx_value,qy_value)); |
---|
[8a48713] | 215 | |
---|
| 216 | } else { |
---|
| 217 | |
---|
| 218 | // We have a scalar q, we will evaluate I(q) |
---|
| 219 | qx_value = CParallelepipedModel_readDouble(pars); |
---|
| 220 | |
---|
[a8d6888] | 221 | return Py_BuildValue("d",parallelepiped_analytical_1D(&(self->model_pars),qx_value)); |
---|
[8a48713] | 222 | } |
---|
| 223 | } |
---|
| 224 | |
---|
| 225 | static PyObject * reset(CParallelepipedModel *self, PyObject *args) { |
---|
| 226 | |
---|
| 227 | |
---|
| 228 | return Py_BuildValue("d",0.0); |
---|
| 229 | } |
---|
| 230 | |
---|
| 231 | |
---|
| 232 | static PyMethodDef CParallelepipedModel_methods[] = { |
---|
| 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 | |
---|
| 244 | static PyTypeObject CParallelepipedModelType = { |
---|
| 245 | PyObject_HEAD_INIT(NULL) |
---|
| 246 | 0, /*ob_size*/ |
---|
| 247 | "CParallelepipedModel", /*tp_name*/ |
---|
| 248 | sizeof(CParallelepipedModel), /*tp_basicsize*/ |
---|
| 249 | 0, /*tp_itemsize*/ |
---|
| 250 | (destructor)CParallelepipedModel_dealloc, /*tp_dealloc*/ |
---|
| 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*/ |
---|
| 266 | "CParallelepipedModel objects", /* tp_doc */ |
---|
| 267 | 0, /* tp_traverse */ |
---|
| 268 | 0, /* tp_clear */ |
---|
| 269 | 0, /* tp_richcompare */ |
---|
| 270 | 0, /* tp_weaklistoffset */ |
---|
| 271 | 0, /* tp_iter */ |
---|
| 272 | 0, /* tp_iternext */ |
---|
| 273 | CParallelepipedModel_methods, /* tp_methods */ |
---|
| 274 | CParallelepipedModel_members, /* tp_members */ |
---|
| 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 */ |
---|
| 281 | (initproc)CParallelepipedModel_init, /* tp_init */ |
---|
| 282 | 0, /* tp_alloc */ |
---|
| 283 | CParallelepipedModel_new, /* tp_new */ |
---|
| 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 | */ |
---|
| 295 | void addCParallelepipedModel(PyObject *module) { |
---|
| 296 | PyObject *d; |
---|
| 297 | |
---|
| 298 | if (PyType_Ready(&CParallelepipedModelType) < 0) |
---|
| 299 | return; |
---|
| 300 | |
---|
| 301 | Py_INCREF(&CParallelepipedModelType); |
---|
| 302 | PyModule_AddObject(module, "CParallelepipedModel", (PyObject *)&CParallelepipedModelType); |
---|
| 303 | |
---|
| 304 | d = PyModule_GetDict(module); |
---|
| 305 | CParallelepipedModelError = PyErr_NewException("CParallelepipedModel.error", NULL, NULL); |
---|
| 306 | PyDict_SetItemString(d, "CParallelepipedModelError", CParallelepipedModelError); |
---|
| 307 | } |
---|
| 308 | |
---|