source: sasview/sansmodels/src/python_wrapper/CStackedDisksModel.cpp @ 31af069

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 31af069 was 67424cd, checked in by Mathieu Doucet <doucetm@…>, 13 years ago

Reorganizing models in preparation of cpp cleanup

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