source: sasview/sansmodels/src/python_wrapper/CCoreShellEllipsoidModel.cpp @ 6e10cff

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

Reorganizing models in preparation of cpp cleanup

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