source: sasview/sansmodels/src/sans/models/c_models/CRPAModel.cpp @ 4c5448c

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 4c5448c was 8f5b34a, checked in by Mathieu Doucet <doucetm@…>, 13 years ago

Re #7 Bring back deprecated numpy function (new call signature doesn't quite work)

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