source: sasview/sansmodels/src/sans/models/c_models/CHollowCylinderModel.cpp @ feadd6f

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 feadd6f was 2605da22, checked in by Mathieu Doucet <doucetm@…>, 13 years ago

Re #4 Still a few more warnings

  • Property mode set to 100644
File size: 25.6 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/** CHollowCylinderModel
16 *
17 * C extension
18 *
19 * WARNING: THIS FILE WAS GENERATED BY WRAPPERGENERATOR.PY
20 *          DO NOT MODIFY THIS FILE, MODIFY hollow_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 "hollow_cylinder.h"
36}
37
38#include "models.hh"
39#include "dispersion_visitor.hh"
40
41/// Error object for raised exceptions
42static PyObject * CHollowCylinderModelError = 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    HollowCylinderModel * model;
54    /// Log for unit testing
55    PyObject * log;
56} CHollowCylinderModel;
57
58
59static void
60CHollowCylinderModel_dealloc(CHollowCylinderModel* 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 *
72CHollowCylinderModel_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
73{
74    CHollowCylinderModel *self;
75   
76    self = (CHollowCylinderModel *)type->tp_alloc(type, 0);
77   
78    return (PyObject *)self;
79}
80
81static int
82CHollowCylinderModel_init(CHollowCylinderModel *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 HollowCylinderModel();
90       
91        // Initialize parameter dictionary
92        PyDict_SetItemString(self->params,"scale",Py_BuildValue("d",1.000000000000));
93        PyDict_SetItemString(self->params,"sldCyl",Py_BuildValue("d",0.000006300000));
94        PyDict_SetItemString(self->params,"core_radius",Py_BuildValue("d",20.000000000000));
95        PyDict_SetItemString(self->params,"axis_theta",Py_BuildValue("d",90.000000000000));
96        PyDict_SetItemString(self->params,"length",Py_BuildValue("d",400.000000000000));
97        PyDict_SetItemString(self->params,"axis_phi",Py_BuildValue("d",0.000000000000));
98        PyDict_SetItemString(self->params,"sldSolv",Py_BuildValue("d",0.000001000000));
99        PyDict_SetItemString(self->params,"background",Py_BuildValue("d",0.010000000000));
100        PyDict_SetItemString(self->params,"radius",Py_BuildValue("d",30.000000000000));
101        // Initialize dispersion / averaging parameter dict
102        DispersionVisitor* visitor = new DispersionVisitor();
103        PyObject * disp_dict;
104        disp_dict = PyDict_New();
105        self->model->core_radius.dispersion->accept_as_source(visitor, self->model->core_radius.dispersion, disp_dict);
106        PyDict_SetItemString(self->dispersion, "core_radius", disp_dict);
107        disp_dict = PyDict_New();
108        self->model->radius.dispersion->accept_as_source(visitor, self->model->radius.dispersion, disp_dict);
109        PyDict_SetItemString(self->dispersion, "radius", 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->axis_theta.dispersion->accept_as_source(visitor, self->model->axis_theta.dispersion, disp_dict);
115        PyDict_SetItemString(self->dispersion, "axis_theta", disp_dict);
116        disp_dict = PyDict_New();
117        self->model->axis_phi.dispersion->accept_as_source(visitor, self->model->axis_phi.dispersion, disp_dict);
118        PyDict_SetItemString(self->dispersion, "axis_phi", 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 CHollowCylinderModel_members[] = {
139    {name_params, T_OBJECT, offsetof(CHollowCylinderModel, params), 0, def_params},
140        {name_dispersion, T_OBJECT, offsetof(CHollowCylinderModel, dispersion), 0, def_dispersion},     
141    {name_log, T_OBJECT, offsetof(CHollowCylinderModel, log), 0, def_log},
142    {NULL}  /* Sentinel */
143};
144
145/** Read double from PyObject
146    @param p PyObject
147    @return double
148*/
149double CHollowCylinderModel_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(HollowCylinderModel* 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( HollowCylinderModel* model, 
197                              PyArrayObject *x, PyArrayObject *y)
198 {
199    PyArrayObject *result;
200    int i, 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(CHollowCylinderModelError, 
235                   "CHollowCylinderModel.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(CHollowCylinderModel *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->scale = PyFloat_AsDouble( PyDict_GetItemString(self->params, "scale") );
253    self->model->sldCyl = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sldCyl") );
254    self->model->core_radius = PyFloat_AsDouble( PyDict_GetItemString(self->params, "core_radius") );
255    self->model->axis_theta = PyFloat_AsDouble( PyDict_GetItemString(self->params, "axis_theta") );
256    self->model->length = PyFloat_AsDouble( PyDict_GetItemString(self->params, "length") );
257    self->model->axis_phi = PyFloat_AsDouble( PyDict_GetItemString(self->params, "axis_phi") );
258    self->model->sldSolv = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sldSolv") );
259    self->model->background = PyFloat_AsDouble( PyDict_GetItemString(self->params, "background") );
260    self->model->radius = PyFloat_AsDouble( PyDict_GetItemString(self->params, "radius") );
261    // Read in dispersion parameters
262    PyObject* disp_dict;
263    DispersionVisitor* visitor = new DispersionVisitor();
264    disp_dict = PyDict_GetItemString(self->dispersion, "core_radius");
265    self->model->core_radius.dispersion->accept_as_destination(visitor, self->model->core_radius.dispersion, disp_dict);
266    disp_dict = PyDict_GetItemString(self->dispersion, "radius");
267    self->model->radius.dispersion->accept_as_destination(visitor, self->model->radius.dispersion, disp_dict);
268    disp_dict = PyDict_GetItemString(self->dispersion, "length");
269    self->model->length.dispersion->accept_as_destination(visitor, self->model->length.dispersion, disp_dict);
270    disp_dict = PyDict_GetItemString(self->dispersion, "axis_theta");
271    self->model->axis_theta.dispersion->accept_as_destination(visitor, self->model->axis_theta.dispersion, disp_dict);
272    disp_dict = PyDict_GetItemString(self->dispersion, "axis_phi");
273    self->model->axis_phi.dispersion->accept_as_destination(visitor, self->model->axis_phi.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(CHollowCylinderModelError, 
279                "CHollowCylinderModel.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(CHollowCylinderModelError, 
295                   "CHollowCylinderModel.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(CHollowCylinderModelError, 
303                        "CHollowCylinderModel.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(CHollowCylinderModelError, 
313                   "CHollowCylinderModel.evalDistribution expect 2 numpy arrays in list.");
314                return NULL;
315             }
316        }
317        PyErr_SetString(CHollowCylinderModelError, 
318                   "CHollowCylinderModel.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(CHollowCylinderModel *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->scale = PyFloat_AsDouble( PyDict_GetItemString(self->params, "scale") );
337    self->model->sldCyl = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sldCyl") );
338    self->model->core_radius = PyFloat_AsDouble( PyDict_GetItemString(self->params, "core_radius") );
339    self->model->axis_theta = PyFloat_AsDouble( PyDict_GetItemString(self->params, "axis_theta") );
340    self->model->length = PyFloat_AsDouble( PyDict_GetItemString(self->params, "length") );
341    self->model->axis_phi = PyFloat_AsDouble( PyDict_GetItemString(self->params, "axis_phi") );
342    self->model->sldSolv = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sldSolv") );
343    self->model->background = PyFloat_AsDouble( PyDict_GetItemString(self->params, "background") );
344    self->model->radius = PyFloat_AsDouble( PyDict_GetItemString(self->params, "radius") );
345    // Read in dispersion parameters
346    PyObject* disp_dict;
347    DispersionVisitor* visitor = new DispersionVisitor();
348    disp_dict = PyDict_GetItemString(self->dispersion, "core_radius");
349    self->model->core_radius.dispersion->accept_as_destination(visitor, self->model->core_radius.dispersion, disp_dict);
350    disp_dict = PyDict_GetItemString(self->dispersion, "radius");
351    self->model->radius.dispersion->accept_as_destination(visitor, self->model->radius.dispersion, disp_dict);
352    disp_dict = PyDict_GetItemString(self->dispersion, "length");
353    self->model->length.dispersion->accept_as_destination(visitor, self->model->length.dispersion, disp_dict);
354    disp_dict = PyDict_GetItemString(self->dispersion, "axis_theta");
355    self->model->axis_theta.dispersion->accept_as_destination(visitor, self->model->axis_theta.dispersion, disp_dict);
356    disp_dict = PyDict_GetItemString(self->dispersion, "axis_phi");
357    self->model->axis_phi.dispersion->accept_as_destination(visitor, self->model->axis_phi.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(CHollowCylinderModelError, 
363                "CHollowCylinderModel.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(CHollowCylinderModelError, 
374                        "CHollowCylinderModel.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 = CHollowCylinderModel_readDouble(PyList_GET_ITEM(pars,0));
380            phi_value = CHollowCylinderModel_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 = CHollowCylinderModel_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(CHollowCylinderModel *self) {
400
401        // Get parameters
402       
403            // Reader parameter dictionary
404    self->model->scale = PyFloat_AsDouble( PyDict_GetItemString(self->params, "scale") );
405    self->model->sldCyl = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sldCyl") );
406    self->model->core_radius = PyFloat_AsDouble( PyDict_GetItemString(self->params, "core_radius") );
407    self->model->axis_theta = PyFloat_AsDouble( PyDict_GetItemString(self->params, "axis_theta") );
408    self->model->length = PyFloat_AsDouble( PyDict_GetItemString(self->params, "length") );
409    self->model->axis_phi = PyFloat_AsDouble( PyDict_GetItemString(self->params, "axis_phi") );
410    self->model->sldSolv = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sldSolv") );
411    self->model->background = PyFloat_AsDouble( PyDict_GetItemString(self->params, "background") );
412    self->model->radius = PyFloat_AsDouble( PyDict_GetItemString(self->params, "radius") );
413    // Read in dispersion parameters
414    PyObject* disp_dict;
415    DispersionVisitor* visitor = new DispersionVisitor();
416    disp_dict = PyDict_GetItemString(self->dispersion, "core_radius");
417    self->model->core_radius.dispersion->accept_as_destination(visitor, self->model->core_radius.dispersion, disp_dict);
418    disp_dict = PyDict_GetItemString(self->dispersion, "radius");
419    self->model->radius.dispersion->accept_as_destination(visitor, self->model->radius.dispersion, disp_dict);
420    disp_dict = PyDict_GetItemString(self->dispersion, "length");
421    self->model->length.dispersion->accept_as_destination(visitor, self->model->length.dispersion, disp_dict);
422    disp_dict = PyDict_GetItemString(self->dispersion, "axis_theta");
423    self->model->axis_theta.dispersion->accept_as_destination(visitor, self->model->axis_theta.dispersion, disp_dict);
424    disp_dict = PyDict_GetItemString(self->dispersion, "axis_phi");
425    self->model->axis_phi.dispersion->accept_as_destination(visitor, self->model->axis_phi.dispersion, disp_dict);
426
427               
428        return Py_BuildValue("d",(*(self->model)).calculate_ER());
429
430}
431/**
432 * Function to call to evaluate model in cartesian coordinates
433 * @param args: input q or [qx, qy]]
434 * @return: function value
435 */
436static PyObject * runXY(CHollowCylinderModel *self, PyObject *args) {
437        double qx_value, qy_value;
438        PyObject* pars;
439        int npars;
440       
441        // Get parameters
442       
443            // Reader parameter dictionary
444    self->model->scale = PyFloat_AsDouble( PyDict_GetItemString(self->params, "scale") );
445    self->model->sldCyl = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sldCyl") );
446    self->model->core_radius = PyFloat_AsDouble( PyDict_GetItemString(self->params, "core_radius") );
447    self->model->axis_theta = PyFloat_AsDouble( PyDict_GetItemString(self->params, "axis_theta") );
448    self->model->length = PyFloat_AsDouble( PyDict_GetItemString(self->params, "length") );
449    self->model->axis_phi = PyFloat_AsDouble( PyDict_GetItemString(self->params, "axis_phi") );
450    self->model->sldSolv = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sldSolv") );
451    self->model->background = PyFloat_AsDouble( PyDict_GetItemString(self->params, "background") );
452    self->model->radius = PyFloat_AsDouble( PyDict_GetItemString(self->params, "radius") );
453    // Read in dispersion parameters
454    PyObject* disp_dict;
455    DispersionVisitor* visitor = new DispersionVisitor();
456    disp_dict = PyDict_GetItemString(self->dispersion, "core_radius");
457    self->model->core_radius.dispersion->accept_as_destination(visitor, self->model->core_radius.dispersion, disp_dict);
458    disp_dict = PyDict_GetItemString(self->dispersion, "radius");
459    self->model->radius.dispersion->accept_as_destination(visitor, self->model->radius.dispersion, disp_dict);
460    disp_dict = PyDict_GetItemString(self->dispersion, "length");
461    self->model->length.dispersion->accept_as_destination(visitor, self->model->length.dispersion, disp_dict);
462    disp_dict = PyDict_GetItemString(self->dispersion, "axis_theta");
463    self->model->axis_theta.dispersion->accept_as_destination(visitor, self->model->axis_theta.dispersion, disp_dict);
464    disp_dict = PyDict_GetItemString(self->dispersion, "axis_phi");
465    self->model->axis_phi.dispersion->accept_as_destination(visitor, self->model->axis_phi.dispersion, disp_dict);
466
467       
468        // Get input and determine whether we have to supply a 1D or 2D return value.
469        if ( !PyArg_ParseTuple(args,"O",&pars) ) {
470            PyErr_SetString(CHollowCylinderModelError, 
471                "CHollowCylinderModel.run expects a q value.");
472                return NULL;
473        }
474         
475        // Check params
476        if( PyList_Check(pars)==1) {
477               
478                // Length of list should be 2 for I(qx, qy))
479            npars = PyList_GET_SIZE(pars); 
480            if(npars!=2) {
481                PyErr_SetString(CHollowCylinderModelError, 
482                        "CHollowCylinderModel.run expects a double or a list of dimension 2.");
483                return NULL;
484            }
485            // We have a vector q, get the qx and qy values at which
486            // to evaluate I(qx,qy)
487            qx_value = CHollowCylinderModel_readDouble(PyList_GET_ITEM(pars,0));
488            qy_value = CHollowCylinderModel_readDouble(PyList_GET_ITEM(pars,1));
489            return Py_BuildValue("d",(*(self->model))(qx_value,qy_value));
490
491        } else {
492
493                // We have a scalar q, we will evaluate I(q)
494                qx_value = CHollowCylinderModel_readDouble(pars);               
495               
496                return Py_BuildValue("d",(*(self->model))(qx_value));
497        }       
498}
499
500static PyObject * reset(CHollowCylinderModel *self, PyObject *args) {
501   
502
503    return Py_BuildValue("d",0.0);
504}
505
506static PyObject * set_dispersion(CHollowCylinderModel *self, PyObject *args) {
507        PyObject * disp;
508        const char * par_name;
509
510        if ( !PyArg_ParseTuple(args,"sO", &par_name, &disp) ) {
511            PyErr_SetString(CHollowCylinderModelError,
512                "CHollowCylinderModel.set_dispersion expects a DispersionModel object.");
513                return NULL;
514        }
515        void *temp = PyCObject_AsVoidPtr(disp);
516        DispersionModel * dispersion = static_cast<DispersionModel *>(temp);
517
518
519        // Ugliness necessary to go from python to C
520            // TODO: refactor this
521    if (!strcmp(par_name, "core_radius")) {
522        self->model->core_radius.dispersion = dispersion;
523    } else    if (!strcmp(par_name, "radius")) {
524        self->model->radius.dispersion = dispersion;
525    } else    if (!strcmp(par_name, "length")) {
526        self->model->length.dispersion = dispersion;
527    } else    if (!strcmp(par_name, "axis_theta")) {
528        self->model->axis_theta.dispersion = dispersion;
529    } else    if (!strcmp(par_name, "axis_phi")) {
530        self->model->axis_phi.dispersion = dispersion;
531    } else {
532            PyErr_SetString(CHollowCylinderModelError,
533                "CHollowCylinderModel.set_dispersion expects a valid parameter name.");
534                return NULL;
535        }
536
537        DispersionVisitor* visitor = new DispersionVisitor();
538        PyObject * disp_dict = PyDict_New();
539        dispersion->accept_as_source(visitor, dispersion, disp_dict);
540        PyDict_SetItemString(self->dispersion, par_name, disp_dict);
541    return Py_BuildValue("i",1);
542}
543
544
545static PyMethodDef CHollowCylinderModel_methods[] = {
546    {"run",      (PyCFunction)run     , METH_VARARGS,
547      "Evaluate the model at a given Q or Q, phi"},
548    {"runXY",      (PyCFunction)runXY     , METH_VARARGS,
549      "Evaluate the model at a given Q or Qx, Qy"},
550    {"calculate_ER",      (PyCFunction)calculate_ER     , METH_VARARGS,
551      "Evaluate the model at a given Q or Q, phi"},
552     
553    {"evalDistribution",  (PyCFunction)evalDistribution , METH_VARARGS,
554      "Evaluate the model at a given Q or Qx, Qy vector "},
555    {"reset",    (PyCFunction)reset   , METH_VARARGS,
556      "Reset pair correlation"},
557    {"set_dispersion",      (PyCFunction)set_dispersion     , METH_VARARGS,
558      "Set the dispersion model for a given parameter"},
559   {NULL}
560};
561
562static PyTypeObject CHollowCylinderModelType = {
563    PyObject_HEAD_INIT(NULL)
564    0,                         /*ob_size*/
565    "CHollowCylinderModel",             /*tp_name*/
566    sizeof(CHollowCylinderModel),             /*tp_basicsize*/
567    0,                         /*tp_itemsize*/
568    (destructor)CHollowCylinderModel_dealloc, /*tp_dealloc*/
569    0,                         /*tp_print*/
570    0,                         /*tp_getattr*/
571    0,                         /*tp_setattr*/
572    0,                         /*tp_compare*/
573    0,                         /*tp_repr*/
574    0,                         /*tp_as_number*/
575    0,                         /*tp_as_sequence*/
576    0,                         /*tp_as_mapping*/
577    0,                         /*tp_hash */
578    0,                         /*tp_call*/
579    0,                         /*tp_str*/
580    0,                         /*tp_getattro*/
581    0,                         /*tp_setattro*/
582    0,                         /*tp_as_buffer*/
583    Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
584    "CHollowCylinderModel objects",           /* tp_doc */
585    0,                         /* tp_traverse */
586    0,                         /* tp_clear */
587    0,                         /* tp_richcompare */
588    0,                         /* tp_weaklistoffset */
589    0,                         /* tp_iter */
590    0,                         /* tp_iternext */
591    CHollowCylinderModel_methods,             /* tp_methods */
592    CHollowCylinderModel_members,             /* tp_members */
593    0,                         /* tp_getset */
594    0,                         /* tp_base */
595    0,                         /* tp_dict */
596    0,                         /* tp_descr_get */
597    0,                         /* tp_descr_set */
598    0,                         /* tp_dictoffset */
599    (initproc)CHollowCylinderModel_init,      /* tp_init */
600    0,                         /* tp_alloc */
601    CHollowCylinderModel_new,                 /* tp_new */
602};
603
604
605//static PyMethodDef module_methods[] = {
606//    {NULL}
607//};
608
609/**
610 * Function used to add the model class to a module
611 * @param module: module to add the class to
612 */ 
613void addCHollowCylinderModel(PyObject *module) {
614        PyObject *d;
615       
616    if (PyType_Ready(&CHollowCylinderModelType) < 0)
617        return;
618
619    Py_INCREF(&CHollowCylinderModelType);
620    PyModule_AddObject(module, "CHollowCylinderModel", (PyObject *)&CHollowCylinderModelType);
621   
622    d = PyModule_GetDict(module);
623    static char error_name[] = "CHollowCylinderModel.error";
624    CHollowCylinderModelError = PyErr_NewException(error_name, NULL, NULL);
625    PyDict_SetItemString(d, "CHollowCylinderModelError", CHollowCylinderModelError);
626}
627
Note: See TracBrowser for help on using the repository browser.