source: sasview/sansmodels/src/sans/models/c_models/CStackedDisksModel.cpp @ 484faf7

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 484faf7 was 71e2de7, checked in by Gervaise Alina <gervyh@…>, 15 years ago

change destructor for models

  • Property mode set to 100644
File size: 27.0 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.000004));
93        PyDict_SetItemString(self->params,"core_thick",Py_BuildValue("d",10.000000));
94        PyDict_SetItemString(self->params,"layer_thick",Py_BuildValue("d",15.000000));
95        PyDict_SetItemString(self->params,"axis_theta",Py_BuildValue("d",0.000000));
96        PyDict_SetItemString(self->params,"layer_sld",Py_BuildValue("d",-0.000000));
97        PyDict_SetItemString(self->params,"axis_phi",Py_BuildValue("d",0.000000));
98        PyDict_SetItemString(self->params,"solvent_sld",Py_BuildValue("d",0.000005));
99        PyDict_SetItemString(self->params,"scale",Py_BuildValue("d",0.010000));
100        PyDict_SetItemString(self->params,"radius",Py_BuildValue("d",3000.000000));
101        PyDict_SetItemString(self->params,"background",Py_BuildValue("d",0.001000));
102        PyDict_SetItemString(self->params,"sigma_d",Py_BuildValue("d",0.000000));
103        PyDict_SetItemString(self->params,"n_stacking",Py_BuildValue("d",1.000000));
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 PyMemberDef CStackedDisksModel_members[] = {
135    {"params", T_OBJECT, offsetof(CStackedDisksModel, params), 0,
136     "Parameters"},
137        {"dispersion", T_OBJECT, offsetof(CStackedDisksModel, dispersion), 0,
138          "Dispersion parameters"},     
139    {"log", T_OBJECT, offsetof(CStackedDisksModel, log), 0,
140     "Log"},
141    {NULL}  /* Sentinel */
142};
143
144/** Read double from PyObject
145    @param p PyObject
146    @return double
147*/
148double CStackedDisksModel_readDouble(PyObject *p) {
149    if (PyFloat_Check(p)==1) {
150        return (double)(((PyFloatObject *)(p))->ob_fval);
151    } else if (PyInt_Check(p)==1) {
152        return (double)(((PyIntObject *)(p))->ob_ival);
153    } else if (PyLong_Check(p)==1) {
154        return (double)PyLong_AsLong(p);
155    } else {
156        return 0.0;
157    }
158}
159/**
160 * Function to call to evaluate model
161 * @param args: input numpy array q[]
162 * @return: numpy array object
163 */
164 
165static PyObject *evaluateOneDim(StackedDisksModel* model, PyArrayObject *q){
166    PyArrayObject *result;
167   
168    // Check validity of array q , q must be of dimension 1, an array of double
169    if (q->nd != 1 || q->descr->type_num != PyArray_DOUBLE)
170    {
171        //const char * message= "Invalid array: q->nd=%d,type_num=%d\n",q->nd,q->descr->type_num;
172        //PyErr_SetString(PyExc_ValueError , message);
173        return NULL;
174    }
175    result = (PyArrayObject *)PyArray_FromDims(q->nd, (int *)(q->dimensions), 
176                                                                                  PyArray_DOUBLE);
177        if (result == NULL) {
178        const char * message= "Could not create result ";
179        PyErr_SetString(PyExc_RuntimeError , message);
180                return NULL;
181        }
182         for (int i = 0; i < q->dimensions[0]; i++){
183      double q_value  = *(double *)(q->data + i*q->strides[0]);
184      double *result_value = (double *)(result->data + i*result->strides[0]);
185      *result_value =(*model)(q_value);
186        }
187    return PyArray_Return(result); 
188 }
189
190 /**
191 * Function to call to evaluate model
192 * @param args: input numpy array  [x[],y[]]
193 * @return: numpy array object
194 */
195 static PyObject * evaluateTwoDimXY( StackedDisksModel* model, 
196                              PyArrayObject *x, PyArrayObject *y)
197 {
198    PyArrayObject *result;
199    int i,j, x_len, y_len, dims[2];
200    //check validity of input vectors
201    if (x->nd != 2 || x->descr->type_num != PyArray_DOUBLE
202        || y->nd != 2 || y->descr->type_num != PyArray_DOUBLE
203        || y->dimensions[1] != x->dimensions[0]){
204        const char * message= "evaluateTwoDimXY  expect 2 numpy arrays";
205        PyErr_SetString(PyExc_ValueError , message); 
206        return NULL;
207    }
208   
209        if (PyArray_Check(x) && PyArray_Check(y)) {
210               
211            x_len = dims[1]= x->dimensions[1];
212        y_len = dims[0]= y->dimensions[0];
213           
214            // Make a new double matrix of same dims
215        result=(PyArrayObject *) PyArray_FromDims(2,dims,NPY_DOUBLE);
216        if (result == NULL){
217            const char * message= "Could not create result ";
218        PyErr_SetString(PyExc_RuntimeError , message);
219            return NULL;
220            }
221       
222        /* Do the calculation. */
223        for ( j=0; j< y_len; j++) {
224            for ( i=0; i< x_len; i++) {
225                double x_value = *(double *)(x->data + i*x->strides[1]);
226                    double y_value = *(double *)(y->data + j*y->strides[0]);
227                        double *result_value = (double *)(result->data +
228                              j*result->strides[0] + i*result->strides[1]);
229                        *result_value = (*model)(x_value, y_value);
230            }           
231        }
232        return PyArray_Return(result); 
233       
234        }else{
235                    PyErr_SetString(CStackedDisksModelError, 
236                   "CStackedDisksModel.evaluateTwoDimXY couldn't run.");
237                return NULL;
238                }       
239}
240/**
241 *  evalDistribution function evaluate a model function with input vector
242 *  @param args: input q as vector or [qx, qy] where qx, qy are vectors
243 *
244 */ 
245static PyObject * evalDistribution(CStackedDisksModel *self, PyObject *args){
246        PyObject *qx, *qy;
247        PyArrayObject * pars;
248        int npars ,mpars;
249       
250        // Get parameters
251       
252            // Reader parameter dictionary
253    self->model->core_sld = PyFloat_AsDouble( PyDict_GetItemString(self->params, "core_sld") );
254    self->model->core_thick = PyFloat_AsDouble( PyDict_GetItemString(self->params, "core_thick") );
255    self->model->layer_thick = PyFloat_AsDouble( PyDict_GetItemString(self->params, "layer_thick") );
256    self->model->axis_theta = PyFloat_AsDouble( PyDict_GetItemString(self->params, "axis_theta") );
257    self->model->layer_sld = PyFloat_AsDouble( PyDict_GetItemString(self->params, "layer_sld") );
258    self->model->axis_phi = PyFloat_AsDouble( PyDict_GetItemString(self->params, "axis_phi") );
259    self->model->solvent_sld = PyFloat_AsDouble( PyDict_GetItemString(self->params, "solvent_sld") );
260    self->model->scale = PyFloat_AsDouble( PyDict_GetItemString(self->params, "scale") );
261    self->model->radius = PyFloat_AsDouble( PyDict_GetItemString(self->params, "radius") );
262    self->model->background = PyFloat_AsDouble( PyDict_GetItemString(self->params, "background") );
263    self->model->sigma_d = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sigma_d") );
264    self->model->n_stacking = PyFloat_AsDouble( PyDict_GetItemString(self->params, "n_stacking") );
265    // Read in dispersion parameters
266    PyObject* disp_dict;
267    DispersionVisitor* visitor = new DispersionVisitor();
268    disp_dict = PyDict_GetItemString(self->dispersion, "core_thick");
269    self->model->core_thick.dispersion->accept_as_destination(visitor, self->model->core_thick.dispersion, disp_dict);
270    disp_dict = PyDict_GetItemString(self->dispersion, "layer_thick");
271    self->model->layer_thick.dispersion->accept_as_destination(visitor, self->model->layer_thick.dispersion, disp_dict);
272    disp_dict = PyDict_GetItemString(self->dispersion, "radius");
273    self->model->radius.dispersion->accept_as_destination(visitor, self->model->radius.dispersion, disp_dict);
274    disp_dict = PyDict_GetItemString(self->dispersion, "axis_theta");
275    self->model->axis_theta.dispersion->accept_as_destination(visitor, self->model->axis_theta.dispersion, disp_dict);
276    disp_dict = PyDict_GetItemString(self->dispersion, "axis_phi");
277    self->model->axis_phi.dispersion->accept_as_destination(visitor, self->model->axis_phi.dispersion, disp_dict);
278
279       
280        // Get input and determine whether we have to supply a 1D or 2D return value.
281        if ( !PyArg_ParseTuple(args,"O",&pars) ) {
282            PyErr_SetString(CStackedDisksModelError, 
283                "CStackedDisksModel.evalDistribution expects a q value.");
284                return NULL;
285        }
286    // Check params
287       
288    if(PyArray_Check(pars)==1) {
289               
290            // Length of list should 1 or 2
291            npars = pars->nd; 
292            if(npars==1) {
293                // input is a numpy array
294                if (PyArray_Check(pars)) {
295                        return evaluateOneDim(self->model, (PyArrayObject*)pars); 
296                    }
297                }else{
298                    PyErr_SetString(CStackedDisksModelError, 
299                   "CStackedDisksModel.evalDistribution expect numpy array of one dimension.");
300                return NULL;
301                }
302    }else if( PyList_Check(pars)==1) {
303        // Length of list should be 2 for I(qx,qy)
304            mpars = PyList_GET_SIZE(pars); 
305            if(mpars!=2) {
306                PyErr_SetString(CStackedDisksModelError, 
307                        "CStackedDisksModel.evalDistribution expects a list of dimension 2.");
308                return NULL;
309            }
310             qx = PyList_GET_ITEM(pars,0);
311             qy = PyList_GET_ITEM(pars,1);
312             if (PyArray_Check(qx) && PyArray_Check(qy)) {
313                 return evaluateTwoDimXY(self->model, (PyArrayObject*)qx,
314                           (PyArrayObject*)qy);
315                 }else{
316                    PyErr_SetString(CStackedDisksModelError, 
317                   "CStackedDisksModel.evalDistribution expect 2 numpy arrays in list.");
318                return NULL;
319             }
320        }
321        PyErr_SetString(CStackedDisksModelError, 
322                   "CStackedDisksModel.evalDistribution couln't be run.");
323        return NULL;
324       
325}
326
327/**
328 * Function to call to evaluate model
329 * @param args: input q or [q,phi]
330 * @return: function value
331 */
332static PyObject * run(CStackedDisksModel *self, PyObject *args) {
333        double q_value, phi_value;
334        PyObject* pars;
335        int npars;
336       
337        // Get parameters
338       
339            // Reader parameter dictionary
340    self->model->core_sld = PyFloat_AsDouble( PyDict_GetItemString(self->params, "core_sld") );
341    self->model->core_thick = PyFloat_AsDouble( PyDict_GetItemString(self->params, "core_thick") );
342    self->model->layer_thick = PyFloat_AsDouble( PyDict_GetItemString(self->params, "layer_thick") );
343    self->model->axis_theta = PyFloat_AsDouble( PyDict_GetItemString(self->params, "axis_theta") );
344    self->model->layer_sld = PyFloat_AsDouble( PyDict_GetItemString(self->params, "layer_sld") );
345    self->model->axis_phi = PyFloat_AsDouble( PyDict_GetItemString(self->params, "axis_phi") );
346    self->model->solvent_sld = PyFloat_AsDouble( PyDict_GetItemString(self->params, "solvent_sld") );
347    self->model->scale = PyFloat_AsDouble( PyDict_GetItemString(self->params, "scale") );
348    self->model->radius = PyFloat_AsDouble( PyDict_GetItemString(self->params, "radius") );
349    self->model->background = PyFloat_AsDouble( PyDict_GetItemString(self->params, "background") );
350    self->model->sigma_d = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sigma_d") );
351    self->model->n_stacking = PyFloat_AsDouble( PyDict_GetItemString(self->params, "n_stacking") );
352    // Read in dispersion parameters
353    PyObject* disp_dict;
354    DispersionVisitor* visitor = new DispersionVisitor();
355    disp_dict = PyDict_GetItemString(self->dispersion, "core_thick");
356    self->model->core_thick.dispersion->accept_as_destination(visitor, self->model->core_thick.dispersion, disp_dict);
357    disp_dict = PyDict_GetItemString(self->dispersion, "layer_thick");
358    self->model->layer_thick.dispersion->accept_as_destination(visitor, self->model->layer_thick.dispersion, disp_dict);
359    disp_dict = PyDict_GetItemString(self->dispersion, "radius");
360    self->model->radius.dispersion->accept_as_destination(visitor, self->model->radius.dispersion, disp_dict);
361    disp_dict = PyDict_GetItemString(self->dispersion, "axis_theta");
362    self->model->axis_theta.dispersion->accept_as_destination(visitor, self->model->axis_theta.dispersion, disp_dict);
363    disp_dict = PyDict_GetItemString(self->dispersion, "axis_phi");
364    self->model->axis_phi.dispersion->accept_as_destination(visitor, self->model->axis_phi.dispersion, disp_dict);
365
366       
367        // Get input and determine whether we have to supply a 1D or 2D return value.
368        if ( !PyArg_ParseTuple(args,"O",&pars) ) {
369            PyErr_SetString(CStackedDisksModelError, 
370                "CStackedDisksModel.run expects a q value.");
371                return NULL;
372        }
373         
374        // Check params
375        if( PyList_Check(pars)==1) {
376               
377                // Length of list should be 2 for I(q,phi)
378            npars = PyList_GET_SIZE(pars); 
379            if(npars!=2) {
380                PyErr_SetString(CStackedDisksModelError, 
381                        "CStackedDisksModel.run expects a double or a list of dimension 2.");
382                return NULL;
383            }
384            // We have a vector q, get the q and phi values at which
385            // to evaluate I(q,phi)
386            q_value = CStackedDisksModel_readDouble(PyList_GET_ITEM(pars,0));
387            phi_value = CStackedDisksModel_readDouble(PyList_GET_ITEM(pars,1));
388            // Skip zero
389            if (q_value==0) {
390                return Py_BuildValue("d",0.0);
391            }
392                return Py_BuildValue("d",(*(self->model)).evaluate_rphi(q_value,phi_value));
393
394        } else {
395
396                // We have a scalar q, we will evaluate I(q)
397                q_value = CStackedDisksModel_readDouble(pars);         
398               
399                return Py_BuildValue("d",(*(self->model))(q_value));
400        }       
401}
402/**
403 * Function to call to calculate_ER
404 * @return: effective radius value
405 */
406static PyObject * calculate_ER(CStackedDisksModel *self) {
407
408        PyObject* pars;
409        int npars;
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    CStackedDisksModelError = PyErr_NewException("CStackedDisksModel.error", NULL, NULL);
640    PyDict_SetItemString(d, "CStackedDisksModelError", CStackedDisksModelError);
641}
642
Note: See TracBrowser for help on using the repository browser.