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

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 eb575b0 was 9ce41c6, checked in by Gervaise Alina <gervyh@…>, 15 years ago

wrote unittest for all model untested , haven' test critial point error handling

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