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

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 f82fe3c was 96656e3, checked in by Jae Cho <jhjcho@…>, 14 years ago

update models due to changes of template

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