source: sasview/sansmodels/src/python_wrapper/CReflModel.cpp @ acd0fd10

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

Reorganizing models in preparation of cpp cleanup

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