source: sasview/sansmodels/src/sans/models/c_models/CEllipticalCylinderModel.cpp @ e2f7b92

ESS_GUIESS_GUI_DocsESS_GUI_batch_fittingESS_GUI_bumps_abstractionESS_GUI_iss1116ESS_GUI_iss879ESS_GUI_iss959ESS_GUI_openclESS_GUI_orderingESS_GUI_sync_sascalccostrafo411magnetic_scattrelease-4.1.1release-4.1.2release-4.2.2release_4.0.1ticket-1009ticket-1094-headlessticket-1242-2d-resolutionticket-1243ticket-1249ticket885unittest-saveload
Last change on this file since e2f7b92 was 9ce41c6, checked in by Gervaise Alina <gervyh@…>, 15 years ago

wrote unittest for all model untested , haven' test critial point error handling

  • Property mode set to 100644
File size: 26.7 KB
Line 
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 
27extern "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
42static PyObject * CEllipticalCylinderModelError = NULL;
43
44
45// Class definition
46typedef 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
59static void
60CEllipticalCylinderModel_dealloc(CEllipticalCylinderModel* self)
61{
62    self->ob_type->tp_free((PyObject*)self);
63   
64
65}
66
67static PyObject *
68CEllipticalCylinderModel_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
77static int
78CEllipticalCylinderModel_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
130static 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*/
144double 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 
161static 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 /**
187 * Function to call to evaluate model
188 * @param args: input numpy array  [x[],y[]]
189 * @return: numpy array object
190 */
191 static PyObject * evaluateTwoDimXY( EllipticalCylinderModel* model, 
192                              PyArrayObject *x, PyArrayObject *y)
193 {
194    PyArrayObject *result;
195    int i,j, x_len, y_len, dims[2];
196    //check validity of input vectors
197    if (x->nd != 2 || x->descr->type_num != PyArray_DOUBLE
198        || y->nd != 2 || y->descr->type_num != PyArray_DOUBLE
199        || y->dimensions[1] != x->dimensions[0]){
200        const char * message= "evaluateTwoDimXY  expect 2 numpy arrays";
201        PyErr_SetString(PyExc_ValueError , message); 
202        return NULL;
203    }
204   
205        if (PyArray_Check(x) && PyArray_Check(y)) {
206               
207            x_len = dims[1]= x->dimensions[1];
208        y_len = dims[0]= y->dimensions[0];
209           
210            // Make a new double matrix of same dims
211        result=(PyArrayObject *) PyArray_FromDims(2,dims,NPY_DOUBLE);
212        if (result == NULL){
213            const char * message= "Could not create result ";
214        PyErr_SetString(PyExc_RuntimeError , message);
215            return NULL;
216            }
217       
218        /* Do the calculation. */
219        for ( j=0; j< y_len; j++) {
220            for ( i=0; i< x_len; i++) {
221                double x_value = *(double *)(x->data + i*x->strides[1]);
222                    double y_value = *(double *)(y->data + j*y->strides[0]);
223                        double *result_value = (double *)(result->data +
224                              j*result->strides[0] + i*result->strides[1]);
225                        *result_value = (*model)(x_value, y_value);
226            }           
227        }
228        return PyArray_Return(result); 
229       
230        }else{
231                    PyErr_SetString(CEllipticalCylinderModelError, 
232                   "CEllipticalCylinderModel.evaluateTwoDimXY couldn't run.");
233                return NULL;
234                }       
235}
236/**
237 *  evalDistribution function evaluate a model function with input vector
238 *  @param args: input q as vector or [qx, qy] where qx, qy are vectors
239 *
240 */ 
241static PyObject * evalDistribution(CEllipticalCylinderModel *self, PyObject *args){
242        PyObject *qx, *qy;
243        PyArrayObject * pars;
244        int npars ,mpars;
245       
246        // Get parameters
247       
248            // Reader parameter dictionary
249    self->model->scale = PyFloat_AsDouble( PyDict_GetItemString(self->params, "scale") );
250    self->model->cyl_psi = PyFloat_AsDouble( PyDict_GetItemString(self->params, "cyl_psi") );
251    self->model->length = PyFloat_AsDouble( PyDict_GetItemString(self->params, "length") );
252    self->model->r_minor = PyFloat_AsDouble( PyDict_GetItemString(self->params, "r_minor") );
253    self->model->cyl_theta = PyFloat_AsDouble( PyDict_GetItemString(self->params, "cyl_theta") );
254    self->model->background = PyFloat_AsDouble( PyDict_GetItemString(self->params, "background") );
255    self->model->r_ratio = PyFloat_AsDouble( PyDict_GetItemString(self->params, "r_ratio") );
256    self->model->contrast = PyFloat_AsDouble( PyDict_GetItemString(self->params, "contrast") );
257    self->model->cyl_phi = PyFloat_AsDouble( PyDict_GetItemString(self->params, "cyl_phi") );
258    // Read in dispersion parameters
259    PyObject* disp_dict;
260    DispersionVisitor* visitor = new DispersionVisitor();
261    disp_dict = PyDict_GetItemString(self->dispersion, "r_minor");
262    self->model->r_minor.dispersion->accept_as_destination(visitor, self->model->r_minor.dispersion, disp_dict);
263    disp_dict = PyDict_GetItemString(self->dispersion, "r_ratio");
264    self->model->r_ratio.dispersion->accept_as_destination(visitor, self->model->r_ratio.dispersion, disp_dict);
265    disp_dict = PyDict_GetItemString(self->dispersion, "length");
266    self->model->length.dispersion->accept_as_destination(visitor, self->model->length.dispersion, disp_dict);
267    disp_dict = PyDict_GetItemString(self->dispersion, "cyl_theta");
268    self->model->cyl_theta.dispersion->accept_as_destination(visitor, self->model->cyl_theta.dispersion, disp_dict);
269    disp_dict = PyDict_GetItemString(self->dispersion, "cyl_phi");
270    self->model->cyl_phi.dispersion->accept_as_destination(visitor, self->model->cyl_phi.dispersion, disp_dict);
271    disp_dict = PyDict_GetItemString(self->dispersion, "cyl_psi");
272    self->model->cyl_psi.dispersion->accept_as_destination(visitor, self->model->cyl_psi.dispersion, disp_dict);
273
274       
275        // Get input and determine whether we have to supply a 1D or 2D return value.
276        if ( !PyArg_ParseTuple(args,"O",&pars) ) {
277            PyErr_SetString(CEllipticalCylinderModelError, 
278                "CEllipticalCylinderModel.evalDistribution expects a q value.");
279                return NULL;
280        }
281    // Check params
282       
283    if(PyArray_Check(pars)==1) {
284               
285            // Length of list should 1 or 2
286            npars = pars->nd; 
287            if(npars==1) {
288                // input is a numpy array
289                if (PyArray_Check(pars)) {
290                        return evaluateOneDim(self->model, (PyArrayObject*)pars); 
291                    }
292                }else{
293                    PyErr_SetString(CEllipticalCylinderModelError, 
294                   "CEllipticalCylinderModel.evalDistribution expect numpy array of one dimension.");
295                return NULL;
296                }
297    }else if( PyList_Check(pars)==1) {
298        // Length of list should be 2 for I(qx,qy)
299            mpars = PyList_GET_SIZE(pars); 
300            if(mpars!=2) {
301                PyErr_SetString(CEllipticalCylinderModelError, 
302                        "CEllipticalCylinderModel.evalDistribution expects a list of dimension 2.");
303                return NULL;
304            }
305             qx = PyList_GET_ITEM(pars,0);
306             qy = PyList_GET_ITEM(pars,1);
307             if (PyArray_Check(qx) && PyArray_Check(qy)) {
308                 return evaluateTwoDimXY(self->model, (PyArrayObject*)qx,
309                           (PyArrayObject*)qy);
310                 }else{
311                    PyErr_SetString(CEllipticalCylinderModelError, 
312                   "CEllipticalCylinderModel.evalDistribution expect 2 numpy arrays in list.");
313                return NULL;
314             }
315        }
316        PyErr_SetString(CEllipticalCylinderModelError, 
317                   "CEllipticalCylinderModel.evalDistribution couln't be run.");
318        return NULL;
319       
320}
321
322/**
323 * Function to call to evaluate model
324 * @param args: input q or [q,phi]
325 * @return: function value
326 */
327static PyObject * run(CEllipticalCylinderModel *self, PyObject *args) {
328        double q_value, phi_value;
329        PyObject* pars;
330        int npars;
331       
332        // Get parameters
333       
334            // Reader parameter dictionary
335    self->model->scale = PyFloat_AsDouble( PyDict_GetItemString(self->params, "scale") );
336    self->model->cyl_psi = PyFloat_AsDouble( PyDict_GetItemString(self->params, "cyl_psi") );
337    self->model->length = PyFloat_AsDouble( PyDict_GetItemString(self->params, "length") );
338    self->model->r_minor = PyFloat_AsDouble( PyDict_GetItemString(self->params, "r_minor") );
339    self->model->cyl_theta = PyFloat_AsDouble( PyDict_GetItemString(self->params, "cyl_theta") );
340    self->model->background = PyFloat_AsDouble( PyDict_GetItemString(self->params, "background") );
341    self->model->r_ratio = PyFloat_AsDouble( PyDict_GetItemString(self->params, "r_ratio") );
342    self->model->contrast = PyFloat_AsDouble( PyDict_GetItemString(self->params, "contrast") );
343    self->model->cyl_phi = PyFloat_AsDouble( PyDict_GetItemString(self->params, "cyl_phi") );
344    // Read in dispersion parameters
345    PyObject* disp_dict;
346    DispersionVisitor* visitor = new DispersionVisitor();
347    disp_dict = PyDict_GetItemString(self->dispersion, "r_minor");
348    self->model->r_minor.dispersion->accept_as_destination(visitor, self->model->r_minor.dispersion, disp_dict);
349    disp_dict = PyDict_GetItemString(self->dispersion, "r_ratio");
350    self->model->r_ratio.dispersion->accept_as_destination(visitor, self->model->r_ratio.dispersion, disp_dict);
351    disp_dict = PyDict_GetItemString(self->dispersion, "length");
352    self->model->length.dispersion->accept_as_destination(visitor, self->model->length.dispersion, disp_dict);
353    disp_dict = PyDict_GetItemString(self->dispersion, "cyl_theta");
354    self->model->cyl_theta.dispersion->accept_as_destination(visitor, self->model->cyl_theta.dispersion, disp_dict);
355    disp_dict = PyDict_GetItemString(self->dispersion, "cyl_phi");
356    self->model->cyl_phi.dispersion->accept_as_destination(visitor, self->model->cyl_phi.dispersion, disp_dict);
357    disp_dict = PyDict_GetItemString(self->dispersion, "cyl_psi");
358    self->model->cyl_psi.dispersion->accept_as_destination(visitor, self->model->cyl_psi.dispersion, disp_dict);
359
360       
361        // Get input and determine whether we have to supply a 1D or 2D return value.
362        if ( !PyArg_ParseTuple(args,"O",&pars) ) {
363            PyErr_SetString(CEllipticalCylinderModelError, 
364                "CEllipticalCylinderModel.run expects a q value.");
365                return NULL;
366        }
367         
368        // Check params
369        if( PyList_Check(pars)==1) {
370               
371                // Length of list should be 2 for I(q,phi)
372            npars = PyList_GET_SIZE(pars); 
373            if(npars!=2) {
374                PyErr_SetString(CEllipticalCylinderModelError, 
375                        "CEllipticalCylinderModel.run expects a double or a list of dimension 2.");
376                return NULL;
377            }
378            // We have a vector q, get the q and phi values at which
379            // to evaluate I(q,phi)
380            q_value = CEllipticalCylinderModel_readDouble(PyList_GET_ITEM(pars,0));
381            phi_value = CEllipticalCylinderModel_readDouble(PyList_GET_ITEM(pars,1));
382            // Skip zero
383            if (q_value==0) {
384                return Py_BuildValue("d",0.0);
385            }
386                return Py_BuildValue("d",(*(self->model)).evaluate_rphi(q_value,phi_value));
387
388        } else {
389
390                // We have a scalar q, we will evaluate I(q)
391                q_value = CEllipticalCylinderModel_readDouble(pars);           
392               
393                return Py_BuildValue("d",(*(self->model))(q_value));
394        }       
395}
396/**
397 * Function to call to calculate_ER
398 * @return: effective radius value
399 */
400static PyObject * calculate_ER(CEllipticalCylinderModel *self) {
401
402        PyObject* pars;
403        int npars;
404       
405        // Get parameters
406       
407            // Reader parameter dictionary
408    self->model->scale = PyFloat_AsDouble( PyDict_GetItemString(self->params, "scale") );
409    self->model->cyl_psi = PyFloat_AsDouble( PyDict_GetItemString(self->params, "cyl_psi") );
410    self->model->length = PyFloat_AsDouble( PyDict_GetItemString(self->params, "length") );
411    self->model->r_minor = PyFloat_AsDouble( PyDict_GetItemString(self->params, "r_minor") );
412    self->model->cyl_theta = PyFloat_AsDouble( PyDict_GetItemString(self->params, "cyl_theta") );
413    self->model->background = PyFloat_AsDouble( PyDict_GetItemString(self->params, "background") );
414    self->model->r_ratio = PyFloat_AsDouble( PyDict_GetItemString(self->params, "r_ratio") );
415    self->model->contrast = PyFloat_AsDouble( PyDict_GetItemString(self->params, "contrast") );
416    self->model->cyl_phi = PyFloat_AsDouble( PyDict_GetItemString(self->params, "cyl_phi") );
417    // Read in dispersion parameters
418    PyObject* disp_dict;
419    DispersionVisitor* visitor = new DispersionVisitor();
420    disp_dict = PyDict_GetItemString(self->dispersion, "r_minor");
421    self->model->r_minor.dispersion->accept_as_destination(visitor, self->model->r_minor.dispersion, disp_dict);
422    disp_dict = PyDict_GetItemString(self->dispersion, "r_ratio");
423    self->model->r_ratio.dispersion->accept_as_destination(visitor, self->model->r_ratio.dispersion, disp_dict);
424    disp_dict = PyDict_GetItemString(self->dispersion, "length");
425    self->model->length.dispersion->accept_as_destination(visitor, self->model->length.dispersion, disp_dict);
426    disp_dict = PyDict_GetItemString(self->dispersion, "cyl_theta");
427    self->model->cyl_theta.dispersion->accept_as_destination(visitor, self->model->cyl_theta.dispersion, disp_dict);
428    disp_dict = PyDict_GetItemString(self->dispersion, "cyl_phi");
429    self->model->cyl_phi.dispersion->accept_as_destination(visitor, self->model->cyl_phi.dispersion, disp_dict);
430    disp_dict = PyDict_GetItemString(self->dispersion, "cyl_psi");
431    self->model->cyl_psi.dispersion->accept_as_destination(visitor, self->model->cyl_psi.dispersion, disp_dict);
432
433               
434        return Py_BuildValue("d",(*(self->model)).calculate_ER());
435
436}
437/**
438 * Function to call to evaluate model in cartesian coordinates
439 * @param args: input q or [qx, qy]]
440 * @return: function value
441 */
442static PyObject * runXY(CEllipticalCylinderModel *self, PyObject *args) {
443        double qx_value, qy_value;
444        PyObject* pars;
445        int npars;
446       
447        // Get parameters
448       
449            // Reader parameter dictionary
450    self->model->scale = PyFloat_AsDouble( PyDict_GetItemString(self->params, "scale") );
451    self->model->cyl_psi = PyFloat_AsDouble( PyDict_GetItemString(self->params, "cyl_psi") );
452    self->model->length = PyFloat_AsDouble( PyDict_GetItemString(self->params, "length") );
453    self->model->r_minor = PyFloat_AsDouble( PyDict_GetItemString(self->params, "r_minor") );
454    self->model->cyl_theta = PyFloat_AsDouble( PyDict_GetItemString(self->params, "cyl_theta") );
455    self->model->background = PyFloat_AsDouble( PyDict_GetItemString(self->params, "background") );
456    self->model->r_ratio = PyFloat_AsDouble( PyDict_GetItemString(self->params, "r_ratio") );
457    self->model->contrast = PyFloat_AsDouble( PyDict_GetItemString(self->params, "contrast") );
458    self->model->cyl_phi = PyFloat_AsDouble( PyDict_GetItemString(self->params, "cyl_phi") );
459    // Read in dispersion parameters
460    PyObject* disp_dict;
461    DispersionVisitor* visitor = new DispersionVisitor();
462    disp_dict = PyDict_GetItemString(self->dispersion, "r_minor");
463    self->model->r_minor.dispersion->accept_as_destination(visitor, self->model->r_minor.dispersion, disp_dict);
464    disp_dict = PyDict_GetItemString(self->dispersion, "r_ratio");
465    self->model->r_ratio.dispersion->accept_as_destination(visitor, self->model->r_ratio.dispersion, disp_dict);
466    disp_dict = PyDict_GetItemString(self->dispersion, "length");
467    self->model->length.dispersion->accept_as_destination(visitor, self->model->length.dispersion, disp_dict);
468    disp_dict = PyDict_GetItemString(self->dispersion, "cyl_theta");
469    self->model->cyl_theta.dispersion->accept_as_destination(visitor, self->model->cyl_theta.dispersion, disp_dict);
470    disp_dict = PyDict_GetItemString(self->dispersion, "cyl_phi");
471    self->model->cyl_phi.dispersion->accept_as_destination(visitor, self->model->cyl_phi.dispersion, disp_dict);
472    disp_dict = PyDict_GetItemString(self->dispersion, "cyl_psi");
473    self->model->cyl_psi.dispersion->accept_as_destination(visitor, self->model->cyl_psi.dispersion, disp_dict);
474
475       
476        // Get input and determine whether we have to supply a 1D or 2D return value.
477        if ( !PyArg_ParseTuple(args,"O",&pars) ) {
478            PyErr_SetString(CEllipticalCylinderModelError, 
479                "CEllipticalCylinderModel.run expects a q value.");
480                return NULL;
481        }
482         
483        // Check params
484        if( PyList_Check(pars)==1) {
485               
486                // Length of list should be 2 for I(qx, qy))
487            npars = PyList_GET_SIZE(pars); 
488            if(npars!=2) {
489                PyErr_SetString(CEllipticalCylinderModelError, 
490                        "CEllipticalCylinderModel.run expects a double or a list of dimension 2.");
491                return NULL;
492            }
493            // We have a vector q, get the qx and qy values at which
494            // to evaluate I(qx,qy)
495            qx_value = CEllipticalCylinderModel_readDouble(PyList_GET_ITEM(pars,0));
496            qy_value = CEllipticalCylinderModel_readDouble(PyList_GET_ITEM(pars,1));
497            return Py_BuildValue("d",(*(self->model))(qx_value,qy_value));
498
499        } else {
500
501                // We have a scalar q, we will evaluate I(q)
502                qx_value = CEllipticalCylinderModel_readDouble(pars);           
503               
504                return Py_BuildValue("d",(*(self->model))(qx_value));
505        }       
506}
507
508static PyObject * reset(CEllipticalCylinderModel *self, PyObject *args) {
509   
510
511    return Py_BuildValue("d",0.0);
512}
513
514static PyObject * set_dispersion(CEllipticalCylinderModel *self, PyObject *args) {
515        PyObject * disp;
516        const char * par_name;
517
518        if ( !PyArg_ParseTuple(args,"sO", &par_name, &disp) ) {
519            PyErr_SetString(CEllipticalCylinderModelError,
520                "CEllipticalCylinderModel.set_dispersion expects a DispersionModel object.");
521                return NULL;
522        }
523        void *temp = PyCObject_AsVoidPtr(disp);
524        DispersionModel * dispersion = static_cast<DispersionModel *>(temp);
525
526
527        // Ugliness necessary to go from python to C
528            // TODO: refactor this
529    if (!strcmp(par_name, "r_minor")) {
530        self->model->r_minor.dispersion = dispersion;
531    } else    if (!strcmp(par_name, "r_ratio")) {
532        self->model->r_ratio.dispersion = dispersion;
533    } else    if (!strcmp(par_name, "length")) {
534        self->model->length.dispersion = dispersion;
535    } else    if (!strcmp(par_name, "cyl_theta")) {
536        self->model->cyl_theta.dispersion = dispersion;
537    } else    if (!strcmp(par_name, "cyl_phi")) {
538        self->model->cyl_phi.dispersion = dispersion;
539    } else    if (!strcmp(par_name, "cyl_psi")) {
540        self->model->cyl_psi.dispersion = dispersion;
541    } else {
542            PyErr_SetString(CEllipticalCylinderModelError,
543                "CEllipticalCylinderModel.set_dispersion expects a valid parameter name.");
544                return NULL;
545        }
546
547        DispersionVisitor* visitor = new DispersionVisitor();
548        PyObject * disp_dict = PyDict_New();
549        dispersion->accept_as_source(visitor, dispersion, disp_dict);
550        PyDict_SetItemString(self->dispersion, par_name, disp_dict);
551    return Py_BuildValue("i",1);
552}
553
554
555static PyMethodDef CEllipticalCylinderModel_methods[] = {
556    {"run",      (PyCFunction)run     , METH_VARARGS,
557      "Evaluate the model at a given Q or Q, phi"},
558    {"runXY",      (PyCFunction)runXY     , METH_VARARGS,
559      "Evaluate the model at a given Q or Qx, Qy"},
560    {"calculate_ER",      (PyCFunction)calculate_ER     , METH_VARARGS,
561      "Evaluate the model at a given Q or Q, phi"},
562     
563    {"evalDistribution",  (PyCFunction)evalDistribution , METH_VARARGS,
564      "Evaluate the model at a given Q or Qx, Qy vector "},
565    {"reset",    (PyCFunction)reset   , METH_VARARGS,
566      "Reset pair correlation"},
567    {"set_dispersion",      (PyCFunction)set_dispersion     , METH_VARARGS,
568      "Set the dispersion model for a given parameter"},
569   {NULL}
570};
571
572static PyTypeObject CEllipticalCylinderModelType = {
573    PyObject_HEAD_INIT(NULL)
574    0,                         /*ob_size*/
575    "CEllipticalCylinderModel",             /*tp_name*/
576    sizeof(CEllipticalCylinderModel),             /*tp_basicsize*/
577    0,                         /*tp_itemsize*/
578    (destructor)CEllipticalCylinderModel_dealloc, /*tp_dealloc*/
579    0,                         /*tp_print*/
580    0,                         /*tp_getattr*/
581    0,                         /*tp_setattr*/
582    0,                         /*tp_compare*/
583    0,                         /*tp_repr*/
584    0,                         /*tp_as_number*/
585    0,                         /*tp_as_sequence*/
586    0,                         /*tp_as_mapping*/
587    0,                         /*tp_hash */
588    0,                         /*tp_call*/
589    0,                         /*tp_str*/
590    0,                         /*tp_getattro*/
591    0,                         /*tp_setattro*/
592    0,                         /*tp_as_buffer*/
593    Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
594    "CEllipticalCylinderModel objects",           /* tp_doc */
595    0,                         /* tp_traverse */
596    0,                         /* tp_clear */
597    0,                         /* tp_richcompare */
598    0,                         /* tp_weaklistoffset */
599    0,                         /* tp_iter */
600    0,                         /* tp_iternext */
601    CEllipticalCylinderModel_methods,             /* tp_methods */
602    CEllipticalCylinderModel_members,             /* tp_members */
603    0,                         /* tp_getset */
604    0,                         /* tp_base */
605    0,                         /* tp_dict */
606    0,                         /* tp_descr_get */
607    0,                         /* tp_descr_set */
608    0,                         /* tp_dictoffset */
609    (initproc)CEllipticalCylinderModel_init,      /* tp_init */
610    0,                         /* tp_alloc */
611    CEllipticalCylinderModel_new,                 /* tp_new */
612};
613
614
615//static PyMethodDef module_methods[] = {
616//    {NULL}
617//};
618
619/**
620 * Function used to add the model class to a module
621 * @param module: module to add the class to
622 */ 
623void addCEllipticalCylinderModel(PyObject *module) {
624        PyObject *d;
625       
626    if (PyType_Ready(&CEllipticalCylinderModelType) < 0)
627        return;
628
629    Py_INCREF(&CEllipticalCylinderModelType);
630    PyModule_AddObject(module, "CEllipticalCylinderModel", (PyObject *)&CEllipticalCylinderModelType);
631   
632    d = PyModule_GetDict(module);
633    CEllipticalCylinderModelError = PyErr_NewException("CEllipticalCylinderModel.error", NULL, NULL);
634    PyDict_SetItemString(d, "CEllipticalCylinderModelError", CEllipticalCylinderModelError);
635}
636
Note: See TracBrowser for help on using the repository browser.