source: sasview/sansmodels/src/sans/models/c_models/CReflModel.cpp @ 2e862a0

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 2e862a0 was 339ce67, checked in by Jae Cho <jhjcho@…>, 14 years ago

added some models and tests

  • Property mode set to 100644
File size: 45.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/** CReflModel
16 *
17 * C extension
18 *
19 * WARNING: THIS FILE WAS GENERATED BY WRAPPERGENERATOR.PY
20 *          DO NOT MODIFY THIS FILE, MODIFY refl.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 "refl.h"
36}
37
38#include "models.hh"
39#include "dispersion_visitor.hh"
40
41/// Error object for raised exceptions
42static PyObject * CReflModelError = 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    ReflModel * model;
54    /// Log for unit testing
55    PyObject * log;
56} CReflModel;
57
58
59static void
60CReflModel_dealloc(CReflModel* 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 *
72CReflModel_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
73{
74    CReflModel *self;
75   
76    self = (CReflModel *)type->tp_alloc(type, 0);
77   
78    return (PyObject *)self;
79}
80
81static int
82CReflModel_init(CReflModel *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 ReflModel();
90       
91        // Initialize parameter dictionary
92        PyDict_SetItemString(self->params,"sldIM_flat4",Py_BuildValue("d",0.000000000000));
93        PyDict_SetItemString(self->params,"thick_flat8",Py_BuildValue("d",100.000000000000));
94        PyDict_SetItemString(self->params,"thick_flat9",Py_BuildValue("d",100.000000000000));
95        PyDict_SetItemString(self->params,"sldIM_flat7",Py_BuildValue("d",0.000000000000));
96        PyDict_SetItemString(self->params,"sldIM_flat10",Py_BuildValue("d",0.000000000000));
97        PyDict_SetItemString(self->params,"sldIM_flat5",Py_BuildValue("d",0.000000000000));
98        PyDict_SetItemString(self->params,"thick_flat1",Py_BuildValue("d",10.000000000000));
99        PyDict_SetItemString(self->params,"thick_flat3",Py_BuildValue("d",100.000000000000));
100        PyDict_SetItemString(self->params,"n_layers",Py_BuildValue("d",1.000000000000));
101        PyDict_SetItemString(self->params,"thick_flat6",Py_BuildValue("d",100.000000000000));
102        PyDict_SetItemString(self->params,"thick_flat7",Py_BuildValue("d",100.000000000000));
103        PyDict_SetItemString(self->params,"thick_flat4",Py_BuildValue("d",100.000000000000));
104        PyDict_SetItemString(self->params,"thick_flat5",Py_BuildValue("d",100.000000000000));
105        PyDict_SetItemString(self->params,"sld_sub0",Py_BuildValue("d",0.000002070000));
106        PyDict_SetItemString(self->params,"thick_inter1",Py_BuildValue("d",1.000000000000));
107        PyDict_SetItemString(self->params,"thick_inter2",Py_BuildValue("d",1.000000000000));
108        PyDict_SetItemString(self->params,"thick_inter3",Py_BuildValue("d",1.000000000000));
109        PyDict_SetItemString(self->params,"thick_inter4",Py_BuildValue("d",1.000000000000));
110        PyDict_SetItemString(self->params,"thick_inter5",Py_BuildValue("d",1.000000000000));
111        PyDict_SetItemString(self->params,"thick_inter6",Py_BuildValue("d",1.000000000000));
112        PyDict_SetItemString(self->params,"thick_inter7",Py_BuildValue("d",1.000000000000));
113        PyDict_SetItemString(self->params,"thick_inter8",Py_BuildValue("d",1.000000000000));
114        PyDict_SetItemString(self->params,"thick_inter9",Py_BuildValue("d",1.000000000000));
115        PyDict_SetItemString(self->params,"sldIM_flat1",Py_BuildValue("d",0.000000000000));
116        PyDict_SetItemString(self->params,"scale",Py_BuildValue("d",1.000000000000));
117        PyDict_SetItemString(self->params,"sldIM_flat2",Py_BuildValue("d",0.000000000000));
118        PyDict_SetItemString(self->params,"func_inter9",Py_BuildValue("d",0.000000000000));
119        PyDict_SetItemString(self->params,"thick_inter10",Py_BuildValue("d",1.000000000000));
120        PyDict_SetItemString(self->params,"sldIM_flat3",Py_BuildValue("d",0.000000000000));
121        PyDict_SetItemString(self->params,"func_inter8",Py_BuildValue("d",0.000000000000));
122        PyDict_SetItemString(self->params,"thick_flat2",Py_BuildValue("d",100.000000000000));
123        PyDict_SetItemString(self->params,"func_inter0",Py_BuildValue("d",0.000000000000));
124        PyDict_SetItemString(self->params,"sld_medium",Py_BuildValue("d",0.000001000000));
125        PyDict_SetItemString(self->params,"func_inter2",Py_BuildValue("d",0.000000000000));
126        PyDict_SetItemString(self->params,"func_inter3",Py_BuildValue("d",0.000000000000));
127        PyDict_SetItemString(self->params,"thick_flat10",Py_BuildValue("d",100.000000000000));
128        PyDict_SetItemString(self->params,"func_inter5",Py_BuildValue("d",0.000000000000));
129        PyDict_SetItemString(self->params,"func_inter6",Py_BuildValue("d",0.000000000000));
130        PyDict_SetItemString(self->params,"func_inter7",Py_BuildValue("d",0.000000000000));
131        PyDict_SetItemString(self->params,"sld_flat4",Py_BuildValue("d",0.000003500000));
132        PyDict_SetItemString(self->params,"sld_flat5",Py_BuildValue("d",0.000004000000));
133        PyDict_SetItemString(self->params,"sld_flat6",Py_BuildValue("d",0.000003500000));
134        PyDict_SetItemString(self->params,"sld_flat7",Py_BuildValue("d",0.000004000000));
135        PyDict_SetItemString(self->params,"sld_flat1",Py_BuildValue("d",0.000004000000));
136        PyDict_SetItemString(self->params,"sld_flat2",Py_BuildValue("d",0.000003500000));
137        PyDict_SetItemString(self->params,"sld_flat3",Py_BuildValue("d",0.000004000000));
138        PyDict_SetItemString(self->params,"sldIM_flat8",Py_BuildValue("d",0.000000000000));
139        PyDict_SetItemString(self->params,"sld_flat8",Py_BuildValue("d",0.000003500000));
140        PyDict_SetItemString(self->params,"sld_flat9",Py_BuildValue("d",0.000004000000));
141        PyDict_SetItemString(self->params,"sldIM_flat9",Py_BuildValue("d",0.000000000000));
142        PyDict_SetItemString(self->params,"background",Py_BuildValue("d",0.000000000000));
143        PyDict_SetItemString(self->params,"func_inter1",Py_BuildValue("d",0.000000000000));
144        PyDict_SetItemString(self->params,"sldIM_sub0",Py_BuildValue("d",0.000000000000));
145        PyDict_SetItemString(self->params,"sldIM_medium",Py_BuildValue("d",0.000000000000));
146        PyDict_SetItemString(self->params,"sld_flat10",Py_BuildValue("d",0.000003500000));
147        PyDict_SetItemString(self->params,"thick_inter0",Py_BuildValue("d",1.000000000000));
148        PyDict_SetItemString(self->params,"sldIM_flat6",Py_BuildValue("d",0.000000000000));
149        PyDict_SetItemString(self->params,"func_inter4",Py_BuildValue("d",0.000000000000));
150        PyDict_SetItemString(self->params,"func_inter10",Py_BuildValue("d",0.000000000000));
151        // Initialize dispersion / averaging parameter dict
152        DispersionVisitor* visitor = new DispersionVisitor();
153        PyObject * disp_dict;
154        disp_dict = PyDict_New();
155        self->model->thick_inter0.dispersion->accept_as_source(visitor, self->model->thick_inter0.dispersion, disp_dict);
156        PyDict_SetItemString(self->dispersion, "thick_inter0", disp_dict);
157
158
159         
160        // Create empty log
161        self->log = PyDict_New();
162       
163       
164
165    }
166    return 0;
167}
168
169static PyMemberDef CReflModel_members[] = {
170    {"params", T_OBJECT, offsetof(CReflModel, params), 0,
171     "Parameters"},
172        {"dispersion", T_OBJECT, offsetof(CReflModel, dispersion), 0,
173          "Dispersion parameters"},     
174    {"log", T_OBJECT, offsetof(CReflModel, log), 0,
175     "Log"},
176    {NULL}  /* Sentinel */
177};
178
179/** Read double from PyObject
180    @param p PyObject
181    @return double
182*/
183double CReflModel_readDouble(PyObject *p) {
184    if (PyFloat_Check(p)==1) {
185        return (double)(((PyFloatObject *)(p))->ob_fval);
186    } else if (PyInt_Check(p)==1) {
187        return (double)(((PyIntObject *)(p))->ob_ival);
188    } else if (PyLong_Check(p)==1) {
189        return (double)PyLong_AsLong(p);
190    } else {
191        return 0.0;
192    }
193}
194/**
195 * Function to call to evaluate model
196 * @param args: input numpy array q[]
197 * @return: numpy array object
198 */
199 
200static PyObject *evaluateOneDim(ReflModel* model, PyArrayObject *q){
201    PyArrayObject *result;
202   
203    // Check validity of array q , q must be of dimension 1, an array of double
204    if (q->nd != 1 || q->descr->type_num != PyArray_DOUBLE)
205    {
206        //const char * message= "Invalid array: q->nd=%d,type_num=%d\n",q->nd,q->descr->type_num;
207        //PyErr_SetString(PyExc_ValueError , message);
208        return NULL;
209    }
210    result = (PyArrayObject *)PyArray_FromDims(q->nd, (int *)(q->dimensions), 
211                                                                                  PyArray_DOUBLE);
212        if (result == NULL) {
213        const char * message= "Could not create result ";
214        PyErr_SetString(PyExc_RuntimeError , message);
215                return NULL;
216        }
217         for (int i = 0; i < q->dimensions[0]; i++){
218      double q_value  = *(double *)(q->data + i*q->strides[0]);
219      double *result_value = (double *)(result->data + i*result->strides[0]);
220      *result_value =(*model)(q_value);
221        }
222    return PyArray_Return(result); 
223 }
224
225 /**
226 * Function to call to evaluate model
227 * @param args: input numpy array  [x[],y[]]
228 * @return: numpy array object
229 */
230 static PyObject * evaluateTwoDimXY( ReflModel* model, 
231                              PyArrayObject *x, PyArrayObject *y)
232 {
233    PyArrayObject *result;
234    int i,j, x_len, y_len, dims[1];
235    //check validity of input vectors
236    if (x->nd != 1 || x->descr->type_num != PyArray_DOUBLE
237        || y->nd != 1 || y->descr->type_num != PyArray_DOUBLE
238        || y->dimensions[0] != x->dimensions[0]){
239        const char * message= "evaluateTwoDimXY  expect 2 numpy arrays";
240        PyErr_SetString(PyExc_ValueError , message); 
241        return NULL;
242    }
243   
244        if (PyArray_Check(x) && PyArray_Check(y)) {
245               
246            x_len = dims[0]= x->dimensions[0];
247        y_len = dims[0]= y->dimensions[0];
248           
249            // Make a new double matrix of same dims
250        result=(PyArrayObject *) PyArray_FromDims(1,dims,NPY_DOUBLE);
251        if (result == NULL){
252            const char * message= "Could not create result ";
253        PyErr_SetString(PyExc_RuntimeError , message);
254            return NULL;
255            }
256       
257        /* Do the calculation. */
258        for ( i=0; i< x_len; i++) {
259            double x_value = *(double *)(x->data + i*x->strides[0]);
260                    double y_value = *(double *)(y->data + i*y->strides[0]);
261                        double *result_value = (double *)(result->data +
262                              i*result->strides[0]);
263                        *result_value = (*model)(x_value, y_value);
264        }           
265        return PyArray_Return(result); 
266       
267        }else{
268                    PyErr_SetString(CReflModelError, 
269                   "CReflModel.evaluateTwoDimXY couldn't run.");
270                return NULL;
271                }       
272}
273/**
274 *  evalDistribution function evaluate a model function with input vector
275 *  @param args: input q as vector or [qx, qy] where qx, qy are vectors
276 *
277 */ 
278static PyObject * evalDistribution(CReflModel *self, PyObject *args){
279        PyObject *qx, *qy;
280        PyArrayObject * pars;
281        int npars ,mpars;
282       
283        // Get parameters
284       
285            // Reader parameter dictionary
286    self->model->sldIM_flat4 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sldIM_flat4") );
287    self->model->thick_flat8 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thick_flat8") );
288    self->model->thick_flat9 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thick_flat9") );
289    self->model->sldIM_flat7 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sldIM_flat7") );
290    self->model->sldIM_flat10 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sldIM_flat10") );
291    self->model->sldIM_flat5 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sldIM_flat5") );
292    self->model->thick_flat1 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thick_flat1") );
293    self->model->thick_flat3 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thick_flat3") );
294    self->model->n_layers = PyFloat_AsDouble( PyDict_GetItemString(self->params, "n_layers") );
295    self->model->thick_flat6 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thick_flat6") );
296    self->model->thick_flat7 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thick_flat7") );
297    self->model->thick_flat4 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thick_flat4") );
298    self->model->thick_flat5 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thick_flat5") );
299    self->model->sld_sub0 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_sub0") );
300    self->model->thick_inter1 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thick_inter1") );
301    self->model->thick_inter2 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thick_inter2") );
302    self->model->thick_inter3 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thick_inter3") );
303    self->model->thick_inter4 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thick_inter4") );
304    self->model->thick_inter5 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thick_inter5") );
305    self->model->thick_inter6 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thick_inter6") );
306    self->model->thick_inter7 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thick_inter7") );
307    self->model->thick_inter8 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thick_inter8") );
308    self->model->thick_inter9 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thick_inter9") );
309    self->model->sldIM_flat1 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sldIM_flat1") );
310    self->model->scale = PyFloat_AsDouble( PyDict_GetItemString(self->params, "scale") );
311    self->model->sldIM_flat2 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sldIM_flat2") );
312    self->model->func_inter9 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "func_inter9") );
313    self->model->thick_inter10 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thick_inter10") );
314    self->model->sldIM_flat3 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sldIM_flat3") );
315    self->model->func_inter8 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "func_inter8") );
316    self->model->thick_flat2 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thick_flat2") );
317    self->model->func_inter0 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "func_inter0") );
318    self->model->sld_medium = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_medium") );
319    self->model->func_inter2 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "func_inter2") );
320    self->model->func_inter3 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "func_inter3") );
321    self->model->thick_flat10 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thick_flat10") );
322    self->model->func_inter5 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "func_inter5") );
323    self->model->func_inter6 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "func_inter6") );
324    self->model->func_inter7 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "func_inter7") );
325    self->model->sld_flat4 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_flat4") );
326    self->model->sld_flat5 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_flat5") );
327    self->model->sld_flat6 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_flat6") );
328    self->model->sld_flat7 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_flat7") );
329    self->model->sld_flat1 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_flat1") );
330    self->model->sld_flat2 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_flat2") );
331    self->model->sld_flat3 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_flat3") );
332    self->model->sldIM_flat8 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sldIM_flat8") );
333    self->model->sld_flat8 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_flat8") );
334    self->model->sld_flat9 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_flat9") );
335    self->model->sldIM_flat9 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sldIM_flat9") );
336    self->model->background = PyFloat_AsDouble( PyDict_GetItemString(self->params, "background") );
337    self->model->func_inter1 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "func_inter1") );
338    self->model->sldIM_sub0 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sldIM_sub0") );
339    self->model->sldIM_medium = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sldIM_medium") );
340    self->model->sld_flat10 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_flat10") );
341    self->model->thick_inter0 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thick_inter0") );
342    self->model->sldIM_flat6 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sldIM_flat6") );
343    self->model->func_inter4 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "func_inter4") );
344    self->model->func_inter10 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "func_inter10") );
345    // Read in dispersion parameters
346    PyObject* disp_dict;
347    DispersionVisitor* visitor = new DispersionVisitor();
348    disp_dict = PyDict_GetItemString(self->dispersion, "thick_inter0");
349    self->model->thick_inter0.dispersion->accept_as_destination(visitor, self->model->thick_inter0.dispersion, disp_dict);
350
351       
352        // Get input and determine whether we have to supply a 1D or 2D return value.
353        if ( !PyArg_ParseTuple(args,"O",&pars) ) {
354            PyErr_SetString(CReflModelError, 
355                "CReflModel.evalDistribution expects a q value.");
356                return NULL;
357        }
358    // Check params
359       
360    if(PyArray_Check(pars)==1) {
361               
362            // Length of list should 1 or 2
363            npars = pars->nd; 
364            if(npars==1) {
365                // input is a numpy array
366                if (PyArray_Check(pars)) {
367                        return evaluateOneDim(self->model, (PyArrayObject*)pars); 
368                    }
369                }else{
370                    PyErr_SetString(CReflModelError, 
371                   "CReflModel.evalDistribution expect numpy array of one dimension.");
372                return NULL;
373                }
374    }else if( PyList_Check(pars)==1) {
375        // Length of list should be 2 for I(qx,qy)
376            mpars = PyList_GET_SIZE(pars); 
377            if(mpars!=2) {
378                PyErr_SetString(CReflModelError, 
379                        "CReflModel.evalDistribution expects a list of dimension 2.");
380                return NULL;
381            }
382             qx = PyList_GET_ITEM(pars,0);
383             qy = PyList_GET_ITEM(pars,1);
384             if (PyArray_Check(qx) && PyArray_Check(qy)) {
385                 return evaluateTwoDimXY(self->model, (PyArrayObject*)qx,
386                           (PyArrayObject*)qy);
387                 }else{
388                    PyErr_SetString(CReflModelError, 
389                   "CReflModel.evalDistribution expect 2 numpy arrays in list.");
390                return NULL;
391             }
392        }
393        PyErr_SetString(CReflModelError, 
394                   "CReflModel.evalDistribution couln't be run.");
395        return NULL;
396       
397}
398
399/**
400 * Function to call to evaluate model
401 * @param args: input q or [q,phi]
402 * @return: function value
403 */
404static PyObject * run(CReflModel *self, PyObject *args) {
405        double q_value, phi_value;
406        PyObject* pars;
407        int npars;
408       
409        // Get parameters
410       
411            // Reader parameter dictionary
412    self->model->sldIM_flat4 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sldIM_flat4") );
413    self->model->thick_flat8 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thick_flat8") );
414    self->model->thick_flat9 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thick_flat9") );
415    self->model->sldIM_flat7 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sldIM_flat7") );
416    self->model->sldIM_flat10 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sldIM_flat10") );
417    self->model->sldIM_flat5 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sldIM_flat5") );
418    self->model->thick_flat1 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thick_flat1") );
419    self->model->thick_flat3 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thick_flat3") );
420    self->model->n_layers = PyFloat_AsDouble( PyDict_GetItemString(self->params, "n_layers") );
421    self->model->thick_flat6 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thick_flat6") );
422    self->model->thick_flat7 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thick_flat7") );
423    self->model->thick_flat4 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thick_flat4") );
424    self->model->thick_flat5 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thick_flat5") );
425    self->model->sld_sub0 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_sub0") );
426    self->model->thick_inter1 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thick_inter1") );
427    self->model->thick_inter2 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thick_inter2") );
428    self->model->thick_inter3 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thick_inter3") );
429    self->model->thick_inter4 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thick_inter4") );
430    self->model->thick_inter5 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thick_inter5") );
431    self->model->thick_inter6 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thick_inter6") );
432    self->model->thick_inter7 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thick_inter7") );
433    self->model->thick_inter8 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thick_inter8") );
434    self->model->thick_inter9 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thick_inter9") );
435    self->model->sldIM_flat1 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sldIM_flat1") );
436    self->model->scale = PyFloat_AsDouble( PyDict_GetItemString(self->params, "scale") );
437    self->model->sldIM_flat2 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sldIM_flat2") );
438    self->model->func_inter9 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "func_inter9") );
439    self->model->thick_inter10 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thick_inter10") );
440    self->model->sldIM_flat3 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sldIM_flat3") );
441    self->model->func_inter8 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "func_inter8") );
442    self->model->thick_flat2 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thick_flat2") );
443    self->model->func_inter0 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "func_inter0") );
444    self->model->sld_medium = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_medium") );
445    self->model->func_inter2 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "func_inter2") );
446    self->model->func_inter3 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "func_inter3") );
447    self->model->thick_flat10 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thick_flat10") );
448    self->model->func_inter5 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "func_inter5") );
449    self->model->func_inter6 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "func_inter6") );
450    self->model->func_inter7 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "func_inter7") );
451    self->model->sld_flat4 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_flat4") );
452    self->model->sld_flat5 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_flat5") );
453    self->model->sld_flat6 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_flat6") );
454    self->model->sld_flat7 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_flat7") );
455    self->model->sld_flat1 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_flat1") );
456    self->model->sld_flat2 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_flat2") );
457    self->model->sld_flat3 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_flat3") );
458    self->model->sldIM_flat8 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sldIM_flat8") );
459    self->model->sld_flat8 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_flat8") );
460    self->model->sld_flat9 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_flat9") );
461    self->model->sldIM_flat9 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sldIM_flat9") );
462    self->model->background = PyFloat_AsDouble( PyDict_GetItemString(self->params, "background") );
463    self->model->func_inter1 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "func_inter1") );
464    self->model->sldIM_sub0 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sldIM_sub0") );
465    self->model->sldIM_medium = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sldIM_medium") );
466    self->model->sld_flat10 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_flat10") );
467    self->model->thick_inter0 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thick_inter0") );
468    self->model->sldIM_flat6 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sldIM_flat6") );
469    self->model->func_inter4 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "func_inter4") );
470    self->model->func_inter10 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "func_inter10") );
471    // Read in dispersion parameters
472    PyObject* disp_dict;
473    DispersionVisitor* visitor = new DispersionVisitor();
474    disp_dict = PyDict_GetItemString(self->dispersion, "thick_inter0");
475    self->model->thick_inter0.dispersion->accept_as_destination(visitor, self->model->thick_inter0.dispersion, disp_dict);
476
477       
478        // Get input and determine whether we have to supply a 1D or 2D return value.
479        if ( !PyArg_ParseTuple(args,"O",&pars) ) {
480            PyErr_SetString(CReflModelError, 
481                "CReflModel.run expects a q value.");
482                return NULL;
483        }
484         
485        // Check params
486        if( PyList_Check(pars)==1) {
487               
488                // Length of list should be 2 for I(q,phi)
489            npars = PyList_GET_SIZE(pars); 
490            if(npars!=2) {
491                PyErr_SetString(CReflModelError, 
492                        "CReflModel.run expects a double or a list of dimension 2.");
493                return NULL;
494            }
495            // We have a vector q, get the q and phi values at which
496            // to evaluate I(q,phi)
497            q_value = CReflModel_readDouble(PyList_GET_ITEM(pars,0));
498            phi_value = CReflModel_readDouble(PyList_GET_ITEM(pars,1));
499            // Skip zero
500            if (q_value==0) {
501                return Py_BuildValue("d",0.0);
502            }
503                return Py_BuildValue("d",(*(self->model)).evaluate_rphi(q_value,phi_value));
504
505        } else {
506
507                // We have a scalar q, we will evaluate I(q)
508                q_value = CReflModel_readDouble(pars);         
509               
510                return Py_BuildValue("d",(*(self->model))(q_value));
511        }       
512}
513/**
514 * Function to call to calculate_ER
515 * @return: effective radius value
516 */
517static PyObject * calculate_ER(CReflModel *self) {
518
519        PyObject* pars;
520        int npars;
521       
522        // Get parameters
523       
524            // Reader parameter dictionary
525    self->model->sldIM_flat4 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sldIM_flat4") );
526    self->model->thick_flat8 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thick_flat8") );
527    self->model->thick_flat9 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thick_flat9") );
528    self->model->sldIM_flat7 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sldIM_flat7") );
529    self->model->sldIM_flat10 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sldIM_flat10") );
530    self->model->sldIM_flat5 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sldIM_flat5") );
531    self->model->thick_flat1 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thick_flat1") );
532    self->model->thick_flat3 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thick_flat3") );
533    self->model->n_layers = PyFloat_AsDouble( PyDict_GetItemString(self->params, "n_layers") );
534    self->model->thick_flat6 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thick_flat6") );
535    self->model->thick_flat7 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thick_flat7") );
536    self->model->thick_flat4 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thick_flat4") );
537    self->model->thick_flat5 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thick_flat5") );
538    self->model->sld_sub0 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_sub0") );
539    self->model->thick_inter1 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thick_inter1") );
540    self->model->thick_inter2 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thick_inter2") );
541    self->model->thick_inter3 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thick_inter3") );
542    self->model->thick_inter4 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thick_inter4") );
543    self->model->thick_inter5 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thick_inter5") );
544    self->model->thick_inter6 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thick_inter6") );
545    self->model->thick_inter7 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thick_inter7") );
546    self->model->thick_inter8 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thick_inter8") );
547    self->model->thick_inter9 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thick_inter9") );
548    self->model->sldIM_flat1 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sldIM_flat1") );
549    self->model->scale = PyFloat_AsDouble( PyDict_GetItemString(self->params, "scale") );
550    self->model->sldIM_flat2 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sldIM_flat2") );
551    self->model->func_inter9 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "func_inter9") );
552    self->model->thick_inter10 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thick_inter10") );
553    self->model->sldIM_flat3 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sldIM_flat3") );
554    self->model->func_inter8 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "func_inter8") );
555    self->model->thick_flat2 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thick_flat2") );
556    self->model->func_inter0 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "func_inter0") );
557    self->model->sld_medium = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_medium") );
558    self->model->func_inter2 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "func_inter2") );
559    self->model->func_inter3 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "func_inter3") );
560    self->model->thick_flat10 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thick_flat10") );
561    self->model->func_inter5 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "func_inter5") );
562    self->model->func_inter6 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "func_inter6") );
563    self->model->func_inter7 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "func_inter7") );
564    self->model->sld_flat4 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_flat4") );
565    self->model->sld_flat5 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_flat5") );
566    self->model->sld_flat6 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_flat6") );
567    self->model->sld_flat7 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_flat7") );
568    self->model->sld_flat1 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_flat1") );
569    self->model->sld_flat2 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_flat2") );
570    self->model->sld_flat3 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_flat3") );
571    self->model->sldIM_flat8 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sldIM_flat8") );
572    self->model->sld_flat8 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_flat8") );
573    self->model->sld_flat9 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_flat9") );
574    self->model->sldIM_flat9 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sldIM_flat9") );
575    self->model->background = PyFloat_AsDouble( PyDict_GetItemString(self->params, "background") );
576    self->model->func_inter1 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "func_inter1") );
577    self->model->sldIM_sub0 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sldIM_sub0") );
578    self->model->sldIM_medium = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sldIM_medium") );
579    self->model->sld_flat10 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_flat10") );
580    self->model->thick_inter0 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thick_inter0") );
581    self->model->sldIM_flat6 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sldIM_flat6") );
582    self->model->func_inter4 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "func_inter4") );
583    self->model->func_inter10 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "func_inter10") );
584    // Read in dispersion parameters
585    PyObject* disp_dict;
586    DispersionVisitor* visitor = new DispersionVisitor();
587    disp_dict = PyDict_GetItemString(self->dispersion, "thick_inter0");
588    self->model->thick_inter0.dispersion->accept_as_destination(visitor, self->model->thick_inter0.dispersion, disp_dict);
589
590               
591        return Py_BuildValue("d",(*(self->model)).calculate_ER());
592
593}
594/**
595 * Function to call to evaluate model in cartesian coordinates
596 * @param args: input q or [qx, qy]]
597 * @return: function value
598 */
599static PyObject * runXY(CReflModel *self, PyObject *args) {
600        double qx_value, qy_value;
601        PyObject* pars;
602        int npars;
603       
604        // Get parameters
605       
606            // Reader parameter dictionary
607    self->model->sldIM_flat4 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sldIM_flat4") );
608    self->model->thick_flat8 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thick_flat8") );
609    self->model->thick_flat9 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thick_flat9") );
610    self->model->sldIM_flat7 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sldIM_flat7") );
611    self->model->sldIM_flat10 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sldIM_flat10") );
612    self->model->sldIM_flat5 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sldIM_flat5") );
613    self->model->thick_flat1 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thick_flat1") );
614    self->model->thick_flat3 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thick_flat3") );
615    self->model->n_layers = PyFloat_AsDouble( PyDict_GetItemString(self->params, "n_layers") );
616    self->model->thick_flat6 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thick_flat6") );
617    self->model->thick_flat7 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thick_flat7") );
618    self->model->thick_flat4 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thick_flat4") );
619    self->model->thick_flat5 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thick_flat5") );
620    self->model->sld_sub0 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_sub0") );
621    self->model->thick_inter1 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thick_inter1") );
622    self->model->thick_inter2 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thick_inter2") );
623    self->model->thick_inter3 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thick_inter3") );
624    self->model->thick_inter4 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thick_inter4") );
625    self->model->thick_inter5 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thick_inter5") );
626    self->model->thick_inter6 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thick_inter6") );
627    self->model->thick_inter7 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thick_inter7") );
628    self->model->thick_inter8 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thick_inter8") );
629    self->model->thick_inter9 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thick_inter9") );
630    self->model->sldIM_flat1 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sldIM_flat1") );
631    self->model->scale = PyFloat_AsDouble( PyDict_GetItemString(self->params, "scale") );
632    self->model->sldIM_flat2 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sldIM_flat2") );
633    self->model->func_inter9 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "func_inter9") );
634    self->model->thick_inter10 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thick_inter10") );
635    self->model->sldIM_flat3 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sldIM_flat3") );
636    self->model->func_inter8 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "func_inter8") );
637    self->model->thick_flat2 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thick_flat2") );
638    self->model->func_inter0 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "func_inter0") );
639    self->model->sld_medium = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_medium") );
640    self->model->func_inter2 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "func_inter2") );
641    self->model->func_inter3 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "func_inter3") );
642    self->model->thick_flat10 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thick_flat10") );
643    self->model->func_inter5 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "func_inter5") );
644    self->model->func_inter6 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "func_inter6") );
645    self->model->func_inter7 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "func_inter7") );
646    self->model->sld_flat4 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_flat4") );
647    self->model->sld_flat5 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_flat5") );
648    self->model->sld_flat6 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_flat6") );
649    self->model->sld_flat7 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_flat7") );
650    self->model->sld_flat1 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_flat1") );
651    self->model->sld_flat2 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_flat2") );
652    self->model->sld_flat3 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_flat3") );
653    self->model->sldIM_flat8 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sldIM_flat8") );
654    self->model->sld_flat8 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_flat8") );
655    self->model->sld_flat9 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_flat9") );
656    self->model->sldIM_flat9 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sldIM_flat9") );
657    self->model->background = PyFloat_AsDouble( PyDict_GetItemString(self->params, "background") );
658    self->model->func_inter1 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "func_inter1") );
659    self->model->sldIM_sub0 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sldIM_sub0") );
660    self->model->sldIM_medium = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sldIM_medium") );
661    self->model->sld_flat10 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_flat10") );
662    self->model->thick_inter0 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thick_inter0") );
663    self->model->sldIM_flat6 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sldIM_flat6") );
664    self->model->func_inter4 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "func_inter4") );
665    self->model->func_inter10 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "func_inter10") );
666    // Read in dispersion parameters
667    PyObject* disp_dict;
668    DispersionVisitor* visitor = new DispersionVisitor();
669    disp_dict = PyDict_GetItemString(self->dispersion, "thick_inter0");
670    self->model->thick_inter0.dispersion->accept_as_destination(visitor, self->model->thick_inter0.dispersion, disp_dict);
671
672       
673        // Get input and determine whether we have to supply a 1D or 2D return value.
674        if ( !PyArg_ParseTuple(args,"O",&pars) ) {
675            PyErr_SetString(CReflModelError, 
676                "CReflModel.run expects a q value.");
677                return NULL;
678        }
679         
680        // Check params
681        if( PyList_Check(pars)==1) {
682               
683                // Length of list should be 2 for I(qx, qy))
684            npars = PyList_GET_SIZE(pars); 
685            if(npars!=2) {
686                PyErr_SetString(CReflModelError, 
687                        "CReflModel.run expects a double or a list of dimension 2.");
688                return NULL;
689            }
690            // We have a vector q, get the qx and qy values at which
691            // to evaluate I(qx,qy)
692            qx_value = CReflModel_readDouble(PyList_GET_ITEM(pars,0));
693            qy_value = CReflModel_readDouble(PyList_GET_ITEM(pars,1));
694            return Py_BuildValue("d",(*(self->model))(qx_value,qy_value));
695
696        } else {
697
698                // We have a scalar q, we will evaluate I(q)
699                qx_value = CReflModel_readDouble(pars);         
700               
701                return Py_BuildValue("d",(*(self->model))(qx_value));
702        }       
703}
704
705static PyObject * reset(CReflModel *self, PyObject *args) {
706   
707
708    return Py_BuildValue("d",0.0);
709}
710
711static PyObject * set_dispersion(CReflModel *self, PyObject *args) {
712        PyObject * disp;
713        const char * par_name;
714
715        if ( !PyArg_ParseTuple(args,"sO", &par_name, &disp) ) {
716            PyErr_SetString(CReflModelError,
717                "CReflModel.set_dispersion expects a DispersionModel object.");
718                return NULL;
719        }
720        void *temp = PyCObject_AsVoidPtr(disp);
721        DispersionModel * dispersion = static_cast<DispersionModel *>(temp);
722
723
724        // Ugliness necessary to go from python to C
725            // TODO: refactor this
726    if (!strcmp(par_name, "thick_inter0")) {
727        self->model->thick_inter0.dispersion = dispersion;
728    } else {
729            PyErr_SetString(CReflModelError,
730                "CReflModel.set_dispersion expects a valid parameter name.");
731                return NULL;
732        }
733
734        DispersionVisitor* visitor = new DispersionVisitor();
735        PyObject * disp_dict = PyDict_New();
736        dispersion->accept_as_source(visitor, dispersion, disp_dict);
737        PyDict_SetItemString(self->dispersion, par_name, disp_dict);
738    return Py_BuildValue("i",1);
739}
740
741
742static PyMethodDef CReflModel_methods[] = {
743    {"run",      (PyCFunction)run     , METH_VARARGS,
744      "Evaluate the model at a given Q or Q, phi"},
745    {"runXY",      (PyCFunction)runXY     , METH_VARARGS,
746      "Evaluate the model at a given Q or Qx, Qy"},
747    {"calculate_ER",      (PyCFunction)calculate_ER     , METH_VARARGS,
748      "Evaluate the model at a given Q or Q, phi"},
749     
750    {"evalDistribution",  (PyCFunction)evalDistribution , METH_VARARGS,
751      "Evaluate the model at a given Q or Qx, Qy vector "},
752    {"reset",    (PyCFunction)reset   , METH_VARARGS,
753      "Reset pair correlation"},
754    {"set_dispersion",      (PyCFunction)set_dispersion     , METH_VARARGS,
755      "Set the dispersion model for a given parameter"},
756   {NULL}
757};
758
759static PyTypeObject CReflModelType = {
760    PyObject_HEAD_INIT(NULL)
761    0,                         /*ob_size*/
762    "CReflModel",             /*tp_name*/
763    sizeof(CReflModel),             /*tp_basicsize*/
764    0,                         /*tp_itemsize*/
765    (destructor)CReflModel_dealloc, /*tp_dealloc*/
766    0,                         /*tp_print*/
767    0,                         /*tp_getattr*/
768    0,                         /*tp_setattr*/
769    0,                         /*tp_compare*/
770    0,                         /*tp_repr*/
771    0,                         /*tp_as_number*/
772    0,                         /*tp_as_sequence*/
773    0,                         /*tp_as_mapping*/
774    0,                         /*tp_hash */
775    0,                         /*tp_call*/
776    0,                         /*tp_str*/
777    0,                         /*tp_getattro*/
778    0,                         /*tp_setattro*/
779    0,                         /*tp_as_buffer*/
780    Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
781    "CReflModel objects",           /* tp_doc */
782    0,                         /* tp_traverse */
783    0,                         /* tp_clear */
784    0,                         /* tp_richcompare */
785    0,                         /* tp_weaklistoffset */
786    0,                         /* tp_iter */
787    0,                         /* tp_iternext */
788    CReflModel_methods,             /* tp_methods */
789    CReflModel_members,             /* tp_members */
790    0,                         /* tp_getset */
791    0,                         /* tp_base */
792    0,                         /* tp_dict */
793    0,                         /* tp_descr_get */
794    0,                         /* tp_descr_set */
795    0,                         /* tp_dictoffset */
796    (initproc)CReflModel_init,      /* tp_init */
797    0,                         /* tp_alloc */
798    CReflModel_new,                 /* tp_new */
799};
800
801
802//static PyMethodDef module_methods[] = {
803//    {NULL}
804//};
805
806/**
807 * Function used to add the model class to a module
808 * @param module: module to add the class to
809 */ 
810void addCReflModel(PyObject *module) {
811        PyObject *d;
812       
813    if (PyType_Ready(&CReflModelType) < 0)
814        return;
815
816    Py_INCREF(&CReflModelType);
817    PyModule_AddObject(module, "CReflModel", (PyObject *)&CReflModelType);
818   
819    d = PyModule_GetDict(module);
820    CReflModelError = PyErr_NewException("CReflModel.error", NULL, NULL);
821    PyDict_SetItemString(d, "CReflModelError", CReflModelError);
822}
823
Note: See TracBrowser for help on using the repository browser.