source: sasview/sansmodels/src/sans/models/c_models/CSphereSLDModel.cpp @ 76b5220

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 76b5220 was 7a8faf8, checked in by Jae Cho <jhjcho@…>, 13 years ago

Added polydispersion in a couple of params in spheresldmodel

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