source: sasview/sansmodels/src/sans/models/c_models/CCoreFourShellModel.cpp @ 2170dee2

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 2170dee2 was c724ccd, checked in by Jae Cho <jhjcho@…>, 15 years ago

More models added and correction of Wrappergenerator on model parameter value precision

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