source: sasview/sansmodels/src/sans/models/c_models/COnionModel.cpp @ 0438933

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 0438933 was 2605da22, checked in by Mathieu Doucet <doucetm@…>, 13 years ago

Re #4 Still a few more warnings

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