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