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