source: sasview/sansmodels/src/python_wrapper/COnionModel.cpp @ acd0fd10

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

Reorganizing models in preparation of cpp cleanup

  • Property mode set to 100644
File size: 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), PyArray_DOUBLE);
242        if (result == NULL) {
243        const char * message= "Could not create result ";
244        PyErr_SetString(PyExc_RuntimeError , message);
245                return NULL;
246        }
247#pragma omp parallel for
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 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#pragma omp parallel for
290        for (int i=0; i< x_len; i++) {
291            double x_value = *(double *)(x->data + i*x->strides[0]);
292                    double y_value = *(double *)(y->data + i*y->strides[0]);
293                        double *result_value = (double *)(result->data +
294                              i*result->strides[0]);
295                        *result_value = (*model)(x_value, y_value);
296        }           
297        return PyArray_Return(result); 
298       
299        }else{
300                    PyErr_SetString(COnionModelError, 
301                   "COnionModel.evaluateTwoDimXY couldn't run.");
302                return NULL;
303                }       
304}
305/**
306 *  evalDistribution function evaluate a model function with input vector
307 *  @param args: input q as vector or [qx, qy] where qx, qy are vectors
308 *
309 */ 
310static PyObject * evalDistribution(COnionModel *self, PyObject *args){
311        PyObject *qx, *qy;
312        PyArrayObject * pars;
313        int npars ,mpars;
314       
315        // Get parameters
316       
317            // Reader parameter dictionary
318    self->model->thick_shell4 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thick_shell4") );
319    self->model->thick_shell5 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thick_shell5") );
320    self->model->thick_shell9 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thick_shell9") );
321    self->model->thick_shell7 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thick_shell7") );
322    self->model->sld_core0 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_core0") );
323    self->model->rad_core0 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "rad_core0") );
324    self->model->A_shell10 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "A_shell10") );
325    self->model->thick_shell1 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thick_shell1") );
326    self->model->func_shell4 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "func_shell4") );
327    self->model->func_shell5 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "func_shell5") );
328    self->model->func_shell6 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "func_shell6") );
329    self->model->scale = PyFloat_AsDouble( PyDict_GetItemString(self->params, "scale") );
330    self->model->func_shell1 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "func_shell1") );
331    self->model->thick_shell10 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thick_shell10") );
332    self->model->func_shell3 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "func_shell3") );
333    self->model->func_shell8 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "func_shell8") );
334    self->model->func_shell9 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "func_shell9") );
335    self->model->sld_in_shell5 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_in_shell5") );
336    self->model->func_shell10 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "func_shell10") );
337    self->model->sld_in_shell4 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_in_shell4") );
338    self->model->sld_in_shell7 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_in_shell7") );
339    self->model->sld_out_shell9 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_out_shell9") );
340    self->model->sld_out_shell8 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_out_shell8") );
341    self->model->thick_shell6 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thick_shell6") );
342    self->model->sld_out_shell2 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_out_shell2") );
343    self->model->sld_in_shell9 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_in_shell9") );
344    self->model->sld_in_shell8 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_in_shell8") );
345    self->model->thick_shell2 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thick_shell2") );
346    self->model->thick_shell3 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thick_shell3") );
347    self->model->sld_out_shell1 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_out_shell1") );
348    self->model->sld_out_shell5 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_out_shell5") );
349    self->model->sld_out_shell3 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_out_shell3") );
350    self->model->sld_in_shell6 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_in_shell6") );
351    self->model->sld_in_shell1 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_in_shell1") );
352    self->model->sld_out_shell4 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_out_shell4") );
353    self->model->sld_in_shell3 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_in_shell3") );
354    self->model->sld_out_shell6 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_out_shell6") );
355    self->model->func_shell7 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "func_shell7") );
356    self->model->sld_out_shell7 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_out_shell7") );
357    self->model->background = PyFloat_AsDouble( PyDict_GetItemString(self->params, "background") );
358    self->model->n_shells = PyFloat_AsDouble( PyDict_GetItemString(self->params, "n_shells") );
359    self->model->sld_in_shell2 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_in_shell2") );
360    self->model->sld_solv = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_solv") );
361    self->model->func_shell2 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "func_shell2") );
362    self->model->thick_shell8 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thick_shell8") );
363    self->model->sld_out_shell10 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_out_shell10") );
364    self->model->sld_in_shell10 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_in_shell10") );
365    self->model->A_shell8 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "A_shell8") );
366    self->model->A_shell9 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "A_shell9") );
367    self->model->A_shell6 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "A_shell6") );
368    self->model->A_shell7 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "A_shell7") );
369    self->model->A_shell4 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "A_shell4") );
370    self->model->A_shell5 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "A_shell5") );
371    self->model->A_shell2 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "A_shell2") );
372    self->model->A_shell3 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "A_shell3") );
373    self->model->A_shell1 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "A_shell1") );
374    // Read in dispersion parameters
375    PyObject* disp_dict;
376    DispersionVisitor* visitor = new DispersionVisitor();
377    disp_dict = PyDict_GetItemString(self->dispersion, "rad_core0");
378    self->model->rad_core0.dispersion->accept_as_destination(visitor, self->model->rad_core0.dispersion, disp_dict);
379    disp_dict = PyDict_GetItemString(self->dispersion, "thick_shell1");
380    self->model->thick_shell1.dispersion->accept_as_destination(visitor, self->model->thick_shell1.dispersion, disp_dict);
381    disp_dict = PyDict_GetItemString(self->dispersion, "thick_shell2");
382    self->model->thick_shell2.dispersion->accept_as_destination(visitor, self->model->thick_shell2.dispersion, disp_dict);
383    disp_dict = PyDict_GetItemString(self->dispersion, "thick_shell3");
384    self->model->thick_shell3.dispersion->accept_as_destination(visitor, self->model->thick_shell3.dispersion, disp_dict);
385    disp_dict = PyDict_GetItemString(self->dispersion, "thick_shell4");
386    self->model->thick_shell4.dispersion->accept_as_destination(visitor, self->model->thick_shell4.dispersion, disp_dict);
387    disp_dict = PyDict_GetItemString(self->dispersion, "thick_shell5");
388    self->model->thick_shell5.dispersion->accept_as_destination(visitor, self->model->thick_shell5.dispersion, disp_dict);
389    disp_dict = PyDict_GetItemString(self->dispersion, "thick_shell6");
390    self->model->thick_shell6.dispersion->accept_as_destination(visitor, self->model->thick_shell6.dispersion, disp_dict);
391    disp_dict = PyDict_GetItemString(self->dispersion, "thick_shell7");
392    self->model->thick_shell7.dispersion->accept_as_destination(visitor, self->model->thick_shell7.dispersion, disp_dict);
393    disp_dict = PyDict_GetItemString(self->dispersion, "thick_shell8");
394    self->model->thick_shell8.dispersion->accept_as_destination(visitor, self->model->thick_shell8.dispersion, disp_dict);
395    disp_dict = PyDict_GetItemString(self->dispersion, "thick_shell9");
396    self->model->thick_shell9.dispersion->accept_as_destination(visitor, self->model->thick_shell9.dispersion, disp_dict);
397    disp_dict = PyDict_GetItemString(self->dispersion, "thick_shell10");
398    self->model->thick_shell10.dispersion->accept_as_destination(visitor, self->model->thick_shell10.dispersion, disp_dict);
399
400       
401        // Get input and determine whether we have to supply a 1D or 2D return value.
402        if ( !PyArg_ParseTuple(args,"O",&pars) ) {
403            PyErr_SetString(COnionModelError, 
404                "COnionModel.evalDistribution expects a q value.");
405                return NULL;
406        }
407    // Check params
408       
409    if(PyArray_Check(pars)==1) {
410               
411            // Length of list should 1 or 2
412            npars = pars->nd; 
413            if(npars==1) {
414                // input is a numpy array
415                if (PyArray_Check(pars)) {
416                        return evaluateOneDim(self->model, (PyArrayObject*)pars); 
417                    }
418                }else{
419                    PyErr_SetString(COnionModelError, 
420                   "COnionModel.evalDistribution expect numpy array of one dimension.");
421                return NULL;
422                }
423    }else if( PyList_Check(pars)==1) {
424        // Length of list should be 2 for I(qx,qy)
425            mpars = PyList_GET_SIZE(pars); 
426            if(mpars!=2) {
427                PyErr_SetString(COnionModelError, 
428                        "COnionModel.evalDistribution expects a list of dimension 2.");
429                return NULL;
430            }
431             qx = PyList_GET_ITEM(pars,0);
432             qy = PyList_GET_ITEM(pars,1);
433             if (PyArray_Check(qx) && PyArray_Check(qy)) {
434                 return evaluateTwoDimXY(self->model, (PyArrayObject*)qx,
435                           (PyArrayObject*)qy);
436                 }else{
437                    PyErr_SetString(COnionModelError, 
438                   "COnionModel.evalDistribution expect 2 numpy arrays in list.");
439                return NULL;
440             }
441        }
442        PyErr_SetString(COnionModelError, 
443                   "COnionModel.evalDistribution couln't be run.");
444        return NULL;
445       
446}
447
448/**
449 * Function to call to evaluate model
450 * @param args: input q or [q,phi]
451 * @return: function value
452 */
453static PyObject * run(COnionModel *self, PyObject *args) {
454        double q_value, phi_value;
455        PyObject* pars;
456        int npars;
457       
458        // Get parameters
459       
460            // Reader parameter dictionary
461    self->model->thick_shell4 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thick_shell4") );
462    self->model->thick_shell5 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thick_shell5") );
463    self->model->thick_shell9 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thick_shell9") );
464    self->model->thick_shell7 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thick_shell7") );
465    self->model->sld_core0 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_core0") );
466    self->model->rad_core0 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "rad_core0") );
467    self->model->A_shell10 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "A_shell10") );
468    self->model->thick_shell1 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thick_shell1") );
469    self->model->func_shell4 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "func_shell4") );
470    self->model->func_shell5 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "func_shell5") );
471    self->model->func_shell6 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "func_shell6") );
472    self->model->scale = PyFloat_AsDouble( PyDict_GetItemString(self->params, "scale") );
473    self->model->func_shell1 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "func_shell1") );
474    self->model->thick_shell10 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thick_shell10") );
475    self->model->func_shell3 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "func_shell3") );
476    self->model->func_shell8 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "func_shell8") );
477    self->model->func_shell9 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "func_shell9") );
478    self->model->sld_in_shell5 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_in_shell5") );
479    self->model->func_shell10 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "func_shell10") );
480    self->model->sld_in_shell4 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_in_shell4") );
481    self->model->sld_in_shell7 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_in_shell7") );
482    self->model->sld_out_shell9 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_out_shell9") );
483    self->model->sld_out_shell8 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_out_shell8") );
484    self->model->thick_shell6 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thick_shell6") );
485    self->model->sld_out_shell2 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_out_shell2") );
486    self->model->sld_in_shell9 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_in_shell9") );
487    self->model->sld_in_shell8 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_in_shell8") );
488    self->model->thick_shell2 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thick_shell2") );
489    self->model->thick_shell3 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thick_shell3") );
490    self->model->sld_out_shell1 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_out_shell1") );
491    self->model->sld_out_shell5 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_out_shell5") );
492    self->model->sld_out_shell3 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_out_shell3") );
493    self->model->sld_in_shell6 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_in_shell6") );
494    self->model->sld_in_shell1 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_in_shell1") );
495    self->model->sld_out_shell4 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_out_shell4") );
496    self->model->sld_in_shell3 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_in_shell3") );
497    self->model->sld_out_shell6 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_out_shell6") );
498    self->model->func_shell7 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "func_shell7") );
499    self->model->sld_out_shell7 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_out_shell7") );
500    self->model->background = PyFloat_AsDouble( PyDict_GetItemString(self->params, "background") );
501    self->model->n_shells = PyFloat_AsDouble( PyDict_GetItemString(self->params, "n_shells") );
502    self->model->sld_in_shell2 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_in_shell2") );
503    self->model->sld_solv = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_solv") );
504    self->model->func_shell2 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "func_shell2") );
505    self->model->thick_shell8 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thick_shell8") );
506    self->model->sld_out_shell10 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_out_shell10") );
507    self->model->sld_in_shell10 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_in_shell10") );
508    self->model->A_shell8 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "A_shell8") );
509    self->model->A_shell9 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "A_shell9") );
510    self->model->A_shell6 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "A_shell6") );
511    self->model->A_shell7 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "A_shell7") );
512    self->model->A_shell4 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "A_shell4") );
513    self->model->A_shell5 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "A_shell5") );
514    self->model->A_shell2 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "A_shell2") );
515    self->model->A_shell3 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "A_shell3") );
516    self->model->A_shell1 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "A_shell1") );
517    // Read in dispersion parameters
518    PyObject* disp_dict;
519    DispersionVisitor* visitor = new DispersionVisitor();
520    disp_dict = PyDict_GetItemString(self->dispersion, "rad_core0");
521    self->model->rad_core0.dispersion->accept_as_destination(visitor, self->model->rad_core0.dispersion, disp_dict);
522    disp_dict = PyDict_GetItemString(self->dispersion, "thick_shell1");
523    self->model->thick_shell1.dispersion->accept_as_destination(visitor, self->model->thick_shell1.dispersion, disp_dict);
524    disp_dict = PyDict_GetItemString(self->dispersion, "thick_shell2");
525    self->model->thick_shell2.dispersion->accept_as_destination(visitor, self->model->thick_shell2.dispersion, disp_dict);
526    disp_dict = PyDict_GetItemString(self->dispersion, "thick_shell3");
527    self->model->thick_shell3.dispersion->accept_as_destination(visitor, self->model->thick_shell3.dispersion, disp_dict);
528    disp_dict = PyDict_GetItemString(self->dispersion, "thick_shell4");
529    self->model->thick_shell4.dispersion->accept_as_destination(visitor, self->model->thick_shell4.dispersion, disp_dict);
530    disp_dict = PyDict_GetItemString(self->dispersion, "thick_shell5");
531    self->model->thick_shell5.dispersion->accept_as_destination(visitor, self->model->thick_shell5.dispersion, disp_dict);
532    disp_dict = PyDict_GetItemString(self->dispersion, "thick_shell6");
533    self->model->thick_shell6.dispersion->accept_as_destination(visitor, self->model->thick_shell6.dispersion, disp_dict);
534    disp_dict = PyDict_GetItemString(self->dispersion, "thick_shell7");
535    self->model->thick_shell7.dispersion->accept_as_destination(visitor, self->model->thick_shell7.dispersion, disp_dict);
536    disp_dict = PyDict_GetItemString(self->dispersion, "thick_shell8");
537    self->model->thick_shell8.dispersion->accept_as_destination(visitor, self->model->thick_shell8.dispersion, disp_dict);
538    disp_dict = PyDict_GetItemString(self->dispersion, "thick_shell9");
539    self->model->thick_shell9.dispersion->accept_as_destination(visitor, self->model->thick_shell9.dispersion, disp_dict);
540    disp_dict = PyDict_GetItemString(self->dispersion, "thick_shell10");
541    self->model->thick_shell10.dispersion->accept_as_destination(visitor, self->model->thick_shell10.dispersion, disp_dict);
542
543       
544        // Get input and determine whether we have to supply a 1D or 2D return value.
545        if ( !PyArg_ParseTuple(args,"O",&pars) ) {
546            PyErr_SetString(COnionModelError, 
547                "COnionModel.run expects a q value.");
548                return NULL;
549        }
550         
551        // Check params
552        if( PyList_Check(pars)==1) {
553               
554                // Length of list should be 2 for I(q,phi)
555            npars = PyList_GET_SIZE(pars); 
556            if(npars!=2) {
557                PyErr_SetString(COnionModelError, 
558                        "COnionModel.run expects a double or a list of dimension 2.");
559                return NULL;
560            }
561            // We have a vector q, get the q and phi values at which
562            // to evaluate I(q,phi)
563            q_value = COnionModel_readDouble(PyList_GET_ITEM(pars,0));
564            phi_value = COnionModel_readDouble(PyList_GET_ITEM(pars,1));
565            // Skip zero
566            if (q_value==0) {
567                return Py_BuildValue("d",0.0);
568            }
569                return Py_BuildValue("d",(*(self->model)).evaluate_rphi(q_value,phi_value));
570
571        } else {
572
573                // We have a scalar q, we will evaluate I(q)
574                q_value = COnionModel_readDouble(pars);         
575               
576                return Py_BuildValue("d",(*(self->model))(q_value));
577        }       
578}
579/**
580 * Function to call to calculate_ER
581 * @return: effective radius value
582 */
583static PyObject * calculate_ER(COnionModel *self) {
584
585        // Get parameters
586       
587            // Reader parameter dictionary
588    self->model->thick_shell4 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thick_shell4") );
589    self->model->thick_shell5 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thick_shell5") );
590    self->model->thick_shell9 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thick_shell9") );
591    self->model->thick_shell7 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thick_shell7") );
592    self->model->sld_core0 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_core0") );
593    self->model->rad_core0 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "rad_core0") );
594    self->model->A_shell10 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "A_shell10") );
595    self->model->thick_shell1 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thick_shell1") );
596    self->model->func_shell4 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "func_shell4") );
597    self->model->func_shell5 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "func_shell5") );
598    self->model->func_shell6 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "func_shell6") );
599    self->model->scale = PyFloat_AsDouble( PyDict_GetItemString(self->params, "scale") );
600    self->model->func_shell1 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "func_shell1") );
601    self->model->thick_shell10 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thick_shell10") );
602    self->model->func_shell3 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "func_shell3") );
603    self->model->func_shell8 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "func_shell8") );
604    self->model->func_shell9 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "func_shell9") );
605    self->model->sld_in_shell5 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_in_shell5") );
606    self->model->func_shell10 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "func_shell10") );
607    self->model->sld_in_shell4 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_in_shell4") );
608    self->model->sld_in_shell7 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_in_shell7") );
609    self->model->sld_out_shell9 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_out_shell9") );
610    self->model->sld_out_shell8 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_out_shell8") );
611    self->model->thick_shell6 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thick_shell6") );
612    self->model->sld_out_shell2 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_out_shell2") );
613    self->model->sld_in_shell9 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_in_shell9") );
614    self->model->sld_in_shell8 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_in_shell8") );
615    self->model->thick_shell2 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thick_shell2") );
616    self->model->thick_shell3 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thick_shell3") );
617    self->model->sld_out_shell1 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_out_shell1") );
618    self->model->sld_out_shell5 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_out_shell5") );
619    self->model->sld_out_shell3 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_out_shell3") );
620    self->model->sld_in_shell6 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_in_shell6") );
621    self->model->sld_in_shell1 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_in_shell1") );
622    self->model->sld_out_shell4 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_out_shell4") );
623    self->model->sld_in_shell3 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_in_shell3") );
624    self->model->sld_out_shell6 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_out_shell6") );
625    self->model->func_shell7 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "func_shell7") );
626    self->model->sld_out_shell7 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_out_shell7") );
627    self->model->background = PyFloat_AsDouble( PyDict_GetItemString(self->params, "background") );
628    self->model->n_shells = PyFloat_AsDouble( PyDict_GetItemString(self->params, "n_shells") );
629    self->model->sld_in_shell2 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_in_shell2") );
630    self->model->sld_solv = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_solv") );
631    self->model->func_shell2 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "func_shell2") );
632    self->model->thick_shell8 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thick_shell8") );
633    self->model->sld_out_shell10 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_out_shell10") );
634    self->model->sld_in_shell10 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_in_shell10") );
635    self->model->A_shell8 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "A_shell8") );
636    self->model->A_shell9 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "A_shell9") );
637    self->model->A_shell6 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "A_shell6") );
638    self->model->A_shell7 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "A_shell7") );
639    self->model->A_shell4 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "A_shell4") );
640    self->model->A_shell5 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "A_shell5") );
641    self->model->A_shell2 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "A_shell2") );
642    self->model->A_shell3 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "A_shell3") );
643    self->model->A_shell1 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "A_shell1") );
644    // Read in dispersion parameters
645    PyObject* disp_dict;
646    DispersionVisitor* visitor = new DispersionVisitor();
647    disp_dict = PyDict_GetItemString(self->dispersion, "rad_core0");
648    self->model->rad_core0.dispersion->accept_as_destination(visitor, self->model->rad_core0.dispersion, disp_dict);
649    disp_dict = PyDict_GetItemString(self->dispersion, "thick_shell1");
650    self->model->thick_shell1.dispersion->accept_as_destination(visitor, self->model->thick_shell1.dispersion, disp_dict);
651    disp_dict = PyDict_GetItemString(self->dispersion, "thick_shell2");
652    self->model->thick_shell2.dispersion->accept_as_destination(visitor, self->model->thick_shell2.dispersion, disp_dict);
653    disp_dict = PyDict_GetItemString(self->dispersion, "thick_shell3");
654    self->model->thick_shell3.dispersion->accept_as_destination(visitor, self->model->thick_shell3.dispersion, disp_dict);
655    disp_dict = PyDict_GetItemString(self->dispersion, "thick_shell4");
656    self->model->thick_shell4.dispersion->accept_as_destination(visitor, self->model->thick_shell4.dispersion, disp_dict);
657    disp_dict = PyDict_GetItemString(self->dispersion, "thick_shell5");
658    self->model->thick_shell5.dispersion->accept_as_destination(visitor, self->model->thick_shell5.dispersion, disp_dict);
659    disp_dict = PyDict_GetItemString(self->dispersion, "thick_shell6");
660    self->model->thick_shell6.dispersion->accept_as_destination(visitor, self->model->thick_shell6.dispersion, disp_dict);
661    disp_dict = PyDict_GetItemString(self->dispersion, "thick_shell7");
662    self->model->thick_shell7.dispersion->accept_as_destination(visitor, self->model->thick_shell7.dispersion, disp_dict);
663    disp_dict = PyDict_GetItemString(self->dispersion, "thick_shell8");
664    self->model->thick_shell8.dispersion->accept_as_destination(visitor, self->model->thick_shell8.dispersion, disp_dict);
665    disp_dict = PyDict_GetItemString(self->dispersion, "thick_shell9");
666    self->model->thick_shell9.dispersion->accept_as_destination(visitor, self->model->thick_shell9.dispersion, disp_dict);
667    disp_dict = PyDict_GetItemString(self->dispersion, "thick_shell10");
668    self->model->thick_shell10.dispersion->accept_as_destination(visitor, self->model->thick_shell10.dispersion, disp_dict);
669
670               
671        return Py_BuildValue("d",(*(self->model)).calculate_ER());
672
673}
674/**
675 * Function to call to evaluate model in cartesian coordinates
676 * @param args: input q or [qx, qy]]
677 * @return: function value
678 */
679static PyObject * runXY(COnionModel *self, PyObject *args) {
680        double qx_value, qy_value;
681        PyObject* pars;
682        int npars;
683       
684        // Get parameters
685       
686            // Reader parameter dictionary
687    self->model->thick_shell4 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thick_shell4") );
688    self->model->thick_shell5 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thick_shell5") );
689    self->model->thick_shell9 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thick_shell9") );
690    self->model->thick_shell7 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thick_shell7") );
691    self->model->sld_core0 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_core0") );
692    self->model->rad_core0 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "rad_core0") );
693    self->model->A_shell10 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "A_shell10") );
694    self->model->thick_shell1 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thick_shell1") );
695    self->model->func_shell4 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "func_shell4") );
696    self->model->func_shell5 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "func_shell5") );
697    self->model->func_shell6 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "func_shell6") );
698    self->model->scale = PyFloat_AsDouble( PyDict_GetItemString(self->params, "scale") );
699    self->model->func_shell1 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "func_shell1") );
700    self->model->thick_shell10 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thick_shell10") );
701    self->model->func_shell3 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "func_shell3") );
702    self->model->func_shell8 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "func_shell8") );
703    self->model->func_shell9 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "func_shell9") );
704    self->model->sld_in_shell5 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_in_shell5") );
705    self->model->func_shell10 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "func_shell10") );
706    self->model->sld_in_shell4 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_in_shell4") );
707    self->model->sld_in_shell7 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_in_shell7") );
708    self->model->sld_out_shell9 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_out_shell9") );
709    self->model->sld_out_shell8 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_out_shell8") );
710    self->model->thick_shell6 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thick_shell6") );
711    self->model->sld_out_shell2 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_out_shell2") );
712    self->model->sld_in_shell9 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_in_shell9") );
713    self->model->sld_in_shell8 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_in_shell8") );
714    self->model->thick_shell2 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thick_shell2") );
715    self->model->thick_shell3 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thick_shell3") );
716    self->model->sld_out_shell1 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_out_shell1") );
717    self->model->sld_out_shell5 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_out_shell5") );
718    self->model->sld_out_shell3 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_out_shell3") );
719    self->model->sld_in_shell6 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_in_shell6") );
720    self->model->sld_in_shell1 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_in_shell1") );
721    self->model->sld_out_shell4 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_out_shell4") );
722    self->model->sld_in_shell3 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_in_shell3") );
723    self->model->sld_out_shell6 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_out_shell6") );
724    self->model->func_shell7 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "func_shell7") );
725    self->model->sld_out_shell7 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_out_shell7") );
726    self->model->background = PyFloat_AsDouble( PyDict_GetItemString(self->params, "background") );
727    self->model->n_shells = PyFloat_AsDouble( PyDict_GetItemString(self->params, "n_shells") );
728    self->model->sld_in_shell2 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_in_shell2") );
729    self->model->sld_solv = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_solv") );
730    self->model->func_shell2 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "func_shell2") );
731    self->model->thick_shell8 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thick_shell8") );
732    self->model->sld_out_shell10 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_out_shell10") );
733    self->model->sld_in_shell10 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_in_shell10") );
734    self->model->A_shell8 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "A_shell8") );
735    self->model->A_shell9 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "A_shell9") );
736    self->model->A_shell6 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "A_shell6") );
737    self->model->A_shell7 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "A_shell7") );
738    self->model->A_shell4 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "A_shell4") );
739    self->model->A_shell5 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "A_shell5") );
740    self->model->A_shell2 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "A_shell2") );
741    self->model->A_shell3 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "A_shell3") );
742    self->model->A_shell1 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "A_shell1") );
743    // Read in dispersion parameters
744    PyObject* disp_dict;
745    DispersionVisitor* visitor = new DispersionVisitor();
746    disp_dict = PyDict_GetItemString(self->dispersion, "rad_core0");
747    self->model->rad_core0.dispersion->accept_as_destination(visitor, self->model->rad_core0.dispersion, disp_dict);
748    disp_dict = PyDict_GetItemString(self->dispersion, "thick_shell1");
749    self->model->thick_shell1.dispersion->accept_as_destination(visitor, self->model->thick_shell1.dispersion, disp_dict);
750    disp_dict = PyDict_GetItemString(self->dispersion, "thick_shell2");
751    self->model->thick_shell2.dispersion->accept_as_destination(visitor, self->model->thick_shell2.dispersion, disp_dict);
752    disp_dict = PyDict_GetItemString(self->dispersion, "thick_shell3");
753    self->model->thick_shell3.dispersion->accept_as_destination(visitor, self->model->thick_shell3.dispersion, disp_dict);
754    disp_dict = PyDict_GetItemString(self->dispersion, "thick_shell4");
755    self->model->thick_shell4.dispersion->accept_as_destination(visitor, self->model->thick_shell4.dispersion, disp_dict);
756    disp_dict = PyDict_GetItemString(self->dispersion, "thick_shell5");
757    self->model->thick_shell5.dispersion->accept_as_destination(visitor, self->model->thick_shell5.dispersion, disp_dict);
758    disp_dict = PyDict_GetItemString(self->dispersion, "thick_shell6");
759    self->model->thick_shell6.dispersion->accept_as_destination(visitor, self->model->thick_shell6.dispersion, disp_dict);
760    disp_dict = PyDict_GetItemString(self->dispersion, "thick_shell7");
761    self->model->thick_shell7.dispersion->accept_as_destination(visitor, self->model->thick_shell7.dispersion, disp_dict);
762    disp_dict = PyDict_GetItemString(self->dispersion, "thick_shell8");
763    self->model->thick_shell8.dispersion->accept_as_destination(visitor, self->model->thick_shell8.dispersion, disp_dict);
764    disp_dict = PyDict_GetItemString(self->dispersion, "thick_shell9");
765    self->model->thick_shell9.dispersion->accept_as_destination(visitor, self->model->thick_shell9.dispersion, disp_dict);
766    disp_dict = PyDict_GetItemString(self->dispersion, "thick_shell10");
767    self->model->thick_shell10.dispersion->accept_as_destination(visitor, self->model->thick_shell10.dispersion, disp_dict);
768
769       
770        // Get input and determine whether we have to supply a 1D or 2D return value.
771        if ( !PyArg_ParseTuple(args,"O",&pars) ) {
772            PyErr_SetString(COnionModelError, 
773                "COnionModel.run expects a q value.");
774                return NULL;
775        }
776         
777        // Check params
778        if( PyList_Check(pars)==1) {
779               
780                // Length of list should be 2 for I(qx, qy))
781            npars = PyList_GET_SIZE(pars); 
782            if(npars!=2) {
783                PyErr_SetString(COnionModelError, 
784                        "COnionModel.run expects a double or a list of dimension 2.");
785                return NULL;
786            }
787            // We have a vector q, get the qx and qy values at which
788            // to evaluate I(qx,qy)
789            qx_value = COnionModel_readDouble(PyList_GET_ITEM(pars,0));
790            qy_value = COnionModel_readDouble(PyList_GET_ITEM(pars,1));
791            return Py_BuildValue("d",(*(self->model))(qx_value,qy_value));
792
793        } else {
794
795                // We have a scalar q, we will evaluate I(q)
796                qx_value = COnionModel_readDouble(pars);               
797               
798                return Py_BuildValue("d",(*(self->model))(qx_value));
799        }       
800}
801
802static PyObject * reset(COnionModel *self, PyObject *args) {
803   
804
805    return Py_BuildValue("d",0.0);
806}
807
808static PyObject * set_dispersion(COnionModel *self, PyObject *args) {
809        PyObject * disp;
810        const char * par_name;
811
812        if ( !PyArg_ParseTuple(args,"sO", &par_name, &disp) ) {
813            PyErr_SetString(COnionModelError,
814                "COnionModel.set_dispersion expects a DispersionModel object.");
815                return NULL;
816        }
817        void *temp = PyCObject_AsVoidPtr(disp);
818        DispersionModel * dispersion = static_cast<DispersionModel *>(temp);
819
820
821        // Ugliness necessary to go from python to C
822            // TODO: refactor this
823    if (!strcmp(par_name, "rad_core0")) {
824        self->model->rad_core0.dispersion = dispersion;
825    } else    if (!strcmp(par_name, "thick_shell1")) {
826        self->model->thick_shell1.dispersion = dispersion;
827    } else    if (!strcmp(par_name, "thick_shell2")) {
828        self->model->thick_shell2.dispersion = dispersion;
829    } else    if (!strcmp(par_name, "thick_shell3")) {
830        self->model->thick_shell3.dispersion = dispersion;
831    } else    if (!strcmp(par_name, "thick_shell4")) {
832        self->model->thick_shell4.dispersion = dispersion;
833    } else    if (!strcmp(par_name, "thick_shell5")) {
834        self->model->thick_shell5.dispersion = dispersion;
835    } else    if (!strcmp(par_name, "thick_shell6")) {
836        self->model->thick_shell6.dispersion = dispersion;
837    } else    if (!strcmp(par_name, "thick_shell7")) {
838        self->model->thick_shell7.dispersion = dispersion;
839    } else    if (!strcmp(par_name, "thick_shell8")) {
840        self->model->thick_shell8.dispersion = dispersion;
841    } else    if (!strcmp(par_name, "thick_shell9")) {
842        self->model->thick_shell9.dispersion = dispersion;
843    } else    if (!strcmp(par_name, "thick_shell10")) {
844        self->model->thick_shell10.dispersion = dispersion;
845    } else {
846            PyErr_SetString(COnionModelError,
847                "COnionModel.set_dispersion expects a valid parameter name.");
848                return NULL;
849        }
850
851        DispersionVisitor* visitor = new DispersionVisitor();
852        PyObject * disp_dict = PyDict_New();
853        dispersion->accept_as_source(visitor, dispersion, disp_dict);
854        PyDict_SetItemString(self->dispersion, par_name, disp_dict);
855    return Py_BuildValue("i",1);
856}
857
858
859static PyMethodDef COnionModel_methods[] = {
860    {"run",      (PyCFunction)run     , METH_VARARGS,
861      "Evaluate the model at a given Q or Q, phi"},
862    {"runXY",      (PyCFunction)runXY     , METH_VARARGS,
863      "Evaluate the model at a given Q or Qx, Qy"},
864    {"calculate_ER",      (PyCFunction)calculate_ER     , METH_VARARGS,
865      "Evaluate the model at a given Q or Q, phi"},
866     
867    {"evalDistribution",  (PyCFunction)evalDistribution , METH_VARARGS,
868      "Evaluate the model at a given Q or Qx, Qy vector "},
869    {"reset",    (PyCFunction)reset   , METH_VARARGS,
870      "Reset pair correlation"},
871    {"set_dispersion",      (PyCFunction)set_dispersion     , METH_VARARGS,
872      "Set the dispersion model for a given parameter"},
873   {NULL}
874};
875
876static PyTypeObject COnionModelType = {
877    PyObject_HEAD_INIT(NULL)
878    0,                         /*ob_size*/
879    "COnionModel",             /*tp_name*/
880    sizeof(COnionModel),             /*tp_basicsize*/
881    0,                         /*tp_itemsize*/
882    (destructor)COnionModel_dealloc, /*tp_dealloc*/
883    0,                         /*tp_print*/
884    0,                         /*tp_getattr*/
885    0,                         /*tp_setattr*/
886    0,                         /*tp_compare*/
887    0,                         /*tp_repr*/
888    0,                         /*tp_as_number*/
889    0,                         /*tp_as_sequence*/
890    0,                         /*tp_as_mapping*/
891    0,                         /*tp_hash */
892    0,                         /*tp_call*/
893    0,                         /*tp_str*/
894    0,                         /*tp_getattro*/
895    0,                         /*tp_setattro*/
896    0,                         /*tp_as_buffer*/
897    Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
898    "COnionModel objects",           /* tp_doc */
899    0,                         /* tp_traverse */
900    0,                         /* tp_clear */
901    0,                         /* tp_richcompare */
902    0,                         /* tp_weaklistoffset */
903    0,                         /* tp_iter */
904    0,                         /* tp_iternext */
905    COnionModel_methods,             /* tp_methods */
906    COnionModel_members,             /* tp_members */
907    0,                         /* tp_getset */
908    0,                         /* tp_base */
909    0,                         /* tp_dict */
910    0,                         /* tp_descr_get */
911    0,                         /* tp_descr_set */
912    0,                         /* tp_dictoffset */
913    (initproc)COnionModel_init,      /* tp_init */
914    0,                         /* tp_alloc */
915    COnionModel_new,                 /* tp_new */
916};
917
918
919//static PyMethodDef module_methods[] = {
920//    {NULL}
921//};
922
923/**
924 * Function used to add the model class to a module
925 * @param module: module to add the class to
926 */ 
927void addCOnionModel(PyObject *module) {
928        PyObject *d;
929       
930    if (PyType_Ready(&COnionModelType) < 0)
931        return;
932
933    Py_INCREF(&COnionModelType);
934    PyModule_AddObject(module, "COnionModel", (PyObject *)&COnionModelType);
935   
936    d = PyModule_GetDict(module);
937    static char error_name[] = "COnionModel.error";
938    COnionModelError = PyErr_NewException(error_name, NULL, NULL);
939    PyDict_SetItemString(d, "COnionModelError", COnionModelError);
940}
941
Note: See TracBrowser for help on using the repository browser.