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