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

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

change destructor for models

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