source: sasview/sansmodels/src/sans/models/c_models/CReflAdvModel.cpp @ 33aea7f

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 33aea7f was 33aea7f, checked in by Jae Cho <jhjcho@…>, 13 years ago

added missing variable

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