1 | /** CSmearCylinderModel |
---|
2 | * |
---|
3 | * C extension |
---|
4 | * |
---|
5 | * WARNING: THIS FILE WAS GENERATED BY WRAPPERGENERATOR.PY |
---|
6 | * DO NOT MODIFY THIS FILE, MODIFY smeared_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 "smeared_cylinder.h" |
---|
20 | |
---|
21 | static double UNDEFINED = 1000010010.1234567; |
---|
22 | |
---|
23 | /// Error object for raised exceptions |
---|
24 | static PyObject * CSmearCylinderModelError = 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 | SmearCylinderParameters model_pars; |
---|
36 | } CSmearCylinderModel; |
---|
37 | |
---|
38 | |
---|
39 | static void |
---|
40 | CSmearCylinderModel_dealloc(CSmearCylinderModel* self) |
---|
41 | { |
---|
42 | self->ob_type->tp_free((PyObject*)self); |
---|
43 | |
---|
44 | |
---|
45 | } |
---|
46 | |
---|
47 | static PyObject * |
---|
48 | CSmearCylinderModel_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
---|
49 | { |
---|
50 | CSmearCylinderModel *self; |
---|
51 | |
---|
52 | self = (CSmearCylinderModel *)type->tp_alloc(type, 0); |
---|
53 | |
---|
54 | return (PyObject *)self; |
---|
55 | } |
---|
56 | |
---|
57 | static int |
---|
58 | CSmearCylinderModel_init(CSmearCylinderModel *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.000000)); |
---|
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,"contrast",Py_BuildValue("d",0.000003)); |
---|
75 | PyDict_SetItemString(self->params,"cyl_phi",Py_BuildValue("d",0.000000)); |
---|
76 | |
---|
77 | |
---|
78 | // Create empty log |
---|
79 | self->log = PyDict_New(); |
---|
80 | |
---|
81 | |
---|
82 | |
---|
83 | } |
---|
84 | return 0; |
---|
85 | } |
---|
86 | |
---|
87 | static PyMemberDef CSmearCylinderModel_members[] = { |
---|
88 | {"params", T_OBJECT, offsetof(CSmearCylinderModel, params), 0, |
---|
89 | "Parameters"}, |
---|
90 | {"log", T_OBJECT, offsetof(CSmearCylinderModel, log), 0, |
---|
91 | "Log"}, |
---|
92 | {NULL} /* Sentinel */ |
---|
93 | }; |
---|
94 | |
---|
95 | /** Read double from PyObject |
---|
96 | @param p PyObject |
---|
97 | @return double |
---|
98 | */ |
---|
99 | double CSmearCylinderModel_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(CSmearCylinderModel *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.scale = PyFloat_AsDouble( PyDict_GetItemString(self->params, "scale") ); |
---|
126 | self->model_pars.sigma_phi = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sigma_phi") ); |
---|
127 | self->model_pars.sigma_radius = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sigma_radius") ); |
---|
128 | self->model_pars.sigma_theta = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sigma_theta") ); |
---|
129 | self->model_pars.length = PyFloat_AsDouble( PyDict_GetItemString(self->params, "length") ); |
---|
130 | self->model_pars.cyl_theta = PyFloat_AsDouble( PyDict_GetItemString(self->params, "cyl_theta") ); |
---|
131 | self->model_pars.background = PyFloat_AsDouble( PyDict_GetItemString(self->params, "background") ); |
---|
132 | self->model_pars.radius = PyFloat_AsDouble( PyDict_GetItemString(self->params, "radius") ); |
---|
133 | self->model_pars.contrast = PyFloat_AsDouble( PyDict_GetItemString(self->params, "contrast") ); |
---|
134 | self->model_pars.cyl_phi = PyFloat_AsDouble( PyDict_GetItemString(self->params, "cyl_phi") ); |
---|
135 | |
---|
136 | |
---|
137 | // Get input and determine whether we have to supply a 1D or 2D return value. |
---|
138 | if ( !PyArg_ParseTuple(args,"O",&pars) ) { |
---|
139 | PyErr_SetString(CSmearCylinderModelError, |
---|
140 | "CSmearCylinderModel.run expects a q value."); |
---|
141 | return NULL; |
---|
142 | } |
---|
143 | |
---|
144 | // Check params |
---|
145 | if( PyList_Check(pars)==1) { |
---|
146 | |
---|
147 | // Length of list should be 2 for I(q,phi) |
---|
148 | npars = PyList_GET_SIZE(pars); |
---|
149 | if(npars!=2) { |
---|
150 | PyErr_SetString(CSmearCylinderModelError, |
---|
151 | "CSmearCylinderModel.run expects a double or a list of dimension 2."); |
---|
152 | return NULL; |
---|
153 | } |
---|
154 | // We have a vector q, get the q and phi values at which |
---|
155 | // to evaluate I(q,phi) |
---|
156 | q_value = CSmearCylinderModel_readDouble(PyList_GET_ITEM(pars,0)); |
---|
157 | phi_value = CSmearCylinderModel_readDouble(PyList_GET_ITEM(pars,1)); |
---|
158 | // Skip zero |
---|
159 | if (q_value==0) { |
---|
160 | return Py_BuildValue("d",0.0); |
---|
161 | } |
---|
162 | return Py_BuildValue("d",smeared_cylinder_analytical_2D(&(self->model_pars),q_value,phi_value)); |
---|
163 | |
---|
164 | } else { |
---|
165 | |
---|
166 | // We have a scalar q, we will evaluate I(q) |
---|
167 | q_value = CSmearCylinderModel_readDouble(pars); |
---|
168 | |
---|
169 | return Py_BuildValue("d",smeared_cylinder_analytical_1D(&(self->model_pars),q_value)); |
---|
170 | } |
---|
171 | } |
---|
172 | |
---|
173 | static PyObject * reset(CSmearCylinderModel *self, PyObject *args) { |
---|
174 | |
---|
175 | |
---|
176 | return Py_BuildValue("d",0.0); |
---|
177 | } |
---|
178 | |
---|
179 | |
---|
180 | static PyMethodDef CSmearCylinderModel_methods[] = { |
---|
181 | {"run", (PyCFunction)run , METH_VARARGS, |
---|
182 | "Evaluate the model at a given Q"}, |
---|
183 | {"reset", (PyCFunction)reset , METH_VARARGS, |
---|
184 | "Reset pair correlation"}, |
---|
185 | //{"numerical_1D", (PyCFunction)numerical_1D , METH_VARARGS, |
---|
186 | // "Evaluate the 1D model at a given Q"}, |
---|
187 | {NULL} |
---|
188 | }; |
---|
189 | |
---|
190 | static PyTypeObject CSmearCylinderModelType = { |
---|
191 | PyObject_HEAD_INIT(NULL) |
---|
192 | 0, /*ob_size*/ |
---|
193 | "CSmearCylinderModel", /*tp_name*/ |
---|
194 | sizeof(CSmearCylinderModel), /*tp_basicsize*/ |
---|
195 | 0, /*tp_itemsize*/ |
---|
196 | (destructor)CSmearCylinderModel_dealloc, /*tp_dealloc*/ |
---|
197 | 0, /*tp_print*/ |
---|
198 | 0, /*tp_getattr*/ |
---|
199 | 0, /*tp_setattr*/ |
---|
200 | 0, /*tp_compare*/ |
---|
201 | 0, /*tp_repr*/ |
---|
202 | 0, /*tp_as_number*/ |
---|
203 | 0, /*tp_as_sequence*/ |
---|
204 | 0, /*tp_as_mapping*/ |
---|
205 | 0, /*tp_hash */ |
---|
206 | 0, /*tp_call*/ |
---|
207 | 0, /*tp_str*/ |
---|
208 | 0, /*tp_getattro*/ |
---|
209 | 0, /*tp_setattro*/ |
---|
210 | 0, /*tp_as_buffer*/ |
---|
211 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/ |
---|
212 | "CSmearCylinderModel objects", /* tp_doc */ |
---|
213 | 0, /* tp_traverse */ |
---|
214 | 0, /* tp_clear */ |
---|
215 | 0, /* tp_richcompare */ |
---|
216 | 0, /* tp_weaklistoffset */ |
---|
217 | 0, /* tp_iter */ |
---|
218 | 0, /* tp_iternext */ |
---|
219 | CSmearCylinderModel_methods, /* tp_methods */ |
---|
220 | CSmearCylinderModel_members, /* tp_members */ |
---|
221 | 0, /* tp_getset */ |
---|
222 | 0, /* tp_base */ |
---|
223 | 0, /* tp_dict */ |
---|
224 | 0, /* tp_descr_get */ |
---|
225 | 0, /* tp_descr_set */ |
---|
226 | 0, /* tp_dictoffset */ |
---|
227 | (initproc)CSmearCylinderModel_init, /* tp_init */ |
---|
228 | 0, /* tp_alloc */ |
---|
229 | CSmearCylinderModel_new, /* tp_new */ |
---|
230 | }; |
---|
231 | |
---|
232 | |
---|
233 | static PyMethodDef module_methods[] = { |
---|
234 | {NULL} |
---|
235 | }; |
---|
236 | |
---|
237 | /** |
---|
238 | * Function used to add the model class to a module |
---|
239 | * @param module: module to add the class to |
---|
240 | */ |
---|
241 | void addCSmearCylinderModel(PyObject *module) { |
---|
242 | PyObject *d; |
---|
243 | |
---|
244 | if (PyType_Ready(&CSmearCylinderModelType) < 0) |
---|
245 | return; |
---|
246 | |
---|
247 | Py_INCREF(&CSmearCylinderModelType); |
---|
248 | PyModule_AddObject(module, "CSmearCylinderModel", (PyObject *)&CSmearCylinderModelType); |
---|
249 | |
---|
250 | d = PyModule_GetDict(module); |
---|
251 | CSmearCylinderModelError = PyErr_NewException("CSmearCylinderModel.error", NULL, NULL); |
---|
252 | PyDict_SetItemString(d, "CSmearCylinderModelError", CSmearCylinderModelError); |
---|
253 | } |
---|
254 | |
---|