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