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

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 484faf7 was 71e2de7, checked in by Gervaise Alina <gervyh@…>, 15 years ago

change destructor for models

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