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 | /** CDiamEllipFunc |
---|
16 | * |
---|
17 | * C extension |
---|
18 | * |
---|
19 | * WARNING: THIS FILE WAS GENERATED BY WRAPPERGENERATOR.PY |
---|
20 | * DO NOT MODIFY THIS FILE, MODIFY DiamEllip.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 "DiamEllip.h" |
---|
33 | } |
---|
34 | |
---|
35 | #include "models.hh" |
---|
36 | #include "dispersion_visitor.hh" |
---|
37 | |
---|
38 | /// Error object for raised exceptions |
---|
39 | static PyObject * CDiamEllipFuncError = 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 | DiamEllipFunc * model; |
---|
51 | /// Log for unit testing |
---|
52 | PyObject * log; |
---|
53 | } CDiamEllipFunc; |
---|
54 | |
---|
55 | |
---|
56 | static void |
---|
57 | CDiamEllipFunc_dealloc(CDiamEllipFunc* self) |
---|
58 | { |
---|
59 | self->ob_type->tp_free((PyObject*)self); |
---|
60 | |
---|
61 | |
---|
62 | } |
---|
63 | |
---|
64 | static PyObject * |
---|
65 | CDiamEllipFunc_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
---|
66 | { |
---|
67 | CDiamEllipFunc *self; |
---|
68 | |
---|
69 | self = (CDiamEllipFunc *)type->tp_alloc(type, 0); |
---|
70 | |
---|
71 | return (PyObject *)self; |
---|
72 | } |
---|
73 | |
---|
74 | static int |
---|
75 | CDiamEllipFunc_init(CDiamEllipFunc *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 DiamEllipFunc(); |
---|
83 | |
---|
84 | // Initialize parameter dictionary |
---|
85 | PyDict_SetItemString(self->params,"radius_b",Py_BuildValue("d",400.000000)); |
---|
86 | PyDict_SetItemString(self->params,"radius_a",Py_BuildValue("d",20.000000)); |
---|
87 | // Initialize dispersion / averaging parameter dict |
---|
88 | DispersionVisitor* visitor = new DispersionVisitor(); |
---|
89 | PyObject * disp_dict; |
---|
90 | disp_dict = PyDict_New(); |
---|
91 | self->model->radius_a.dispersion->accept_as_source(visitor, self->model->radius_a.dispersion, disp_dict); |
---|
92 | PyDict_SetItemString(self->dispersion, "radius_a", disp_dict); |
---|
93 | disp_dict = PyDict_New(); |
---|
94 | self->model->radius_b.dispersion->accept_as_source(visitor, self->model->radius_b.dispersion, disp_dict); |
---|
95 | PyDict_SetItemString(self->dispersion, "radius_b", disp_dict); |
---|
96 | |
---|
97 | |
---|
98 | |
---|
99 | // Create empty log |
---|
100 | self->log = PyDict_New(); |
---|
101 | |
---|
102 | |
---|
103 | |
---|
104 | } |
---|
105 | return 0; |
---|
106 | } |
---|
107 | |
---|
108 | static PyMemberDef CDiamEllipFunc_members[] = { |
---|
109 | {"params", T_OBJECT, offsetof(CDiamEllipFunc, params), 0, |
---|
110 | "Parameters"}, |
---|
111 | {"dispersion", T_OBJECT, offsetof(CDiamEllipFunc, dispersion), 0, |
---|
112 | "Dispersion parameters"}, |
---|
113 | {"log", T_OBJECT, offsetof(CDiamEllipFunc, log), 0, |
---|
114 | "Log"}, |
---|
115 | {NULL} /* Sentinel */ |
---|
116 | }; |
---|
117 | |
---|
118 | /** Read double from PyObject |
---|
119 | @param p PyObject |
---|
120 | @return double |
---|
121 | */ |
---|
122 | double CDiamEllipFunc_readDouble(PyObject *p) { |
---|
123 | if (PyFloat_Check(p)==1) { |
---|
124 | return (double)(((PyFloatObject *)(p))->ob_fval); |
---|
125 | } else if (PyInt_Check(p)==1) { |
---|
126 | return (double)(((PyIntObject *)(p))->ob_ival); |
---|
127 | } else if (PyLong_Check(p)==1) { |
---|
128 | return (double)PyLong_AsLong(p); |
---|
129 | } else { |
---|
130 | return 0.0; |
---|
131 | } |
---|
132 | } |
---|
133 | |
---|
134 | |
---|
135 | /** |
---|
136 | * Function to call to evaluate model |
---|
137 | * @param args: input q or [q,phi] |
---|
138 | * @return: function value |
---|
139 | */ |
---|
140 | static PyObject * run(CDiamEllipFunc *self, PyObject *args) { |
---|
141 | double q_value, phi_value; |
---|
142 | PyObject* pars; |
---|
143 | int npars; |
---|
144 | |
---|
145 | // Get parameters |
---|
146 | |
---|
147 | // Reader parameter dictionary |
---|
148 | self->model->radius_b = PyFloat_AsDouble( PyDict_GetItemString(self->params, "radius_b") ); |
---|
149 | self->model->radius_a = PyFloat_AsDouble( PyDict_GetItemString(self->params, "radius_a") ); |
---|
150 | // Read in dispersion parameters |
---|
151 | PyObject* disp_dict; |
---|
152 | DispersionVisitor* visitor = new DispersionVisitor(); |
---|
153 | disp_dict = PyDict_GetItemString(self->dispersion, "radius_a"); |
---|
154 | self->model->radius_a.dispersion->accept_as_destination(visitor, self->model->radius_a.dispersion, disp_dict); |
---|
155 | disp_dict = PyDict_GetItemString(self->dispersion, "radius_b"); |
---|
156 | self->model->radius_b.dispersion->accept_as_destination(visitor, self->model->radius_b.dispersion, disp_dict); |
---|
157 | |
---|
158 | |
---|
159 | // Get input and determine whether we have to supply a 1D or 2D return value. |
---|
160 | if ( !PyArg_ParseTuple(args,"O",&pars) ) { |
---|
161 | PyErr_SetString(CDiamEllipFuncError, |
---|
162 | "CDiamEllipFunc.run expects a q value."); |
---|
163 | return NULL; |
---|
164 | } |
---|
165 | |
---|
166 | // Check params |
---|
167 | if( PyList_Check(pars)==1) { |
---|
168 | |
---|
169 | // Length of list should be 2 for I(q,phi) |
---|
170 | npars = PyList_GET_SIZE(pars); |
---|
171 | if(npars!=2) { |
---|
172 | PyErr_SetString(CDiamEllipFuncError, |
---|
173 | "CDiamEllipFunc.run expects a double or a list of dimension 2."); |
---|
174 | return NULL; |
---|
175 | } |
---|
176 | // We have a vector q, get the q and phi values at which |
---|
177 | // to evaluate I(q,phi) |
---|
178 | q_value = CDiamEllipFunc_readDouble(PyList_GET_ITEM(pars,0)); |
---|
179 | phi_value = CDiamEllipFunc_readDouble(PyList_GET_ITEM(pars,1)); |
---|
180 | // Skip zero |
---|
181 | if (q_value==0) { |
---|
182 | return Py_BuildValue("d",0.0); |
---|
183 | } |
---|
184 | return Py_BuildValue("d",(*(self->model)).evaluate_rphi(q_value,phi_value)); |
---|
185 | |
---|
186 | } else { |
---|
187 | |
---|
188 | // We have a scalar q, we will evaluate I(q) |
---|
189 | q_value = CDiamEllipFunc_readDouble(pars); |
---|
190 | |
---|
191 | return Py_BuildValue("d",(*(self->model))(q_value)); |
---|
192 | } |
---|
193 | } |
---|
194 | |
---|
195 | /** |
---|
196 | * Function to call to evaluate model in cartesian coordinates |
---|
197 | * @param args: input q or [qx, qy]] |
---|
198 | * @return: function value |
---|
199 | */ |
---|
200 | static PyObject * runXY(CDiamEllipFunc *self, PyObject *args) { |
---|
201 | double qx_value, qy_value; |
---|
202 | PyObject* pars; |
---|
203 | int npars; |
---|
204 | |
---|
205 | // Get parameters |
---|
206 | |
---|
207 | // Reader parameter dictionary |
---|
208 | self->model->radius_b = PyFloat_AsDouble( PyDict_GetItemString(self->params, "radius_b") ); |
---|
209 | self->model->radius_a = PyFloat_AsDouble( PyDict_GetItemString(self->params, "radius_a") ); |
---|
210 | // Read in dispersion parameters |
---|
211 | PyObject* disp_dict; |
---|
212 | DispersionVisitor* visitor = new DispersionVisitor(); |
---|
213 | disp_dict = PyDict_GetItemString(self->dispersion, "radius_a"); |
---|
214 | self->model->radius_a.dispersion->accept_as_destination(visitor, self->model->radius_a.dispersion, disp_dict); |
---|
215 | disp_dict = PyDict_GetItemString(self->dispersion, "radius_b"); |
---|
216 | self->model->radius_b.dispersion->accept_as_destination(visitor, self->model->radius_b.dispersion, disp_dict); |
---|
217 | |
---|
218 | |
---|
219 | // Get input and determine whether we have to supply a 1D or 2D return value. |
---|
220 | if ( !PyArg_ParseTuple(args,"O",&pars) ) { |
---|
221 | PyErr_SetString(CDiamEllipFuncError, |
---|
222 | "CDiamEllipFunc.run expects a q value."); |
---|
223 | return NULL; |
---|
224 | } |
---|
225 | |
---|
226 | // Check params |
---|
227 | if( PyList_Check(pars)==1) { |
---|
228 | |
---|
229 | // Length of list should be 2 for I(qx, qy)) |
---|
230 | npars = PyList_GET_SIZE(pars); |
---|
231 | if(npars!=2) { |
---|
232 | PyErr_SetString(CDiamEllipFuncError, |
---|
233 | "CDiamEllipFunc.run expects a double or a list of dimension 2."); |
---|
234 | return NULL; |
---|
235 | } |
---|
236 | // We have a vector q, get the qx and qy values at which |
---|
237 | // to evaluate I(qx,qy) |
---|
238 | qx_value = CDiamEllipFunc_readDouble(PyList_GET_ITEM(pars,0)); |
---|
239 | qy_value = CDiamEllipFunc_readDouble(PyList_GET_ITEM(pars,1)); |
---|
240 | return Py_BuildValue("d",(*(self->model))(qx_value,qy_value)); |
---|
241 | |
---|
242 | } else { |
---|
243 | |
---|
244 | // We have a scalar q, we will evaluate I(q) |
---|
245 | qx_value = CDiamEllipFunc_readDouble(pars); |
---|
246 | |
---|
247 | return Py_BuildValue("d",(*(self->model))(qx_value)); |
---|
248 | } |
---|
249 | } |
---|
250 | |
---|
251 | static PyObject * reset(CDiamEllipFunc *self, PyObject *args) { |
---|
252 | |
---|
253 | |
---|
254 | return Py_BuildValue("d",0.0); |
---|
255 | } |
---|
256 | |
---|
257 | static PyObject * set_dispersion(CDiamEllipFunc *self, PyObject *args) { |
---|
258 | PyObject * disp; |
---|
259 | const char * par_name; |
---|
260 | |
---|
261 | if ( !PyArg_ParseTuple(args,"sO", &par_name, &disp) ) { |
---|
262 | PyErr_SetString(CDiamEllipFuncError, |
---|
263 | "CDiamEllipFunc.set_dispersion expects a DispersionModel object."); |
---|
264 | return NULL; |
---|
265 | } |
---|
266 | void *temp = PyCObject_AsVoidPtr(disp); |
---|
267 | DispersionModel * dispersion = static_cast<DispersionModel *>(temp); |
---|
268 | |
---|
269 | |
---|
270 | // Ugliness necessary to go from python to C |
---|
271 | // TODO: refactor this |
---|
272 | if (!strcmp(par_name, "radius_a")) { |
---|
273 | self->model->radius_a.dispersion = dispersion; |
---|
274 | } else if (!strcmp(par_name, "radius_b")) { |
---|
275 | self->model->radius_b.dispersion = dispersion; |
---|
276 | } else { |
---|
277 | PyErr_SetString(CDiamEllipFuncError, |
---|
278 | "CDiamEllipFunc.set_dispersion expects a valid parameter name."); |
---|
279 | return NULL; |
---|
280 | } |
---|
281 | |
---|
282 | DispersionVisitor* visitor = new DispersionVisitor(); |
---|
283 | PyObject * disp_dict = PyDict_New(); |
---|
284 | dispersion->accept_as_source(visitor, dispersion, disp_dict); |
---|
285 | PyDict_SetItemString(self->dispersion, par_name, disp_dict); |
---|
286 | return Py_BuildValue("i",1); |
---|
287 | } |
---|
288 | |
---|
289 | |
---|
290 | static PyMethodDef CDiamEllipFunc_methods[] = { |
---|
291 | {"run", (PyCFunction)run , METH_VARARGS, |
---|
292 | "Evaluate the model at a given Q or Q, phi"}, |
---|
293 | {"runXY", (PyCFunction)runXY , METH_VARARGS, |
---|
294 | "Evaluate the model at a given Q or Qx, Qy"}, |
---|
295 | {"reset", (PyCFunction)reset , METH_VARARGS, |
---|
296 | "Reset pair correlation"}, |
---|
297 | {"set_dispersion", (PyCFunction)set_dispersion , METH_VARARGS, |
---|
298 | "Set the dispersion model for a given parameter"}, |
---|
299 | {NULL} |
---|
300 | }; |
---|
301 | |
---|
302 | static PyTypeObject CDiamEllipFuncType = { |
---|
303 | PyObject_HEAD_INIT(NULL) |
---|
304 | 0, /*ob_size*/ |
---|
305 | "CDiamEllipFunc", /*tp_name*/ |
---|
306 | sizeof(CDiamEllipFunc), /*tp_basicsize*/ |
---|
307 | 0, /*tp_itemsize*/ |
---|
308 | (destructor)CDiamEllipFunc_dealloc, /*tp_dealloc*/ |
---|
309 | 0, /*tp_print*/ |
---|
310 | 0, /*tp_getattr*/ |
---|
311 | 0, /*tp_setattr*/ |
---|
312 | 0, /*tp_compare*/ |
---|
313 | 0, /*tp_repr*/ |
---|
314 | 0, /*tp_as_number*/ |
---|
315 | 0, /*tp_as_sequence*/ |
---|
316 | 0, /*tp_as_mapping*/ |
---|
317 | 0, /*tp_hash */ |
---|
318 | 0, /*tp_call*/ |
---|
319 | 0, /*tp_str*/ |
---|
320 | 0, /*tp_getattro*/ |
---|
321 | 0, /*tp_setattro*/ |
---|
322 | 0, /*tp_as_buffer*/ |
---|
323 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/ |
---|
324 | "CDiamEllipFunc objects", /* tp_doc */ |
---|
325 | 0, /* tp_traverse */ |
---|
326 | 0, /* tp_clear */ |
---|
327 | 0, /* tp_richcompare */ |
---|
328 | 0, /* tp_weaklistoffset */ |
---|
329 | 0, /* tp_iter */ |
---|
330 | 0, /* tp_iternext */ |
---|
331 | CDiamEllipFunc_methods, /* tp_methods */ |
---|
332 | CDiamEllipFunc_members, /* tp_members */ |
---|
333 | 0, /* tp_getset */ |
---|
334 | 0, /* tp_base */ |
---|
335 | 0, /* tp_dict */ |
---|
336 | 0, /* tp_descr_get */ |
---|
337 | 0, /* tp_descr_set */ |
---|
338 | 0, /* tp_dictoffset */ |
---|
339 | (initproc)CDiamEllipFunc_init, /* tp_init */ |
---|
340 | 0, /* tp_alloc */ |
---|
341 | CDiamEllipFunc_new, /* tp_new */ |
---|
342 | }; |
---|
343 | |
---|
344 | |
---|
345 | static PyMethodDef module_methods[] = { |
---|
346 | {NULL} |
---|
347 | }; |
---|
348 | |
---|
349 | /** |
---|
350 | * Function used to add the model class to a module |
---|
351 | * @param module: module to add the class to |
---|
352 | */ |
---|
353 | void addCDiamEllipFunc(PyObject *module) { |
---|
354 | PyObject *d; |
---|
355 | |
---|
356 | if (PyType_Ready(&CDiamEllipFuncType) < 0) |
---|
357 | return; |
---|
358 | |
---|
359 | Py_INCREF(&CDiamEllipFuncType); |
---|
360 | PyModule_AddObject(module, "CDiamEllipFunc", (PyObject *)&CDiamEllipFuncType); |
---|
361 | |
---|
362 | d = PyModule_GetDict(module); |
---|
363 | CDiamEllipFuncError = PyErr_NewException("CDiamEllipFunc.error", NULL, NULL); |
---|
364 | PyDict_SetItemString(d, "CDiamEllipFuncError", CDiamEllipFuncError); |
---|
365 | } |
---|
366 | |
---|