source: sasview/sansmodels/src/python_wrapper/CHollowCylinderModel.cpp @ 31af069

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 31af069 was 67424cd, checked in by Mathieu Doucet <doucetm@…>, 13 years ago

Reorganizing models in preparation of cpp cleanup

  • Property mode set to 100644
File size: 25.7 KB
Line 
1/**
2        This software was developed by the University of Tennessee as part of the
3        Distributed Data Analysis of Neutron Scattering Experiments (DANSE)
4        project funded by the US National Science Foundation.
5
6        If you use DANSE applications to do scientific research that leads to
7        publication, we ask that you acknowledge the use of the software with the
8        following sentence:
9
10        "This work benefited from DANSE software developed under NSF award DMR-0520547."
11
12        copyright 2008, University of Tennessee
13 */
14
15/** 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), 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( HollowCylinderModel* 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(CHollowCylinderModelError, 
236                   "CHollowCylinderModel.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(CHollowCylinderModel *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->sldCyl = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sldCyl") );
255    self->model->core_radius = PyFloat_AsDouble( PyDict_GetItemString(self->params, "core_radius") );
256    self->model->axis_theta = PyFloat_AsDouble( PyDict_GetItemString(self->params, "axis_theta") );
257    self->model->length = PyFloat_AsDouble( PyDict_GetItemString(self->params, "length") );
258    self->model->axis_phi = PyFloat_AsDouble( PyDict_GetItemString(self->params, "axis_phi") );
259    self->model->sldSolv = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sldSolv") );
260    self->model->background = PyFloat_AsDouble( PyDict_GetItemString(self->params, "background") );
261    self->model->radius = PyFloat_AsDouble( PyDict_GetItemString(self->params, "radius") );
262    // Read in dispersion parameters
263    PyObject* disp_dict;
264    DispersionVisitor* visitor = new DispersionVisitor();
265    disp_dict = PyDict_GetItemString(self->dispersion, "core_radius");
266    self->model->core_radius.dispersion->accept_as_destination(visitor, self->model->core_radius.dispersion, disp_dict);
267    disp_dict = PyDict_GetItemString(self->dispersion, "radius");
268    self->model->radius.dispersion->accept_as_destination(visitor, self->model->radius.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, "axis_theta");
272    self->model->axis_theta.dispersion->accept_as_destination(visitor, self->model->axis_theta.dispersion, disp_dict);
273    disp_dict = PyDict_GetItemString(self->dispersion, "axis_phi");
274    self->model->axis_phi.dispersion->accept_as_destination(visitor, self->model->axis_phi.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(CHollowCylinderModelError, 
280                "CHollowCylinderModel.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(CHollowCylinderModelError, 
296                   "CHollowCylinderModel.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(CHollowCylinderModelError, 
304                        "CHollowCylinderModel.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(CHollowCylinderModelError, 
314                   "CHollowCylinderModel.evalDistribution expect 2 numpy arrays in list.");
315                return NULL;
316             }
317        }
318        PyErr_SetString(CHollowCylinderModelError, 
319                   "CHollowCylinderModel.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(CHollowCylinderModel *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->scale = PyFloat_AsDouble( PyDict_GetItemString(self->params, "scale") );
338    self->model->sldCyl = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sldCyl") );
339    self->model->core_radius = PyFloat_AsDouble( PyDict_GetItemString(self->params, "core_radius") );
340    self->model->axis_theta = PyFloat_AsDouble( PyDict_GetItemString(self->params, "axis_theta") );
341    self->model->length = PyFloat_AsDouble( PyDict_GetItemString(self->params, "length") );
342    self->model->axis_phi = PyFloat_AsDouble( PyDict_GetItemString(self->params, "axis_phi") );
343    self->model->sldSolv = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sldSolv") );
344    self->model->background = PyFloat_AsDouble( PyDict_GetItemString(self->params, "background") );
345    self->model->radius = PyFloat_AsDouble( PyDict_GetItemString(self->params, "radius") );
346    // Read in dispersion parameters
347    PyObject* disp_dict;
348    DispersionVisitor* visitor = new DispersionVisitor();
349    disp_dict = PyDict_GetItemString(self->dispersion, "core_radius");
350    self->model->core_radius.dispersion->accept_as_destination(visitor, self->model->core_radius.dispersion, disp_dict);
351    disp_dict = PyDict_GetItemString(self->dispersion, "radius");
352    self->model->radius.dispersion->accept_as_destination(visitor, self->model->radius.dispersion, disp_dict);
353    disp_dict = PyDict_GetItemString(self->dispersion, "length");
354    self->model->length.dispersion->accept_as_destination(visitor, self->model->length.dispersion, disp_dict);
355    disp_dict = PyDict_GetItemString(self->dispersion, "axis_theta");
356    self->model->axis_theta.dispersion->accept_as_destination(visitor, self->model->axis_theta.dispersion, disp_dict);
357    disp_dict = PyDict_GetItemString(self->dispersion, "axis_phi");
358    self->model->axis_phi.dispersion->accept_as_destination(visitor, self->model->axis_phi.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(CHollowCylinderModelError, 
364                "CHollowCylinderModel.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(CHollowCylinderModelError, 
375                        "CHollowCylinderModel.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 = CHollowCylinderModel_readDouble(PyList_GET_ITEM(pars,0));
381            phi_value = CHollowCylinderModel_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 = CHollowCylinderModel_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(CHollowCylinderModel *self) {
401
402        // Get parameters
403       
404            // Reader parameter dictionary
405    self->model->scale = PyFloat_AsDouble( PyDict_GetItemString(self->params, "scale") );
406    self->model->sldCyl = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sldCyl") );
407    self->model->core_radius = PyFloat_AsDouble( PyDict_GetItemString(self->params, "core_radius") );
408    self->model->axis_theta = PyFloat_AsDouble( PyDict_GetItemString(self->params, "axis_theta") );
409    self->model->length = PyFloat_AsDouble( PyDict_GetItemString(self->params, "length") );
410    self->model->axis_phi = PyFloat_AsDouble( PyDict_GetItemString(self->params, "axis_phi") );
411    self->model->sldSolv = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sldSolv") );
412    self->model->background = PyFloat_AsDouble( PyDict_GetItemString(self->params, "background") );
413    self->model->radius = PyFloat_AsDouble( PyDict_GetItemString(self->params, "radius") );
414    // Read in dispersion parameters
415    PyObject* disp_dict;
416    DispersionVisitor* visitor = new DispersionVisitor();
417    disp_dict = PyDict_GetItemString(self->dispersion, "core_radius");
418    self->model->core_radius.dispersion->accept_as_destination(visitor, self->model->core_radius.dispersion, disp_dict);
419    disp_dict = PyDict_GetItemString(self->dispersion, "radius");
420    self->model->radius.dispersion->accept_as_destination(visitor, self->model->radius.dispersion, disp_dict);
421    disp_dict = PyDict_GetItemString(self->dispersion, "length");
422    self->model->length.dispersion->accept_as_destination(visitor, self->model->length.dispersion, disp_dict);
423    disp_dict = PyDict_GetItemString(self->dispersion, "axis_theta");
424    self->model->axis_theta.dispersion->accept_as_destination(visitor, self->model->axis_theta.dispersion, disp_dict);
425    disp_dict = PyDict_GetItemString(self->dispersion, "axis_phi");
426    self->model->axis_phi.dispersion->accept_as_destination(visitor, self->model->axis_phi.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(CHollowCylinderModel *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->scale = PyFloat_AsDouble( PyDict_GetItemString(self->params, "scale") );
446    self->model->sldCyl = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sldCyl") );
447    self->model->core_radius = PyFloat_AsDouble( PyDict_GetItemString(self->params, "core_radius") );
448    self->model->axis_theta = PyFloat_AsDouble( PyDict_GetItemString(self->params, "axis_theta") );
449    self->model->length = PyFloat_AsDouble( PyDict_GetItemString(self->params, "length") );
450    self->model->axis_phi = PyFloat_AsDouble( PyDict_GetItemString(self->params, "axis_phi") );
451    self->model->sldSolv = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sldSolv") );
452    self->model->background = PyFloat_AsDouble( PyDict_GetItemString(self->params, "background") );
453    self->model->radius = PyFloat_AsDouble( PyDict_GetItemString(self->params, "radius") );
454    // Read in dispersion parameters
455    PyObject* disp_dict;
456    DispersionVisitor* visitor = new DispersionVisitor();
457    disp_dict = PyDict_GetItemString(self->dispersion, "core_radius");
458    self->model->core_radius.dispersion->accept_as_destination(visitor, self->model->core_radius.dispersion, disp_dict);
459    disp_dict = PyDict_GetItemString(self->dispersion, "radius");
460    self->model->radius.dispersion->accept_as_destination(visitor, self->model->radius.dispersion, disp_dict);
461    disp_dict = PyDict_GetItemString(self->dispersion, "length");
462    self->model->length.dispersion->accept_as_destination(visitor, self->model->length.dispersion, disp_dict);
463    disp_dict = PyDict_GetItemString(self->dispersion, "axis_theta");
464    self->model->axis_theta.dispersion->accept_as_destination(visitor, self->model->axis_theta.dispersion, disp_dict);
465    disp_dict = PyDict_GetItemString(self->dispersion, "axis_phi");
466    self->model->axis_phi.dispersion->accept_as_destination(visitor, self->model->axis_phi.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(CHollowCylinderModelError, 
472                "CHollowCylinderModel.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(CHollowCylinderModelError, 
483                        "CHollowCylinderModel.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 = CHollowCylinderModel_readDouble(PyList_GET_ITEM(pars,0));
489            qy_value = CHollowCylinderModel_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 = CHollowCylinderModel_readDouble(pars);               
496               
497                return Py_BuildValue("d",(*(self->model))(qx_value));
498        }       
499}
500
501static PyObject * reset(CHollowCylinderModel *self, PyObject *args) {
502   
503
504    return Py_BuildValue("d",0.0);
505}
506
507static PyObject * set_dispersion(CHollowCylinderModel *self, PyObject *args) {
508        PyObject * disp;
509        const char * par_name;
510
511        if ( !PyArg_ParseTuple(args,"sO", &par_name, &disp) ) {
512            PyErr_SetString(CHollowCylinderModelError,
513                "CHollowCylinderModel.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, "core_radius")) {
523        self->model->core_radius.dispersion = dispersion;
524    } else    if (!strcmp(par_name, "radius")) {
525        self->model->radius.dispersion = dispersion;
526    } else    if (!strcmp(par_name, "length")) {
527        self->model->length.dispersion = dispersion;
528    } else    if (!strcmp(par_name, "axis_theta")) {
529        self->model->axis_theta.dispersion = dispersion;
530    } else    if (!strcmp(par_name, "axis_phi")) {
531        self->model->axis_phi.dispersion = dispersion;
532    } else {
533            PyErr_SetString(CHollowCylinderModelError,
534                "CHollowCylinderModel.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 CHollowCylinderModel_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 CHollowCylinderModelType = {
564    PyObject_HEAD_INIT(NULL)
565    0,                         /*ob_size*/
566    "CHollowCylinderModel",             /*tp_name*/
567    sizeof(CHollowCylinderModel),             /*tp_basicsize*/
568    0,                         /*tp_itemsize*/
569    (destructor)CHollowCylinderModel_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    "CHollowCylinderModel 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    CHollowCylinderModel_methods,             /* tp_methods */
593    CHollowCylinderModel_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)CHollowCylinderModel_init,      /* tp_init */
601    0,                         /* tp_alloc */
602    CHollowCylinderModel_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 addCHollowCylinderModel(PyObject *module) {
615        PyObject *d;
616       
617    if (PyType_Ready(&CHollowCylinderModelType) < 0)
618        return;
619
620    Py_INCREF(&CHollowCylinderModelType);
621    PyModule_AddObject(module, "CHollowCylinderModel", (PyObject *)&CHollowCylinderModelType);
622   
623    d = PyModule_GetDict(module);
624    static char error_name[] = "CHollowCylinderModel.error";
625    CHollowCylinderModelError = PyErr_NewException(error_name, NULL, NULL);
626    PyDict_SetItemString(d, "CHollowCylinderModelError", CHollowCylinderModelError);
627}
628
Note: See TracBrowser for help on using the repository browser.