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 | /** CEllipticalCylinderModel |
---|
16 | * |
---|
17 | * C extension |
---|
18 | * |
---|
19 | * WARNING: THIS FILE WAS GENERATED BY WRAPPERGENERATOR.PY |
---|
20 | * DO NOT MODIFY THIS FILE, MODIFY elliptical_cylinder.h |
---|
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 "elliptical_cylinder.h" |
---|
36 | } |
---|
37 | |
---|
38 | #include "models.hh" |
---|
39 | #include "dispersion_visitor.hh" |
---|
40 | |
---|
41 | /// Error object for raised exceptions |
---|
42 | static PyObject * CEllipticalCylinderModelError = 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 | EllipticalCylinderModel * model; |
---|
54 | /// Log for unit testing |
---|
55 | PyObject * log; |
---|
56 | } CEllipticalCylinderModel; |
---|
57 | |
---|
58 | |
---|
59 | static void |
---|
60 | CEllipticalCylinderModel_dealloc(CEllipticalCylinderModel* self) |
---|
61 | { |
---|
62 | self->ob_type->tp_free((PyObject*)self); |
---|
63 | |
---|
64 | |
---|
65 | } |
---|
66 | |
---|
67 | static PyObject * |
---|
68 | CEllipticalCylinderModel_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
---|
69 | { |
---|
70 | CEllipticalCylinderModel *self; |
---|
71 | |
---|
72 | self = (CEllipticalCylinderModel *)type->tp_alloc(type, 0); |
---|
73 | |
---|
74 | return (PyObject *)self; |
---|
75 | } |
---|
76 | |
---|
77 | static int |
---|
78 | CEllipticalCylinderModel_init(CEllipticalCylinderModel *self, PyObject *args, PyObject *kwds) |
---|
79 | { |
---|
80 | if (self != NULL) { |
---|
81 | |
---|
82 | // Create parameters |
---|
83 | self->params = PyDict_New(); |
---|
84 | self->dispersion = PyDict_New(); |
---|
85 | self->model = new EllipticalCylinderModel(); |
---|
86 | |
---|
87 | // Initialize parameter dictionary |
---|
88 | PyDict_SetItemString(self->params,"scale",Py_BuildValue("d",1.000000)); |
---|
89 | PyDict_SetItemString(self->params,"cyl_psi",Py_BuildValue("d",0.000000)); |
---|
90 | PyDict_SetItemString(self->params,"length",Py_BuildValue("d",400.000000)); |
---|
91 | PyDict_SetItemString(self->params,"r_minor",Py_BuildValue("d",20.000000)); |
---|
92 | PyDict_SetItemString(self->params,"cyl_theta",Py_BuildValue("d",1.570000)); |
---|
93 | PyDict_SetItemString(self->params,"background",Py_BuildValue("d",0.000000)); |
---|
94 | PyDict_SetItemString(self->params,"r_ratio",Py_BuildValue("d",1.500000)); |
---|
95 | PyDict_SetItemString(self->params,"contrast",Py_BuildValue("d",0.000003)); |
---|
96 | PyDict_SetItemString(self->params,"cyl_phi",Py_BuildValue("d",0.000000)); |
---|
97 | // Initialize dispersion / averaging parameter dict |
---|
98 | DispersionVisitor* visitor = new DispersionVisitor(); |
---|
99 | PyObject * disp_dict; |
---|
100 | disp_dict = PyDict_New(); |
---|
101 | self->model->r_minor.dispersion->accept_as_source(visitor, self->model->r_minor.dispersion, disp_dict); |
---|
102 | PyDict_SetItemString(self->dispersion, "r_minor", disp_dict); |
---|
103 | disp_dict = PyDict_New(); |
---|
104 | self->model->r_ratio.dispersion->accept_as_source(visitor, self->model->r_ratio.dispersion, disp_dict); |
---|
105 | PyDict_SetItemString(self->dispersion, "r_ratio", disp_dict); |
---|
106 | disp_dict = PyDict_New(); |
---|
107 | self->model->length.dispersion->accept_as_source(visitor, self->model->length.dispersion, disp_dict); |
---|
108 | PyDict_SetItemString(self->dispersion, "length", disp_dict); |
---|
109 | disp_dict = PyDict_New(); |
---|
110 | self->model->cyl_theta.dispersion->accept_as_source(visitor, self->model->cyl_theta.dispersion, disp_dict); |
---|
111 | PyDict_SetItemString(self->dispersion, "cyl_theta", disp_dict); |
---|
112 | disp_dict = PyDict_New(); |
---|
113 | self->model->cyl_phi.dispersion->accept_as_source(visitor, self->model->cyl_phi.dispersion, disp_dict); |
---|
114 | PyDict_SetItemString(self->dispersion, "cyl_phi", disp_dict); |
---|
115 | disp_dict = PyDict_New(); |
---|
116 | self->model->cyl_psi.dispersion->accept_as_source(visitor, self->model->cyl_psi.dispersion, disp_dict); |
---|
117 | PyDict_SetItemString(self->dispersion, "cyl_psi", disp_dict); |
---|
118 | |
---|
119 | |
---|
120 | |
---|
121 | // Create empty log |
---|
122 | self->log = PyDict_New(); |
---|
123 | |
---|
124 | |
---|
125 | |
---|
126 | } |
---|
127 | return 0; |
---|
128 | } |
---|
129 | |
---|
130 | static PyMemberDef CEllipticalCylinderModel_members[] = { |
---|
131 | {"params", T_OBJECT, offsetof(CEllipticalCylinderModel, params), 0, |
---|
132 | "Parameters"}, |
---|
133 | {"dispersion", T_OBJECT, offsetof(CEllipticalCylinderModel, dispersion), 0, |
---|
134 | "Dispersion parameters"}, |
---|
135 | {"log", T_OBJECT, offsetof(CEllipticalCylinderModel, log), 0, |
---|
136 | "Log"}, |
---|
137 | {NULL} /* Sentinel */ |
---|
138 | }; |
---|
139 | |
---|
140 | /** Read double from PyObject |
---|
141 | @param p PyObject |
---|
142 | @return double |
---|
143 | */ |
---|
144 | double CEllipticalCylinderModel_readDouble(PyObject *p) { |
---|
145 | if (PyFloat_Check(p)==1) { |
---|
146 | return (double)(((PyFloatObject *)(p))->ob_fval); |
---|
147 | } else if (PyInt_Check(p)==1) { |
---|
148 | return (double)(((PyIntObject *)(p))->ob_ival); |
---|
149 | } else if (PyLong_Check(p)==1) { |
---|
150 | return (double)PyLong_AsLong(p); |
---|
151 | } else { |
---|
152 | return 0.0; |
---|
153 | } |
---|
154 | } |
---|
155 | /** |
---|
156 | * Function to call to evaluate model |
---|
157 | * @param args: input numpy array q[] |
---|
158 | * @return: numpy array object |
---|
159 | */ |
---|
160 | |
---|
161 | static PyObject *evaluateOneDim(EllipticalCylinderModel* model, PyArrayObject *q){ |
---|
162 | PyArrayObject *result; |
---|
163 | |
---|
164 | // Check validity of array q , q must be of dimension 1, an array of double |
---|
165 | if (q->nd != 1 || q->descr->type_num != PyArray_DOUBLE) |
---|
166 | { |
---|
167 | //const char * message= "Invalid array: q->nd=%d,type_num=%d\n",q->nd,q->descr->type_num; |
---|
168 | //PyErr_SetString(PyExc_ValueError , message); |
---|
169 | return NULL; |
---|
170 | } |
---|
171 | result = (PyArrayObject *)PyArray_FromDims(q->nd, (int *)(q->dimensions), |
---|
172 | PyArray_DOUBLE); |
---|
173 | if (result == NULL) { |
---|
174 | const char * message= "Could not create result "; |
---|
175 | PyErr_SetString(PyExc_RuntimeError , message); |
---|
176 | return NULL; |
---|
177 | } |
---|
178 | for (int i = 0; i < q->dimensions[0]; i++){ |
---|
179 | double q_value = *(double *)(q->data + i*q->strides[0]); |
---|
180 | double *result_value = (double *)(result->data + i*result->strides[0]); |
---|
181 | *result_value =(*model)(q_value); |
---|
182 | } |
---|
183 | return PyArray_Return(result); |
---|
184 | } |
---|
185 | /** |
---|
186 | * Function to call to evaluate model |
---|
187 | * @param args: input numpy array [q[],phi[]] |
---|
188 | * @return: numpy array object |
---|
189 | */ |
---|
190 | static PyObject * evaluateTwoDim( EllipticalCylinderModel* model, |
---|
191 | PyArrayObject *q, PyArrayObject *phi) |
---|
192 | { |
---|
193 | PyArrayObject *result; |
---|
194 | //check validity of input vectors |
---|
195 | if (q->nd != 1 || q->descr->type_num != PyArray_DOUBLE |
---|
196 | || phi->nd != 1 || phi->descr->type_num != PyArray_DOUBLE |
---|
197 | || phi->dimensions[0] != q->dimensions[0]){ |
---|
198 | |
---|
199 | //const char * message= "Invalid array: q->nd=%d,type_num=%d\n",q->nd,q->descr->type_num; |
---|
200 | PyErr_SetString(PyExc_ValueError ,"wrong input"); |
---|
201 | return NULL; |
---|
202 | } |
---|
203 | result= (PyArrayObject *)PyArray_FromDims(q->nd,(int*)(q->dimensions), PyArray_DOUBLE); |
---|
204 | |
---|
205 | if (result == NULL){ |
---|
206 | const char * message= "Could not create result "; |
---|
207 | PyErr_SetString(PyExc_RuntimeError , message); |
---|
208 | return NULL; |
---|
209 | } |
---|
210 | |
---|
211 | for (int i = 0; i < q->dimensions[0]; i++) { |
---|
212 | double q_value = *(double *)(q->data + i*q->strides[0]); |
---|
213 | double phi_value = *(double *)(phi->data + i*phi->strides[0]); |
---|
214 | double *result_value = (double *)(result->data + i*result->strides[0]); |
---|
215 | if (q_value == 0) |
---|
216 | *result_value = 0.0; |
---|
217 | else |
---|
218 | *result_value = model->evaluate_rphi(q_value, phi_value); |
---|
219 | } |
---|
220 | return PyArray_Return(result); |
---|
221 | } |
---|
222 | /** |
---|
223 | * Function to call to evaluate model |
---|
224 | * @param args: input numpy array [x[],y[]] |
---|
225 | * @return: numpy array object |
---|
226 | */ |
---|
227 | static PyObject * evaluateTwoDimXY( EllipticalCylinderModel* model, |
---|
228 | PyArrayObject *x, PyArrayObject *y) |
---|
229 | { |
---|
230 | PyArrayObject *result; |
---|
231 | int i,j, x_len, y_len, dims[2]; |
---|
232 | //check validity of input vectors |
---|
233 | if (x->nd != 2 || x->descr->type_num != PyArray_DOUBLE |
---|
234 | || y->nd != 2 || y->descr->type_num != PyArray_DOUBLE |
---|
235 | || y->dimensions[1] != x->dimensions[0]){ |
---|
236 | const char * message= "evaluateTwoDimXY expect 2 numpy arrays"; |
---|
237 | PyErr_SetString(PyExc_ValueError , message); |
---|
238 | return NULL; |
---|
239 | } |
---|
240 | |
---|
241 | if (PyArray_Check(x) && PyArray_Check(y)) { |
---|
242 | x_len = dims[0]= x->dimensions[0]; |
---|
243 | y_len = dims[1]= y->dimensions[1]; |
---|
244 | |
---|
245 | // Make a new double matrix of same dims |
---|
246 | result=(PyArrayObject *) PyArray_FromDims(2,dims,NPY_DOUBLE); |
---|
247 | if (result == NULL){ |
---|
248 | const char * message= "Could not create result "; |
---|
249 | PyErr_SetString(PyExc_RuntimeError , message); |
---|
250 | return NULL; |
---|
251 | } |
---|
252 | |
---|
253 | /* Do the calculation. */ |
---|
254 | for ( i=0; i< x_len; i++) { |
---|
255 | for ( j=0; j< y_len; j++) { |
---|
256 | double x_value = *(double *)(x->data + i*x->strides[0]); |
---|
257 | double y_value = *(double *)(y->data + j*y->strides[1]); |
---|
258 | double *result_value = (double *)(result->data + |
---|
259 | i*result->strides[0] + j*result->strides[1]); |
---|
260 | *result_value = (*model)(x_value, y_value); |
---|
261 | } |
---|
262 | } |
---|
263 | return PyArray_Return(result); |
---|
264 | |
---|
265 | }else{ |
---|
266 | PyErr_SetString(CEllipticalCylinderModelError, |
---|
267 | "CEllipticalCylinderModel.evaluateTwoDimXY couldn't run."); |
---|
268 | return NULL; |
---|
269 | } |
---|
270 | } |
---|
271 | /** |
---|
272 | * evalDistribution function evaluate a model function with input vector |
---|
273 | * @param args: input q as vector or [qx, qy] where qx, qy are vectors |
---|
274 | * |
---|
275 | */ |
---|
276 | static PyObject * evalDistribution(CEllipticalCylinderModel *self, PyObject *args){ |
---|
277 | PyObject *qx, *qy; |
---|
278 | PyArrayObject * pars; |
---|
279 | int npars ,mpars; |
---|
280 | |
---|
281 | // Get parameters |
---|
282 | |
---|
283 | // Reader parameter dictionary |
---|
284 | self->model->scale = PyFloat_AsDouble( PyDict_GetItemString(self->params, "scale") ); |
---|
285 | self->model->cyl_psi = PyFloat_AsDouble( PyDict_GetItemString(self->params, "cyl_psi") ); |
---|
286 | self->model->length = PyFloat_AsDouble( PyDict_GetItemString(self->params, "length") ); |
---|
287 | self->model->r_minor = PyFloat_AsDouble( PyDict_GetItemString(self->params, "r_minor") ); |
---|
288 | self->model->cyl_theta = PyFloat_AsDouble( PyDict_GetItemString(self->params, "cyl_theta") ); |
---|
289 | self->model->background = PyFloat_AsDouble( PyDict_GetItemString(self->params, "background") ); |
---|
290 | self->model->r_ratio = PyFloat_AsDouble( PyDict_GetItemString(self->params, "r_ratio") ); |
---|
291 | self->model->contrast = PyFloat_AsDouble( PyDict_GetItemString(self->params, "contrast") ); |
---|
292 | self->model->cyl_phi = PyFloat_AsDouble( PyDict_GetItemString(self->params, "cyl_phi") ); |
---|
293 | // Read in dispersion parameters |
---|
294 | PyObject* disp_dict; |
---|
295 | DispersionVisitor* visitor = new DispersionVisitor(); |
---|
296 | disp_dict = PyDict_GetItemString(self->dispersion, "r_minor"); |
---|
297 | self->model->r_minor.dispersion->accept_as_destination(visitor, self->model->r_minor.dispersion, disp_dict); |
---|
298 | disp_dict = PyDict_GetItemString(self->dispersion, "r_ratio"); |
---|
299 | self->model->r_ratio.dispersion->accept_as_destination(visitor, self->model->r_ratio.dispersion, disp_dict); |
---|
300 | disp_dict = PyDict_GetItemString(self->dispersion, "length"); |
---|
301 | self->model->length.dispersion->accept_as_destination(visitor, self->model->length.dispersion, disp_dict); |
---|
302 | disp_dict = PyDict_GetItemString(self->dispersion, "cyl_theta"); |
---|
303 | self->model->cyl_theta.dispersion->accept_as_destination(visitor, self->model->cyl_theta.dispersion, disp_dict); |
---|
304 | disp_dict = PyDict_GetItemString(self->dispersion, "cyl_phi"); |
---|
305 | self->model->cyl_phi.dispersion->accept_as_destination(visitor, self->model->cyl_phi.dispersion, disp_dict); |
---|
306 | disp_dict = PyDict_GetItemString(self->dispersion, "cyl_psi"); |
---|
307 | self->model->cyl_psi.dispersion->accept_as_destination(visitor, self->model->cyl_psi.dispersion, disp_dict); |
---|
308 | |
---|
309 | |
---|
310 | // Get input and determine whether we have to supply a 1D or 2D return value. |
---|
311 | if ( !PyArg_ParseTuple(args,"O",&pars) ) { |
---|
312 | PyErr_SetString(CEllipticalCylinderModelError, |
---|
313 | "CEllipticalCylinderModel.evalDistribution expects a q value."); |
---|
314 | return NULL; |
---|
315 | } |
---|
316 | // Check params |
---|
317 | |
---|
318 | if(PyArray_Check(pars)==1) { |
---|
319 | |
---|
320 | // Length of list should 1 or 2 |
---|
321 | npars = pars->nd; |
---|
322 | if(npars==1) { |
---|
323 | // input is a numpy array |
---|
324 | if (PyArray_Check(pars)) { |
---|
325 | return evaluateOneDim(self->model, (PyArrayObject*)pars); |
---|
326 | } |
---|
327 | }else{ |
---|
328 | PyErr_SetString(CEllipticalCylinderModelError, |
---|
329 | "CEllipticalCylinderModel.evalDistribution expect numpy array of one dimension."); |
---|
330 | return NULL; |
---|
331 | } |
---|
332 | }else if( PyList_Check(pars)==1) { |
---|
333 | // Length of list should be 2 for I(qx,qy) |
---|
334 | mpars = PyList_GET_SIZE(pars); |
---|
335 | if(mpars!=2) { |
---|
336 | PyErr_SetString(CEllipticalCylinderModelError, |
---|
337 | "CEllipticalCylinderModel.evalDistribution expects a list of dimension 2."); |
---|
338 | return NULL; |
---|
339 | } |
---|
340 | qx = PyList_GET_ITEM(pars,0); |
---|
341 | qy = PyList_GET_ITEM(pars,1); |
---|
342 | if (PyArray_Check(qx) && PyArray_Check(qy)) { |
---|
343 | return evaluateTwoDimXY(self->model, (PyArrayObject*)qx, |
---|
344 | (PyArrayObject*)qy); |
---|
345 | }else{ |
---|
346 | PyErr_SetString(CEllipticalCylinderModelError, |
---|
347 | "CEllipticalCylinderModel.evalDistribution expect 2 numpy arrays in list."); |
---|
348 | return NULL; |
---|
349 | } |
---|
350 | }else{ |
---|
351 | PyErr_SetString(CEllipticalCylinderModelError, |
---|
352 | "CEllipticalCylinderModel.evalDistribution couln't be run."); |
---|
353 | return NULL; |
---|
354 | } |
---|
355 | } |
---|
356 | |
---|
357 | /** |
---|
358 | * Function to call to evaluate model |
---|
359 | * @param args: input q or [q,phi] |
---|
360 | * @return: function value |
---|
361 | */ |
---|
362 | static PyObject * run(CEllipticalCylinderModel *self, PyObject *args) { |
---|
363 | double q_value, phi_value; |
---|
364 | PyObject* pars; |
---|
365 | int npars; |
---|
366 | |
---|
367 | // Get parameters |
---|
368 | |
---|
369 | // Reader parameter dictionary |
---|
370 | self->model->scale = PyFloat_AsDouble( PyDict_GetItemString(self->params, "scale") ); |
---|
371 | self->model->cyl_psi = PyFloat_AsDouble( PyDict_GetItemString(self->params, "cyl_psi") ); |
---|
372 | self->model->length = PyFloat_AsDouble( PyDict_GetItemString(self->params, "length") ); |
---|
373 | self->model->r_minor = PyFloat_AsDouble( PyDict_GetItemString(self->params, "r_minor") ); |
---|
374 | self->model->cyl_theta = PyFloat_AsDouble( PyDict_GetItemString(self->params, "cyl_theta") ); |
---|
375 | self->model->background = PyFloat_AsDouble( PyDict_GetItemString(self->params, "background") ); |
---|
376 | self->model->r_ratio = PyFloat_AsDouble( PyDict_GetItemString(self->params, "r_ratio") ); |
---|
377 | self->model->contrast = PyFloat_AsDouble( PyDict_GetItemString(self->params, "contrast") ); |
---|
378 | self->model->cyl_phi = PyFloat_AsDouble( PyDict_GetItemString(self->params, "cyl_phi") ); |
---|
379 | // Read in dispersion parameters |
---|
380 | PyObject* disp_dict; |
---|
381 | DispersionVisitor* visitor = new DispersionVisitor(); |
---|
382 | disp_dict = PyDict_GetItemString(self->dispersion, "r_minor"); |
---|
383 | self->model->r_minor.dispersion->accept_as_destination(visitor, self->model->r_minor.dispersion, disp_dict); |
---|
384 | disp_dict = PyDict_GetItemString(self->dispersion, "r_ratio"); |
---|
385 | self->model->r_ratio.dispersion->accept_as_destination(visitor, self->model->r_ratio.dispersion, disp_dict); |
---|
386 | disp_dict = PyDict_GetItemString(self->dispersion, "length"); |
---|
387 | self->model->length.dispersion->accept_as_destination(visitor, self->model->length.dispersion, disp_dict); |
---|
388 | disp_dict = PyDict_GetItemString(self->dispersion, "cyl_theta"); |
---|
389 | self->model->cyl_theta.dispersion->accept_as_destination(visitor, self->model->cyl_theta.dispersion, disp_dict); |
---|
390 | disp_dict = PyDict_GetItemString(self->dispersion, "cyl_phi"); |
---|
391 | self->model->cyl_phi.dispersion->accept_as_destination(visitor, self->model->cyl_phi.dispersion, disp_dict); |
---|
392 | disp_dict = PyDict_GetItemString(self->dispersion, "cyl_psi"); |
---|
393 | self->model->cyl_psi.dispersion->accept_as_destination(visitor, self->model->cyl_psi.dispersion, disp_dict); |
---|
394 | |
---|
395 | |
---|
396 | // Get input and determine whether we have to supply a 1D or 2D return value. |
---|
397 | if ( !PyArg_ParseTuple(args,"O",&pars) ) { |
---|
398 | PyErr_SetString(CEllipticalCylinderModelError, |
---|
399 | "CEllipticalCylinderModel.run expects a q value."); |
---|
400 | return NULL; |
---|
401 | } |
---|
402 | |
---|
403 | // Check params |
---|
404 | if( PyList_Check(pars)==1) { |
---|
405 | |
---|
406 | // Length of list should be 2 for I(q,phi) |
---|
407 | npars = PyList_GET_SIZE(pars); |
---|
408 | if(npars!=2) { |
---|
409 | PyErr_SetString(CEllipticalCylinderModelError, |
---|
410 | "CEllipticalCylinderModel.run expects a double or a list of dimension 2."); |
---|
411 | return NULL; |
---|
412 | } |
---|
413 | // We have a vector q, get the q and phi values at which |
---|
414 | // to evaluate I(q,phi) |
---|
415 | q_value = CEllipticalCylinderModel_readDouble(PyList_GET_ITEM(pars,0)); |
---|
416 | phi_value = CEllipticalCylinderModel_readDouble(PyList_GET_ITEM(pars,1)); |
---|
417 | // Skip zero |
---|
418 | if (q_value==0) { |
---|
419 | return Py_BuildValue("d",0.0); |
---|
420 | } |
---|
421 | return Py_BuildValue("d",(*(self->model)).evaluate_rphi(q_value,phi_value)); |
---|
422 | |
---|
423 | } else { |
---|
424 | |
---|
425 | // We have a scalar q, we will evaluate I(q) |
---|
426 | q_value = CEllipticalCylinderModel_readDouble(pars); |
---|
427 | |
---|
428 | return Py_BuildValue("d",(*(self->model))(q_value)); |
---|
429 | } |
---|
430 | } |
---|
431 | |
---|
432 | /** |
---|
433 | * Function to call to evaluate model in cartesian coordinates |
---|
434 | * @param args: input q or [qx, qy]] |
---|
435 | * @return: function value |
---|
436 | */ |
---|
437 | static PyObject * runXY(CEllipticalCylinderModel *self, PyObject *args) { |
---|
438 | double qx_value, qy_value; |
---|
439 | PyObject* pars; |
---|
440 | int npars; |
---|
441 | |
---|
442 | // Get parameters |
---|
443 | |
---|
444 | // Reader parameter dictionary |
---|
445 | self->model->scale = PyFloat_AsDouble( PyDict_GetItemString(self->params, "scale") ); |
---|
446 | self->model->cyl_psi = PyFloat_AsDouble( PyDict_GetItemString(self->params, "cyl_psi") ); |
---|
447 | self->model->length = PyFloat_AsDouble( PyDict_GetItemString(self->params, "length") ); |
---|
448 | self->model->r_minor = PyFloat_AsDouble( PyDict_GetItemString(self->params, "r_minor") ); |
---|
449 | self->model->cyl_theta = PyFloat_AsDouble( PyDict_GetItemString(self->params, "cyl_theta") ); |
---|
450 | self->model->background = PyFloat_AsDouble( PyDict_GetItemString(self->params, "background") ); |
---|
451 | self->model->r_ratio = PyFloat_AsDouble( PyDict_GetItemString(self->params, "r_ratio") ); |
---|
452 | self->model->contrast = PyFloat_AsDouble( PyDict_GetItemString(self->params, "contrast") ); |
---|
453 | self->model->cyl_phi = PyFloat_AsDouble( PyDict_GetItemString(self->params, "cyl_phi") ); |
---|
454 | // Read in dispersion parameters |
---|
455 | PyObject* disp_dict; |
---|
456 | DispersionVisitor* visitor = new DispersionVisitor(); |
---|
457 | disp_dict = PyDict_GetItemString(self->dispersion, "r_minor"); |
---|
458 | self->model->r_minor.dispersion->accept_as_destination(visitor, self->model->r_minor.dispersion, disp_dict); |
---|
459 | disp_dict = PyDict_GetItemString(self->dispersion, "r_ratio"); |
---|
460 | self->model->r_ratio.dispersion->accept_as_destination(visitor, self->model->r_ratio.dispersion, disp_dict); |
---|
461 | disp_dict = PyDict_GetItemString(self->dispersion, "length"); |
---|
462 | self->model->length.dispersion->accept_as_destination(visitor, self->model->length.dispersion, disp_dict); |
---|
463 | disp_dict = PyDict_GetItemString(self->dispersion, "cyl_theta"); |
---|
464 | self->model->cyl_theta.dispersion->accept_as_destination(visitor, self->model->cyl_theta.dispersion, disp_dict); |
---|
465 | disp_dict = PyDict_GetItemString(self->dispersion, "cyl_phi"); |
---|
466 | self->model->cyl_phi.dispersion->accept_as_destination(visitor, self->model->cyl_phi.dispersion, disp_dict); |
---|
467 | disp_dict = PyDict_GetItemString(self->dispersion, "cyl_psi"); |
---|
468 | self->model->cyl_psi.dispersion->accept_as_destination(visitor, self->model->cyl_psi.dispersion, disp_dict); |
---|
469 | |
---|
470 | |
---|
471 | // Get input and determine whether we have to supply a 1D or 2D return value. |
---|
472 | if ( !PyArg_ParseTuple(args,"O",&pars) ) { |
---|
473 | PyErr_SetString(CEllipticalCylinderModelError, |
---|
474 | "CEllipticalCylinderModel.run expects a q value."); |
---|
475 | return NULL; |
---|
476 | } |
---|
477 | |
---|
478 | // Check params |
---|
479 | if( PyList_Check(pars)==1) { |
---|
480 | |
---|
481 | // Length of list should be 2 for I(qx, qy)) |
---|
482 | npars = PyList_GET_SIZE(pars); |
---|
483 | if(npars!=2) { |
---|
484 | PyErr_SetString(CEllipticalCylinderModelError, |
---|
485 | "CEllipticalCylinderModel.run expects a double or a list of dimension 2."); |
---|
486 | return NULL; |
---|
487 | } |
---|
488 | // We have a vector q, get the qx and qy values at which |
---|
489 | // to evaluate I(qx,qy) |
---|
490 | qx_value = CEllipticalCylinderModel_readDouble(PyList_GET_ITEM(pars,0)); |
---|
491 | qy_value = CEllipticalCylinderModel_readDouble(PyList_GET_ITEM(pars,1)); |
---|
492 | return Py_BuildValue("d",(*(self->model))(qx_value,qy_value)); |
---|
493 | |
---|
494 | } else { |
---|
495 | |
---|
496 | // We have a scalar q, we will evaluate I(q) |
---|
497 | qx_value = CEllipticalCylinderModel_readDouble(pars); |
---|
498 | |
---|
499 | return Py_BuildValue("d",(*(self->model))(qx_value)); |
---|
500 | } |
---|
501 | } |
---|
502 | |
---|
503 | static PyObject * reset(CEllipticalCylinderModel *self, PyObject *args) { |
---|
504 | |
---|
505 | |
---|
506 | return Py_BuildValue("d",0.0); |
---|
507 | } |
---|
508 | |
---|
509 | static PyObject * set_dispersion(CEllipticalCylinderModel *self, PyObject *args) { |
---|
510 | PyObject * disp; |
---|
511 | const char * par_name; |
---|
512 | |
---|
513 | if ( !PyArg_ParseTuple(args,"sO", &par_name, &disp) ) { |
---|
514 | PyErr_SetString(CEllipticalCylinderModelError, |
---|
515 | "CEllipticalCylinderModel.set_dispersion expects a DispersionModel object."); |
---|
516 | return NULL; |
---|
517 | } |
---|
518 | void *temp = PyCObject_AsVoidPtr(disp); |
---|
519 | DispersionModel * dispersion = static_cast<DispersionModel *>(temp); |
---|
520 | |
---|
521 | |
---|
522 | // Ugliness necessary to go from python to C |
---|
523 | // TODO: refactor this |
---|
524 | if (!strcmp(par_name, "r_minor")) { |
---|
525 | self->model->r_minor.dispersion = dispersion; |
---|
526 | } else if (!strcmp(par_name, "r_ratio")) { |
---|
527 | self->model->r_ratio.dispersion = dispersion; |
---|
528 | } else if (!strcmp(par_name, "length")) { |
---|
529 | self->model->length.dispersion = dispersion; |
---|
530 | } else if (!strcmp(par_name, "cyl_theta")) { |
---|
531 | self->model->cyl_theta.dispersion = dispersion; |
---|
532 | } else if (!strcmp(par_name, "cyl_phi")) { |
---|
533 | self->model->cyl_phi.dispersion = dispersion; |
---|
534 | } else if (!strcmp(par_name, "cyl_psi")) { |
---|
535 | self->model->cyl_psi.dispersion = dispersion; |
---|
536 | } else { |
---|
537 | PyErr_SetString(CEllipticalCylinderModelError, |
---|
538 | "CEllipticalCylinderModel.set_dispersion expects a valid parameter name."); |
---|
539 | return NULL; |
---|
540 | } |
---|
541 | |
---|
542 | DispersionVisitor* visitor = new DispersionVisitor(); |
---|
543 | PyObject * disp_dict = PyDict_New(); |
---|
544 | dispersion->accept_as_source(visitor, dispersion, disp_dict); |
---|
545 | PyDict_SetItemString(self->dispersion, par_name, disp_dict); |
---|
546 | return Py_BuildValue("i",1); |
---|
547 | } |
---|
548 | |
---|
549 | |
---|
550 | static PyMethodDef CEllipticalCylinderModel_methods[] = { |
---|
551 | {"run", (PyCFunction)run , METH_VARARGS, |
---|
552 | "Evaluate the model at a given Q or Q, phi"}, |
---|
553 | {"runXY", (PyCFunction)runXY , METH_VARARGS, |
---|
554 | "Evaluate the model at a given Q or Qx, Qy"}, |
---|
555 | |
---|
556 | {"evalDistribution", (PyCFunction)evalDistribution , METH_VARARGS, |
---|
557 | "Evaluate the model at a given Q or Qx, Qy vector "}, |
---|
558 | {"reset", (PyCFunction)reset , METH_VARARGS, |
---|
559 | "Reset pair correlation"}, |
---|
560 | {"set_dispersion", (PyCFunction)set_dispersion , METH_VARARGS, |
---|
561 | "Set the dispersion model for a given parameter"}, |
---|
562 | {NULL} |
---|
563 | }; |
---|
564 | |
---|
565 | static PyTypeObject CEllipticalCylinderModelType = { |
---|
566 | PyObject_HEAD_INIT(NULL) |
---|
567 | 0, /*ob_size*/ |
---|
568 | "CEllipticalCylinderModel", /*tp_name*/ |
---|
569 | sizeof(CEllipticalCylinderModel), /*tp_basicsize*/ |
---|
570 | 0, /*tp_itemsize*/ |
---|
571 | (destructor)CEllipticalCylinderModel_dealloc, /*tp_dealloc*/ |
---|
572 | 0, /*tp_print*/ |
---|
573 | 0, /*tp_getattr*/ |
---|
574 | 0, /*tp_setattr*/ |
---|
575 | 0, /*tp_compare*/ |
---|
576 | 0, /*tp_repr*/ |
---|
577 | 0, /*tp_as_number*/ |
---|
578 | 0, /*tp_as_sequence*/ |
---|
579 | 0, /*tp_as_mapping*/ |
---|
580 | 0, /*tp_hash */ |
---|
581 | 0, /*tp_call*/ |
---|
582 | 0, /*tp_str*/ |
---|
583 | 0, /*tp_getattro*/ |
---|
584 | 0, /*tp_setattro*/ |
---|
585 | 0, /*tp_as_buffer*/ |
---|
586 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/ |
---|
587 | "CEllipticalCylinderModel objects", /* tp_doc */ |
---|
588 | 0, /* tp_traverse */ |
---|
589 | 0, /* tp_clear */ |
---|
590 | 0, /* tp_richcompare */ |
---|
591 | 0, /* tp_weaklistoffset */ |
---|
592 | 0, /* tp_iter */ |
---|
593 | 0, /* tp_iternext */ |
---|
594 | CEllipticalCylinderModel_methods, /* tp_methods */ |
---|
595 | CEllipticalCylinderModel_members, /* tp_members */ |
---|
596 | 0, /* tp_getset */ |
---|
597 | 0, /* tp_base */ |
---|
598 | 0, /* tp_dict */ |
---|
599 | 0, /* tp_descr_get */ |
---|
600 | 0, /* tp_descr_set */ |
---|
601 | 0, /* tp_dictoffset */ |
---|
602 | (initproc)CEllipticalCylinderModel_init, /* tp_init */ |
---|
603 | 0, /* tp_alloc */ |
---|
604 | CEllipticalCylinderModel_new, /* tp_new */ |
---|
605 | }; |
---|
606 | |
---|
607 | |
---|
608 | //static PyMethodDef module_methods[] = { |
---|
609 | // {NULL} |
---|
610 | //}; |
---|
611 | |
---|
612 | /** |
---|
613 | * Function used to add the model class to a module |
---|
614 | * @param module: module to add the class to |
---|
615 | */ |
---|
616 | void addCEllipticalCylinderModel(PyObject *module) { |
---|
617 | PyObject *d; |
---|
618 | |
---|
619 | if (PyType_Ready(&CEllipticalCylinderModelType) < 0) |
---|
620 | return; |
---|
621 | |
---|
622 | Py_INCREF(&CEllipticalCylinderModelType); |
---|
623 | PyModule_AddObject(module, "CEllipticalCylinderModel", (PyObject *)&CEllipticalCylinderModelType); |
---|
624 | |
---|
625 | d = PyModule_GetDict(module); |
---|
626 | CEllipticalCylinderModelError = PyErr_NewException("CEllipticalCylinderModel.error", NULL, NULL); |
---|
627 | PyDict_SetItemString(d, "CEllipticalCylinderModelError", CEllipticalCylinderModelError); |
---|
628 | } |
---|
629 | |
---|