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 [INCLUDE_FILE] |
---|
21 | * AND RE-RUN THE GENERATOR SCRIPT |
---|
22 | * |
---|
23 | */ |
---|
24 | #define NO_IMPORT_ARRAY |
---|
25 | #define PY_ARRAY_UNIQUE_SYMBOL PyArray_API_sans |
---|
26 | |
---|
27 | extern "C" { |
---|
28 | #include <Python.h> |
---|
29 | #include <arrayobject.h> |
---|
30 | #include "structmember.h" |
---|
31 | #include <stdio.h> |
---|
32 | #include <stdlib.h> |
---|
33 | #include <math.h> |
---|
34 | #include <time.h> |
---|
35 | #include "[INCLUDE_FILE]" |
---|
36 | } |
---|
37 | |
---|
38 | #include "models.hh" |
---|
39 | #include "dispersion_visitor.hh" |
---|
40 | |
---|
41 | /// Error object for raised exceptions |
---|
42 | static PyObject * [PYTHONCLASS]Error = NULL; |
---|
43 | |
---|
44 | |
---|
45 | // Class definition |
---|
46 | typedef struct { |
---|
47 | PyObject_HEAD |
---|
48 | /// Parameters |
---|
49 | PyObject * params; |
---|
50 | /// Dispersion parameters |
---|
51 | PyObject * dispersion; |
---|
52 | /// Underlying model object |
---|
53 | [CMODEL] * model; |
---|
54 | /// Log for unit testing |
---|
55 | PyObject * log; |
---|
56 | } [PYTHONCLASS]; |
---|
57 | |
---|
58 | |
---|
59 | static void |
---|
60 | [PYTHONCLASS]_dealloc([PYTHONCLASS]* self) |
---|
61 | { |
---|
62 | self->ob_type->tp_free((PyObject*)self); |
---|
63 | [NUMERICAL_DEALLOC] |
---|
64 | } |
---|
65 | |
---|
66 | static PyObject * |
---|
67 | [PYTHONCLASS]_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
---|
68 | { |
---|
69 | [PYTHONCLASS] *self; |
---|
70 | |
---|
71 | self = ([PYTHONCLASS] *)type->tp_alloc(type, 0); |
---|
72 | |
---|
73 | return (PyObject *)self; |
---|
74 | } |
---|
75 | |
---|
76 | static int |
---|
77 | [PYTHONCLASS]_init([PYTHONCLASS] *self, PyObject *args, PyObject *kwds) |
---|
78 | { |
---|
79 | if (self != NULL) { |
---|
80 | |
---|
81 | // Create parameters |
---|
82 | self->params = PyDict_New(); |
---|
83 | self->dispersion = PyDict_New(); |
---|
84 | self->model = new [CMODEL](); |
---|
85 | |
---|
86 | [INITDICTIONARY] |
---|
87 | |
---|
88 | // Create empty log |
---|
89 | self->log = PyDict_New(); |
---|
90 | |
---|
91 | [NUMERICAL_INIT] |
---|
92 | } |
---|
93 | return 0; |
---|
94 | } |
---|
95 | |
---|
96 | static PyMemberDef [PYTHONCLASS]_members[] = { |
---|
97 | {"params", T_OBJECT, offsetof([PYTHONCLASS], params), 0, |
---|
98 | "Parameters"}, |
---|
99 | {"dispersion", T_OBJECT, offsetof([PYTHONCLASS], dispersion), 0, |
---|
100 | "Dispersion parameters"}, |
---|
101 | {"log", T_OBJECT, offsetof([PYTHONCLASS], log), 0, |
---|
102 | "Log"}, |
---|
103 | {NULL} /* Sentinel */ |
---|
104 | }; |
---|
105 | |
---|
106 | /** Read double from PyObject |
---|
107 | @param p PyObject |
---|
108 | @return double |
---|
109 | */ |
---|
110 | double [PYTHONCLASS]_readDouble(PyObject *p) { |
---|
111 | if (PyFloat_Check(p)==1) { |
---|
112 | return (double)(((PyFloatObject *)(p))->ob_fval); |
---|
113 | } else if (PyInt_Check(p)==1) { |
---|
114 | return (double)(((PyIntObject *)(p))->ob_ival); |
---|
115 | } else if (PyLong_Check(p)==1) { |
---|
116 | return (double)PyLong_AsLong(p); |
---|
117 | } else { |
---|
118 | return 0.0; |
---|
119 | } |
---|
120 | } |
---|
121 | /** |
---|
122 | * Function to call to evaluate model |
---|
123 | * @param args: input numpy array q[] |
---|
124 | * @return: numpy array object |
---|
125 | */ |
---|
126 | |
---|
127 | static PyObject *evaluateOneDim([CMODEL]* model, PyArrayObject *q){ |
---|
128 | PyArrayObject *result; |
---|
129 | |
---|
130 | // Check validity of array q , q must be of dimension 1, an array of double |
---|
131 | if (q->nd != 1 || q->descr->type_num != PyArray_DOUBLE) |
---|
132 | { |
---|
133 | //const char * message= "Invalid array: q->nd=%d,type_num=%d\n",q->nd,q->descr->type_num; |
---|
134 | //PyErr_SetString(PyExc_ValueError , message); |
---|
135 | return NULL; |
---|
136 | } |
---|
137 | result = (PyArrayObject *)PyArray_FromDims(q->nd, (int *)(q->dimensions), |
---|
138 | PyArray_DOUBLE); |
---|
139 | if (result == NULL) { |
---|
140 | const char * message= "Could not create result "; |
---|
141 | PyErr_SetString(PyExc_RuntimeError , message); |
---|
142 | return NULL; |
---|
143 | } |
---|
144 | for (int i = 0; i < q->dimensions[0]; i++){ |
---|
145 | double q_value = *(double *)(q->data + i*q->strides[0]); |
---|
146 | double *result_value = (double *)(result->data + i*result->strides[0]); |
---|
147 | *result_value =(*model)(q_value); |
---|
148 | } |
---|
149 | return PyArray_Return(result); |
---|
150 | } |
---|
151 | |
---|
152 | /** |
---|
153 | * Function to call to evaluate model |
---|
154 | * @param args: input numpy array [x[],y[]] |
---|
155 | * @return: numpy array object |
---|
156 | */ |
---|
157 | static PyObject * evaluateTwoDimXY( [CMODEL]* model, |
---|
158 | PyArrayObject *x, PyArrayObject *y) |
---|
159 | { |
---|
160 | PyArrayObject *result; |
---|
161 | int i,j, x_len, y_len, dims[2]; |
---|
162 | //check validity of input vectors |
---|
163 | if (x->nd != 2 || x->descr->type_num != PyArray_DOUBLE |
---|
164 | || y->nd != 2 || y->descr->type_num != PyArray_DOUBLE |
---|
165 | || y->dimensions[1] != x->dimensions[0]){ |
---|
166 | const char * message= "evaluateTwoDimXY expect 2 numpy arrays"; |
---|
167 | PyErr_SetString(PyExc_ValueError , message); |
---|
168 | return NULL; |
---|
169 | } |
---|
170 | |
---|
171 | if (PyArray_Check(x) && PyArray_Check(y)) { |
---|
172 | x_len = dims[0]= x->dimensions[0]; |
---|
173 | y_len = dims[1]= y->dimensions[1]; |
---|
174 | |
---|
175 | // Make a new double matrix of same dims |
---|
176 | result=(PyArrayObject *) PyArray_FromDims(2,dims,NPY_DOUBLE); |
---|
177 | if (result == NULL){ |
---|
178 | const char * message= "Could not create result "; |
---|
179 | PyErr_SetString(PyExc_RuntimeError , message); |
---|
180 | return NULL; |
---|
181 | } |
---|
182 | |
---|
183 | /* Do the calculation. */ |
---|
184 | for ( i=0; i< x_len; i++) { |
---|
185 | for ( j=0; j< y_len; j++) { |
---|
186 | double x_value = *(double *)(x->data + i*x->strides[0]); |
---|
187 | double y_value = *(double *)(y->data + j*y->strides[1]); |
---|
188 | double *result_value = (double *)(result->data + |
---|
189 | i*result->strides[0] + j*result->strides[1]); |
---|
190 | *result_value = (*model)(x_value, y_value); |
---|
191 | } |
---|
192 | } |
---|
193 | return PyArray_Return(result); |
---|
194 | |
---|
195 | }else{ |
---|
196 | PyErr_SetString([PYTHONCLASS]Error, |
---|
197 | "[PYTHONCLASS].evaluateTwoDimXY couldn't run."); |
---|
198 | return NULL; |
---|
199 | } |
---|
200 | } |
---|
201 | /** |
---|
202 | * evalDistribution function evaluate a model function with input vector |
---|
203 | * @param args: input q as vector or [qx, qy] where qx, qy are vectors |
---|
204 | * |
---|
205 | */ |
---|
206 | static PyObject * evalDistribution([PYTHONCLASS] *self, PyObject *args){ |
---|
207 | PyObject *qx, *qy; |
---|
208 | PyArrayObject * pars; |
---|
209 | int npars ,mpars; |
---|
210 | |
---|
211 | // Get parameters |
---|
212 | |
---|
213 | [READDICTIONARY] |
---|
214 | |
---|
215 | // Get input and determine whether we have to supply a 1D or 2D return value. |
---|
216 | if ( !PyArg_ParseTuple(args,"O",&pars) ) { |
---|
217 | PyErr_SetString([PYTHONCLASS]Error, |
---|
218 | "[PYTHONCLASS].evalDistribution expects a q value."); |
---|
219 | return NULL; |
---|
220 | } |
---|
221 | // Check params |
---|
222 | |
---|
223 | if(PyArray_Check(pars)==1) { |
---|
224 | |
---|
225 | // Length of list should 1 or 2 |
---|
226 | npars = pars->nd; |
---|
227 | if(npars==1) { |
---|
228 | // input is a numpy array |
---|
229 | if (PyArray_Check(pars)) { |
---|
230 | return evaluateOneDim(self->model, (PyArrayObject*)pars); |
---|
231 | } |
---|
232 | }else{ |
---|
233 | PyErr_SetString([PYTHONCLASS]Error, |
---|
234 | "[PYTHONCLASS].evalDistribution expect numpy array of one dimension."); |
---|
235 | return NULL; |
---|
236 | } |
---|
237 | }else if( PyList_Check(pars)==1) { |
---|
238 | // Length of list should be 2 for I(qx,qy) |
---|
239 | mpars = PyList_GET_SIZE(pars); |
---|
240 | if(mpars!=2) { |
---|
241 | PyErr_SetString([PYTHONCLASS]Error, |
---|
242 | "[PYTHONCLASS].evalDistribution expects a list of dimension 2."); |
---|
243 | return NULL; |
---|
244 | } |
---|
245 | qx = PyList_GET_ITEM(pars,0); |
---|
246 | qy = PyList_GET_ITEM(pars,1); |
---|
247 | if (PyArray_Check(qx) && PyArray_Check(qy)) { |
---|
248 | return evaluateTwoDimXY(self->model, (PyArrayObject*)qx, |
---|
249 | (PyArrayObject*)qy); |
---|
250 | }else{ |
---|
251 | PyErr_SetString([PYTHONCLASS]Error, |
---|
252 | "[PYTHONCLASS].evalDistribution expect 2 numpy arrays in list."); |
---|
253 | return NULL; |
---|
254 | } |
---|
255 | }else{ |
---|
256 | PyErr_SetString([PYTHONCLASS]Error, |
---|
257 | "[PYTHONCLASS].evalDistribution couln't be run."); |
---|
258 | return NULL; |
---|
259 | } |
---|
260 | } |
---|
261 | |
---|
262 | /** |
---|
263 | * Function to call to evaluate model |
---|
264 | * @param args: input q or [q,phi] |
---|
265 | * @return: function value |
---|
266 | */ |
---|
267 | static PyObject * run([PYTHONCLASS] *self, PyObject *args) { |
---|
268 | double q_value, phi_value; |
---|
269 | PyObject* pars; |
---|
270 | int npars; |
---|
271 | |
---|
272 | // Get parameters |
---|
273 | |
---|
274 | [READDICTIONARY] |
---|
275 | |
---|
276 | // Get input and determine whether we have to supply a 1D or 2D return value. |
---|
277 | if ( !PyArg_ParseTuple(args,"O",&pars) ) { |
---|
278 | PyErr_SetString([PYTHONCLASS]Error, |
---|
279 | "[PYTHONCLASS].run expects a q value."); |
---|
280 | return NULL; |
---|
281 | } |
---|
282 | |
---|
283 | // Check params |
---|
284 | if( PyList_Check(pars)==1) { |
---|
285 | |
---|
286 | // Length of list should be 2 for I(q,phi) |
---|
287 | npars = PyList_GET_SIZE(pars); |
---|
288 | if(npars!=2) { |
---|
289 | PyErr_SetString([PYTHONCLASS]Error, |
---|
290 | "[PYTHONCLASS].run expects a double or a list of dimension 2."); |
---|
291 | return NULL; |
---|
292 | } |
---|
293 | // We have a vector q, get the q and phi values at which |
---|
294 | // to evaluate I(q,phi) |
---|
295 | q_value = [PYTHONCLASS]_readDouble(PyList_GET_ITEM(pars,0)); |
---|
296 | phi_value = [PYTHONCLASS]_readDouble(PyList_GET_ITEM(pars,1)); |
---|
297 | // Skip zero |
---|
298 | if (q_value==0) { |
---|
299 | return Py_BuildValue("d",0.0); |
---|
300 | } |
---|
301 | return Py_BuildValue("d",(*(self->model)).evaluate_rphi(q_value,phi_value)); |
---|
302 | |
---|
303 | } else { |
---|
304 | |
---|
305 | // We have a scalar q, we will evaluate I(q) |
---|
306 | q_value = [PYTHONCLASS]_readDouble(pars); |
---|
307 | |
---|
308 | return Py_BuildValue("d",(*(self->model))(q_value)); |
---|
309 | } |
---|
310 | } |
---|
311 | |
---|
312 | /** |
---|
313 | * Function to call to evaluate model in cartesian coordinates |
---|
314 | * @param args: input q or [qx, qy]] |
---|
315 | * @return: function value |
---|
316 | */ |
---|
317 | static PyObject * runXY([PYTHONCLASS] *self, PyObject *args) { |
---|
318 | double qx_value, qy_value; |
---|
319 | PyObject* pars; |
---|
320 | int npars; |
---|
321 | |
---|
322 | // Get parameters |
---|
323 | |
---|
324 | [READDICTIONARY] |
---|
325 | |
---|
326 | // Get input and determine whether we have to supply a 1D or 2D return value. |
---|
327 | if ( !PyArg_ParseTuple(args,"O",&pars) ) { |
---|
328 | PyErr_SetString([PYTHONCLASS]Error, |
---|
329 | "[PYTHONCLASS].run expects a q value."); |
---|
330 | return NULL; |
---|
331 | } |
---|
332 | |
---|
333 | // Check params |
---|
334 | if( PyList_Check(pars)==1) { |
---|
335 | |
---|
336 | // Length of list should be 2 for I(qx, qy)) |
---|
337 | npars = PyList_GET_SIZE(pars); |
---|
338 | if(npars!=2) { |
---|
339 | PyErr_SetString([PYTHONCLASS]Error, |
---|
340 | "[PYTHONCLASS].run expects a double or a list of dimension 2."); |
---|
341 | return NULL; |
---|
342 | } |
---|
343 | // We have a vector q, get the qx and qy values at which |
---|
344 | // to evaluate I(qx,qy) |
---|
345 | qx_value = [PYTHONCLASS]_readDouble(PyList_GET_ITEM(pars,0)); |
---|
346 | qy_value = [PYTHONCLASS]_readDouble(PyList_GET_ITEM(pars,1)); |
---|
347 | return Py_BuildValue("d",(*(self->model))(qx_value,qy_value)); |
---|
348 | |
---|
349 | } else { |
---|
350 | |
---|
351 | // We have a scalar q, we will evaluate I(q) |
---|
352 | qx_value = [PYTHONCLASS]_readDouble(pars); |
---|
353 | |
---|
354 | return Py_BuildValue("d",(*(self->model))(qx_value)); |
---|
355 | } |
---|
356 | } |
---|
357 | |
---|
358 | static PyObject * reset([PYTHONCLASS] *self, PyObject *args) { |
---|
359 | [NUMERICAL_RESET] |
---|
360 | return Py_BuildValue("d",0.0); |
---|
361 | } |
---|
362 | |
---|
363 | static PyObject * set_dispersion([PYTHONCLASS] *self, PyObject *args) { |
---|
364 | PyObject * disp; |
---|
365 | const char * par_name; |
---|
366 | |
---|
367 | if ( !PyArg_ParseTuple(args,"sO", &par_name, &disp) ) { |
---|
368 | PyErr_SetString([PYTHONCLASS]Error, |
---|
369 | "[PYTHONCLASS].set_dispersion expects a DispersionModel object."); |
---|
370 | return NULL; |
---|
371 | } |
---|
372 | void *temp = PyCObject_AsVoidPtr(disp); |
---|
373 | DispersionModel * dispersion = static_cast<DispersionModel *>(temp); |
---|
374 | |
---|
375 | |
---|
376 | // Ugliness necessary to go from python to C |
---|
377 | [SET_DISPERSION] { |
---|
378 | PyErr_SetString([PYTHONCLASS]Error, |
---|
379 | "[PYTHONCLASS].set_dispersion expects a valid parameter name."); |
---|
380 | return NULL; |
---|
381 | } |
---|
382 | |
---|
383 | DispersionVisitor* visitor = new DispersionVisitor(); |
---|
384 | PyObject * disp_dict = PyDict_New(); |
---|
385 | dispersion->accept_as_source(visitor, dispersion, disp_dict); |
---|
386 | PyDict_SetItemString(self->dispersion, par_name, disp_dict); |
---|
387 | return Py_BuildValue("i",1); |
---|
388 | } |
---|
389 | |
---|
390 | |
---|
391 | static PyMethodDef [PYTHONCLASS]_methods[] = { |
---|
392 | {"run", (PyCFunction)run , METH_VARARGS, |
---|
393 | "Evaluate the model at a given Q or Q, phi"}, |
---|
394 | {"runXY", (PyCFunction)runXY , METH_VARARGS, |
---|
395 | "Evaluate the model at a given Q or Qx, Qy"}, |
---|
396 | |
---|
397 | {"evalDistribution", (PyCFunction)evalDistribution , METH_VARARGS, |
---|
398 | "Evaluate the model at a given Q or Qx, Qy vector "}, |
---|
399 | {"reset", (PyCFunction)reset , METH_VARARGS, |
---|
400 | "Reset pair correlation"}, |
---|
401 | {"set_dispersion", (PyCFunction)set_dispersion , METH_VARARGS, |
---|
402 | "Set the dispersion model for a given parameter"}, |
---|
403 | {NULL} |
---|
404 | }; |
---|
405 | |
---|
406 | static PyTypeObject [PYTHONCLASS]Type = { |
---|
407 | PyObject_HEAD_INIT(NULL) |
---|
408 | 0, /*ob_size*/ |
---|
409 | "[PYTHONCLASS]", /*tp_name*/ |
---|
410 | sizeof([PYTHONCLASS]), /*tp_basicsize*/ |
---|
411 | 0, /*tp_itemsize*/ |
---|
412 | (destructor)[PYTHONCLASS]_dealloc, /*tp_dealloc*/ |
---|
413 | 0, /*tp_print*/ |
---|
414 | 0, /*tp_getattr*/ |
---|
415 | 0, /*tp_setattr*/ |
---|
416 | 0, /*tp_compare*/ |
---|
417 | 0, /*tp_repr*/ |
---|
418 | 0, /*tp_as_number*/ |
---|
419 | 0, /*tp_as_sequence*/ |
---|
420 | 0, /*tp_as_mapping*/ |
---|
421 | 0, /*tp_hash */ |
---|
422 | 0, /*tp_call*/ |
---|
423 | 0, /*tp_str*/ |
---|
424 | 0, /*tp_getattro*/ |
---|
425 | 0, /*tp_setattro*/ |
---|
426 | 0, /*tp_as_buffer*/ |
---|
427 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/ |
---|
428 | "[PYTHONCLASS] objects", /* tp_doc */ |
---|
429 | 0, /* tp_traverse */ |
---|
430 | 0, /* tp_clear */ |
---|
431 | 0, /* tp_richcompare */ |
---|
432 | 0, /* tp_weaklistoffset */ |
---|
433 | 0, /* tp_iter */ |
---|
434 | 0, /* tp_iternext */ |
---|
435 | [PYTHONCLASS]_methods, /* tp_methods */ |
---|
436 | [PYTHONCLASS]_members, /* tp_members */ |
---|
437 | 0, /* tp_getset */ |
---|
438 | 0, /* tp_base */ |
---|
439 | 0, /* tp_dict */ |
---|
440 | 0, /* tp_descr_get */ |
---|
441 | 0, /* tp_descr_set */ |
---|
442 | 0, /* tp_dictoffset */ |
---|
443 | (initproc)[PYTHONCLASS]_init, /* tp_init */ |
---|
444 | 0, /* tp_alloc */ |
---|
445 | [PYTHONCLASS]_new, /* tp_new */ |
---|
446 | }; |
---|
447 | |
---|
448 | |
---|
449 | //static PyMethodDef module_methods[] = { |
---|
450 | // {NULL} |
---|
451 | //}; |
---|
452 | |
---|
453 | /** |
---|
454 | * Function used to add the model class to a module |
---|
455 | * @param module: module to add the class to |
---|
456 | */ |
---|
457 | void add[PYTHONCLASS](PyObject *module) { |
---|
458 | PyObject *d; |
---|
459 | |
---|
460 | if (PyType_Ready(&[PYTHONCLASS]Type) < 0) |
---|
461 | return; |
---|
462 | |
---|
463 | Py_INCREF(&[PYTHONCLASS]Type); |
---|
464 | PyModule_AddObject(module, "[PYTHONCLASS]", (PyObject *)&[PYTHONCLASS]Type); |
---|
465 | |
---|
466 | d = PyModule_GetDict(module); |
---|
467 | [PYTHONCLASS]Error = PyErr_NewException("[PYTHONCLASS].error", NULL, NULL); |
---|
468 | PyDict_SetItemString(d, "[PYTHONCLASS]Error", [PYTHONCLASS]Error); |
---|
469 | } |
---|