source: sasview/sansmodels/src/sans/models/c_models/CBarBellModel.cpp @ b1c3295

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 b1c3295 was b1c3295, checked in by Mathieu Doucet <doucetm@…>, 12 years ago

Re #4 This should clean up a whole bunch of C++ warnings.

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