source: sasview/sansmodels/src/sans/models/c_models/CCoreShellEllipsoidModel.cpp @ 0438933

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