source: sasview/sansmodels/src/python_wrapper/CSphereSLDModel.cpp @ fb732dc

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 fb732dc was 67424cd, checked in by Mathieu Doucet <doucetm@…>, 13 years ago

Reorganizing models in preparation of cpp cleanup

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