source: sasview/sansmodels/src/sans/models/c_models/CStackedDisksModel.cpp @ 916f5c0

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 916f5c0 was 2605da22, checked in by Mathieu Doucet <doucetm@…>, 13 years ago

Re #4 Still a few more warnings

  • 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.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), 
180                                                                                  PyArray_DOUBLE);
181        if (result == NULL) {
182        const char * message= "Could not create result ";
183        PyErr_SetString(PyExc_RuntimeError , message);
184                return NULL;
185        }
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 i, 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        for ( i=0; i< x_len; i++) {
228            double x_value = *(double *)(x->data + i*x->strides[0]);
229                    double y_value = *(double *)(y->data + i*y->strides[0]);
230                        double *result_value = (double *)(result->data +
231                              i*result->strides[0]);
232                        *result_value = (*model)(x_value, y_value);
233        }           
234        return PyArray_Return(result); 
235       
236        }else{
237                    PyErr_SetString(CStackedDisksModelError, 
238                   "CStackedDisksModel.evaluateTwoDimXY couldn't run.");
239                return NULL;
240                }       
241}
242/**
243 *  evalDistribution function evaluate a model function with input vector
244 *  @param args: input q as vector or [qx, qy] where qx, qy are vectors
245 *
246 */ 
247static PyObject * evalDistribution(CStackedDisksModel *self, PyObject *args){
248        PyObject *qx, *qy;
249        PyArrayObject * pars;
250        int npars ,mpars;
251       
252        // Get parameters
253       
254            // Reader parameter dictionary
255    self->model->core_sld = PyFloat_AsDouble( PyDict_GetItemString(self->params, "core_sld") );
256    self->model->core_thick = PyFloat_AsDouble( PyDict_GetItemString(self->params, "core_thick") );
257    self->model->layer_thick = PyFloat_AsDouble( PyDict_GetItemString(self->params, "layer_thick") );
258    self->model->axis_theta = PyFloat_AsDouble( PyDict_GetItemString(self->params, "axis_theta") );
259    self->model->layer_sld = PyFloat_AsDouble( PyDict_GetItemString(self->params, "layer_sld") );
260    self->model->axis_phi = PyFloat_AsDouble( PyDict_GetItemString(self->params, "axis_phi") );
261    self->model->solvent_sld = PyFloat_AsDouble( PyDict_GetItemString(self->params, "solvent_sld") );
262    self->model->scale = PyFloat_AsDouble( PyDict_GetItemString(self->params, "scale") );
263    self->model->radius = PyFloat_AsDouble( PyDict_GetItemString(self->params, "radius") );
264    self->model->background = PyFloat_AsDouble( PyDict_GetItemString(self->params, "background") );
265    self->model->sigma_d = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sigma_d") );
266    self->model->n_stacking = PyFloat_AsDouble( PyDict_GetItemString(self->params, "n_stacking") );
267    // Read in dispersion parameters
268    PyObject* disp_dict;
269    DispersionVisitor* visitor = new DispersionVisitor();
270    disp_dict = PyDict_GetItemString(self->dispersion, "core_thick");
271    self->model->core_thick.dispersion->accept_as_destination(visitor, self->model->core_thick.dispersion, disp_dict);
272    disp_dict = PyDict_GetItemString(self->dispersion, "layer_thick");
273    self->model->layer_thick.dispersion->accept_as_destination(visitor, self->model->layer_thick.dispersion, disp_dict);
274    disp_dict = PyDict_GetItemString(self->dispersion, "radius");
275    self->model->radius.dispersion->accept_as_destination(visitor, self->model->radius.dispersion, disp_dict);
276    disp_dict = PyDict_GetItemString(self->dispersion, "axis_theta");
277    self->model->axis_theta.dispersion->accept_as_destination(visitor, self->model->axis_theta.dispersion, disp_dict);
278    disp_dict = PyDict_GetItemString(self->dispersion, "axis_phi");
279    self->model->axis_phi.dispersion->accept_as_destination(visitor, self->model->axis_phi.dispersion, disp_dict);
280
281       
282        // Get input and determine whether we have to supply a 1D or 2D return value.
283        if ( !PyArg_ParseTuple(args,"O",&pars) ) {
284            PyErr_SetString(CStackedDisksModelError, 
285                "CStackedDisksModel.evalDistribution expects a q value.");
286                return NULL;
287        }
288    // Check params
289       
290    if(PyArray_Check(pars)==1) {
291               
292            // Length of list should 1 or 2
293            npars = pars->nd; 
294            if(npars==1) {
295                // input is a numpy array
296                if (PyArray_Check(pars)) {
297                        return evaluateOneDim(self->model, (PyArrayObject*)pars); 
298                    }
299                }else{
300                    PyErr_SetString(CStackedDisksModelError, 
301                   "CStackedDisksModel.evalDistribution expect numpy array of one dimension.");
302                return NULL;
303                }
304    }else if( PyList_Check(pars)==1) {
305        // Length of list should be 2 for I(qx,qy)
306            mpars = PyList_GET_SIZE(pars); 
307            if(mpars!=2) {
308                PyErr_SetString(CStackedDisksModelError, 
309                        "CStackedDisksModel.evalDistribution expects a list of dimension 2.");
310                return NULL;
311            }
312             qx = PyList_GET_ITEM(pars,0);
313             qy = PyList_GET_ITEM(pars,1);
314             if (PyArray_Check(qx) && PyArray_Check(qy)) {
315                 return evaluateTwoDimXY(self->model, (PyArrayObject*)qx,
316                           (PyArrayObject*)qy);
317                 }else{
318                    PyErr_SetString(CStackedDisksModelError, 
319                   "CStackedDisksModel.evalDistribution expect 2 numpy arrays in list.");
320                return NULL;
321             }
322        }
323        PyErr_SetString(CStackedDisksModelError, 
324                   "CStackedDisksModel.evalDistribution couln't be run.");
325        return NULL;
326       
327}
328
329/**
330 * Function to call to evaluate model
331 * @param args: input q or [q,phi]
332 * @return: function value
333 */
334static PyObject * run(CStackedDisksModel *self, PyObject *args) {
335        double q_value, phi_value;
336        PyObject* pars;
337        int npars;
338       
339        // Get parameters
340       
341            // Reader parameter dictionary
342    self->model->core_sld = PyFloat_AsDouble( PyDict_GetItemString(self->params, "core_sld") );
343    self->model->core_thick = PyFloat_AsDouble( PyDict_GetItemString(self->params, "core_thick") );
344    self->model->layer_thick = PyFloat_AsDouble( PyDict_GetItemString(self->params, "layer_thick") );
345    self->model->axis_theta = PyFloat_AsDouble( PyDict_GetItemString(self->params, "axis_theta") );
346    self->model->layer_sld = PyFloat_AsDouble( PyDict_GetItemString(self->params, "layer_sld") );
347    self->model->axis_phi = PyFloat_AsDouble( PyDict_GetItemString(self->params, "axis_phi") );
348    self->model->solvent_sld = PyFloat_AsDouble( PyDict_GetItemString(self->params, "solvent_sld") );
349    self->model->scale = PyFloat_AsDouble( PyDict_GetItemString(self->params, "scale") );
350    self->model->radius = PyFloat_AsDouble( PyDict_GetItemString(self->params, "radius") );
351    self->model->background = PyFloat_AsDouble( PyDict_GetItemString(self->params, "background") );
352    self->model->sigma_d = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sigma_d") );
353    self->model->n_stacking = PyFloat_AsDouble( PyDict_GetItemString(self->params, "n_stacking") );
354    // Read in dispersion parameters
355    PyObject* disp_dict;
356    DispersionVisitor* visitor = new DispersionVisitor();
357    disp_dict = PyDict_GetItemString(self->dispersion, "core_thick");
358    self->model->core_thick.dispersion->accept_as_destination(visitor, self->model->core_thick.dispersion, disp_dict);
359    disp_dict = PyDict_GetItemString(self->dispersion, "layer_thick");
360    self->model->layer_thick.dispersion->accept_as_destination(visitor, self->model->layer_thick.dispersion, disp_dict);
361    disp_dict = PyDict_GetItemString(self->dispersion, "radius");
362    self->model->radius.dispersion->accept_as_destination(visitor, self->model->radius.dispersion, disp_dict);
363    disp_dict = PyDict_GetItemString(self->dispersion, "axis_theta");
364    self->model->axis_theta.dispersion->accept_as_destination(visitor, self->model->axis_theta.dispersion, disp_dict);
365    disp_dict = PyDict_GetItemString(self->dispersion, "axis_phi");
366    self->model->axis_phi.dispersion->accept_as_destination(visitor, self->model->axis_phi.dispersion, disp_dict);
367
368       
369        // Get input and determine whether we have to supply a 1D or 2D return value.
370        if ( !PyArg_ParseTuple(args,"O",&pars) ) {
371            PyErr_SetString(CStackedDisksModelError, 
372                "CStackedDisksModel.run expects a q value.");
373                return NULL;
374        }
375         
376        // Check params
377        if( PyList_Check(pars)==1) {
378               
379                // Length of list should be 2 for I(q,phi)
380            npars = PyList_GET_SIZE(pars); 
381            if(npars!=2) {
382                PyErr_SetString(CStackedDisksModelError, 
383                        "CStackedDisksModel.run expects a double or a list of dimension 2.");
384                return NULL;
385            }
386            // We have a vector q, get the q and phi values at which
387            // to evaluate I(q,phi)
388            q_value = CStackedDisksModel_readDouble(PyList_GET_ITEM(pars,0));
389            phi_value = CStackedDisksModel_readDouble(PyList_GET_ITEM(pars,1));
390            // Skip zero
391            if (q_value==0) {
392                return Py_BuildValue("d",0.0);
393            }
394                return Py_BuildValue("d",(*(self->model)).evaluate_rphi(q_value,phi_value));
395
396        } else {
397
398                // We have a scalar q, we will evaluate I(q)
399                q_value = CStackedDisksModel_readDouble(pars);         
400               
401                return Py_BuildValue("d",(*(self->model))(q_value));
402        }       
403}
404/**
405 * Function to call to calculate_ER
406 * @return: effective radius value
407 */
408static PyObject * calculate_ER(CStackedDisksModel *self) {
409
410        // Get parameters
411       
412            // Reader parameter dictionary
413    self->model->core_sld = PyFloat_AsDouble( PyDict_GetItemString(self->params, "core_sld") );
414    self->model->core_thick = PyFloat_AsDouble( PyDict_GetItemString(self->params, "core_thick") );
415    self->model->layer_thick = PyFloat_AsDouble( PyDict_GetItemString(self->params, "layer_thick") );
416    self->model->axis_theta = PyFloat_AsDouble( PyDict_GetItemString(self->params, "axis_theta") );
417    self->model->layer_sld = PyFloat_AsDouble( PyDict_GetItemString(self->params, "layer_sld") );
418    self->model->axis_phi = PyFloat_AsDouble( PyDict_GetItemString(self->params, "axis_phi") );
419    self->model->solvent_sld = PyFloat_AsDouble( PyDict_GetItemString(self->params, "solvent_sld") );
420    self->model->scale = PyFloat_AsDouble( PyDict_GetItemString(self->params, "scale") );
421    self->model->radius = PyFloat_AsDouble( PyDict_GetItemString(self->params, "radius") );
422    self->model->background = PyFloat_AsDouble( PyDict_GetItemString(self->params, "background") );
423    self->model->sigma_d = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sigma_d") );
424    self->model->n_stacking = PyFloat_AsDouble( PyDict_GetItemString(self->params, "n_stacking") );
425    // Read in dispersion parameters
426    PyObject* disp_dict;
427    DispersionVisitor* visitor = new DispersionVisitor();
428    disp_dict = PyDict_GetItemString(self->dispersion, "core_thick");
429    self->model->core_thick.dispersion->accept_as_destination(visitor, self->model->core_thick.dispersion, disp_dict);
430    disp_dict = PyDict_GetItemString(self->dispersion, "layer_thick");
431    self->model->layer_thick.dispersion->accept_as_destination(visitor, self->model->layer_thick.dispersion, disp_dict);
432    disp_dict = PyDict_GetItemString(self->dispersion, "radius");
433    self->model->radius.dispersion->accept_as_destination(visitor, self->model->radius.dispersion, disp_dict);
434    disp_dict = PyDict_GetItemString(self->dispersion, "axis_theta");
435    self->model->axis_theta.dispersion->accept_as_destination(visitor, self->model->axis_theta.dispersion, disp_dict);
436    disp_dict = PyDict_GetItemString(self->dispersion, "axis_phi");
437    self->model->axis_phi.dispersion->accept_as_destination(visitor, self->model->axis_phi.dispersion, disp_dict);
438
439               
440        return Py_BuildValue("d",(*(self->model)).calculate_ER());
441
442}
443/**
444 * Function to call to evaluate model in cartesian coordinates
445 * @param args: input q or [qx, qy]]
446 * @return: function value
447 */
448static PyObject * runXY(CStackedDisksModel *self, PyObject *args) {
449        double qx_value, qy_value;
450        PyObject* pars;
451        int npars;
452       
453        // Get parameters
454       
455            // Reader parameter dictionary
456    self->model->core_sld = PyFloat_AsDouble( PyDict_GetItemString(self->params, "core_sld") );
457    self->model->core_thick = PyFloat_AsDouble( PyDict_GetItemString(self->params, "core_thick") );
458    self->model->layer_thick = PyFloat_AsDouble( PyDict_GetItemString(self->params, "layer_thick") );
459    self->model->axis_theta = PyFloat_AsDouble( PyDict_GetItemString(self->params, "axis_theta") );
460    self->model->layer_sld = PyFloat_AsDouble( PyDict_GetItemString(self->params, "layer_sld") );
461    self->model->axis_phi = PyFloat_AsDouble( PyDict_GetItemString(self->params, "axis_phi") );
462    self->model->solvent_sld = PyFloat_AsDouble( PyDict_GetItemString(self->params, "solvent_sld") );
463    self->model->scale = PyFloat_AsDouble( PyDict_GetItemString(self->params, "scale") );
464    self->model->radius = PyFloat_AsDouble( PyDict_GetItemString(self->params, "radius") );
465    self->model->background = PyFloat_AsDouble( PyDict_GetItemString(self->params, "background") );
466    self->model->sigma_d = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sigma_d") );
467    self->model->n_stacking = PyFloat_AsDouble( PyDict_GetItemString(self->params, "n_stacking") );
468    // Read in dispersion parameters
469    PyObject* disp_dict;
470    DispersionVisitor* visitor = new DispersionVisitor();
471    disp_dict = PyDict_GetItemString(self->dispersion, "core_thick");
472    self->model->core_thick.dispersion->accept_as_destination(visitor, self->model->core_thick.dispersion, disp_dict);
473    disp_dict = PyDict_GetItemString(self->dispersion, "layer_thick");
474    self->model->layer_thick.dispersion->accept_as_destination(visitor, self->model->layer_thick.dispersion, disp_dict);
475    disp_dict = PyDict_GetItemString(self->dispersion, "radius");
476    self->model->radius.dispersion->accept_as_destination(visitor, self->model->radius.dispersion, disp_dict);
477    disp_dict = PyDict_GetItemString(self->dispersion, "axis_theta");
478    self->model->axis_theta.dispersion->accept_as_destination(visitor, self->model->axis_theta.dispersion, disp_dict);
479    disp_dict = PyDict_GetItemString(self->dispersion, "axis_phi");
480    self->model->axis_phi.dispersion->accept_as_destination(visitor, self->model->axis_phi.dispersion, disp_dict);
481
482       
483        // Get input and determine whether we have to supply a 1D or 2D return value.
484        if ( !PyArg_ParseTuple(args,"O",&pars) ) {
485            PyErr_SetString(CStackedDisksModelError, 
486                "CStackedDisksModel.run expects a q value.");
487                return NULL;
488        }
489         
490        // Check params
491        if( PyList_Check(pars)==1) {
492               
493                // Length of list should be 2 for I(qx, qy))
494            npars = PyList_GET_SIZE(pars); 
495            if(npars!=2) {
496                PyErr_SetString(CStackedDisksModelError, 
497                        "CStackedDisksModel.run expects a double or a list of dimension 2.");
498                return NULL;
499            }
500            // We have a vector q, get the qx and qy values at which
501            // to evaluate I(qx,qy)
502            qx_value = CStackedDisksModel_readDouble(PyList_GET_ITEM(pars,0));
503            qy_value = CStackedDisksModel_readDouble(PyList_GET_ITEM(pars,1));
504            return Py_BuildValue("d",(*(self->model))(qx_value,qy_value));
505
506        } else {
507
508                // We have a scalar q, we will evaluate I(q)
509                qx_value = CStackedDisksModel_readDouble(pars);         
510               
511                return Py_BuildValue("d",(*(self->model))(qx_value));
512        }       
513}
514
515static PyObject * reset(CStackedDisksModel *self, PyObject *args) {
516   
517
518    return Py_BuildValue("d",0.0);
519}
520
521static PyObject * set_dispersion(CStackedDisksModel *self, PyObject *args) {
522        PyObject * disp;
523        const char * par_name;
524
525        if ( !PyArg_ParseTuple(args,"sO", &par_name, &disp) ) {
526            PyErr_SetString(CStackedDisksModelError,
527                "CStackedDisksModel.set_dispersion expects a DispersionModel object.");
528                return NULL;
529        }
530        void *temp = PyCObject_AsVoidPtr(disp);
531        DispersionModel * dispersion = static_cast<DispersionModel *>(temp);
532
533
534        // Ugliness necessary to go from python to C
535            // TODO: refactor this
536    if (!strcmp(par_name, "core_thick")) {
537        self->model->core_thick.dispersion = dispersion;
538    } else    if (!strcmp(par_name, "layer_thick")) {
539        self->model->layer_thick.dispersion = dispersion;
540    } else    if (!strcmp(par_name, "radius")) {
541        self->model->radius.dispersion = dispersion;
542    } else    if (!strcmp(par_name, "axis_theta")) {
543        self->model->axis_theta.dispersion = dispersion;
544    } else    if (!strcmp(par_name, "axis_phi")) {
545        self->model->axis_phi.dispersion = dispersion;
546    } else {
547            PyErr_SetString(CStackedDisksModelError,
548                "CStackedDisksModel.set_dispersion expects a valid parameter name.");
549                return NULL;
550        }
551
552        DispersionVisitor* visitor = new DispersionVisitor();
553        PyObject * disp_dict = PyDict_New();
554        dispersion->accept_as_source(visitor, dispersion, disp_dict);
555        PyDict_SetItemString(self->dispersion, par_name, disp_dict);
556    return Py_BuildValue("i",1);
557}
558
559
560static PyMethodDef CStackedDisksModel_methods[] = {
561    {"run",      (PyCFunction)run     , METH_VARARGS,
562      "Evaluate the model at a given Q or Q, phi"},
563    {"runXY",      (PyCFunction)runXY     , METH_VARARGS,
564      "Evaluate the model at a given Q or Qx, Qy"},
565    {"calculate_ER",      (PyCFunction)calculate_ER     , METH_VARARGS,
566      "Evaluate the model at a given Q or Q, phi"},
567     
568    {"evalDistribution",  (PyCFunction)evalDistribution , METH_VARARGS,
569      "Evaluate the model at a given Q or Qx, Qy vector "},
570    {"reset",    (PyCFunction)reset   , METH_VARARGS,
571      "Reset pair correlation"},
572    {"set_dispersion",      (PyCFunction)set_dispersion     , METH_VARARGS,
573      "Set the dispersion model for a given parameter"},
574   {NULL}
575};
576
577static PyTypeObject CStackedDisksModelType = {
578    PyObject_HEAD_INIT(NULL)
579    0,                         /*ob_size*/
580    "CStackedDisksModel",             /*tp_name*/
581    sizeof(CStackedDisksModel),             /*tp_basicsize*/
582    0,                         /*tp_itemsize*/
583    (destructor)CStackedDisksModel_dealloc, /*tp_dealloc*/
584    0,                         /*tp_print*/
585    0,                         /*tp_getattr*/
586    0,                         /*tp_setattr*/
587    0,                         /*tp_compare*/
588    0,                         /*tp_repr*/
589    0,                         /*tp_as_number*/
590    0,                         /*tp_as_sequence*/
591    0,                         /*tp_as_mapping*/
592    0,                         /*tp_hash */
593    0,                         /*tp_call*/
594    0,                         /*tp_str*/
595    0,                         /*tp_getattro*/
596    0,                         /*tp_setattro*/
597    0,                         /*tp_as_buffer*/
598    Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
599    "CStackedDisksModel objects",           /* tp_doc */
600    0,                         /* tp_traverse */
601    0,                         /* tp_clear */
602    0,                         /* tp_richcompare */
603    0,                         /* tp_weaklistoffset */
604    0,                         /* tp_iter */
605    0,                         /* tp_iternext */
606    CStackedDisksModel_methods,             /* tp_methods */
607    CStackedDisksModel_members,             /* tp_members */
608    0,                         /* tp_getset */
609    0,                         /* tp_base */
610    0,                         /* tp_dict */
611    0,                         /* tp_descr_get */
612    0,                         /* tp_descr_set */
613    0,                         /* tp_dictoffset */
614    (initproc)CStackedDisksModel_init,      /* tp_init */
615    0,                         /* tp_alloc */
616    CStackedDisksModel_new,                 /* tp_new */
617};
618
619
620//static PyMethodDef module_methods[] = {
621//    {NULL}
622//};
623
624/**
625 * Function used to add the model class to a module
626 * @param module: module to add the class to
627 */ 
628void addCStackedDisksModel(PyObject *module) {
629        PyObject *d;
630       
631    if (PyType_Ready(&CStackedDisksModelType) < 0)
632        return;
633
634    Py_INCREF(&CStackedDisksModelType);
635    PyModule_AddObject(module, "CStackedDisksModel", (PyObject *)&CStackedDisksModelType);
636   
637    d = PyModule_GetDict(module);
638    static char error_name[] = "CStackedDisksModel.error";
639    CStackedDisksModelError = PyErr_NewException(error_name, NULL, NULL);
640    PyDict_SetItemString(d, "CStackedDisksModelError", CStackedDisksModelError);
641}
642
Note: See TracBrowser for help on using the repository browser.