source: sasview/sansmodels/src/sans/models/c_models/CRPAModel.cpp @ 2cef9d3

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

Re #7 Enable openmp for all models

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