source: sasview/src/sans/models/c_extension/python_wrapper/generated/CCylinderModel.cpp @ 400155b

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 400155b was 400155b, checked in by gonzalezm, 9 years ago

Implementing request from ticket 261 - default number of bins in Annulus [Phi View] is now 36 and the first bin is now centered at 0 degrees

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