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