source: sasview/sansmodels/src/sans/models/c_models/CSphereSLDModel.cpp @ 600eca2

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 600eca2 was 0164899a, checked in by Jae Cho <jhjcho@…>, 14 years ago

new models and some bug fixes

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