source: sasview/sansmodels/src/sans/models/c_models/CCoreFourShellModel.cpp @ 2605da22

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 2605da22 was 2605da22, checked in by Mathieu Doucet <doucetm@…>, 12 years ago

Re #4 Still a few more warnings

  • 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), 
181                                                                                  PyArray_DOUBLE);
182        if (result == NULL) {
183        const char * message= "Could not create result ";
184        PyErr_SetString(PyExc_RuntimeError , message);
185                return NULL;
186        }
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 i, 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        for ( i=0; i< x_len; i++) {
229            double x_value = *(double *)(x->data + i*x->strides[0]);
230                    double y_value = *(double *)(y->data + i*y->strides[0]);
231                        double *result_value = (double *)(result->data +
232                              i*result->strides[0]);
233                        *result_value = (*model)(x_value, y_value);
234        }           
235        return PyArray_Return(result); 
236       
237        }else{
238                    PyErr_SetString(CCoreFourShellModelError, 
239                   "CCoreFourShellModel.evaluateTwoDimXY couldn't run.");
240                return NULL;
241                }       
242}
243/**
244 *  evalDistribution function evaluate a model function with input vector
245 *  @param args: input q as vector or [qx, qy] where qx, qy are vectors
246 *
247 */ 
248static PyObject * evalDistribution(CCoreFourShellModel *self, PyObject *args){
249        PyObject *qx, *qy;
250        PyArrayObject * pars;
251        int npars ,mpars;
252       
253        // Get parameters
254       
255            // Reader parameter dictionary
256    self->model->thick_shell4 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thick_shell4") );
257    self->model->sld_shell4 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_shell4") );
258    self->model->scale = PyFloat_AsDouble( PyDict_GetItemString(self->params, "scale") );
259    self->model->sld_shell2 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_shell2") );
260    self->model->thick_shell1 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thick_shell1") );
261    self->model->thick_shell2 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thick_shell2") );
262    self->model->sld_shell1 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_shell1") );
263    self->model->thick_shell3 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thick_shell3") );
264    self->model->sld_core0 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_core0") );
265    self->model->background = PyFloat_AsDouble( PyDict_GetItemString(self->params, "background") );
266    self->model->rad_core0 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "rad_core0") );
267    self->model->sld_solv = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_solv") );
268    self->model->sld_shell3 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_shell3") );
269    // Read in dispersion parameters
270    PyObject* disp_dict;
271    DispersionVisitor* visitor = new DispersionVisitor();
272    disp_dict = PyDict_GetItemString(self->dispersion, "rad_core0");
273    self->model->rad_core0.dispersion->accept_as_destination(visitor, self->model->rad_core0.dispersion, disp_dict);
274    disp_dict = PyDict_GetItemString(self->dispersion, "thick_shell1");
275    self->model->thick_shell1.dispersion->accept_as_destination(visitor, self->model->thick_shell1.dispersion, disp_dict);
276    disp_dict = PyDict_GetItemString(self->dispersion, "thick_shell2");
277    self->model->thick_shell2.dispersion->accept_as_destination(visitor, self->model->thick_shell2.dispersion, disp_dict);
278    disp_dict = PyDict_GetItemString(self->dispersion, "thick_shell3");
279    self->model->thick_shell3.dispersion->accept_as_destination(visitor, self->model->thick_shell3.dispersion, disp_dict);
280    disp_dict = PyDict_GetItemString(self->dispersion, "thick_shell4");
281    self->model->thick_shell4.dispersion->accept_as_destination(visitor, self->model->thick_shell4.dispersion, disp_dict);
282
283       
284        // Get input and determine whether we have to supply a 1D or 2D return value.
285        if ( !PyArg_ParseTuple(args,"O",&pars) ) {
286            PyErr_SetString(CCoreFourShellModelError, 
287                "CCoreFourShellModel.evalDistribution expects a q value.");
288                return NULL;
289        }
290    // Check params
291       
292    if(PyArray_Check(pars)==1) {
293               
294            // Length of list should 1 or 2
295            npars = pars->nd; 
296            if(npars==1) {
297                // input is a numpy array
298                if (PyArray_Check(pars)) {
299                        return evaluateOneDim(self->model, (PyArrayObject*)pars); 
300                    }
301                }else{
302                    PyErr_SetString(CCoreFourShellModelError, 
303                   "CCoreFourShellModel.evalDistribution expect numpy array of one dimension.");
304                return NULL;
305                }
306    }else if( PyList_Check(pars)==1) {
307        // Length of list should be 2 for I(qx,qy)
308            mpars = PyList_GET_SIZE(pars); 
309            if(mpars!=2) {
310                PyErr_SetString(CCoreFourShellModelError, 
311                        "CCoreFourShellModel.evalDistribution expects a list of dimension 2.");
312                return NULL;
313            }
314             qx = PyList_GET_ITEM(pars,0);
315             qy = PyList_GET_ITEM(pars,1);
316             if (PyArray_Check(qx) && PyArray_Check(qy)) {
317                 return evaluateTwoDimXY(self->model, (PyArrayObject*)qx,
318                           (PyArrayObject*)qy);
319                 }else{
320                    PyErr_SetString(CCoreFourShellModelError, 
321                   "CCoreFourShellModel.evalDistribution expect 2 numpy arrays in list.");
322                return NULL;
323             }
324        }
325        PyErr_SetString(CCoreFourShellModelError, 
326                   "CCoreFourShellModel.evalDistribution couln't be run.");
327        return NULL;
328       
329}
330
331/**
332 * Function to call to evaluate model
333 * @param args: input q or [q,phi]
334 * @return: function value
335 */
336static PyObject * run(CCoreFourShellModel *self, PyObject *args) {
337        double q_value, phi_value;
338        PyObject* pars;
339        int npars;
340       
341        // Get parameters
342       
343            // Reader parameter dictionary
344    self->model->thick_shell4 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thick_shell4") );
345    self->model->sld_shell4 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_shell4") );
346    self->model->scale = PyFloat_AsDouble( PyDict_GetItemString(self->params, "scale") );
347    self->model->sld_shell2 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_shell2") );
348    self->model->thick_shell1 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thick_shell1") );
349    self->model->thick_shell2 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thick_shell2") );
350    self->model->sld_shell1 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_shell1") );
351    self->model->thick_shell3 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thick_shell3") );
352    self->model->sld_core0 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_core0") );
353    self->model->background = PyFloat_AsDouble( PyDict_GetItemString(self->params, "background") );
354    self->model->rad_core0 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "rad_core0") );
355    self->model->sld_solv = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_solv") );
356    self->model->sld_shell3 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_shell3") );
357    // Read in dispersion parameters
358    PyObject* disp_dict;
359    DispersionVisitor* visitor = new DispersionVisitor();
360    disp_dict = PyDict_GetItemString(self->dispersion, "rad_core0");
361    self->model->rad_core0.dispersion->accept_as_destination(visitor, self->model->rad_core0.dispersion, disp_dict);
362    disp_dict = PyDict_GetItemString(self->dispersion, "thick_shell1");
363    self->model->thick_shell1.dispersion->accept_as_destination(visitor, self->model->thick_shell1.dispersion, disp_dict);
364    disp_dict = PyDict_GetItemString(self->dispersion, "thick_shell2");
365    self->model->thick_shell2.dispersion->accept_as_destination(visitor, self->model->thick_shell2.dispersion, disp_dict);
366    disp_dict = PyDict_GetItemString(self->dispersion, "thick_shell3");
367    self->model->thick_shell3.dispersion->accept_as_destination(visitor, self->model->thick_shell3.dispersion, disp_dict);
368    disp_dict = PyDict_GetItemString(self->dispersion, "thick_shell4");
369    self->model->thick_shell4.dispersion->accept_as_destination(visitor, self->model->thick_shell4.dispersion, disp_dict);
370
371       
372        // Get input and determine whether we have to supply a 1D or 2D return value.
373        if ( !PyArg_ParseTuple(args,"O",&pars) ) {
374            PyErr_SetString(CCoreFourShellModelError, 
375                "CCoreFourShellModel.run expects a q value.");
376                return NULL;
377        }
378         
379        // Check params
380        if( PyList_Check(pars)==1) {
381               
382                // Length of list should be 2 for I(q,phi)
383            npars = PyList_GET_SIZE(pars); 
384            if(npars!=2) {
385                PyErr_SetString(CCoreFourShellModelError, 
386                        "CCoreFourShellModel.run expects a double or a list of dimension 2.");
387                return NULL;
388            }
389            // We have a vector q, get the q and phi values at which
390            // to evaluate I(q,phi)
391            q_value = CCoreFourShellModel_readDouble(PyList_GET_ITEM(pars,0));
392            phi_value = CCoreFourShellModel_readDouble(PyList_GET_ITEM(pars,1));
393            // Skip zero
394            if (q_value==0) {
395                return Py_BuildValue("d",0.0);
396            }
397                return Py_BuildValue("d",(*(self->model)).evaluate_rphi(q_value,phi_value));
398
399        } else {
400
401                // We have a scalar q, we will evaluate I(q)
402                q_value = CCoreFourShellModel_readDouble(pars);         
403               
404                return Py_BuildValue("d",(*(self->model))(q_value));
405        }       
406}
407/**
408 * Function to call to calculate_ER
409 * @return: effective radius value
410 */
411static PyObject * calculate_ER(CCoreFourShellModel *self) {
412
413        // Get parameters
414       
415            // Reader parameter dictionary
416    self->model->thick_shell4 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thick_shell4") );
417    self->model->sld_shell4 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_shell4") );
418    self->model->scale = PyFloat_AsDouble( PyDict_GetItemString(self->params, "scale") );
419    self->model->sld_shell2 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_shell2") );
420    self->model->thick_shell1 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thick_shell1") );
421    self->model->thick_shell2 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thick_shell2") );
422    self->model->sld_shell1 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_shell1") );
423    self->model->thick_shell3 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thick_shell3") );
424    self->model->sld_core0 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_core0") );
425    self->model->background = PyFloat_AsDouble( PyDict_GetItemString(self->params, "background") );
426    self->model->rad_core0 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "rad_core0") );
427    self->model->sld_solv = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_solv") );
428    self->model->sld_shell3 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_shell3") );
429    // Read in dispersion parameters
430    PyObject* disp_dict;
431    DispersionVisitor* visitor = new DispersionVisitor();
432    disp_dict = PyDict_GetItemString(self->dispersion, "rad_core0");
433    self->model->rad_core0.dispersion->accept_as_destination(visitor, self->model->rad_core0.dispersion, disp_dict);
434    disp_dict = PyDict_GetItemString(self->dispersion, "thick_shell1");
435    self->model->thick_shell1.dispersion->accept_as_destination(visitor, self->model->thick_shell1.dispersion, disp_dict);
436    disp_dict = PyDict_GetItemString(self->dispersion, "thick_shell2");
437    self->model->thick_shell2.dispersion->accept_as_destination(visitor, self->model->thick_shell2.dispersion, disp_dict);
438    disp_dict = PyDict_GetItemString(self->dispersion, "thick_shell3");
439    self->model->thick_shell3.dispersion->accept_as_destination(visitor, self->model->thick_shell3.dispersion, disp_dict);
440    disp_dict = PyDict_GetItemString(self->dispersion, "thick_shell4");
441    self->model->thick_shell4.dispersion->accept_as_destination(visitor, self->model->thick_shell4.dispersion, disp_dict);
442
443               
444        return Py_BuildValue("d",(*(self->model)).calculate_ER());
445
446}
447/**
448 * Function to call to evaluate model in cartesian coordinates
449 * @param args: input q or [qx, qy]]
450 * @return: function value
451 */
452static PyObject * runXY(CCoreFourShellModel *self, PyObject *args) {
453        double qx_value, qy_value;
454        PyObject* pars;
455        int npars;
456       
457        // Get parameters
458       
459            // Reader parameter dictionary
460    self->model->thick_shell4 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thick_shell4") );
461    self->model->sld_shell4 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_shell4") );
462    self->model->scale = PyFloat_AsDouble( PyDict_GetItemString(self->params, "scale") );
463    self->model->sld_shell2 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_shell2") );
464    self->model->thick_shell1 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thick_shell1") );
465    self->model->thick_shell2 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thick_shell2") );
466    self->model->sld_shell1 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_shell1") );
467    self->model->thick_shell3 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thick_shell3") );
468    self->model->sld_core0 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_core0") );
469    self->model->background = PyFloat_AsDouble( PyDict_GetItemString(self->params, "background") );
470    self->model->rad_core0 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "rad_core0") );
471    self->model->sld_solv = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_solv") );
472    self->model->sld_shell3 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_shell3") );
473    // Read in dispersion parameters
474    PyObject* disp_dict;
475    DispersionVisitor* visitor = new DispersionVisitor();
476    disp_dict = PyDict_GetItemString(self->dispersion, "rad_core0");
477    self->model->rad_core0.dispersion->accept_as_destination(visitor, self->model->rad_core0.dispersion, disp_dict);
478    disp_dict = PyDict_GetItemString(self->dispersion, "thick_shell1");
479    self->model->thick_shell1.dispersion->accept_as_destination(visitor, self->model->thick_shell1.dispersion, disp_dict);
480    disp_dict = PyDict_GetItemString(self->dispersion, "thick_shell2");
481    self->model->thick_shell2.dispersion->accept_as_destination(visitor, self->model->thick_shell2.dispersion, disp_dict);
482    disp_dict = PyDict_GetItemString(self->dispersion, "thick_shell3");
483    self->model->thick_shell3.dispersion->accept_as_destination(visitor, self->model->thick_shell3.dispersion, disp_dict);
484    disp_dict = PyDict_GetItemString(self->dispersion, "thick_shell4");
485    self->model->thick_shell4.dispersion->accept_as_destination(visitor, self->model->thick_shell4.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(CCoreFourShellModelError, 
491                "CCoreFourShellModel.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(qx, qy))
499            npars = PyList_GET_SIZE(pars); 
500            if(npars!=2) {
501                PyErr_SetString(CCoreFourShellModelError, 
502                        "CCoreFourShellModel.run expects a double or a list of dimension 2.");
503                return NULL;
504            }
505            // We have a vector q, get the qx and qy values at which
506            // to evaluate I(qx,qy)
507            qx_value = CCoreFourShellModel_readDouble(PyList_GET_ITEM(pars,0));
508            qy_value = CCoreFourShellModel_readDouble(PyList_GET_ITEM(pars,1));
509            return Py_BuildValue("d",(*(self->model))(qx_value,qy_value));
510
511        } else {
512
513                // We have a scalar q, we will evaluate I(q)
514                qx_value = CCoreFourShellModel_readDouble(pars);               
515               
516                return Py_BuildValue("d",(*(self->model))(qx_value));
517        }       
518}
519
520static PyObject * reset(CCoreFourShellModel *self, PyObject *args) {
521   
522
523    return Py_BuildValue("d",0.0);
524}
525
526static PyObject * set_dispersion(CCoreFourShellModel *self, PyObject *args) {
527        PyObject * disp;
528        const char * par_name;
529
530        if ( !PyArg_ParseTuple(args,"sO", &par_name, &disp) ) {
531            PyErr_SetString(CCoreFourShellModelError,
532                "CCoreFourShellModel.set_dispersion expects a DispersionModel object.");
533                return NULL;
534        }
535        void *temp = PyCObject_AsVoidPtr(disp);
536        DispersionModel * dispersion = static_cast<DispersionModel *>(temp);
537
538
539        // Ugliness necessary to go from python to C
540            // TODO: refactor this
541    if (!strcmp(par_name, "rad_core0")) {
542        self->model->rad_core0.dispersion = dispersion;
543    } else    if (!strcmp(par_name, "thick_shell1")) {
544        self->model->thick_shell1.dispersion = dispersion;
545    } else    if (!strcmp(par_name, "thick_shell2")) {
546        self->model->thick_shell2.dispersion = dispersion;
547    } else    if (!strcmp(par_name, "thick_shell3")) {
548        self->model->thick_shell3.dispersion = dispersion;
549    } else    if (!strcmp(par_name, "thick_shell4")) {
550        self->model->thick_shell4.dispersion = dispersion;
551    } else {
552            PyErr_SetString(CCoreFourShellModelError,
553                "CCoreFourShellModel.set_dispersion expects a valid parameter name.");
554                return NULL;
555        }
556
557        DispersionVisitor* visitor = new DispersionVisitor();
558        PyObject * disp_dict = PyDict_New();
559        dispersion->accept_as_source(visitor, dispersion, disp_dict);
560        PyDict_SetItemString(self->dispersion, par_name, disp_dict);
561    return Py_BuildValue("i",1);
562}
563
564
565static PyMethodDef CCoreFourShellModel_methods[] = {
566    {"run",      (PyCFunction)run     , METH_VARARGS,
567      "Evaluate the model at a given Q or Q, phi"},
568    {"runXY",      (PyCFunction)runXY     , METH_VARARGS,
569      "Evaluate the model at a given Q or Qx, Qy"},
570    {"calculate_ER",      (PyCFunction)calculate_ER     , METH_VARARGS,
571      "Evaluate the model at a given Q or Q, phi"},
572     
573    {"evalDistribution",  (PyCFunction)evalDistribution , METH_VARARGS,
574      "Evaluate the model at a given Q or Qx, Qy vector "},
575    {"reset",    (PyCFunction)reset   , METH_VARARGS,
576      "Reset pair correlation"},
577    {"set_dispersion",      (PyCFunction)set_dispersion     , METH_VARARGS,
578      "Set the dispersion model for a given parameter"},
579   {NULL}
580};
581
582static PyTypeObject CCoreFourShellModelType = {
583    PyObject_HEAD_INIT(NULL)
584    0,                         /*ob_size*/
585    "CCoreFourShellModel",             /*tp_name*/
586    sizeof(CCoreFourShellModel),             /*tp_basicsize*/
587    0,                         /*tp_itemsize*/
588    (destructor)CCoreFourShellModel_dealloc, /*tp_dealloc*/
589    0,                         /*tp_print*/
590    0,                         /*tp_getattr*/
591    0,                         /*tp_setattr*/
592    0,                         /*tp_compare*/
593    0,                         /*tp_repr*/
594    0,                         /*tp_as_number*/
595    0,                         /*tp_as_sequence*/
596    0,                         /*tp_as_mapping*/
597    0,                         /*tp_hash */
598    0,                         /*tp_call*/
599    0,                         /*tp_str*/
600    0,                         /*tp_getattro*/
601    0,                         /*tp_setattro*/
602    0,                         /*tp_as_buffer*/
603    Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
604    "CCoreFourShellModel objects",           /* tp_doc */
605    0,                         /* tp_traverse */
606    0,                         /* tp_clear */
607    0,                         /* tp_richcompare */
608    0,                         /* tp_weaklistoffset */
609    0,                         /* tp_iter */
610    0,                         /* tp_iternext */
611    CCoreFourShellModel_methods,             /* tp_methods */
612    CCoreFourShellModel_members,             /* tp_members */
613    0,                         /* tp_getset */
614    0,                         /* tp_base */
615    0,                         /* tp_dict */
616    0,                         /* tp_descr_get */
617    0,                         /* tp_descr_set */
618    0,                         /* tp_dictoffset */
619    (initproc)CCoreFourShellModel_init,      /* tp_init */
620    0,                         /* tp_alloc */
621    CCoreFourShellModel_new,                 /* tp_new */
622};
623
624
625//static PyMethodDef module_methods[] = {
626//    {NULL}
627//};
628
629/**
630 * Function used to add the model class to a module
631 * @param module: module to add the class to
632 */ 
633void addCCoreFourShellModel(PyObject *module) {
634        PyObject *d;
635       
636    if (PyType_Ready(&CCoreFourShellModelType) < 0)
637        return;
638
639    Py_INCREF(&CCoreFourShellModelType);
640    PyModule_AddObject(module, "CCoreFourShellModel", (PyObject *)&CCoreFourShellModelType);
641   
642    d = PyModule_GetDict(module);
643    static char error_name[] = "CCoreFourShellModel.error";
644    CCoreFourShellModelError = PyErr_NewException(error_name, NULL, NULL);
645    PyDict_SetItemString(d, "CCoreFourShellModelError", CCoreFourShellModelError);
646}
647
Note: See TracBrowser for help on using the repository browser.