1 | /** CHollowCylinderModel |
---|
2 | * |
---|
3 | * C extension |
---|
4 | * |
---|
5 | * WARNING: THIS FILE WAS GENERATED BY WRAPPERGENERATOR.PY |
---|
6 | * DO NOT MODIFY THIS FILE, MODIFY hollow_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 "hollow_cylinder.h" |
---|
20 | |
---|
21 | /// Error object for raised exceptions |
---|
22 | static PyObject * CHollowCylinderModelError = 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 | HollowCylinderParameters model_pars; |
---|
34 | } CHollowCylinderModel; |
---|
35 | |
---|
36 | |
---|
37 | static void |
---|
38 | CHollowCylinderModel_dealloc(CHollowCylinderModel* self) |
---|
39 | { |
---|
40 | self->ob_type->tp_free((PyObject*)self); |
---|
41 | |
---|
42 | |
---|
43 | } |
---|
44 | |
---|
45 | static PyObject * |
---|
46 | CHollowCylinderModel_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
---|
47 | { |
---|
48 | CHollowCylinderModel *self; |
---|
49 | |
---|
50 | self = (CHollowCylinderModel *)type->tp_alloc(type, 0); |
---|
51 | |
---|
52 | return (PyObject *)self; |
---|
53 | } |
---|
54 | |
---|
55 | static int |
---|
56 | CHollowCylinderModel_init(CHollowCylinderModel *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,"core_radius",Py_BuildValue("d",20.000000)); |
---|
66 | PyDict_SetItemString(self->params,"axis_theta",Py_BuildValue("d",1.570000)); |
---|
67 | PyDict_SetItemString(self->params,"length",Py_BuildValue("d",400.000000)); |
---|
68 | PyDict_SetItemString(self->params,"axis_phi",Py_BuildValue("d",0.000000)); |
---|
69 | PyDict_SetItemString(self->params,"background",Py_BuildValue("d",0.010000)); |
---|
70 | PyDict_SetItemString(self->params,"shell_radius",Py_BuildValue("d",30.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 CHollowCylinderModel_members[] = { |
---|
84 | {"params", T_OBJECT, offsetof(CHollowCylinderModel, params), 0, |
---|
85 | "Parameters"}, |
---|
86 | {"log", T_OBJECT, offsetof(CHollowCylinderModel, log), 0, |
---|
87 | "Log"}, |
---|
88 | {NULL} /* Sentinel */ |
---|
89 | }; |
---|
90 | |
---|
91 | /** Read double from PyObject |
---|
92 | @param p PyObject |
---|
93 | @return double |
---|
94 | */ |
---|
95 | double CHollowCylinderModel_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(CHollowCylinderModel *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.core_radius = PyFloat_AsDouble( PyDict_GetItemString(self->params, "core_radius") ); |
---|
123 | self->model_pars.axis_theta = PyFloat_AsDouble( PyDict_GetItemString(self->params, "axis_theta") ); |
---|
124 | self->model_pars.length = PyFloat_AsDouble( PyDict_GetItemString(self->params, "length") ); |
---|
125 | self->model_pars.axis_phi = PyFloat_AsDouble( PyDict_GetItemString(self->params, "axis_phi") ); |
---|
126 | self->model_pars.background = PyFloat_AsDouble( PyDict_GetItemString(self->params, "background") ); |
---|
127 | self->model_pars.shell_radius = PyFloat_AsDouble( PyDict_GetItemString(self->params, "shell_radius") ); |
---|
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(CHollowCylinderModelError, |
---|
134 | "CHollowCylinderModel.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(CHollowCylinderModelError, |
---|
145 | "CHollowCylinderModel.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 = CHollowCylinderModel_readDouble(PyList_GET_ITEM(pars,0)); |
---|
151 | phi_value = CHollowCylinderModel_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",hollow_cylinder_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 = CHollowCylinderModel_readDouble(pars); |
---|
162 | |
---|
163 | return Py_BuildValue("d",hollow_cylinder_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(CHollowCylinderModel *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.core_radius = PyFloat_AsDouble( PyDict_GetItemString(self->params, "core_radius") ); |
---|
182 | self->model_pars.axis_theta = PyFloat_AsDouble( PyDict_GetItemString(self->params, "axis_theta") ); |
---|
183 | self->model_pars.length = PyFloat_AsDouble( PyDict_GetItemString(self->params, "length") ); |
---|
184 | self->model_pars.axis_phi = PyFloat_AsDouble( PyDict_GetItemString(self->params, "axis_phi") ); |
---|
185 | self->model_pars.background = PyFloat_AsDouble( PyDict_GetItemString(self->params, "background") ); |
---|
186 | self->model_pars.shell_radius = PyFloat_AsDouble( PyDict_GetItemString(self->params, "shell_radius") ); |
---|
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(CHollowCylinderModelError, |
---|
193 | "CHollowCylinderModel.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(CHollowCylinderModelError, |
---|
204 | "CHollowCylinderModel.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 = CHollowCylinderModel_readDouble(PyList_GET_ITEM(pars,0)); |
---|
210 | qy_value = CHollowCylinderModel_readDouble(PyList_GET_ITEM(pars,1)); |
---|
211 | return Py_BuildValue("d",hollow_cylinder_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 = CHollowCylinderModel_readDouble(pars); |
---|
217 | |
---|
218 | return Py_BuildValue("d",hollow_cylinder_analytical_1D(&(self->model_pars),qx_value)); |
---|
219 | } |
---|
220 | } |
---|
221 | |
---|
222 | static PyObject * reset(CHollowCylinderModel *self, PyObject *args) { |
---|
223 | |
---|
224 | |
---|
225 | return Py_BuildValue("d",0.0); |
---|
226 | } |
---|
227 | |
---|
228 | |
---|
229 | static PyMethodDef CHollowCylinderModel_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 CHollowCylinderModelType = { |
---|
242 | PyObject_HEAD_INIT(NULL) |
---|
243 | 0, /*ob_size*/ |
---|
244 | "CHollowCylinderModel", /*tp_name*/ |
---|
245 | sizeof(CHollowCylinderModel), /*tp_basicsize*/ |
---|
246 | 0, /*tp_itemsize*/ |
---|
247 | (destructor)CHollowCylinderModel_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 | "CHollowCylinderModel 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 | CHollowCylinderModel_methods, /* tp_methods */ |
---|
271 | CHollowCylinderModel_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)CHollowCylinderModel_init, /* tp_init */ |
---|
279 | 0, /* tp_alloc */ |
---|
280 | CHollowCylinderModel_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 addCHollowCylinderModel(PyObject *module) { |
---|
293 | PyObject *d; |
---|
294 | |
---|
295 | if (PyType_Ready(&CHollowCylinderModelType) < 0) |
---|
296 | return; |
---|
297 | |
---|
298 | Py_INCREF(&CHollowCylinderModelType); |
---|
299 | PyModule_AddObject(module, "CHollowCylinderModel", (PyObject *)&CHollowCylinderModelType); |
---|
300 | |
---|
301 | d = PyModule_GetDict(module); |
---|
302 | CHollowCylinderModelError = PyErr_NewException("CHollowCylinderModel.error", NULL, NULL); |
---|
303 | PyDict_SetItemString(d, "CHollowCylinderModelError", CHollowCylinderModelError); |
---|
304 | } |
---|
305 | |
---|