[ae3ce4e] | 1 | /** CDispCylinderModel |
---|
| 2 | * |
---|
| 3 | * C extension |
---|
| 4 | * |
---|
| 5 | * WARNING: THIS FILE WAS GENERATED BY WRAPPERGENERATOR.PY |
---|
| 6 | * DO NOT MODIFY THIS FILE, MODIFY disperse_cylinder.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 "disperse_cylinder.h" |
---|
| 20 | |
---|
| 21 | static double UNDEFINED = 1000010010.1234567; |
---|
| 22 | |
---|
| 23 | /// Error object for raised exceptions |
---|
| 24 | static PyObject * CDispCylinderModelError = NULL; |
---|
| 25 | |
---|
| 26 | |
---|
| 27 | // Class definition |
---|
| 28 | typedef struct { |
---|
| 29 | PyObject_HEAD |
---|
| 30 | /// Parameters |
---|
| 31 | PyObject * params; |
---|
| 32 | /// Log for unit testing |
---|
| 33 | PyObject * log; |
---|
| 34 | /// Model parameters |
---|
| 35 | DispCylinderParameters model_pars; |
---|
| 36 | } CDispCylinderModel; |
---|
| 37 | |
---|
| 38 | |
---|
| 39 | static void |
---|
| 40 | CDispCylinderModel_dealloc(CDispCylinderModel* self) |
---|
| 41 | { |
---|
| 42 | self->ob_type->tp_free((PyObject*)self); |
---|
| 43 | |
---|
| 44 | |
---|
| 45 | } |
---|
| 46 | |
---|
| 47 | static PyObject * |
---|
| 48 | CDispCylinderModel_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
---|
| 49 | { |
---|
| 50 | CDispCylinderModel *self; |
---|
| 51 | |
---|
| 52 | self = (CDispCylinderModel *)type->tp_alloc(type, 0); |
---|
| 53 | |
---|
| 54 | return (PyObject *)self; |
---|
| 55 | } |
---|
| 56 | |
---|
| 57 | static int |
---|
| 58 | CDispCylinderModel_init(CDispCylinderModel *self, PyObject *args, PyObject *kwds) |
---|
| 59 | { |
---|
| 60 | if (self != NULL) { |
---|
| 61 | |
---|
| 62 | // Create parameters |
---|
| 63 | self->params = PyDict_New(); |
---|
| 64 | |
---|
| 65 | // Initialize parameter dictionary |
---|
| 66 | PyDict_SetItemString(self->params,"scale",Py_BuildValue("d",1.000000)); |
---|
| 67 | PyDict_SetItemString(self->params,"sigma_phi",Py_BuildValue("d",0.000000)); |
---|
| 68 | PyDict_SetItemString(self->params,"sigma_radius",Py_BuildValue("d",0.000000)); |
---|
| 69 | PyDict_SetItemString(self->params,"sigma_theta",Py_BuildValue("d",0.000000)); |
---|
| 70 | PyDict_SetItemString(self->params,"length",Py_BuildValue("d",400.000000)); |
---|
| 71 | PyDict_SetItemString(self->params,"cyl_theta",Py_BuildValue("d",1.570000)); |
---|
| 72 | PyDict_SetItemString(self->params,"background",Py_BuildValue("d",0.000000)); |
---|
| 73 | PyDict_SetItemString(self->params,"radius",Py_BuildValue("d",20.000000)); |
---|
| 74 | PyDict_SetItemString(self->params,"n_pts",Py_BuildValue("d",25.000000)); |
---|
| 75 | PyDict_SetItemString(self->params,"contrast",Py_BuildValue("d",0.000003)); |
---|
| 76 | PyDict_SetItemString(self->params,"cyl_phi",Py_BuildValue("d",0.000000)); |
---|
| 77 | |
---|
| 78 | |
---|
| 79 | // Create empty log |
---|
| 80 | self->log = PyDict_New(); |
---|
| 81 | |
---|
| 82 | |
---|
| 83 | |
---|
| 84 | } |
---|
| 85 | return 0; |
---|
| 86 | } |
---|
| 87 | |
---|
| 88 | static PyMemberDef CDispCylinderModel_members[] = { |
---|
| 89 | {"params", T_OBJECT, offsetof(CDispCylinderModel, params), 0, |
---|
| 90 | "Parameters"}, |
---|
| 91 | {"log", T_OBJECT, offsetof(CDispCylinderModel, log), 0, |
---|
| 92 | "Log"}, |
---|
| 93 | {NULL} /* Sentinel */ |
---|
| 94 | }; |
---|
| 95 | |
---|
| 96 | /** Read double from PyObject |
---|
| 97 | @param p PyObject |
---|
| 98 | @return double |
---|
| 99 | */ |
---|
| 100 | double CDispCylinderModel_readDouble(PyObject *p) { |
---|
| 101 | if (PyFloat_Check(p)==1) { |
---|
| 102 | return (double)(((PyFloatObject *)(p))->ob_fval); |
---|
| 103 | } else if (PyInt_Check(p)==1) { |
---|
| 104 | return (double)(((PyIntObject *)(p))->ob_ival); |
---|
| 105 | } else if (PyLong_Check(p)==1) { |
---|
| 106 | return (double)PyLong_AsLong(p); |
---|
| 107 | } else { |
---|
| 108 | return 0.0; |
---|
| 109 | } |
---|
| 110 | } |
---|
| 111 | |
---|
| 112 | |
---|
| 113 | /** |
---|
| 114 | * Function to call to evaluate model |
---|
| 115 | * @param args: input q or [q,phi] |
---|
| 116 | * @return: function value |
---|
| 117 | */ |
---|
| 118 | static PyObject * run(CDispCylinderModel *self, PyObject *args) { |
---|
| 119 | double q_value, phi_value; |
---|
| 120 | PyObject* pars; |
---|
| 121 | int npars; |
---|
| 122 | |
---|
| 123 | // Get parameters |
---|
| 124 | |
---|
| 125 | // Reader parameter dictionary |
---|
| 126 | self->model_pars.scale = PyFloat_AsDouble( PyDict_GetItemString(self->params, "scale") ); |
---|
| 127 | self->model_pars.sigma_phi = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sigma_phi") ); |
---|
| 128 | self->model_pars.sigma_radius = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sigma_radius") ); |
---|
| 129 | self->model_pars.sigma_theta = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sigma_theta") ); |
---|
| 130 | self->model_pars.length = PyFloat_AsDouble( PyDict_GetItemString(self->params, "length") ); |
---|
| 131 | self->model_pars.cyl_theta = PyFloat_AsDouble( PyDict_GetItemString(self->params, "cyl_theta") ); |
---|
| 132 | self->model_pars.background = PyFloat_AsDouble( PyDict_GetItemString(self->params, "background") ); |
---|
| 133 | self->model_pars.radius = PyFloat_AsDouble( PyDict_GetItemString(self->params, "radius") ); |
---|
| 134 | self->model_pars.n_pts = PyFloat_AsDouble( PyDict_GetItemString(self->params, "n_pts") ); |
---|
| 135 | self->model_pars.contrast = PyFloat_AsDouble( PyDict_GetItemString(self->params, "contrast") ); |
---|
| 136 | self->model_pars.cyl_phi = PyFloat_AsDouble( PyDict_GetItemString(self->params, "cyl_phi") ); |
---|
| 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(CDispCylinderModelError, |
---|
| 142 | "CDispCylinderModel.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(CDispCylinderModelError, |
---|
| 153 | "CDispCylinderModel.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 = CDispCylinderModel_readDouble(PyList_GET_ITEM(pars,0)); |
---|
| 159 | phi_value = CDispCylinderModel_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",disperse_cylinder_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 = CDispCylinderModel_readDouble(pars); |
---|
| 170 | |
---|
| 171 | return Py_BuildValue("d",disperse_cylinder_analytical_1D(&(self->model_pars),q_value)); |
---|
| 172 | } |
---|
| 173 | } |
---|
| 174 | |
---|
| 175 | static PyObject * reset(CDispCylinderModel *self, PyObject *args) { |
---|
| 176 | |
---|
| 177 | |
---|
| 178 | return Py_BuildValue("d",0.0); |
---|
| 179 | } |
---|
| 180 | |
---|
| 181 | |
---|
| 182 | static PyMethodDef CDispCylinderModel_methods[] = { |
---|
| 183 | {"run", (PyCFunction)run , METH_VARARGS, |
---|
| 184 | "Evaluate the model at a given Q"}, |
---|
| 185 | {"reset", (PyCFunction)reset , METH_VARARGS, |
---|
| 186 | "Reset pair correlation"}, |
---|
| 187 | //{"numerical_1D", (PyCFunction)numerical_1D , METH_VARARGS, |
---|
| 188 | // "Evaluate the 1D model at a given Q"}, |
---|
| 189 | {NULL} |
---|
| 190 | }; |
---|
| 191 | |
---|
| 192 | static PyTypeObject CDispCylinderModelType = { |
---|
| 193 | PyObject_HEAD_INIT(NULL) |
---|
| 194 | 0, /*ob_size*/ |
---|
| 195 | "CDispCylinderModel", /*tp_name*/ |
---|
| 196 | sizeof(CDispCylinderModel), /*tp_basicsize*/ |
---|
| 197 | 0, /*tp_itemsize*/ |
---|
| 198 | (destructor)CDispCylinderModel_dealloc, /*tp_dealloc*/ |
---|
| 199 | 0, /*tp_print*/ |
---|
| 200 | 0, /*tp_getattr*/ |
---|
| 201 | 0, /*tp_setattr*/ |
---|
| 202 | 0, /*tp_compare*/ |
---|
| 203 | 0, /*tp_repr*/ |
---|
| 204 | 0, /*tp_as_number*/ |
---|
| 205 | 0, /*tp_as_sequence*/ |
---|
| 206 | 0, /*tp_as_mapping*/ |
---|
| 207 | 0, /*tp_hash */ |
---|
| 208 | 0, /*tp_call*/ |
---|
| 209 | 0, /*tp_str*/ |
---|
| 210 | 0, /*tp_getattro*/ |
---|
| 211 | 0, /*tp_setattro*/ |
---|
| 212 | 0, /*tp_as_buffer*/ |
---|
| 213 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/ |
---|
| 214 | "CDispCylinderModel objects", /* tp_doc */ |
---|
| 215 | 0, /* tp_traverse */ |
---|
| 216 | 0, /* tp_clear */ |
---|
| 217 | 0, /* tp_richcompare */ |
---|
| 218 | 0, /* tp_weaklistoffset */ |
---|
| 219 | 0, /* tp_iter */ |
---|
| 220 | 0, /* tp_iternext */ |
---|
| 221 | CDispCylinderModel_methods, /* tp_methods */ |
---|
| 222 | CDispCylinderModel_members, /* tp_members */ |
---|
| 223 | 0, /* tp_getset */ |
---|
| 224 | 0, /* tp_base */ |
---|
| 225 | 0, /* tp_dict */ |
---|
| 226 | 0, /* tp_descr_get */ |
---|
| 227 | 0, /* tp_descr_set */ |
---|
| 228 | 0, /* tp_dictoffset */ |
---|
| 229 | (initproc)CDispCylinderModel_init, /* tp_init */ |
---|
| 230 | 0, /* tp_alloc */ |
---|
| 231 | CDispCylinderModel_new, /* tp_new */ |
---|
| 232 | }; |
---|
| 233 | |
---|
| 234 | |
---|
| 235 | static PyMethodDef module_methods[] = { |
---|
| 236 | {NULL} |
---|
| 237 | }; |
---|
| 238 | |
---|
| 239 | /** |
---|
| 240 | * Function used to add the model class to a module |
---|
| 241 | * @param module: module to add the class to |
---|
| 242 | */ |
---|
| 243 | void addCDispCylinderModel(PyObject *module) { |
---|
| 244 | PyObject *d; |
---|
| 245 | |
---|
| 246 | if (PyType_Ready(&CDispCylinderModelType) < 0) |
---|
| 247 | return; |
---|
| 248 | |
---|
| 249 | Py_INCREF(&CDispCylinderModelType); |
---|
| 250 | PyModule_AddObject(module, "CDispCylinderModel", (PyObject *)&CDispCylinderModelType); |
---|
| 251 | |
---|
| 252 | d = PyModule_GetDict(module); |
---|
| 253 | CDispCylinderModelError = PyErr_NewException("CDispCylinderModel.error", NULL, NULL); |
---|
| 254 | PyDict_SetItemString(d, "CDispCylinderModelError", CDispCylinderModelError); |
---|
| 255 | } |
---|
| 256 | |
---|