source: sasview/sansmodels/src/sans/models/c_models/CCoreShellCylinderModel.cpp @ 1d67243

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 1d67243 was 9bd69098, checked in by Jae Cho <jhjcho@…>, 15 years ago

recompiled all due to Alina's new eval(run) function

  • Property mode set to 100644
File size: 25.1 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/** CCoreShellCylinderModel
16 *
17 * C extension
18 *
19 * WARNING: THIS FILE WAS GENERATED BY WRAPPERGENERATOR.PY
20 *          DO NOT MODIFY THIS FILE, MODIFY core_shell_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 "core_shell_cylinder.h"
36}
37
38#include "models.hh"
39#include "dispersion_visitor.hh"
40
41/// Error object for raised exceptions
42static PyObject * CCoreShellCylinderModelError = 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    CoreShellCylinderModel * model;
54    /// Log for unit testing
55    PyObject * log;
56} CCoreShellCylinderModel;
57
58
59static void
60CCoreShellCylinderModel_dealloc(CCoreShellCylinderModel* self)
61{
62    self->ob_type->tp_free((PyObject*)self);
63   
64
65}
66
67static PyObject *
68CCoreShellCylinderModel_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
69{
70    CCoreShellCylinderModel *self;
71   
72    self = (CCoreShellCylinderModel *)type->tp_alloc(type, 0);
73   
74    return (PyObject *)self;
75}
76
77static int
78CCoreShellCylinderModel_init(CCoreShellCylinderModel *self, PyObject *args, PyObject *kwds)
79{
80    if (self != NULL) {
81       
82        // Create parameters
83        self->params = PyDict_New();
84        self->dispersion = PyDict_New();
85        self->model = new CoreShellCylinderModel();
86       
87        // Initialize parameter dictionary
88        PyDict_SetItemString(self->params,"scale",Py_BuildValue("d",1.000000));
89        PyDict_SetItemString(self->params,"axis_theta",Py_BuildValue("d",1.570000));
90        PyDict_SetItemString(self->params,"solvent_sld",Py_BuildValue("d",0.000001));
91        PyDict_SetItemString(self->params,"thickness",Py_BuildValue("d",10.000000));
92        PyDict_SetItemString(self->params,"axis_phi",Py_BuildValue("d",0.000000));
93        PyDict_SetItemString(self->params,"length",Py_BuildValue("d",400.000000));
94        PyDict_SetItemString(self->params,"core_sld",Py_BuildValue("d",0.000001));
95        PyDict_SetItemString(self->params,"radius",Py_BuildValue("d",20.000000));
96        PyDict_SetItemString(self->params,"background",Py_BuildValue("d",0.000000));
97        PyDict_SetItemString(self->params,"shell_sld",Py_BuildValue("d",0.000004));
98        // Initialize dispersion / averaging parameter dict
99        DispersionVisitor* visitor = new DispersionVisitor();
100        PyObject * disp_dict;
101        disp_dict = PyDict_New();
102        self->model->radius.dispersion->accept_as_source(visitor, self->model->radius.dispersion, disp_dict);
103        PyDict_SetItemString(self->dispersion, "radius", disp_dict);
104        disp_dict = PyDict_New();
105        self->model->thickness.dispersion->accept_as_source(visitor, self->model->thickness.dispersion, disp_dict);
106        PyDict_SetItemString(self->dispersion, "thickness", disp_dict);
107        disp_dict = PyDict_New();
108        self->model->length.dispersion->accept_as_source(visitor, self->model->length.dispersion, disp_dict);
109        PyDict_SetItemString(self->dispersion, "length", disp_dict);
110        disp_dict = PyDict_New();
111        self->model->axis_theta.dispersion->accept_as_source(visitor, self->model->axis_theta.dispersion, disp_dict);
112        PyDict_SetItemString(self->dispersion, "axis_theta", disp_dict);
113        disp_dict = PyDict_New();
114        self->model->axis_phi.dispersion->accept_as_source(visitor, self->model->axis_phi.dispersion, disp_dict);
115        PyDict_SetItemString(self->dispersion, "axis_phi", disp_dict);
116
117
118         
119        // Create empty log
120        self->log = PyDict_New();
121       
122       
123
124    }
125    return 0;
126}
127
128static PyMemberDef CCoreShellCylinderModel_members[] = {
129    {"params", T_OBJECT, offsetof(CCoreShellCylinderModel, params), 0,
130     "Parameters"},
131        {"dispersion", T_OBJECT, offsetof(CCoreShellCylinderModel, dispersion), 0,
132          "Dispersion parameters"},     
133    {"log", T_OBJECT, offsetof(CCoreShellCylinderModel, log), 0,
134     "Log"},
135    {NULL}  /* Sentinel */
136};
137
138/** Read double from PyObject
139    @param p PyObject
140    @return double
141*/
142double CCoreShellCylinderModel_readDouble(PyObject *p) {
143    if (PyFloat_Check(p)==1) {
144        return (double)(((PyFloatObject *)(p))->ob_fval);
145    } else if (PyInt_Check(p)==1) {
146        return (double)(((PyIntObject *)(p))->ob_ival);
147    } else if (PyLong_Check(p)==1) {
148        return (double)PyLong_AsLong(p);
149    } else {
150        return 0.0;
151    }
152}
153/**
154 * Function to call to evaluate model
155 * @param args: input numpy array q[]
156 * @return: numpy array object
157 */
158 
159static PyObject *evaluateOneDim(CoreShellCylinderModel* model, PyArrayObject *q){
160    PyArrayObject *result;
161   
162    // Check validity of array q , q must be of dimension 1, an array of double
163    if (q->nd != 1 || q->descr->type_num != PyArray_DOUBLE)
164    {
165        //const char * message= "Invalid array: q->nd=%d,type_num=%d\n",q->nd,q->descr->type_num;
166        //PyErr_SetString(PyExc_ValueError , message);
167        return NULL;
168    }
169    result = (PyArrayObject *)PyArray_FromDims(q->nd, (int *)(q->dimensions), 
170                                                                                  PyArray_DOUBLE);
171        if (result == NULL) {
172        const char * message= "Could not create result ";
173        PyErr_SetString(PyExc_RuntimeError , message);
174                return NULL;
175        }
176         for (int i = 0; i < q->dimensions[0]; i++){
177      double q_value  = *(double *)(q->data + i*q->strides[0]);
178      double *result_value = (double *)(result->data + i*result->strides[0]);
179      *result_value =(*model)(q_value);
180        }
181    return PyArray_Return(result); 
182 }
183/**
184 * Function to call to evaluate model
185 * @param args: input numpy array  [q[],phi[]]
186 * @return: numpy array object
187 */
188static PyObject * evaluateTwoDim( CoreShellCylinderModel* model, 
189                              PyArrayObject *q, PyArrayObject *phi)
190 {
191    PyArrayObject *result;
192    //check validity of input vectors
193    if (q->nd != 1 || q->descr->type_num != PyArray_DOUBLE
194        || phi->nd != 1 || phi->descr->type_num != PyArray_DOUBLE
195        || phi->dimensions[0] != q->dimensions[0]){
196     
197        //const char * message= "Invalid array: q->nd=%d,type_num=%d\n",q->nd,q->descr->type_num;
198        PyErr_SetString(PyExc_ValueError ,"wrong input"); 
199        return NULL;
200    }
201        result= (PyArrayObject *)PyArray_FromDims(q->nd,(int*)(q->dimensions), PyArray_DOUBLE);
202
203        if (result == NULL){
204            const char * message= "Could not create result ";
205        PyErr_SetString(PyExc_RuntimeError , message);
206            return NULL;
207        }
208       
209    for (int i = 0; i < q->dimensions[0]; i++) {
210      double q_value = *(double *)(q->data + i*q->strides[0]);
211      double phi_value = *(double *)(phi->data + i*phi->strides[0]);
212      double *result_value = (double *)(result->data + i*result->strides[0]);
213      if (q_value == 0)
214          *result_value = 0.0;
215      else
216          *result_value = model->evaluate_rphi(q_value, phi_value);
217    }
218    return PyArray_Return(result); 
219 }
220 /**
221 * Function to call to evaluate model
222 * @param args: input numpy array  [x[],y[]]
223 * @return: numpy array object
224 */
225 static PyObject * evaluateTwoDimXY( CoreShellCylinderModel* model, 
226                              PyArrayObject *x, PyArrayObject *y)
227 {
228    PyArrayObject *result;
229    int i,j, x_len, y_len, dims[2];
230    //check validity of input vectors
231    if (x->nd != 2 || x->descr->type_num != PyArray_DOUBLE
232        || y->nd != 2 || y->descr->type_num != PyArray_DOUBLE
233        || y->dimensions[1] != x->dimensions[0]){
234        const char * message= "evaluateTwoDimXY  expect 2 numpy arrays";
235        PyErr_SetString(PyExc_ValueError , message); 
236        return NULL;
237    }
238   
239        if (PyArray_Check(x) && PyArray_Check(y)) {
240            x_len = dims[0]= x->dimensions[0];
241        y_len = dims[1]= y->dimensions[1];
242           
243            // Make a new double matrix of same dims
244        result=(PyArrayObject *) PyArray_FromDims(2,dims,NPY_DOUBLE);
245        if (result == NULL){
246            const char * message= "Could not create result ";
247        PyErr_SetString(PyExc_RuntimeError , message);
248            return NULL;
249            }
250       
251        /* Do the calculation. */
252        for ( i=0; i< x_len; i++) {
253            for ( j=0; j< y_len; j++) {
254                double x_value = *(double *)(x->data + i*x->strides[0]);
255                    double y_value = *(double *)(y->data + j*y->strides[1]);
256                        double *result_value = (double *)(result->data +
257                              i*result->strides[0] + j*result->strides[1]);
258                        *result_value = (*model)(x_value, y_value);
259            }           
260        }
261        return PyArray_Return(result); 
262       
263        }else{
264                    PyErr_SetString(CCoreShellCylinderModelError, 
265                   "CCoreShellCylinderModel.evaluateTwoDimXY couldn't run.");
266                return NULL;
267                }       
268}
269/**
270 *  evalDistribution function evaluate a model function with input vector
271 *  @param args: input q as vector or [qx, qy] where qx, qy are vectors
272 *
273 */ 
274static PyObject * evalDistribution(CCoreShellCylinderModel *self, PyObject *args){
275        PyObject *qx, *qy;
276        PyArrayObject * pars;
277        int npars ,mpars;
278       
279        // Get parameters
280       
281            // Reader parameter dictionary
282    self->model->scale = PyFloat_AsDouble( PyDict_GetItemString(self->params, "scale") );
283    self->model->axis_theta = PyFloat_AsDouble( PyDict_GetItemString(self->params, "axis_theta") );
284    self->model->solvent_sld = PyFloat_AsDouble( PyDict_GetItemString(self->params, "solvent_sld") );
285    self->model->thickness = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thickness") );
286    self->model->axis_phi = PyFloat_AsDouble( PyDict_GetItemString(self->params, "axis_phi") );
287    self->model->length = PyFloat_AsDouble( PyDict_GetItemString(self->params, "length") );
288    self->model->core_sld = PyFloat_AsDouble( PyDict_GetItemString(self->params, "core_sld") );
289    self->model->radius = PyFloat_AsDouble( PyDict_GetItemString(self->params, "radius") );
290    self->model->background = PyFloat_AsDouble( PyDict_GetItemString(self->params, "background") );
291    self->model->shell_sld = PyFloat_AsDouble( PyDict_GetItemString(self->params, "shell_sld") );
292    // Read in dispersion parameters
293    PyObject* disp_dict;
294    DispersionVisitor* visitor = new DispersionVisitor();
295    disp_dict = PyDict_GetItemString(self->dispersion, "radius");
296    self->model->radius.dispersion->accept_as_destination(visitor, self->model->radius.dispersion, disp_dict);
297    disp_dict = PyDict_GetItemString(self->dispersion, "thickness");
298    self->model->thickness.dispersion->accept_as_destination(visitor, self->model->thickness.dispersion, disp_dict);
299    disp_dict = PyDict_GetItemString(self->dispersion, "length");
300    self->model->length.dispersion->accept_as_destination(visitor, self->model->length.dispersion, disp_dict);
301    disp_dict = PyDict_GetItemString(self->dispersion, "axis_theta");
302    self->model->axis_theta.dispersion->accept_as_destination(visitor, self->model->axis_theta.dispersion, disp_dict);
303    disp_dict = PyDict_GetItemString(self->dispersion, "axis_phi");
304    self->model->axis_phi.dispersion->accept_as_destination(visitor, self->model->axis_phi.dispersion, disp_dict);
305
306       
307        // Get input and determine whether we have to supply a 1D or 2D return value.
308        if ( !PyArg_ParseTuple(args,"O",&pars) ) {
309            PyErr_SetString(CCoreShellCylinderModelError, 
310                "CCoreShellCylinderModel.evalDistribution expects a q value.");
311                return NULL;
312        }
313    // Check params
314       
315    if(PyArray_Check(pars)==1) {
316               
317            // Length of list should 1 or 2
318            npars = pars->nd; 
319            if(npars==1) {
320                // input is a numpy array
321                if (PyArray_Check(pars)) {
322                        return evaluateOneDim(self->model, (PyArrayObject*)pars); 
323                    }
324                }else{
325                    PyErr_SetString(CCoreShellCylinderModelError, 
326                   "CCoreShellCylinderModel.evalDistribution expect numpy array of one dimension.");
327                return NULL;
328                }
329    }else if( PyList_Check(pars)==1) {
330        // Length of list should be 2 for I(qx,qy)
331            mpars = PyList_GET_SIZE(pars); 
332            if(mpars!=2) {
333                PyErr_SetString(CCoreShellCylinderModelError, 
334                        "CCoreShellCylinderModel.evalDistribution expects a list of dimension 2.");
335                return NULL;
336            }
337             qx = PyList_GET_ITEM(pars,0);
338             qy = PyList_GET_ITEM(pars,1);
339             if (PyArray_Check(qx) && PyArray_Check(qy)) {
340                 return evaluateTwoDimXY(self->model, (PyArrayObject*)qx,
341                           (PyArrayObject*)qy);
342                 }else{
343                    PyErr_SetString(CCoreShellCylinderModelError, 
344                   "CCoreShellCylinderModel.evalDistribution expect 2 numpy arrays in list.");
345                return NULL;
346             }
347        }else{
348            PyErr_SetString(CCoreShellCylinderModelError, 
349                   "CCoreShellCylinderModel.evalDistribution couln't be run.");
350            return NULL;
351        }
352}
353
354/**
355 * Function to call to evaluate model
356 * @param args: input q or [q,phi]
357 * @return: function value
358 */
359static PyObject * run(CCoreShellCylinderModel *self, PyObject *args) {
360        double q_value, phi_value;
361        PyObject* pars;
362        int npars;
363       
364        // Get parameters
365       
366            // Reader parameter dictionary
367    self->model->scale = PyFloat_AsDouble( PyDict_GetItemString(self->params, "scale") );
368    self->model->axis_theta = PyFloat_AsDouble( PyDict_GetItemString(self->params, "axis_theta") );
369    self->model->solvent_sld = PyFloat_AsDouble( PyDict_GetItemString(self->params, "solvent_sld") );
370    self->model->thickness = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thickness") );
371    self->model->axis_phi = PyFloat_AsDouble( PyDict_GetItemString(self->params, "axis_phi") );
372    self->model->length = PyFloat_AsDouble( PyDict_GetItemString(self->params, "length") );
373    self->model->core_sld = PyFloat_AsDouble( PyDict_GetItemString(self->params, "core_sld") );
374    self->model->radius = PyFloat_AsDouble( PyDict_GetItemString(self->params, "radius") );
375    self->model->background = PyFloat_AsDouble( PyDict_GetItemString(self->params, "background") );
376    self->model->shell_sld = PyFloat_AsDouble( PyDict_GetItemString(self->params, "shell_sld") );
377    // Read in dispersion parameters
378    PyObject* disp_dict;
379    DispersionVisitor* visitor = new DispersionVisitor();
380    disp_dict = PyDict_GetItemString(self->dispersion, "radius");
381    self->model->radius.dispersion->accept_as_destination(visitor, self->model->radius.dispersion, disp_dict);
382    disp_dict = PyDict_GetItemString(self->dispersion, "thickness");
383    self->model->thickness.dispersion->accept_as_destination(visitor, self->model->thickness.dispersion, disp_dict);
384    disp_dict = PyDict_GetItemString(self->dispersion, "length");
385    self->model->length.dispersion->accept_as_destination(visitor, self->model->length.dispersion, disp_dict);
386    disp_dict = PyDict_GetItemString(self->dispersion, "axis_theta");
387    self->model->axis_theta.dispersion->accept_as_destination(visitor, self->model->axis_theta.dispersion, disp_dict);
388    disp_dict = PyDict_GetItemString(self->dispersion, "axis_phi");
389    self->model->axis_phi.dispersion->accept_as_destination(visitor, self->model->axis_phi.dispersion, disp_dict);
390
391       
392        // Get input and determine whether we have to supply a 1D or 2D return value.
393        if ( !PyArg_ParseTuple(args,"O",&pars) ) {
394            PyErr_SetString(CCoreShellCylinderModelError, 
395                "CCoreShellCylinderModel.run expects a q value.");
396                return NULL;
397        }
398         
399        // Check params
400        if( PyList_Check(pars)==1) {
401               
402                // Length of list should be 2 for I(q,phi)
403            npars = PyList_GET_SIZE(pars); 
404            if(npars!=2) {
405                PyErr_SetString(CCoreShellCylinderModelError, 
406                        "CCoreShellCylinderModel.run expects a double or a list of dimension 2.");
407                return NULL;
408            }
409            // We have a vector q, get the q and phi values at which
410            // to evaluate I(q,phi)
411            q_value = CCoreShellCylinderModel_readDouble(PyList_GET_ITEM(pars,0));
412            phi_value = CCoreShellCylinderModel_readDouble(PyList_GET_ITEM(pars,1));
413            // Skip zero
414            if (q_value==0) {
415                return Py_BuildValue("d",0.0);
416            }
417                return Py_BuildValue("d",(*(self->model)).evaluate_rphi(q_value,phi_value));
418
419        } else {
420
421                // We have a scalar q, we will evaluate I(q)
422                q_value = CCoreShellCylinderModel_readDouble(pars);             
423               
424                return Py_BuildValue("d",(*(self->model))(q_value));
425        }       
426}
427
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(CCoreShellCylinderModel *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->axis_theta = PyFloat_AsDouble( PyDict_GetItemString(self->params, "axis_theta") );
443    self->model->solvent_sld = PyFloat_AsDouble( PyDict_GetItemString(self->params, "solvent_sld") );
444    self->model->thickness = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thickness") );
445    self->model->axis_phi = PyFloat_AsDouble( PyDict_GetItemString(self->params, "axis_phi") );
446    self->model->length = PyFloat_AsDouble( PyDict_GetItemString(self->params, "length") );
447    self->model->core_sld = PyFloat_AsDouble( PyDict_GetItemString(self->params, "core_sld") );
448    self->model->radius = PyFloat_AsDouble( PyDict_GetItemString(self->params, "radius") );
449    self->model->background = PyFloat_AsDouble( PyDict_GetItemString(self->params, "background") );
450    self->model->shell_sld = PyFloat_AsDouble( PyDict_GetItemString(self->params, "shell_sld") );
451    // Read in dispersion parameters
452    PyObject* disp_dict;
453    DispersionVisitor* visitor = new DispersionVisitor();
454    disp_dict = PyDict_GetItemString(self->dispersion, "radius");
455    self->model->radius.dispersion->accept_as_destination(visitor, self->model->radius.dispersion, disp_dict);
456    disp_dict = PyDict_GetItemString(self->dispersion, "thickness");
457    self->model->thickness.dispersion->accept_as_destination(visitor, self->model->thickness.dispersion, disp_dict);
458    disp_dict = PyDict_GetItemString(self->dispersion, "length");
459    self->model->length.dispersion->accept_as_destination(visitor, self->model->length.dispersion, disp_dict);
460    disp_dict = PyDict_GetItemString(self->dispersion, "axis_theta");
461    self->model->axis_theta.dispersion->accept_as_destination(visitor, self->model->axis_theta.dispersion, disp_dict);
462    disp_dict = PyDict_GetItemString(self->dispersion, "axis_phi");
463    self->model->axis_phi.dispersion->accept_as_destination(visitor, self->model->axis_phi.dispersion, disp_dict);
464
465       
466        // Get input and determine whether we have to supply a 1D or 2D return value.
467        if ( !PyArg_ParseTuple(args,"O",&pars) ) {
468            PyErr_SetString(CCoreShellCylinderModelError, 
469                "CCoreShellCylinderModel.run expects a q value.");
470                return NULL;
471        }
472         
473        // Check params
474        if( PyList_Check(pars)==1) {
475               
476                // Length of list should be 2 for I(qx, qy))
477            npars = PyList_GET_SIZE(pars); 
478            if(npars!=2) {
479                PyErr_SetString(CCoreShellCylinderModelError, 
480                        "CCoreShellCylinderModel.run expects a double or a list of dimension 2.");
481                return NULL;
482            }
483            // We have a vector q, get the qx and qy values at which
484            // to evaluate I(qx,qy)
485            qx_value = CCoreShellCylinderModel_readDouble(PyList_GET_ITEM(pars,0));
486            qy_value = CCoreShellCylinderModel_readDouble(PyList_GET_ITEM(pars,1));
487            return Py_BuildValue("d",(*(self->model))(qx_value,qy_value));
488
489        } else {
490
491                // We have a scalar q, we will evaluate I(q)
492                qx_value = CCoreShellCylinderModel_readDouble(pars);           
493               
494                return Py_BuildValue("d",(*(self->model))(qx_value));
495        }       
496}
497
498static PyObject * reset(CCoreShellCylinderModel *self, PyObject *args) {
499   
500
501    return Py_BuildValue("d",0.0);
502}
503
504static PyObject * set_dispersion(CCoreShellCylinderModel *self, PyObject *args) {
505        PyObject * disp;
506        const char * par_name;
507
508        if ( !PyArg_ParseTuple(args,"sO", &par_name, &disp) ) {
509            PyErr_SetString(CCoreShellCylinderModelError,
510                "CCoreShellCylinderModel.set_dispersion expects a DispersionModel object.");
511                return NULL;
512        }
513        void *temp = PyCObject_AsVoidPtr(disp);
514        DispersionModel * dispersion = static_cast<DispersionModel *>(temp);
515
516
517        // Ugliness necessary to go from python to C
518            // TODO: refactor this
519    if (!strcmp(par_name, "radius")) {
520        self->model->radius.dispersion = dispersion;
521    } else    if (!strcmp(par_name, "thickness")) {
522        self->model->thickness.dispersion = dispersion;
523    } else    if (!strcmp(par_name, "length")) {
524        self->model->length.dispersion = dispersion;
525    } else    if (!strcmp(par_name, "axis_theta")) {
526        self->model->axis_theta.dispersion = dispersion;
527    } else    if (!strcmp(par_name, "axis_phi")) {
528        self->model->axis_phi.dispersion = dispersion;
529    } else {
530            PyErr_SetString(CCoreShellCylinderModelError,
531                "CCoreShellCylinderModel.set_dispersion expects a valid parameter name.");
532                return NULL;
533        }
534
535        DispersionVisitor* visitor = new DispersionVisitor();
536        PyObject * disp_dict = PyDict_New();
537        dispersion->accept_as_source(visitor, dispersion, disp_dict);
538        PyDict_SetItemString(self->dispersion, par_name, disp_dict);
539    return Py_BuildValue("i",1);
540}
541
542
543static PyMethodDef CCoreShellCylinderModel_methods[] = {
544    {"run",      (PyCFunction)run     , METH_VARARGS,
545      "Evaluate the model at a given Q or Q, phi"},
546    {"runXY",      (PyCFunction)runXY     , METH_VARARGS,
547      "Evaluate the model at a given Q or Qx, Qy"},
548     
549    {"evalDistribution",  (PyCFunction)evalDistribution , METH_VARARGS,
550      "Evaluate the model at a given Q or Qx, Qy vector "},
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 CCoreShellCylinderModelType = {
559    PyObject_HEAD_INIT(NULL)
560    0,                         /*ob_size*/
561    "CCoreShellCylinderModel",             /*tp_name*/
562    sizeof(CCoreShellCylinderModel),             /*tp_basicsize*/
563    0,                         /*tp_itemsize*/
564    (destructor)CCoreShellCylinderModel_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    "CCoreShellCylinderModel 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    CCoreShellCylinderModel_methods,             /* tp_methods */
588    CCoreShellCylinderModel_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)CCoreShellCylinderModel_init,      /* tp_init */
596    0,                         /* tp_alloc */
597    CCoreShellCylinderModel_new,                 /* tp_new */
598};
599
600
601//static PyMethodDef module_methods[] = {
602//    {NULL}
603//};
604
605/**
606 * Function used to add the model class to a module
607 * @param module: module to add the class to
608 */ 
609void addCCoreShellCylinderModel(PyObject *module) {
610        PyObject *d;
611       
612    if (PyType_Ready(&CCoreShellCylinderModelType) < 0)
613        return;
614
615    Py_INCREF(&CCoreShellCylinderModelType);
616    PyModule_AddObject(module, "CCoreShellCylinderModel", (PyObject *)&CCoreShellCylinderModelType);
617   
618    d = PyModule_GetDict(module);
619    CCoreShellCylinderModelError = PyErr_NewException("CCoreShellCylinderModel.error", NULL, NULL);
620    PyDict_SetItemString(d, "CCoreShellCylinderModelError", CCoreShellCylinderModelError);
621}
622
Note: See TracBrowser for help on using the repository browser.