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