source: sasview/sansmodels/src/python_wrapper/CBarBellModel.cpp @ a1d1b90

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 a1d1b90 was 43ccd258, checked in by Mathieu Doucet <doucetm@…>, 13 years ago

cosmetics on BarBell?

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