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