source: sasview/sansmodels/src/sans/models/c_models/CSphereSLDModel.cpp @ 916f5c0

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