1 | /** |
---|
2 | This software was developed by the University of Tennessee as part of the |
---|
3 | Distributed Data Analysis of Neutron Scattering Experiments (DANSE) |
---|
4 | project funded by the US National Science Foundation. |
---|
5 | |
---|
6 | If you use DANSE applications to do scientific research that leads to |
---|
7 | publication, we ask that you acknowledge the use of the software with the |
---|
8 | following sentence: |
---|
9 | |
---|
10 | "This work benefited from DANSE software developed under NSF award DMR-0520547." |
---|
11 | |
---|
12 | copyright 2008, University of Tennessee |
---|
13 | */ |
---|
14 | |
---|
15 | /** [PYTHONCLASS] |
---|
16 | * |
---|
17 | * C extension |
---|
18 | * |
---|
19 | * WARNING: THIS FILE WAS GENERATED BY WRAPPERGENERATOR.PY |
---|
20 | * DO NOT MODIFY THIS FILE, MODIFY lorentzian.h |
---|
21 | * AND RE-RUN THE GENERATOR SCRIPT |
---|
22 | * |
---|
23 | */ |
---|
24 | |
---|
25 | extern "C" { |
---|
26 | #include <Python.h> |
---|
27 | #include "structmember.h" |
---|
28 | #include <stdio.h> |
---|
29 | #include <stdlib.h> |
---|
30 | #include <math.h> |
---|
31 | #include <time.h> |
---|
32 | #include "lorentzian.h" |
---|
33 | } |
---|
34 | |
---|
35 | #include "models.hh" |
---|
36 | #include "dispersion_visitor.hh" |
---|
37 | |
---|
38 | /// Error object for raised exceptions |
---|
39 | static PyObject * [PYTHONCLASS]Error = NULL; |
---|
40 | |
---|
41 | |
---|
42 | // Class definition |
---|
43 | typedef struct { |
---|
44 | PyObject_HEAD |
---|
45 | /// Parameters |
---|
46 | PyObject * params; |
---|
47 | /// Dispersion parameters |
---|
48 | PyObject * dispersion; |
---|
49 | /// Underlying model object |
---|
50 | Lorentzian * model; |
---|
51 | /// Log for unit testing |
---|
52 | PyObject * log; |
---|
53 | } [PYTHONCLASS]; |
---|
54 | |
---|
55 | |
---|
56 | static void |
---|
57 | [PYTHONCLASS]_dealloc([PYTHONCLASS]* self) |
---|
58 | { |
---|
59 | self->ob_type->tp_free((PyObject*)self); |
---|
60 | |
---|
61 | |
---|
62 | } |
---|
63 | |
---|
64 | static PyObject * |
---|
65 | [PYTHONCLASS]_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
---|
66 | { |
---|
67 | [PYTHONCLASS] *self; |
---|
68 | |
---|
69 | self = ([PYTHONCLASS] *)type->tp_alloc(type, 0); |
---|
70 | |
---|
71 | return (PyObject *)self; |
---|
72 | } |
---|
73 | |
---|
74 | static int |
---|
75 | [PYTHONCLASS]_init([PYTHONCLASS] *self, PyObject *args, PyObject *kwds) |
---|
76 | { |
---|
77 | if (self != NULL) { |
---|
78 | |
---|
79 | // Create parameters |
---|
80 | self->params = PyDict_New(); |
---|
81 | self->dispersion = PyDict_New(); |
---|
82 | self->model = new Lorentzian(); |
---|
83 | |
---|
84 | // Initialize parameter dictionary |
---|
85 | PyDict_SetItemString(self->params,"scale",Py_BuildValue("d",1.000000)); |
---|
86 | PyDict_SetItemString(self->params,"center",Py_BuildValue("d",0.000000)); |
---|
87 | PyDict_SetItemString(self->params,"gamma",Py_BuildValue("d",1.000000)); |
---|
88 | // Initialize dispersion / averaging parameter dict |
---|
89 | DispersionVisitor* visitor = new DispersionVisitor(); |
---|
90 | PyObject * disp_dict; |
---|
91 | |
---|
92 | |
---|
93 | |
---|
94 | // Create empty log |
---|
95 | self->log = PyDict_New(); |
---|
96 | |
---|
97 | |
---|
98 | |
---|
99 | } |
---|
100 | return 0; |
---|
101 | } |
---|
102 | |
---|
103 | static PyMemberDef [PYTHONCLASS]_members[] = { |
---|
104 | {"params", T_OBJECT, offsetof([PYTHONCLASS], params), 0, |
---|
105 | "Parameters"}, |
---|
106 | {"dispersion", T_OBJECT, offsetof([PYTHONCLASS], dispersion), 0, |
---|
107 | "Dispersion parameters"}, |
---|
108 | {"log", T_OBJECT, offsetof([PYTHONCLASS], log), 0, |
---|
109 | "Log"}, |
---|
110 | {NULL} /* Sentinel */ |
---|
111 | }; |
---|
112 | |
---|
113 | /** Read double from PyObject |
---|
114 | @param p PyObject |
---|
115 | @return double |
---|
116 | */ |
---|
117 | double [PYTHONCLASS]_readDouble(PyObject *p) { |
---|
118 | if (PyFloat_Check(p)==1) { |
---|
119 | return (double)(((PyFloatObject *)(p))->ob_fval); |
---|
120 | } else if (PyInt_Check(p)==1) { |
---|
121 | return (double)(((PyIntObject *)(p))->ob_ival); |
---|
122 | } else if (PyLong_Check(p)==1) { |
---|
123 | return (double)PyLong_AsLong(p); |
---|
124 | } else { |
---|
125 | return 0.0; |
---|
126 | } |
---|
127 | } |
---|
128 | |
---|
129 | |
---|
130 | /** |
---|
131 | * Function to call to evaluate model |
---|
132 | * @param args: input q or [q,phi] |
---|
133 | * @return: function value |
---|
134 | */ |
---|
135 | static PyObject * run([PYTHONCLASS] *self, PyObject *args) { |
---|
136 | double q_value, phi_value; |
---|
137 | PyObject* pars; |
---|
138 | int npars; |
---|
139 | |
---|
140 | // Get parameters |
---|
141 | |
---|
142 | // Reader parameter dictionary |
---|
143 | self->model->scale = PyFloat_AsDouble( PyDict_GetItemString(self->params, "scale") ); |
---|
144 | self->model->center = PyFloat_AsDouble( PyDict_GetItemString(self->params, "center") ); |
---|
145 | self->model->gamma = PyFloat_AsDouble( PyDict_GetItemString(self->params, "gamma") ); |
---|
146 | // Read in dispersion parameters |
---|
147 | PyObject* disp_dict; |
---|
148 | DispersionVisitor* visitor = new DispersionVisitor(); |
---|
149 | |
---|
150 | |
---|
151 | // Get input and determine whether we have to supply a 1D or 2D return value. |
---|
152 | if ( !PyArg_ParseTuple(args,"O",&pars) ) { |
---|
153 | PyErr_SetString([PYTHONCLASS]Error, |
---|
154 | "[PYTHONCLASS].run expects a q value."); |
---|
155 | return NULL; |
---|
156 | } |
---|
157 | |
---|
158 | // Check params |
---|
159 | if( PyList_Check(pars)==1) { |
---|
160 | |
---|
161 | // Length of list should be 2 for I(q,phi) |
---|
162 | npars = PyList_GET_SIZE(pars); |
---|
163 | if(npars!=2) { |
---|
164 | PyErr_SetString([PYTHONCLASS]Error, |
---|
165 | "[PYTHONCLASS].run expects a double or a list of dimension 2."); |
---|
166 | return NULL; |
---|
167 | } |
---|
168 | // We have a vector q, get the q and phi values at which |
---|
169 | // to evaluate I(q,phi) |
---|
170 | q_value = [PYTHONCLASS]_readDouble(PyList_GET_ITEM(pars,0)); |
---|
171 | phi_value = [PYTHONCLASS]_readDouble(PyList_GET_ITEM(pars,1)); |
---|
172 | // Skip zero |
---|
173 | if (q_value==0) { |
---|
174 | return Py_BuildValue("d",0.0); |
---|
175 | } |
---|
176 | return Py_BuildValue("d",(*(self->model)).evaluate_rphi(q_value,phi_value)); |
---|
177 | |
---|
178 | } else { |
---|
179 | |
---|
180 | // We have a scalar q, we will evaluate I(q) |
---|
181 | q_value = [PYTHONCLASS]_readDouble(pars); |
---|
182 | |
---|
183 | return Py_BuildValue("d",(*(self->model))(q_value)); |
---|
184 | } |
---|
185 | } |
---|
186 | |
---|
187 | /** |
---|
188 | * Function to call to evaluate model in cartesian coordinates |
---|
189 | * @param args: input q or [qx, qy]] |
---|
190 | * @return: function value |
---|
191 | */ |
---|
192 | static PyObject * runXY([PYTHONCLASS] *self, PyObject *args) { |
---|
193 | double qx_value, qy_value; |
---|
194 | PyObject* pars; |
---|
195 | int npars; |
---|
196 | |
---|
197 | // Get parameters |
---|
198 | |
---|
199 | // Reader parameter dictionary |
---|
200 | self->model->scale = PyFloat_AsDouble( PyDict_GetItemString(self->params, "scale") ); |
---|
201 | self->model->center = PyFloat_AsDouble( PyDict_GetItemString(self->params, "center") ); |
---|
202 | self->model->gamma = PyFloat_AsDouble( PyDict_GetItemString(self->params, "gamma") ); |
---|
203 | // Read in dispersion parameters |
---|
204 | PyObject* disp_dict; |
---|
205 | DispersionVisitor* visitor = new DispersionVisitor(); |
---|
206 | |
---|
207 | |
---|
208 | // Get input and determine whether we have to supply a 1D or 2D return value. |
---|
209 | if ( !PyArg_ParseTuple(args,"O",&pars) ) { |
---|
210 | PyErr_SetString([PYTHONCLASS]Error, |
---|
211 | "[PYTHONCLASS].run expects a q value."); |
---|
212 | return NULL; |
---|
213 | } |
---|
214 | |
---|
215 | // Check params |
---|
216 | if( PyList_Check(pars)==1) { |
---|
217 | |
---|
218 | // Length of list should be 2 for I(qx, qy)) |
---|
219 | npars = PyList_GET_SIZE(pars); |
---|
220 | if(npars!=2) { |
---|
221 | PyErr_SetString([PYTHONCLASS]Error, |
---|
222 | "[PYTHONCLASS].run expects a double or a list of dimension 2."); |
---|
223 | return NULL; |
---|
224 | } |
---|
225 | // We have a vector q, get the qx and qy values at which |
---|
226 | // to evaluate I(qx,qy) |
---|
227 | qx_value = [PYTHONCLASS]_readDouble(PyList_GET_ITEM(pars,0)); |
---|
228 | qy_value = [PYTHONCLASS]_readDouble(PyList_GET_ITEM(pars,1)); |
---|
229 | return Py_BuildValue("d",(*(self->model))(qx_value,qy_value)); |
---|
230 | |
---|
231 | } else { |
---|
232 | |
---|
233 | // We have a scalar q, we will evaluate I(q) |
---|
234 | qx_value = [PYTHONCLASS]_readDouble(pars); |
---|
235 | |
---|
236 | return Py_BuildValue("d",(*(self->model))(qx_value)); |
---|
237 | } |
---|
238 | } |
---|
239 | |
---|
240 | static PyObject * reset([PYTHONCLASS] *self, PyObject *args) { |
---|
241 | |
---|
242 | |
---|
243 | return Py_BuildValue("d",0.0); |
---|
244 | } |
---|
245 | |
---|
246 | static PyObject * set_dispersion([PYTHONCLASS] *self, PyObject *args) { |
---|
247 | PyObject * disp; |
---|
248 | const char * par_name; |
---|
249 | |
---|
250 | if ( !PyArg_ParseTuple(args,"sO", &par_name, &disp) ) { |
---|
251 | PyErr_SetString([PYTHONCLASS]Error, |
---|
252 | "[PYTHONCLASS].set_dispersion expects a DispersionModel object."); |
---|
253 | return NULL; |
---|
254 | } |
---|
255 | void *temp = PyCObject_AsVoidPtr(disp); |
---|
256 | DispersionModel * dispersion = static_cast<DispersionModel *>(temp); |
---|
257 | |
---|
258 | |
---|
259 | // Ugliness necessary to go from python to C |
---|
260 | // TODO: refactor this |
---|
261 | { |
---|
262 | PyErr_SetString([PYTHONCLASS]Error, |
---|
263 | "[PYTHONCLASS].set_dispersion expects a valid parameter name."); |
---|
264 | return NULL; |
---|
265 | } |
---|
266 | |
---|
267 | DispersionVisitor* visitor = new DispersionVisitor(); |
---|
268 | PyObject * disp_dict = PyDict_New(); |
---|
269 | dispersion->accept_as_source(visitor, dispersion, disp_dict); |
---|
270 | PyDict_SetItemString(self->dispersion, par_name, disp_dict); |
---|
271 | return Py_BuildValue("i",1); |
---|
272 | } |
---|
273 | |
---|
274 | |
---|
275 | static PyMethodDef [PYTHONCLASS]_methods[] = { |
---|
276 | {"run", (PyCFunction)run , METH_VARARGS, |
---|
277 | "Evaluate the model at a given Q or Q, phi"}, |
---|
278 | {"runXY", (PyCFunction)runXY , METH_VARARGS, |
---|
279 | "Evaluate the model at a given Q or Qx, Qy"}, |
---|
280 | {"reset", (PyCFunction)reset , METH_VARARGS, |
---|
281 | "Reset pair correlation"}, |
---|
282 | {"set_dispersion", (PyCFunction)set_dispersion , METH_VARARGS, |
---|
283 | "Set the dispersion model for a given parameter"}, |
---|
284 | {NULL} |
---|
285 | }; |
---|
286 | |
---|
287 | static PyTypeObject [PYTHONCLASS]Type = { |
---|
288 | PyObject_HEAD_INIT(NULL) |
---|
289 | 0, /*ob_size*/ |
---|
290 | "[PYTHONCLASS]", /*tp_name*/ |
---|
291 | sizeof([PYTHONCLASS]), /*tp_basicsize*/ |
---|
292 | 0, /*tp_itemsize*/ |
---|
293 | (destructor)[PYTHONCLASS]_dealloc, /*tp_dealloc*/ |
---|
294 | 0, /*tp_print*/ |
---|
295 | 0, /*tp_getattr*/ |
---|
296 | 0, /*tp_setattr*/ |
---|
297 | 0, /*tp_compare*/ |
---|
298 | 0, /*tp_repr*/ |
---|
299 | 0, /*tp_as_number*/ |
---|
300 | 0, /*tp_as_sequence*/ |
---|
301 | 0, /*tp_as_mapping*/ |
---|
302 | 0, /*tp_hash */ |
---|
303 | 0, /*tp_call*/ |
---|
304 | 0, /*tp_str*/ |
---|
305 | 0, /*tp_getattro*/ |
---|
306 | 0, /*tp_setattro*/ |
---|
307 | 0, /*tp_as_buffer*/ |
---|
308 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/ |
---|
309 | "[PYTHONCLASS] objects", /* tp_doc */ |
---|
310 | 0, /* tp_traverse */ |
---|
311 | 0, /* tp_clear */ |
---|
312 | 0, /* tp_richcompare */ |
---|
313 | 0, /* tp_weaklistoffset */ |
---|
314 | 0, /* tp_iter */ |
---|
315 | 0, /* tp_iternext */ |
---|
316 | [PYTHONCLASS]_methods, /* tp_methods */ |
---|
317 | [PYTHONCLASS]_members, /* tp_members */ |
---|
318 | 0, /* tp_getset */ |
---|
319 | 0, /* tp_base */ |
---|
320 | 0, /* tp_dict */ |
---|
321 | 0, /* tp_descr_get */ |
---|
322 | 0, /* tp_descr_set */ |
---|
323 | 0, /* tp_dictoffset */ |
---|
324 | (initproc)[PYTHONCLASS]_init, /* tp_init */ |
---|
325 | 0, /* tp_alloc */ |
---|
326 | [PYTHONCLASS]_new, /* tp_new */ |
---|
327 | }; |
---|
328 | |
---|
329 | |
---|
330 | static PyMethodDef module_methods[] = { |
---|
331 | {NULL} |
---|
332 | }; |
---|
333 | |
---|
334 | /** |
---|
335 | * Function used to add the model class to a module |
---|
336 | * @param module: module to add the class to |
---|
337 | */ |
---|
338 | void add[PYTHONCLASS](PyObject *module) { |
---|
339 | PyObject *d; |
---|
340 | |
---|
341 | if (PyType_Ready(&[PYTHONCLASS]Type) < 0) |
---|
342 | return; |
---|
343 | |
---|
344 | Py_INCREF(&[PYTHONCLASS]Type); |
---|
345 | PyModule_AddObject(module, "[PYTHONCLASS]", (PyObject *)&[PYTHONCLASS]Type); |
---|
346 | |
---|
347 | d = PyModule_GetDict(module); |
---|
348 | [PYTHONCLASS]Error = PyErr_NewException("[PYTHONCLASS].error", NULL, NULL); |
---|
349 | PyDict_SetItemString(d, "[PYTHONCLASS]Error", [PYTHONCLASS]Error); |
---|
350 | } |
---|
351 | |
---|