source: sasview/sansmodels/src/sans/models/c_models/CRPAModel.cpp @ 916f5c0

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

Re #4 Still a few more warnings

  • Property mode set to 100644
File size: 28.7 KB
Line 
1/**
2        This software was developed by the University of Tennessee as part of the
3        Distributed Data Analysis of Neutron Scattering Experiments (DANSE)
4        project funded by the US National Science Foundation.
5
6        If you use DANSE applications to do scientific research that leads to
7        publication, we ask that you acknowledge the use of the software with the
8        following sentence:
9
10        "This work benefited from DANSE software developed under NSF award DMR-0520547."
11
12        copyright 2008, University of Tennessee
13 */
14
15/** CRPAModel
16 *
17 * C extension
18 *
19 * WARNING: THIS FILE WAS GENERATED BY WRAPPERGENERATOR.PY
20 *          DO NOT MODIFY THIS FILE, MODIFY rpa.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 "rpa.h"
36}
37
38#include "models.hh"
39#include "dispersion_visitor.hh"
40
41/// Error object for raised exceptions
42static PyObject * CRPAModelError = 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    RPAModel * model;
54    /// Log for unit testing
55    PyObject * log;
56} CRPAModel;
57
58
59static void
60CRPAModel_dealloc(CRPAModel* 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 *
72CRPAModel_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
73{
74    CRPAModel *self;
75   
76    self = (CRPAModel *)type->tp_alloc(type, 0);
77   
78    return (PyObject *)self;
79}
80
81static int
82CRPAModel_init(CRPAModel *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 RPAModel();
90       
91        // Initialize parameter dictionary
92        PyDict_SetItemString(self->params,"va",Py_BuildValue("d",100.000000000000));
93        PyDict_SetItemString(self->params,"vb",Py_BuildValue("d",100.000000000000));
94        PyDict_SetItemString(self->params,"vc",Py_BuildValue("d",100.000000000000));
95        PyDict_SetItemString(self->params,"Lb",Py_BuildValue("d",0.000000000001));
96        PyDict_SetItemString(self->params,"Lc",Py_BuildValue("d",0.000000000001));
97        PyDict_SetItemString(self->params,"La",Py_BuildValue("d",0.000000000001));
98        PyDict_SetItemString(self->params,"Na",Py_BuildValue("d",1000.000000000000));
99        PyDict_SetItemString(self->params,"Nb",Py_BuildValue("d",1000.000000000000));
100        PyDict_SetItemString(self->params,"Nc",Py_BuildValue("d",1000.000000000000));
101        PyDict_SetItemString(self->params,"Nd",Py_BuildValue("d",1000.000000000000));
102        PyDict_SetItemString(self->params,"Ld",Py_BuildValue("d",0.000000000000));
103        PyDict_SetItemString(self->params,"Kcd",Py_BuildValue("d",-0.000400000000));
104        PyDict_SetItemString(self->params,"vd",Py_BuildValue("d",100.000000000000));
105        PyDict_SetItemString(self->params,"Phib",Py_BuildValue("d",0.250000000000));
106        PyDict_SetItemString(self->params,"Kac",Py_BuildValue("d",-0.000400000000));
107        PyDict_SetItemString(self->params,"Kab",Py_BuildValue("d",-0.000400000000));
108        PyDict_SetItemString(self->params,"scale",Py_BuildValue("d",1.000000000000));
109        PyDict_SetItemString(self->params,"bb",Py_BuildValue("d",5.000000000000));
110        PyDict_SetItemString(self->params,"Kad",Py_BuildValue("d",-0.000400000000));
111        PyDict_SetItemString(self->params,"bd",Py_BuildValue("d",5.000000000000));
112        PyDict_SetItemString(self->params,"Phid",Py_BuildValue("d",0.250000000000));
113        PyDict_SetItemString(self->params,"Phic",Py_BuildValue("d",0.250000000000));
114        PyDict_SetItemString(self->params,"ba",Py_BuildValue("d",5.000000000000));
115        PyDict_SetItemString(self->params,"Phia",Py_BuildValue("d",0.250000000000));
116        PyDict_SetItemString(self->params,"bc",Py_BuildValue("d",5.000000000000));
117        PyDict_SetItemString(self->params,"background",Py_BuildValue("d",0.000000000000));
118        PyDict_SetItemString(self->params,"Kbc",Py_BuildValue("d",-0.000400000000));
119        PyDict_SetItemString(self->params,"Kbd",Py_BuildValue("d",-0.000400000000));
120        PyDict_SetItemString(self->params,"lcase_n",Py_BuildValue("d",0.000000000000));
121        // Initialize dispersion / averaging parameter dict
122        DispersionVisitor* visitor = new DispersionVisitor();
123        PyObject * disp_dict;
124        disp_dict = PyDict_New();
125        self->model->background.dispersion->accept_as_source(visitor, self->model->background.dispersion, disp_dict);
126        PyDict_SetItemString(self->dispersion, "background", disp_dict);
127
128
129         
130        // Create empty log
131        self->log = PyDict_New();
132       
133       
134
135    }
136    return 0;
137}
138
139static char name_params[] = "params";
140static char def_params[] = "Parameters";
141static char name_dispersion[] = "dispersion";
142static char def_dispersion[] = "Dispersion parameters";
143static char name_log[] = "log";
144static char def_log[] = "Log";
145
146static PyMemberDef CRPAModel_members[] = {
147    {name_params, T_OBJECT, offsetof(CRPAModel, params), 0, def_params},
148        {name_dispersion, T_OBJECT, offsetof(CRPAModel, dispersion), 0, def_dispersion},     
149    {name_log, T_OBJECT, offsetof(CRPAModel, log), 0, def_log},
150    {NULL}  /* Sentinel */
151};
152
153/** Read double from PyObject
154    @param p PyObject
155    @return double
156*/
157double CRPAModel_readDouble(PyObject *p) {
158    if (PyFloat_Check(p)==1) {
159        return (double)(((PyFloatObject *)(p))->ob_fval);
160    } else if (PyInt_Check(p)==1) {
161        return (double)(((PyIntObject *)(p))->ob_ival);
162    } else if (PyLong_Check(p)==1) {
163        return (double)PyLong_AsLong(p);
164    } else {
165        return 0.0;
166    }
167}
168/**
169 * Function to call to evaluate model
170 * @param args: input numpy array q[]
171 * @return: numpy array object
172 */
173 
174static PyObject *evaluateOneDim(RPAModel* model, PyArrayObject *q){
175    PyArrayObject *result;
176   
177    // Check validity of array q , q must be of dimension 1, an array of double
178    if (q->nd != 1 || q->descr->type_num != PyArray_DOUBLE)
179    {
180        //const char * message= "Invalid array: q->nd=%d,type_num=%d\n",q->nd,q->descr->type_num;
181        //PyErr_SetString(PyExc_ValueError , message);
182        return NULL;
183    }
184    result = (PyArrayObject *)PyArray_FromDims(q->nd, (int *)(q->dimensions), 
185                                                                                  PyArray_DOUBLE);
186        if (result == NULL) {
187        const char * message= "Could not create result ";
188        PyErr_SetString(PyExc_RuntimeError , message);
189                return NULL;
190        }
191         for (int i = 0; i < q->dimensions[0]; i++){
192      double q_value  = *(double *)(q->data + i*q->strides[0]);
193      double *result_value = (double *)(result->data + i*result->strides[0]);
194      *result_value =(*model)(q_value);
195        }
196    return PyArray_Return(result); 
197 }
198
199 /**
200 * Function to call to evaluate model
201 * @param args: input numpy array  [x[],y[]]
202 * @return: numpy array object
203 */
204 static PyObject * evaluateTwoDimXY( RPAModel* model, 
205                              PyArrayObject *x, PyArrayObject *y)
206 {
207    PyArrayObject *result;
208    int i, x_len, y_len, dims[1];
209    //check validity of input vectors
210    if (x->nd != 1 || x->descr->type_num != PyArray_DOUBLE
211        || y->nd != 1 || y->descr->type_num != PyArray_DOUBLE
212        || y->dimensions[0] != x->dimensions[0]){
213        const char * message= "evaluateTwoDimXY  expect 2 numpy arrays";
214        PyErr_SetString(PyExc_ValueError , message); 
215        return NULL;
216    }
217   
218        if (PyArray_Check(x) && PyArray_Check(y)) {
219               
220            x_len = dims[0]= x->dimensions[0];
221        y_len = dims[0]= y->dimensions[0];
222           
223            // Make a new double matrix of same dims
224        result=(PyArrayObject *) PyArray_FromDims(1,dims,NPY_DOUBLE);
225        if (result == NULL){
226            const char * message= "Could not create result ";
227        PyErr_SetString(PyExc_RuntimeError , message);
228            return NULL;
229            }
230       
231        /* Do the calculation. */
232        for ( i=0; i< x_len; i++) {
233            double x_value = *(double *)(x->data + i*x->strides[0]);
234                    double y_value = *(double *)(y->data + i*y->strides[0]);
235                        double *result_value = (double *)(result->data +
236                              i*result->strides[0]);
237                        *result_value = (*model)(x_value, y_value);
238        }           
239        return PyArray_Return(result); 
240       
241        }else{
242                    PyErr_SetString(CRPAModelError, 
243                   "CRPAModel.evaluateTwoDimXY couldn't run.");
244                return NULL;
245                }       
246}
247/**
248 *  evalDistribution function evaluate a model function with input vector
249 *  @param args: input q as vector or [qx, qy] where qx, qy are vectors
250 *
251 */ 
252static PyObject * evalDistribution(CRPAModel *self, PyObject *args){
253        PyObject *qx, *qy;
254        PyArrayObject * pars;
255        int npars ,mpars;
256       
257        // Get parameters
258       
259            // Reader parameter dictionary
260    self->model->va = PyFloat_AsDouble( PyDict_GetItemString(self->params, "va") );
261    self->model->vb = PyFloat_AsDouble( PyDict_GetItemString(self->params, "vb") );
262    self->model->vc = PyFloat_AsDouble( PyDict_GetItemString(self->params, "vc") );
263    self->model->Lb = PyFloat_AsDouble( PyDict_GetItemString(self->params, "Lb") );
264    self->model->Lc = PyFloat_AsDouble( PyDict_GetItemString(self->params, "Lc") );
265    self->model->La = PyFloat_AsDouble( PyDict_GetItemString(self->params, "La") );
266    self->model->Na = PyFloat_AsDouble( PyDict_GetItemString(self->params, "Na") );
267    self->model->Nb = PyFloat_AsDouble( PyDict_GetItemString(self->params, "Nb") );
268    self->model->Nc = PyFloat_AsDouble( PyDict_GetItemString(self->params, "Nc") );
269    self->model->Nd = PyFloat_AsDouble( PyDict_GetItemString(self->params, "Nd") );
270    self->model->Ld = PyFloat_AsDouble( PyDict_GetItemString(self->params, "Ld") );
271    self->model->Kcd = PyFloat_AsDouble( PyDict_GetItemString(self->params, "Kcd") );
272    self->model->vd = PyFloat_AsDouble( PyDict_GetItemString(self->params, "vd") );
273    self->model->Phib = PyFloat_AsDouble( PyDict_GetItemString(self->params, "Phib") );
274    self->model->Kac = PyFloat_AsDouble( PyDict_GetItemString(self->params, "Kac") );
275    self->model->Kab = PyFloat_AsDouble( PyDict_GetItemString(self->params, "Kab") );
276    self->model->scale = PyFloat_AsDouble( PyDict_GetItemString(self->params, "scale") );
277    self->model->bb = PyFloat_AsDouble( PyDict_GetItemString(self->params, "bb") );
278    self->model->Kad = PyFloat_AsDouble( PyDict_GetItemString(self->params, "Kad") );
279    self->model->bd = PyFloat_AsDouble( PyDict_GetItemString(self->params, "bd") );
280    self->model->Phid = PyFloat_AsDouble( PyDict_GetItemString(self->params, "Phid") );
281    self->model->Phic = PyFloat_AsDouble( PyDict_GetItemString(self->params, "Phic") );
282    self->model->ba = PyFloat_AsDouble( PyDict_GetItemString(self->params, "ba") );
283    self->model->Phia = PyFloat_AsDouble( PyDict_GetItemString(self->params, "Phia") );
284    self->model->bc = PyFloat_AsDouble( PyDict_GetItemString(self->params, "bc") );
285    self->model->background = PyFloat_AsDouble( PyDict_GetItemString(self->params, "background") );
286    self->model->Kbc = PyFloat_AsDouble( PyDict_GetItemString(self->params, "Kbc") );
287    self->model->Kbd = PyFloat_AsDouble( PyDict_GetItemString(self->params, "Kbd") );
288    self->model->lcase_n = PyFloat_AsDouble( PyDict_GetItemString(self->params, "lcase_n") );
289    // Read in dispersion parameters
290    PyObject* disp_dict;
291    DispersionVisitor* visitor = new DispersionVisitor();
292    disp_dict = PyDict_GetItemString(self->dispersion, "background");
293    self->model->background.dispersion->accept_as_destination(visitor, self->model->background.dispersion, disp_dict);
294
295       
296        // Get input and determine whether we have to supply a 1D or 2D return value.
297        if ( !PyArg_ParseTuple(args,"O",&pars) ) {
298            PyErr_SetString(CRPAModelError, 
299                "CRPAModel.evalDistribution expects a q value.");
300                return NULL;
301        }
302    // Check params
303       
304    if(PyArray_Check(pars)==1) {
305               
306            // Length of list should 1 or 2
307            npars = pars->nd; 
308            if(npars==1) {
309                // input is a numpy array
310                if (PyArray_Check(pars)) {
311                        return evaluateOneDim(self->model, (PyArrayObject*)pars); 
312                    }
313                }else{
314                    PyErr_SetString(CRPAModelError, 
315                   "CRPAModel.evalDistribution expect numpy array of one dimension.");
316                return NULL;
317                }
318    }else if( PyList_Check(pars)==1) {
319        // Length of list should be 2 for I(qx,qy)
320            mpars = PyList_GET_SIZE(pars); 
321            if(mpars!=2) {
322                PyErr_SetString(CRPAModelError, 
323                        "CRPAModel.evalDistribution expects a list of dimension 2.");
324                return NULL;
325            }
326             qx = PyList_GET_ITEM(pars,0);
327             qy = PyList_GET_ITEM(pars,1);
328             if (PyArray_Check(qx) && PyArray_Check(qy)) {
329                 return evaluateTwoDimXY(self->model, (PyArrayObject*)qx,
330                           (PyArrayObject*)qy);
331                 }else{
332                    PyErr_SetString(CRPAModelError, 
333                   "CRPAModel.evalDistribution expect 2 numpy arrays in list.");
334                return NULL;
335             }
336        }
337        PyErr_SetString(CRPAModelError, 
338                   "CRPAModel.evalDistribution couln't be run.");
339        return NULL;
340       
341}
342
343/**
344 * Function to call to evaluate model
345 * @param args: input q or [q,phi]
346 * @return: function value
347 */
348static PyObject * run(CRPAModel *self, PyObject *args) {
349        double q_value, phi_value;
350        PyObject* pars;
351        int npars;
352       
353        // Get parameters
354       
355            // Reader parameter dictionary
356    self->model->va = PyFloat_AsDouble( PyDict_GetItemString(self->params, "va") );
357    self->model->vb = PyFloat_AsDouble( PyDict_GetItemString(self->params, "vb") );
358    self->model->vc = PyFloat_AsDouble( PyDict_GetItemString(self->params, "vc") );
359    self->model->Lb = PyFloat_AsDouble( PyDict_GetItemString(self->params, "Lb") );
360    self->model->Lc = PyFloat_AsDouble( PyDict_GetItemString(self->params, "Lc") );
361    self->model->La = PyFloat_AsDouble( PyDict_GetItemString(self->params, "La") );
362    self->model->Na = PyFloat_AsDouble( PyDict_GetItemString(self->params, "Na") );
363    self->model->Nb = PyFloat_AsDouble( PyDict_GetItemString(self->params, "Nb") );
364    self->model->Nc = PyFloat_AsDouble( PyDict_GetItemString(self->params, "Nc") );
365    self->model->Nd = PyFloat_AsDouble( PyDict_GetItemString(self->params, "Nd") );
366    self->model->Ld = PyFloat_AsDouble( PyDict_GetItemString(self->params, "Ld") );
367    self->model->Kcd = PyFloat_AsDouble( PyDict_GetItemString(self->params, "Kcd") );
368    self->model->vd = PyFloat_AsDouble( PyDict_GetItemString(self->params, "vd") );
369    self->model->Phib = PyFloat_AsDouble( PyDict_GetItemString(self->params, "Phib") );
370    self->model->Kac = PyFloat_AsDouble( PyDict_GetItemString(self->params, "Kac") );
371    self->model->Kab = PyFloat_AsDouble( PyDict_GetItemString(self->params, "Kab") );
372    self->model->scale = PyFloat_AsDouble( PyDict_GetItemString(self->params, "scale") );
373    self->model->bb = PyFloat_AsDouble( PyDict_GetItemString(self->params, "bb") );
374    self->model->Kad = PyFloat_AsDouble( PyDict_GetItemString(self->params, "Kad") );
375    self->model->bd = PyFloat_AsDouble( PyDict_GetItemString(self->params, "bd") );
376    self->model->Phid = PyFloat_AsDouble( PyDict_GetItemString(self->params, "Phid") );
377    self->model->Phic = PyFloat_AsDouble( PyDict_GetItemString(self->params, "Phic") );
378    self->model->ba = PyFloat_AsDouble( PyDict_GetItemString(self->params, "ba") );
379    self->model->Phia = PyFloat_AsDouble( PyDict_GetItemString(self->params, "Phia") );
380    self->model->bc = PyFloat_AsDouble( PyDict_GetItemString(self->params, "bc") );
381    self->model->background = PyFloat_AsDouble( PyDict_GetItemString(self->params, "background") );
382    self->model->Kbc = PyFloat_AsDouble( PyDict_GetItemString(self->params, "Kbc") );
383    self->model->Kbd = PyFloat_AsDouble( PyDict_GetItemString(self->params, "Kbd") );
384    self->model->lcase_n = PyFloat_AsDouble( PyDict_GetItemString(self->params, "lcase_n") );
385    // Read in dispersion parameters
386    PyObject* disp_dict;
387    DispersionVisitor* visitor = new DispersionVisitor();
388    disp_dict = PyDict_GetItemString(self->dispersion, "background");
389    self->model->background.dispersion->accept_as_destination(visitor, self->model->background.dispersion, disp_dict);
390
391       
392        // Get input and determine whether we have to supply a 1D or 2D return value.
393        if ( !PyArg_ParseTuple(args,"O",&pars) ) {
394            PyErr_SetString(CRPAModelError, 
395                "CRPAModel.run expects a q value.");
396                return NULL;
397        }
398         
399        // Check params
400        if( PyList_Check(pars)==1) {
401               
402                // Length of list should be 2 for I(q,phi)
403            npars = PyList_GET_SIZE(pars); 
404            if(npars!=2) {
405                PyErr_SetString(CRPAModelError, 
406                        "CRPAModel.run expects a double or a list of dimension 2.");
407                return NULL;
408            }
409            // We have a vector q, get the q and phi values at which
410            // to evaluate I(q,phi)
411            q_value = CRPAModel_readDouble(PyList_GET_ITEM(pars,0));
412            phi_value = CRPAModel_readDouble(PyList_GET_ITEM(pars,1));
413            // Skip zero
414            if (q_value==0) {
415                return Py_BuildValue("d",0.0);
416            }
417                return Py_BuildValue("d",(*(self->model)).evaluate_rphi(q_value,phi_value));
418
419        } else {
420
421                // We have a scalar q, we will evaluate I(q)
422                q_value = CRPAModel_readDouble(pars);           
423               
424                return Py_BuildValue("d",(*(self->model))(q_value));
425        }       
426}
427/**
428 * Function to call to calculate_ER
429 * @return: effective radius value
430 */
431static PyObject * calculate_ER(CRPAModel *self) {
432
433        // Get parameters
434       
435            // Reader parameter dictionary
436    self->model->va = PyFloat_AsDouble( PyDict_GetItemString(self->params, "va") );
437    self->model->vb = PyFloat_AsDouble( PyDict_GetItemString(self->params, "vb") );
438    self->model->vc = PyFloat_AsDouble( PyDict_GetItemString(self->params, "vc") );
439    self->model->Lb = PyFloat_AsDouble( PyDict_GetItemString(self->params, "Lb") );
440    self->model->Lc = PyFloat_AsDouble( PyDict_GetItemString(self->params, "Lc") );
441    self->model->La = PyFloat_AsDouble( PyDict_GetItemString(self->params, "La") );
442    self->model->Na = PyFloat_AsDouble( PyDict_GetItemString(self->params, "Na") );
443    self->model->Nb = PyFloat_AsDouble( PyDict_GetItemString(self->params, "Nb") );
444    self->model->Nc = PyFloat_AsDouble( PyDict_GetItemString(self->params, "Nc") );
445    self->model->Nd = PyFloat_AsDouble( PyDict_GetItemString(self->params, "Nd") );
446    self->model->Ld = PyFloat_AsDouble( PyDict_GetItemString(self->params, "Ld") );
447    self->model->Kcd = PyFloat_AsDouble( PyDict_GetItemString(self->params, "Kcd") );
448    self->model->vd = PyFloat_AsDouble( PyDict_GetItemString(self->params, "vd") );
449    self->model->Phib = PyFloat_AsDouble( PyDict_GetItemString(self->params, "Phib") );
450    self->model->Kac = PyFloat_AsDouble( PyDict_GetItemString(self->params, "Kac") );
451    self->model->Kab = PyFloat_AsDouble( PyDict_GetItemString(self->params, "Kab") );
452    self->model->scale = PyFloat_AsDouble( PyDict_GetItemString(self->params, "scale") );
453    self->model->bb = PyFloat_AsDouble( PyDict_GetItemString(self->params, "bb") );
454    self->model->Kad = PyFloat_AsDouble( PyDict_GetItemString(self->params, "Kad") );
455    self->model->bd = PyFloat_AsDouble( PyDict_GetItemString(self->params, "bd") );
456    self->model->Phid = PyFloat_AsDouble( PyDict_GetItemString(self->params, "Phid") );
457    self->model->Phic = PyFloat_AsDouble( PyDict_GetItemString(self->params, "Phic") );
458    self->model->ba = PyFloat_AsDouble( PyDict_GetItemString(self->params, "ba") );
459    self->model->Phia = PyFloat_AsDouble( PyDict_GetItemString(self->params, "Phia") );
460    self->model->bc = PyFloat_AsDouble( PyDict_GetItemString(self->params, "bc") );
461    self->model->background = PyFloat_AsDouble( PyDict_GetItemString(self->params, "background") );
462    self->model->Kbc = PyFloat_AsDouble( PyDict_GetItemString(self->params, "Kbc") );
463    self->model->Kbd = PyFloat_AsDouble( PyDict_GetItemString(self->params, "Kbd") );
464    self->model->lcase_n = PyFloat_AsDouble( PyDict_GetItemString(self->params, "lcase_n") );
465    // Read in dispersion parameters
466    PyObject* disp_dict;
467    DispersionVisitor* visitor = new DispersionVisitor();
468    disp_dict = PyDict_GetItemString(self->dispersion, "background");
469    self->model->background.dispersion->accept_as_destination(visitor, self->model->background.dispersion, disp_dict);
470
471               
472        return Py_BuildValue("d",(*(self->model)).calculate_ER());
473
474}
475/**
476 * Function to call to evaluate model in cartesian coordinates
477 * @param args: input q or [qx, qy]]
478 * @return: function value
479 */
480static PyObject * runXY(CRPAModel *self, PyObject *args) {
481        double qx_value, qy_value;
482        PyObject* pars;
483        int npars;
484       
485        // Get parameters
486       
487            // Reader parameter dictionary
488    self->model->va = PyFloat_AsDouble( PyDict_GetItemString(self->params, "va") );
489    self->model->vb = PyFloat_AsDouble( PyDict_GetItemString(self->params, "vb") );
490    self->model->vc = PyFloat_AsDouble( PyDict_GetItemString(self->params, "vc") );
491    self->model->Lb = PyFloat_AsDouble( PyDict_GetItemString(self->params, "Lb") );
492    self->model->Lc = PyFloat_AsDouble( PyDict_GetItemString(self->params, "Lc") );
493    self->model->La = PyFloat_AsDouble( PyDict_GetItemString(self->params, "La") );
494    self->model->Na = PyFloat_AsDouble( PyDict_GetItemString(self->params, "Na") );
495    self->model->Nb = PyFloat_AsDouble( PyDict_GetItemString(self->params, "Nb") );
496    self->model->Nc = PyFloat_AsDouble( PyDict_GetItemString(self->params, "Nc") );
497    self->model->Nd = PyFloat_AsDouble( PyDict_GetItemString(self->params, "Nd") );
498    self->model->Ld = PyFloat_AsDouble( PyDict_GetItemString(self->params, "Ld") );
499    self->model->Kcd = PyFloat_AsDouble( PyDict_GetItemString(self->params, "Kcd") );
500    self->model->vd = PyFloat_AsDouble( PyDict_GetItemString(self->params, "vd") );
501    self->model->Phib = PyFloat_AsDouble( PyDict_GetItemString(self->params, "Phib") );
502    self->model->Kac = PyFloat_AsDouble( PyDict_GetItemString(self->params, "Kac") );
503    self->model->Kab = PyFloat_AsDouble( PyDict_GetItemString(self->params, "Kab") );
504    self->model->scale = PyFloat_AsDouble( PyDict_GetItemString(self->params, "scale") );
505    self->model->bb = PyFloat_AsDouble( PyDict_GetItemString(self->params, "bb") );
506    self->model->Kad = PyFloat_AsDouble( PyDict_GetItemString(self->params, "Kad") );
507    self->model->bd = PyFloat_AsDouble( PyDict_GetItemString(self->params, "bd") );
508    self->model->Phid = PyFloat_AsDouble( PyDict_GetItemString(self->params, "Phid") );
509    self->model->Phic = PyFloat_AsDouble( PyDict_GetItemString(self->params, "Phic") );
510    self->model->ba = PyFloat_AsDouble( PyDict_GetItemString(self->params, "ba") );
511    self->model->Phia = PyFloat_AsDouble( PyDict_GetItemString(self->params, "Phia") );
512    self->model->bc = PyFloat_AsDouble( PyDict_GetItemString(self->params, "bc") );
513    self->model->background = PyFloat_AsDouble( PyDict_GetItemString(self->params, "background") );
514    self->model->Kbc = PyFloat_AsDouble( PyDict_GetItemString(self->params, "Kbc") );
515    self->model->Kbd = PyFloat_AsDouble( PyDict_GetItemString(self->params, "Kbd") );
516    self->model->lcase_n = PyFloat_AsDouble( PyDict_GetItemString(self->params, "lcase_n") );
517    // Read in dispersion parameters
518    PyObject* disp_dict;
519    DispersionVisitor* visitor = new DispersionVisitor();
520    disp_dict = PyDict_GetItemString(self->dispersion, "background");
521    self->model->background.dispersion->accept_as_destination(visitor, self->model->background.dispersion, disp_dict);
522
523       
524        // Get input and determine whether we have to supply a 1D or 2D return value.
525        if ( !PyArg_ParseTuple(args,"O",&pars) ) {
526            PyErr_SetString(CRPAModelError, 
527                "CRPAModel.run expects a q value.");
528                return NULL;
529        }
530         
531        // Check params
532        if( PyList_Check(pars)==1) {
533               
534                // Length of list should be 2 for I(qx, qy))
535            npars = PyList_GET_SIZE(pars); 
536            if(npars!=2) {
537                PyErr_SetString(CRPAModelError, 
538                        "CRPAModel.run expects a double or a list of dimension 2.");
539                return NULL;
540            }
541            // We have a vector q, get the qx and qy values at which
542            // to evaluate I(qx,qy)
543            qx_value = CRPAModel_readDouble(PyList_GET_ITEM(pars,0));
544            qy_value = CRPAModel_readDouble(PyList_GET_ITEM(pars,1));
545            return Py_BuildValue("d",(*(self->model))(qx_value,qy_value));
546
547        } else {
548
549                // We have a scalar q, we will evaluate I(q)
550                qx_value = CRPAModel_readDouble(pars);         
551               
552                return Py_BuildValue("d",(*(self->model))(qx_value));
553        }       
554}
555
556static PyObject * reset(CRPAModel *self, PyObject *args) {
557   
558
559    return Py_BuildValue("d",0.0);
560}
561
562static PyObject * set_dispersion(CRPAModel *self, PyObject *args) {
563        PyObject * disp;
564        const char * par_name;
565
566        if ( !PyArg_ParseTuple(args,"sO", &par_name, &disp) ) {
567            PyErr_SetString(CRPAModelError,
568                "CRPAModel.set_dispersion expects a DispersionModel object.");
569                return NULL;
570        }
571        void *temp = PyCObject_AsVoidPtr(disp);
572        DispersionModel * dispersion = static_cast<DispersionModel *>(temp);
573
574
575        // Ugliness necessary to go from python to C
576            // TODO: refactor this
577    if (!strcmp(par_name, "background")) {
578        self->model->background.dispersion = dispersion;
579    } else {
580            PyErr_SetString(CRPAModelError,
581                "CRPAModel.set_dispersion expects a valid parameter name.");
582                return NULL;
583        }
584
585        DispersionVisitor* visitor = new DispersionVisitor();
586        PyObject * disp_dict = PyDict_New();
587        dispersion->accept_as_source(visitor, dispersion, disp_dict);
588        PyDict_SetItemString(self->dispersion, par_name, disp_dict);
589    return Py_BuildValue("i",1);
590}
591
592
593static PyMethodDef CRPAModel_methods[] = {
594    {"run",      (PyCFunction)run     , METH_VARARGS,
595      "Evaluate the model at a given Q or Q, phi"},
596    {"runXY",      (PyCFunction)runXY     , METH_VARARGS,
597      "Evaluate the model at a given Q or Qx, Qy"},
598    {"calculate_ER",      (PyCFunction)calculate_ER     , METH_VARARGS,
599      "Evaluate the model at a given Q or Q, phi"},
600     
601    {"evalDistribution",  (PyCFunction)evalDistribution , METH_VARARGS,
602      "Evaluate the model at a given Q or Qx, Qy vector "},
603    {"reset",    (PyCFunction)reset   , METH_VARARGS,
604      "Reset pair correlation"},
605    {"set_dispersion",      (PyCFunction)set_dispersion     , METH_VARARGS,
606      "Set the dispersion model for a given parameter"},
607   {NULL}
608};
609
610static PyTypeObject CRPAModelType = {
611    PyObject_HEAD_INIT(NULL)
612    0,                         /*ob_size*/
613    "CRPAModel",             /*tp_name*/
614    sizeof(CRPAModel),             /*tp_basicsize*/
615    0,                         /*tp_itemsize*/
616    (destructor)CRPAModel_dealloc, /*tp_dealloc*/
617    0,                         /*tp_print*/
618    0,                         /*tp_getattr*/
619    0,                         /*tp_setattr*/
620    0,                         /*tp_compare*/
621    0,                         /*tp_repr*/
622    0,                         /*tp_as_number*/
623    0,                         /*tp_as_sequence*/
624    0,                         /*tp_as_mapping*/
625    0,                         /*tp_hash */
626    0,                         /*tp_call*/
627    0,                         /*tp_str*/
628    0,                         /*tp_getattro*/
629    0,                         /*tp_setattro*/
630    0,                         /*tp_as_buffer*/
631    Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
632    "CRPAModel objects",           /* tp_doc */
633    0,                         /* tp_traverse */
634    0,                         /* tp_clear */
635    0,                         /* tp_richcompare */
636    0,                         /* tp_weaklistoffset */
637    0,                         /* tp_iter */
638    0,                         /* tp_iternext */
639    CRPAModel_methods,             /* tp_methods */
640    CRPAModel_members,             /* tp_members */
641    0,                         /* tp_getset */
642    0,                         /* tp_base */
643    0,                         /* tp_dict */
644    0,                         /* tp_descr_get */
645    0,                         /* tp_descr_set */
646    0,                         /* tp_dictoffset */
647    (initproc)CRPAModel_init,      /* tp_init */
648    0,                         /* tp_alloc */
649    CRPAModel_new,                 /* tp_new */
650};
651
652
653//static PyMethodDef module_methods[] = {
654//    {NULL}
655//};
656
657/**
658 * Function used to add the model class to a module
659 * @param module: module to add the class to
660 */ 
661void addCRPAModel(PyObject *module) {
662        PyObject *d;
663       
664    if (PyType_Ready(&CRPAModelType) < 0)
665        return;
666
667    Py_INCREF(&CRPAModelType);
668    PyModule_AddObject(module, "CRPAModel", (PyObject *)&CRPAModelType);
669   
670    d = PyModule_GetDict(module);
671    static char error_name[] = "CRPAModel.error";
672    CRPAModelError = PyErr_NewException(error_name, NULL, NULL);
673    PyDict_SetItemString(d, "CRPAModelError", CRPAModelError);
674}
675
Note: See TracBrowser for help on using the repository browser.