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

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

addede new models and attr. non_fittable

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