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 | /** CDiamCylFunc |
---|
16 | * |
---|
17 | * C extension |
---|
18 | * |
---|
19 | * WARNING: THIS FILE WAS GENERATED BY WRAPPERGENERATOR.PY |
---|
20 | * DO NOT MODIFY THIS FILE, MODIFY DiamCyl.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 "DiamCyl.h" |
---|
33 | } |
---|
34 | |
---|
35 | #include "models.hh" |
---|
36 | #include "dispersion_visitor.hh" |
---|
37 | |
---|
38 | /// Error object for raised exceptions |
---|
39 | static PyObject * CDiamCylFuncError = 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 | DiamCylFunc * model; |
---|
51 | /// Log for unit testing |
---|
52 | PyObject * log; |
---|
53 | } CDiamCylFunc; |
---|
54 | |
---|
55 | |
---|
56 | static void |
---|
57 | CDiamCylFunc_dealloc(CDiamCylFunc* self) |
---|
58 | { |
---|
59 | self->ob_type->tp_free((PyObject*)self); |
---|
60 | |
---|
61 | |
---|
62 | } |
---|
63 | |
---|
64 | static PyObject * |
---|
65 | CDiamCylFunc_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
---|
66 | { |
---|
67 | CDiamCylFunc *self; |
---|
68 | |
---|
69 | self = (CDiamCylFunc *)type->tp_alloc(type, 0); |
---|
70 | |
---|
71 | return (PyObject *)self; |
---|
72 | } |
---|
73 | |
---|
74 | static int |
---|
75 | CDiamCylFunc_init(CDiamCylFunc *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 DiamCylFunc(); |
---|
83 | |
---|
84 | // Initialize parameter dictionary |
---|
85 | PyDict_SetItemString(self->params,"length",Py_BuildValue("d",400.000000)); |
---|
86 | PyDict_SetItemString(self->params,"radius",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.dispersion->accept_as_source(visitor, self->model->radius.dispersion, disp_dict); |
---|
92 | PyDict_SetItemString(self->dispersion, "radius", disp_dict); |
---|
93 | disp_dict = PyDict_New(); |
---|
94 | self->model->length.dispersion->accept_as_source(visitor, self->model->length.dispersion, disp_dict); |
---|
95 | PyDict_SetItemString(self->dispersion, "length", 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 CDiamCylFunc_members[] = { |
---|
109 | {"params", T_OBJECT, offsetof(CDiamCylFunc, params), 0, |
---|
110 | "Parameters"}, |
---|
111 | {"dispersion", T_OBJECT, offsetof(CDiamCylFunc, dispersion), 0, |
---|
112 | "Dispersion parameters"}, |
---|
113 | {"log", T_OBJECT, offsetof(CDiamCylFunc, log), 0, |
---|
114 | "Log"}, |
---|
115 | {NULL} /* Sentinel */ |
---|
116 | }; |
---|
117 | |
---|
118 | /** Read double from PyObject |
---|
119 | @param p PyObject |
---|
120 | @return double |
---|
121 | */ |
---|
122 | double CDiamCylFunc_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(CDiamCylFunc *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->length = PyFloat_AsDouble( PyDict_GetItemString(self->params, "length") ); |
---|
149 | self->model->radius = PyFloat_AsDouble( PyDict_GetItemString(self->params, "radius") ); |
---|
150 | // Read in dispersion parameters |
---|
151 | PyObject* disp_dict; |
---|
152 | DispersionVisitor* visitor = new DispersionVisitor(); |
---|
153 | disp_dict = PyDict_GetItemString(self->dispersion, "radius"); |
---|
154 | self->model->radius.dispersion->accept_as_destination(visitor, self->model->radius.dispersion, disp_dict); |
---|
155 | disp_dict = PyDict_GetItemString(self->dispersion, "length"); |
---|
156 | self->model->length.dispersion->accept_as_destination(visitor, self->model->length.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(CDiamCylFuncError, |
---|
162 | "CDiamCylFunc.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(CDiamCylFuncError, |
---|
173 | "CDiamCylFunc.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 = CDiamCylFunc_readDouble(PyList_GET_ITEM(pars,0)); |
---|
179 | phi_value = CDiamCylFunc_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 = CDiamCylFunc_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(CDiamCylFunc *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->length = PyFloat_AsDouble( PyDict_GetItemString(self->params, "length") ); |
---|
209 | self->model->radius = PyFloat_AsDouble( PyDict_GetItemString(self->params, "radius") ); |
---|
210 | // Read in dispersion parameters |
---|
211 | PyObject* disp_dict; |
---|
212 | DispersionVisitor* visitor = new DispersionVisitor(); |
---|
213 | disp_dict = PyDict_GetItemString(self->dispersion, "radius"); |
---|
214 | self->model->radius.dispersion->accept_as_destination(visitor, self->model->radius.dispersion, disp_dict); |
---|
215 | disp_dict = PyDict_GetItemString(self->dispersion, "length"); |
---|
216 | self->model->length.dispersion->accept_as_destination(visitor, self->model->length.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(CDiamCylFuncError, |
---|
222 | "CDiamCylFunc.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(CDiamCylFuncError, |
---|
233 | "CDiamCylFunc.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 = CDiamCylFunc_readDouble(PyList_GET_ITEM(pars,0)); |
---|
239 | qy_value = CDiamCylFunc_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 = CDiamCylFunc_readDouble(pars); |
---|
246 | |
---|
247 | return Py_BuildValue("d",(*(self->model))(qx_value)); |
---|
248 | } |
---|
249 | } |
---|
250 | |
---|
251 | static PyObject * reset(CDiamCylFunc *self, PyObject *args) { |
---|
252 | |
---|
253 | |
---|
254 | return Py_BuildValue("d",0.0); |
---|
255 | } |
---|
256 | |
---|
257 | static PyObject * set_dispersion(CDiamCylFunc *self, PyObject *args) { |
---|
258 | PyObject * disp; |
---|
259 | const char * par_name; |
---|
260 | |
---|
261 | if ( !PyArg_ParseTuple(args,"sO", &par_name, &disp) ) { |
---|
262 | PyErr_SetString(CDiamCylFuncError, |
---|
263 | "CDiamCylFunc.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")) { |
---|
273 | self->model->radius.dispersion = dispersion; |
---|
274 | } else if (!strcmp(par_name, "length")) { |
---|
275 | self->model->length.dispersion = dispersion; |
---|
276 | } else { |
---|
277 | PyErr_SetString(CDiamCylFuncError, |
---|
278 | "CDiamCylFunc.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 CDiamCylFunc_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 CDiamCylFuncType = { |
---|
303 | PyObject_HEAD_INIT(NULL) |
---|
304 | 0, /*ob_size*/ |
---|
305 | "CDiamCylFunc", /*tp_name*/ |
---|
306 | sizeof(CDiamCylFunc), /*tp_basicsize*/ |
---|
307 | 0, /*tp_itemsize*/ |
---|
308 | (destructor)CDiamCylFunc_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 | "CDiamCylFunc 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 | CDiamCylFunc_methods, /* tp_methods */ |
---|
332 | CDiamCylFunc_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)CDiamCylFunc_init, /* tp_init */ |
---|
340 | 0, /* tp_alloc */ |
---|
341 | CDiamCylFunc_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 addCDiamCylFunc(PyObject *module) { |
---|
354 | PyObject *d; |
---|
355 | |
---|
356 | if (PyType_Ready(&CDiamCylFuncType) < 0) |
---|
357 | return; |
---|
358 | |
---|
359 | Py_INCREF(&CDiamCylFuncType); |
---|
360 | PyModule_AddObject(module, "CDiamCylFunc", (PyObject *)&CDiamCylFuncType); |
---|
361 | |
---|
362 | d = PyModule_GetDict(module); |
---|
363 | CDiamCylFuncError = PyErr_NewException("CDiamCylFunc.error", NULL, NULL); |
---|
364 | PyDict_SetItemString(d, "CDiamCylFuncError", CDiamCylFuncError); |
---|
365 | } |
---|
366 | |
---|