source: sasview/sansmodels/src/sans/models/c_models/CCoreFourShellModel.cpp @ 0b082f3

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

Re #7 Enable openmp for all models

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