source: sasview/sansmodels/src/python_wrapper/CCoreFourShellModel.cpp @ dd60b45

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

Reorganizing models in preparation of cpp cleanup

  • Property mode set to 100644
File size: 27.9 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/** CCoreFourShellModel
16 *
17 * C extension
18 *
19 * WARNING: THIS FILE WAS GENERATED BY WRAPPERGENERATOR.PY
20 *          DO NOT MODIFY THIS FILE, MODIFY corefourshell.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 "corefourshell.h"
36}
37
38#include "models.hh"
39#include "dispersion_visitor.hh"
40
41/// Error object for raised exceptions
42static PyObject * CCoreFourShellModelError = 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    CoreFourShellModel * model;
54    /// Log for unit testing
55    PyObject * log;
56} CCoreFourShellModel;
57
58
59static void
60CCoreFourShellModel_dealloc(CCoreFourShellModel* 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 *
72CCoreFourShellModel_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
73{
74    CCoreFourShellModel *self;
75   
76    self = (CCoreFourShellModel *)type->tp_alloc(type, 0);
77   
78    return (PyObject *)self;
79}
80
81static int
82CCoreFourShellModel_init(CCoreFourShellModel *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 CoreFourShellModel();
90       
91        // Initialize parameter dictionary
92        PyDict_SetItemString(self->params,"thick_shell4",Py_BuildValue("d",10.000000000000));
93        PyDict_SetItemString(self->params,"sld_shell4",Py_BuildValue("d",0.000004000000));
94        PyDict_SetItemString(self->params,"scale",Py_BuildValue("d",1.000000000000));
95        PyDict_SetItemString(self->params,"sld_shell2",Py_BuildValue("d",0.000002000000));
96        PyDict_SetItemString(self->params,"thick_shell1",Py_BuildValue("d",10.000000000000));
97        PyDict_SetItemString(self->params,"thick_shell2",Py_BuildValue("d",10.000000000000));
98        PyDict_SetItemString(self->params,"sld_shell1",Py_BuildValue("d",0.000001000000));
99        PyDict_SetItemString(self->params,"thick_shell3",Py_BuildValue("d",10.000000000000));
100        PyDict_SetItemString(self->params,"sld_core0",Py_BuildValue("d",0.000006400000));
101        PyDict_SetItemString(self->params,"background",Py_BuildValue("d",0.001000000000));
102        PyDict_SetItemString(self->params,"rad_core0",Py_BuildValue("d",60.000000000000));
103        PyDict_SetItemString(self->params,"sld_solv",Py_BuildValue("d",0.000006400000));
104        PyDict_SetItemString(self->params,"sld_shell3",Py_BuildValue("d",0.000003000000));
105        // Initialize dispersion / averaging parameter dict
106        DispersionVisitor* visitor = new DispersionVisitor();
107        PyObject * disp_dict;
108        disp_dict = PyDict_New();
109        self->model->rad_core0.dispersion->accept_as_source(visitor, self->model->rad_core0.dispersion, disp_dict);
110        PyDict_SetItemString(self->dispersion, "rad_core0", disp_dict);
111        disp_dict = PyDict_New();
112        self->model->thick_shell1.dispersion->accept_as_source(visitor, self->model->thick_shell1.dispersion, disp_dict);
113        PyDict_SetItemString(self->dispersion, "thick_shell1", disp_dict);
114        disp_dict = PyDict_New();
115        self->model->thick_shell2.dispersion->accept_as_source(visitor, self->model->thick_shell2.dispersion, disp_dict);
116        PyDict_SetItemString(self->dispersion, "thick_shell2", disp_dict);
117        disp_dict = PyDict_New();
118        self->model->thick_shell3.dispersion->accept_as_source(visitor, self->model->thick_shell3.dispersion, disp_dict);
119        PyDict_SetItemString(self->dispersion, "thick_shell3", disp_dict);
120        disp_dict = PyDict_New();
121        self->model->thick_shell4.dispersion->accept_as_source(visitor, self->model->thick_shell4.dispersion, disp_dict);
122        PyDict_SetItemString(self->dispersion, "thick_shell4", disp_dict);
123
124
125         
126        // Create empty log
127        self->log = PyDict_New();
128       
129       
130
131    }
132    return 0;
133}
134
135static char name_params[] = "params";
136static char def_params[] = "Parameters";
137static char name_dispersion[] = "dispersion";
138static char def_dispersion[] = "Dispersion parameters";
139static char name_log[] = "log";
140static char def_log[] = "Log";
141
142static PyMemberDef CCoreFourShellModel_members[] = {
143    {name_params, T_OBJECT, offsetof(CCoreFourShellModel, params), 0, def_params},
144        {name_dispersion, T_OBJECT, offsetof(CCoreFourShellModel, dispersion), 0, def_dispersion},     
145    {name_log, T_OBJECT, offsetof(CCoreFourShellModel, log), 0, def_log},
146    {NULL}  /* Sentinel */
147};
148
149/** Read double from PyObject
150    @param p PyObject
151    @return double
152*/
153double CCoreFourShellModel_readDouble(PyObject *p) {
154    if (PyFloat_Check(p)==1) {
155        return (double)(((PyFloatObject *)(p))->ob_fval);
156    } else if (PyInt_Check(p)==1) {
157        return (double)(((PyIntObject *)(p))->ob_ival);
158    } else if (PyLong_Check(p)==1) {
159        return (double)PyLong_AsLong(p);
160    } else {
161        return 0.0;
162    }
163}
164/**
165 * Function to call to evaluate model
166 * @param args: input numpy array q[]
167 * @return: numpy array object
168 */
169 
170static PyObject *evaluateOneDim(CoreFourShellModel* model, PyArrayObject *q){
171    PyArrayObject *result;
172   
173    // Check validity of array q , q must be of dimension 1, an array of double
174    if (q->nd != 1 || q->descr->type_num != PyArray_DOUBLE)
175    {
176        //const char * message= "Invalid array: q->nd=%d,type_num=%d\n",q->nd,q->descr->type_num;
177        //PyErr_SetString(PyExc_ValueError , message);
178        return NULL;
179    }
180    result = (PyArrayObject *)PyArray_FromDims(q->nd, (int *)(q->dimensions), PyArray_DOUBLE);
181        if (result == NULL) {
182        const char * message= "Could not create result ";
183        PyErr_SetString(PyExc_RuntimeError , message);
184                return NULL;
185        }
186#pragma omp parallel for
187         for (int i = 0; i < q->dimensions[0]; i++){
188      double q_value  = *(double *)(q->data + i*q->strides[0]);
189      double *result_value = (double *)(result->data + i*result->strides[0]);
190      *result_value =(*model)(q_value);
191        }
192    return PyArray_Return(result); 
193 }
194
195 /**
196 * Function to call to evaluate model
197 * @param args: input numpy array  [x[],y[]]
198 * @return: numpy array object
199 */
200 static PyObject * evaluateTwoDimXY( CoreFourShellModel* model, 
201                              PyArrayObject *x, PyArrayObject *y)
202 {
203    PyArrayObject *result;
204    int x_len, y_len, dims[1];
205    //check validity of input vectors
206    if (x->nd != 1 || x->descr->type_num != PyArray_DOUBLE
207        || y->nd != 1 || y->descr->type_num != PyArray_DOUBLE
208        || y->dimensions[0] != x->dimensions[0]){
209        const char * message= "evaluateTwoDimXY  expect 2 numpy arrays";
210        PyErr_SetString(PyExc_ValueError , message); 
211        return NULL;
212    }
213   
214        if (PyArray_Check(x) && PyArray_Check(y)) {
215               
216            x_len = dims[0]= x->dimensions[0];
217        y_len = dims[0]= y->dimensions[0];
218           
219            // Make a new double matrix of same dims
220        result=(PyArrayObject *) PyArray_FromDims(1,dims,NPY_DOUBLE);
221        if (result == NULL){
222            const char * message= "Could not create result ";
223        PyErr_SetString(PyExc_RuntimeError , message);
224            return NULL;
225            }
226       
227        /* Do the calculation. */
228#pragma omp parallel for
229        for (int i=0; i< x_len; i++) {
230            double x_value = *(double *)(x->data + i*x->strides[0]);
231                    double y_value = *(double *)(y->data + i*y->strides[0]);
232                        double *result_value = (double *)(result->data +
233                              i*result->strides[0]);
234                        *result_value = (*model)(x_value, y_value);
235        }           
236        return PyArray_Return(result); 
237       
238        }else{
239                    PyErr_SetString(CCoreFourShellModelError, 
240                   "CCoreFourShellModel.evaluateTwoDimXY couldn't run.");
241                return NULL;
242                }       
243}
244/**
245 *  evalDistribution function evaluate a model function with input vector
246 *  @param args: input q as vector or [qx, qy] where qx, qy are vectors
247 *
248 */ 
249static PyObject * evalDistribution(CCoreFourShellModel *self, PyObject *args){
250        PyObject *qx, *qy;
251        PyArrayObject * pars;
252        int npars ,mpars;
253       
254        // Get parameters
255       
256            // Reader parameter dictionary
257    self->model->thick_shell4 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thick_shell4") );
258    self->model->sld_shell4 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_shell4") );
259    self->model->scale = PyFloat_AsDouble( PyDict_GetItemString(self->params, "scale") );
260    self->model->sld_shell2 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_shell2") );
261    self->model->thick_shell1 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thick_shell1") );
262    self->model->thick_shell2 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thick_shell2") );
263    self->model->sld_shell1 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_shell1") );
264    self->model->thick_shell3 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thick_shell3") );
265    self->model->sld_core0 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_core0") );
266    self->model->background = PyFloat_AsDouble( PyDict_GetItemString(self->params, "background") );
267    self->model->rad_core0 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "rad_core0") );
268    self->model->sld_solv = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_solv") );
269    self->model->sld_shell3 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_shell3") );
270    // Read in dispersion parameters
271    PyObject* disp_dict;
272    DispersionVisitor* visitor = new DispersionVisitor();
273    disp_dict = PyDict_GetItemString(self->dispersion, "rad_core0");
274    self->model->rad_core0.dispersion->accept_as_destination(visitor, self->model->rad_core0.dispersion, disp_dict);
275    disp_dict = PyDict_GetItemString(self->dispersion, "thick_shell1");
276    self->model->thick_shell1.dispersion->accept_as_destination(visitor, self->model->thick_shell1.dispersion, disp_dict);
277    disp_dict = PyDict_GetItemString(self->dispersion, "thick_shell2");
278    self->model->thick_shell2.dispersion->accept_as_destination(visitor, self->model->thick_shell2.dispersion, disp_dict);
279    disp_dict = PyDict_GetItemString(self->dispersion, "thick_shell3");
280    self->model->thick_shell3.dispersion->accept_as_destination(visitor, self->model->thick_shell3.dispersion, disp_dict);
281    disp_dict = PyDict_GetItemString(self->dispersion, "thick_shell4");
282    self->model->thick_shell4.dispersion->accept_as_destination(visitor, self->model->thick_shell4.dispersion, disp_dict);
283
284       
285        // Get input and determine whether we have to supply a 1D or 2D return value.
286        if ( !PyArg_ParseTuple(args,"O",&pars) ) {
287            PyErr_SetString(CCoreFourShellModelError, 
288                "CCoreFourShellModel.evalDistribution expects a q value.");
289                return NULL;
290        }
291    // Check params
292       
293    if(PyArray_Check(pars)==1) {
294               
295            // Length of list should 1 or 2
296            npars = pars->nd; 
297            if(npars==1) {
298                // input is a numpy array
299                if (PyArray_Check(pars)) {
300                        return evaluateOneDim(self->model, (PyArrayObject*)pars); 
301                    }
302                }else{
303                    PyErr_SetString(CCoreFourShellModelError, 
304                   "CCoreFourShellModel.evalDistribution expect numpy array of one dimension.");
305                return NULL;
306                }
307    }else if( PyList_Check(pars)==1) {
308        // Length of list should be 2 for I(qx,qy)
309            mpars = PyList_GET_SIZE(pars); 
310            if(mpars!=2) {
311                PyErr_SetString(CCoreFourShellModelError, 
312                        "CCoreFourShellModel.evalDistribution expects a list of dimension 2.");
313                return NULL;
314            }
315             qx = PyList_GET_ITEM(pars,0);
316             qy = PyList_GET_ITEM(pars,1);
317             if (PyArray_Check(qx) && PyArray_Check(qy)) {
318                 return evaluateTwoDimXY(self->model, (PyArrayObject*)qx,
319                           (PyArrayObject*)qy);
320                 }else{
321                    PyErr_SetString(CCoreFourShellModelError, 
322                   "CCoreFourShellModel.evalDistribution expect 2 numpy arrays in list.");
323                return NULL;
324             }
325        }
326        PyErr_SetString(CCoreFourShellModelError, 
327                   "CCoreFourShellModel.evalDistribution couln't be run.");
328        return NULL;
329       
330}
331
332/**
333 * Function to call to evaluate model
334 * @param args: input q or [q,phi]
335 * @return: function value
336 */
337static PyObject * run(CCoreFourShellModel *self, PyObject *args) {
338        double q_value, phi_value;
339        PyObject* pars;
340        int npars;
341       
342        // Get parameters
343       
344            // Reader parameter dictionary
345    self->model->thick_shell4 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thick_shell4") );
346    self->model->sld_shell4 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_shell4") );
347    self->model->scale = PyFloat_AsDouble( PyDict_GetItemString(self->params, "scale") );
348    self->model->sld_shell2 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_shell2") );
349    self->model->thick_shell1 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thick_shell1") );
350    self->model->thick_shell2 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thick_shell2") );
351    self->model->sld_shell1 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_shell1") );
352    self->model->thick_shell3 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thick_shell3") );
353    self->model->sld_core0 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_core0") );
354    self->model->background = PyFloat_AsDouble( PyDict_GetItemString(self->params, "background") );
355    self->model->rad_core0 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "rad_core0") );
356    self->model->sld_solv = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_solv") );
357    self->model->sld_shell3 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_shell3") );
358    // Read in dispersion parameters
359    PyObject* disp_dict;
360    DispersionVisitor* visitor = new DispersionVisitor();
361    disp_dict = PyDict_GetItemString(self->dispersion, "rad_core0");
362    self->model->rad_core0.dispersion->accept_as_destination(visitor, self->model->rad_core0.dispersion, disp_dict);
363    disp_dict = PyDict_GetItemString(self->dispersion, "thick_shell1");
364    self->model->thick_shell1.dispersion->accept_as_destination(visitor, self->model->thick_shell1.dispersion, disp_dict);
365    disp_dict = PyDict_GetItemString(self->dispersion, "thick_shell2");
366    self->model->thick_shell2.dispersion->accept_as_destination(visitor, self->model->thick_shell2.dispersion, disp_dict);
367    disp_dict = PyDict_GetItemString(self->dispersion, "thick_shell3");
368    self->model->thick_shell3.dispersion->accept_as_destination(visitor, self->model->thick_shell3.dispersion, disp_dict);
369    disp_dict = PyDict_GetItemString(self->dispersion, "thick_shell4");
370    self->model->thick_shell4.dispersion->accept_as_destination(visitor, self->model->thick_shell4.dispersion, disp_dict);
371
372       
373        // Get input and determine whether we have to supply a 1D or 2D return value.
374        if ( !PyArg_ParseTuple(args,"O",&pars) ) {
375            PyErr_SetString(CCoreFourShellModelError, 
376                "CCoreFourShellModel.run expects a q value.");
377                return NULL;
378        }
379         
380        // Check params
381        if( PyList_Check(pars)==1) {
382               
383                // Length of list should be 2 for I(q,phi)
384            npars = PyList_GET_SIZE(pars); 
385            if(npars!=2) {
386                PyErr_SetString(CCoreFourShellModelError, 
387                        "CCoreFourShellModel.run expects a double or a list of dimension 2.");
388                return NULL;
389            }
390            // We have a vector q, get the q and phi values at which
391            // to evaluate I(q,phi)
392            q_value = CCoreFourShellModel_readDouble(PyList_GET_ITEM(pars,0));
393            phi_value = CCoreFourShellModel_readDouble(PyList_GET_ITEM(pars,1));
394            // Skip zero
395            if (q_value==0) {
396                return Py_BuildValue("d",0.0);
397            }
398                return Py_BuildValue("d",(*(self->model)).evaluate_rphi(q_value,phi_value));
399
400        } else {
401
402                // We have a scalar q, we will evaluate I(q)
403                q_value = CCoreFourShellModel_readDouble(pars);         
404               
405                return Py_BuildValue("d",(*(self->model))(q_value));
406        }       
407}
408/**
409 * Function to call to calculate_ER
410 * @return: effective radius value
411 */
412static PyObject * calculate_ER(CCoreFourShellModel *self) {
413
414        // Get parameters
415       
416            // Reader parameter dictionary
417    self->model->thick_shell4 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thick_shell4") );
418    self->model->sld_shell4 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_shell4") );
419    self->model->scale = PyFloat_AsDouble( PyDict_GetItemString(self->params, "scale") );
420    self->model->sld_shell2 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_shell2") );
421    self->model->thick_shell1 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thick_shell1") );
422    self->model->thick_shell2 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thick_shell2") );
423    self->model->sld_shell1 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_shell1") );
424    self->model->thick_shell3 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thick_shell3") );
425    self->model->sld_core0 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_core0") );
426    self->model->background = PyFloat_AsDouble( PyDict_GetItemString(self->params, "background") );
427    self->model->rad_core0 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "rad_core0") );
428    self->model->sld_solv = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_solv") );
429    self->model->sld_shell3 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_shell3") );
430    // Read in dispersion parameters
431    PyObject* disp_dict;
432    DispersionVisitor* visitor = new DispersionVisitor();
433    disp_dict = PyDict_GetItemString(self->dispersion, "rad_core0");
434    self->model->rad_core0.dispersion->accept_as_destination(visitor, self->model->rad_core0.dispersion, disp_dict);
435    disp_dict = PyDict_GetItemString(self->dispersion, "thick_shell1");
436    self->model->thick_shell1.dispersion->accept_as_destination(visitor, self->model->thick_shell1.dispersion, disp_dict);
437    disp_dict = PyDict_GetItemString(self->dispersion, "thick_shell2");
438    self->model->thick_shell2.dispersion->accept_as_destination(visitor, self->model->thick_shell2.dispersion, disp_dict);
439    disp_dict = PyDict_GetItemString(self->dispersion, "thick_shell3");
440    self->model->thick_shell3.dispersion->accept_as_destination(visitor, self->model->thick_shell3.dispersion, disp_dict);
441    disp_dict = PyDict_GetItemString(self->dispersion, "thick_shell4");
442    self->model->thick_shell4.dispersion->accept_as_destination(visitor, self->model->thick_shell4.dispersion, disp_dict);
443
444               
445        return Py_BuildValue("d",(*(self->model)).calculate_ER());
446
447}
448/**
449 * Function to call to evaluate model in cartesian coordinates
450 * @param args: input q or [qx, qy]]
451 * @return: function value
452 */
453static PyObject * runXY(CCoreFourShellModel *self, PyObject *args) {
454        double qx_value, qy_value;
455        PyObject* pars;
456        int npars;
457       
458        // Get parameters
459       
460            // Reader parameter dictionary
461    self->model->thick_shell4 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thick_shell4") );
462    self->model->sld_shell4 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_shell4") );
463    self->model->scale = PyFloat_AsDouble( PyDict_GetItemString(self->params, "scale") );
464    self->model->sld_shell2 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_shell2") );
465    self->model->thick_shell1 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thick_shell1") );
466    self->model->thick_shell2 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thick_shell2") );
467    self->model->sld_shell1 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_shell1") );
468    self->model->thick_shell3 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thick_shell3") );
469    self->model->sld_core0 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_core0") );
470    self->model->background = PyFloat_AsDouble( PyDict_GetItemString(self->params, "background") );
471    self->model->rad_core0 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "rad_core0") );
472    self->model->sld_solv = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_solv") );
473    self->model->sld_shell3 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_shell3") );
474    // Read in dispersion parameters
475    PyObject* disp_dict;
476    DispersionVisitor* visitor = new DispersionVisitor();
477    disp_dict = PyDict_GetItemString(self->dispersion, "rad_core0");
478    self->model->rad_core0.dispersion->accept_as_destination(visitor, self->model->rad_core0.dispersion, disp_dict);
479    disp_dict = PyDict_GetItemString(self->dispersion, "thick_shell1");
480    self->model->thick_shell1.dispersion->accept_as_destination(visitor, self->model->thick_shell1.dispersion, disp_dict);
481    disp_dict = PyDict_GetItemString(self->dispersion, "thick_shell2");
482    self->model->thick_shell2.dispersion->accept_as_destination(visitor, self->model->thick_shell2.dispersion, disp_dict);
483    disp_dict = PyDict_GetItemString(self->dispersion, "thick_shell3");
484    self->model->thick_shell3.dispersion->accept_as_destination(visitor, self->model->thick_shell3.dispersion, disp_dict);
485    disp_dict = PyDict_GetItemString(self->dispersion, "thick_shell4");
486    self->model->thick_shell4.dispersion->accept_as_destination(visitor, self->model->thick_shell4.dispersion, disp_dict);
487
488       
489        // Get input and determine whether we have to supply a 1D or 2D return value.
490        if ( !PyArg_ParseTuple(args,"O",&pars) ) {
491            PyErr_SetString(CCoreFourShellModelError, 
492                "CCoreFourShellModel.run expects a q value.");
493                return NULL;
494        }
495         
496        // Check params
497        if( PyList_Check(pars)==1) {
498               
499                // Length of list should be 2 for I(qx, qy))
500            npars = PyList_GET_SIZE(pars); 
501            if(npars!=2) {
502                PyErr_SetString(CCoreFourShellModelError, 
503                        "CCoreFourShellModel.run expects a double or a list of dimension 2.");
504                return NULL;
505            }
506            // We have a vector q, get the qx and qy values at which
507            // to evaluate I(qx,qy)
508            qx_value = CCoreFourShellModel_readDouble(PyList_GET_ITEM(pars,0));
509            qy_value = CCoreFourShellModel_readDouble(PyList_GET_ITEM(pars,1));
510            return Py_BuildValue("d",(*(self->model))(qx_value,qy_value));
511
512        } else {
513
514                // We have a scalar q, we will evaluate I(q)
515                qx_value = CCoreFourShellModel_readDouble(pars);               
516               
517                return Py_BuildValue("d",(*(self->model))(qx_value));
518        }       
519}
520
521static PyObject * reset(CCoreFourShellModel *self, PyObject *args) {
522   
523
524    return Py_BuildValue("d",0.0);
525}
526
527static PyObject * set_dispersion(CCoreFourShellModel *self, PyObject *args) {
528        PyObject * disp;
529        const char * par_name;
530
531        if ( !PyArg_ParseTuple(args,"sO", &par_name, &disp) ) {
532            PyErr_SetString(CCoreFourShellModelError,
533                "CCoreFourShellModel.set_dispersion expects a DispersionModel object.");
534                return NULL;
535        }
536        void *temp = PyCObject_AsVoidPtr(disp);
537        DispersionModel * dispersion = static_cast<DispersionModel *>(temp);
538
539
540        // Ugliness necessary to go from python to C
541            // TODO: refactor this
542    if (!strcmp(par_name, "rad_core0")) {
543        self->model->rad_core0.dispersion = dispersion;
544    } else    if (!strcmp(par_name, "thick_shell1")) {
545        self->model->thick_shell1.dispersion = dispersion;
546    } else    if (!strcmp(par_name, "thick_shell2")) {
547        self->model->thick_shell2.dispersion = dispersion;
548    } else    if (!strcmp(par_name, "thick_shell3")) {
549        self->model->thick_shell3.dispersion = dispersion;
550    } else    if (!strcmp(par_name, "thick_shell4")) {
551        self->model->thick_shell4.dispersion = dispersion;
552    } else {
553            PyErr_SetString(CCoreFourShellModelError,
554                "CCoreFourShellModel.set_dispersion expects a valid parameter name.");
555                return NULL;
556        }
557
558        DispersionVisitor* visitor = new DispersionVisitor();
559        PyObject * disp_dict = PyDict_New();
560        dispersion->accept_as_source(visitor, dispersion, disp_dict);
561        PyDict_SetItemString(self->dispersion, par_name, disp_dict);
562    return Py_BuildValue("i",1);
563}
564
565
566static PyMethodDef CCoreFourShellModel_methods[] = {
567    {"run",      (PyCFunction)run     , METH_VARARGS,
568      "Evaluate the model at a given Q or Q, phi"},
569    {"runXY",      (PyCFunction)runXY     , METH_VARARGS,
570      "Evaluate the model at a given Q or Qx, Qy"},
571    {"calculate_ER",      (PyCFunction)calculate_ER     , METH_VARARGS,
572      "Evaluate the model at a given Q or Q, phi"},
573     
574    {"evalDistribution",  (PyCFunction)evalDistribution , METH_VARARGS,
575      "Evaluate the model at a given Q or Qx, Qy vector "},
576    {"reset",    (PyCFunction)reset   , METH_VARARGS,
577      "Reset pair correlation"},
578    {"set_dispersion",      (PyCFunction)set_dispersion     , METH_VARARGS,
579      "Set the dispersion model for a given parameter"},
580   {NULL}
581};
582
583static PyTypeObject CCoreFourShellModelType = {
584    PyObject_HEAD_INIT(NULL)
585    0,                         /*ob_size*/
586    "CCoreFourShellModel",             /*tp_name*/
587    sizeof(CCoreFourShellModel),             /*tp_basicsize*/
588    0,                         /*tp_itemsize*/
589    (destructor)CCoreFourShellModel_dealloc, /*tp_dealloc*/
590    0,                         /*tp_print*/
591    0,                         /*tp_getattr*/
592    0,                         /*tp_setattr*/
593    0,                         /*tp_compare*/
594    0,                         /*tp_repr*/
595    0,                         /*tp_as_number*/
596    0,                         /*tp_as_sequence*/
597    0,                         /*tp_as_mapping*/
598    0,                         /*tp_hash */
599    0,                         /*tp_call*/
600    0,                         /*tp_str*/
601    0,                         /*tp_getattro*/
602    0,                         /*tp_setattro*/
603    0,                         /*tp_as_buffer*/
604    Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
605    "CCoreFourShellModel objects",           /* tp_doc */
606    0,                         /* tp_traverse */
607    0,                         /* tp_clear */
608    0,                         /* tp_richcompare */
609    0,                         /* tp_weaklistoffset */
610    0,                         /* tp_iter */
611    0,                         /* tp_iternext */
612    CCoreFourShellModel_methods,             /* tp_methods */
613    CCoreFourShellModel_members,             /* tp_members */
614    0,                         /* tp_getset */
615    0,                         /* tp_base */
616    0,                         /* tp_dict */
617    0,                         /* tp_descr_get */
618    0,                         /* tp_descr_set */
619    0,                         /* tp_dictoffset */
620    (initproc)CCoreFourShellModel_init,      /* tp_init */
621    0,                         /* tp_alloc */
622    CCoreFourShellModel_new,                 /* tp_new */
623};
624
625
626//static PyMethodDef module_methods[] = {
627//    {NULL}
628//};
629
630/**
631 * Function used to add the model class to a module
632 * @param module: module to add the class to
633 */ 
634void addCCoreFourShellModel(PyObject *module) {
635        PyObject *d;
636       
637    if (PyType_Ready(&CCoreFourShellModelType) < 0)
638        return;
639
640    Py_INCREF(&CCoreFourShellModelType);
641    PyModule_AddObject(module, "CCoreFourShellModel", (PyObject *)&CCoreFourShellModelType);
642   
643    d = PyModule_GetDict(module);
644    static char error_name[] = "CCoreFourShellModel.error";
645    CCoreFourShellModelError = PyErr_NewException(error_name, NULL, NULL);
646    PyDict_SetItemString(d, "CCoreFourShellModelError", CCoreFourShellModelError);
647}
648
Note: See TracBrowser for help on using the repository browser.