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

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 a4cd162 was 35aface, checked in by Jae Cho <jhjcho@…>, 14 years ago

addede new models and attr. non_fittable

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