source: sasview/sansmodels/src/sans/models/c_models/CStackedDisksModel.cpp @ 0a9871c

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 0a9871c was 35aface, checked in by Jae Cho <jhjcho@…>, 14 years ago

addede new models and attr. non_fittable

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