source: sasview/sansmodels/src/sans/models/c_models/CReflModel.cpp @ 600eca2

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 600eca2 was 0164899a, checked in by Jae Cho <jhjcho@…>, 14 years ago

new models and some bug fixes

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