source: sasview/sansmodels/src/sans/models/c_models/CCoreShellCylinderModel.cpp @ 01de557

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 01de557 was 0b082f3, checked in by Mathieu Doucet <doucetm@…>, 13 years ago

Re #7 Enable openmp for all models

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