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

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 44fa116 was 4628e31, checked in by Jae Cho <jhjcho@…>, 14 years ago

changed the unit of angles into degrees

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