source: sasview/sansmodels/src/sans/models/c_models/CCoreShellSpheroidModel.cpp @ 975ec8e

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 975ec8e was 975ec8e, checked in by Jae Cho <jhjcho@…>, 15 years ago

working on 2D models. Still need smore corrections and unit tests.

  • Property mode set to 100644
File size: 24.9 KB
Line 
1/**
2        This software was developed by the University of Tennessee as part of the
3        Distributed Data Analysis of Neutron Scattering Experiments (DANSE)
4        project funded by the US National Science Foundation.
5
6        If you use DANSE applications to do scientific research that leads to
7        publication, we ask that you acknowledge the use of the software with the
8        following sentence:
9
10        "This work benefited from DANSE software developed under NSF award DMR-0520547."
11
12        copyright 2008, University of Tennessee
13 */
14
15/** CCoreShellSpheroidModel
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 * CCoreShellSpheroidModelError = 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    CoreShellSpheroidModel * model;
54    /// Log for unit testing
55    PyObject * log;
56} CCoreShellSpheroidModel;
57
58
59static void
60CCoreShellSpheroidModel_dealloc(CCoreShellSpheroidModel* self)
61{
62    self->ob_type->tp_free((PyObject*)self);
63   
64
65}
66
67static PyObject *
68CCoreShellSpheroidModel_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
69{
70    CCoreShellSpheroidModel *self;
71   
72    self = (CCoreShellSpheroidModel *)type->tp_alloc(type, 0);
73   
74    return (PyObject *)self;
75}
76
77static int
78CCoreShellSpheroidModel_init(CCoreShellSpheroidModel *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 CoreShellSpheroidModel();
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 CCoreShellSpheroidModel_members[] = {
132    {"params", T_OBJECT, offsetof(CCoreShellSpheroidModel, params), 0,
133     "Parameters"},
134        {"dispersion", T_OBJECT, offsetof(CCoreShellSpheroidModel, dispersion), 0,
135          "Dispersion parameters"},     
136    {"log", T_OBJECT, offsetof(CCoreShellSpheroidModel, log), 0,
137     "Log"},
138    {NULL}  /* Sentinel */
139};
140
141/** Read double from PyObject
142    @param p PyObject
143    @return double
144*/
145double CCoreShellSpheroidModel_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(CoreShellSpheroidModel* 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( CoreShellSpheroidModel* 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            x_len = dims[0]= x->dimensions[0];
208        y_len = dims[1]= y->dimensions[1];
209           
210            // Make a new double matrix of same dims
211        result=(PyArrayObject *) PyArray_FromDims(2,dims,NPY_DOUBLE);
212        if (result == NULL){
213            const char * message= "Could not create result ";
214        PyErr_SetString(PyExc_RuntimeError , message);
215            return NULL;
216            }
217       
218        /* Do the calculation. */
219        for ( i=0; i< x_len; i++) {
220            for ( j=0; j< y_len; j++) {
221                double x_value = *(double *)(x->data + i*x->strides[0]);
222                    double y_value = *(double *)(y->data + j*y->strides[1]);
223                        double *result_value = (double *)(result->data +
224                              i*result->strides[0] + j*result->strides[1]);
225                        *result_value = (*model)(x_value, y_value);
226            }           
227        }
228        return PyArray_Return(result); 
229       
230        }else{
231                    PyErr_SetString(CCoreShellSpheroidModelError, 
232                   "CCoreShellSpheroidModel.evaluateTwoDimXY couldn't run.");
233                return NULL;
234                }       
235}
236/**
237 *  evalDistribution function evaluate a model function with input vector
238 *  @param args: input q as vector or [qx, qy] where qx, qy are vectors
239 *
240 */ 
241static PyObject * evalDistribution(CCoreShellSpheroidModel *self, PyObject *args){
242        PyObject *qx, *qy;
243        PyArrayObject * pars;
244        int npars ,mpars;
245       
246        // Get parameters
247       
248            // Reader parameter dictionary
249    self->model->scale = PyFloat_AsDouble( PyDict_GetItemString(self->params, "scale") );
250    self->model->axis_theta = PyFloat_AsDouble( PyDict_GetItemString(self->params, "axis_theta") );
251    self->model->polar_shell = PyFloat_AsDouble( PyDict_GetItemString(self->params, "polar_shell") );
252    self->model->sld_solvent = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_solvent") );
253    self->model->equat_shell = PyFloat_AsDouble( PyDict_GetItemString(self->params, "equat_shell") );
254    self->model->axis_phi = PyFloat_AsDouble( PyDict_GetItemString(self->params, "axis_phi") );
255    self->model->background = PyFloat_AsDouble( PyDict_GetItemString(self->params, "background") );
256    self->model->equat_core = PyFloat_AsDouble( PyDict_GetItemString(self->params, "equat_core") );
257    self->model->polar_core = PyFloat_AsDouble( PyDict_GetItemString(self->params, "polar_core") );
258    self->model->contrast = PyFloat_AsDouble( PyDict_GetItemString(self->params, "contrast") );
259    // Read in dispersion parameters
260    PyObject* disp_dict;
261    DispersionVisitor* visitor = new DispersionVisitor();
262    disp_dict = PyDict_GetItemString(self->dispersion, "equat_core");
263    self->model->equat_core.dispersion->accept_as_destination(visitor, self->model->equat_core.dispersion, disp_dict);
264    disp_dict = PyDict_GetItemString(self->dispersion, "polar_core");
265    self->model->polar_core.dispersion->accept_as_destination(visitor, self->model->polar_core.dispersion, disp_dict);
266    disp_dict = PyDict_GetItemString(self->dispersion, "equat_shell");
267    self->model->equat_shell.dispersion->accept_as_destination(visitor, self->model->equat_shell.dispersion, disp_dict);
268    disp_dict = PyDict_GetItemString(self->dispersion, "polar_shell");
269    self->model->polar_shell.dispersion->accept_as_destination(visitor, self->model->polar_shell.dispersion, disp_dict);
270    disp_dict = PyDict_GetItemString(self->dispersion, "axis_phi");
271    self->model->axis_phi.dispersion->accept_as_destination(visitor, self->model->axis_phi.dispersion, disp_dict);
272    disp_dict = PyDict_GetItemString(self->dispersion, "axis_theta");
273    self->model->axis_theta.dispersion->accept_as_destination(visitor, self->model->axis_theta.dispersion, disp_dict);
274
275       
276        // Get input and determine whether we have to supply a 1D or 2D return value.
277        if ( !PyArg_ParseTuple(args,"O",&pars) ) {
278            PyErr_SetString(CCoreShellSpheroidModelError, 
279                "CCoreShellSpheroidModel.evalDistribution expects a q value.");
280                return NULL;
281        }
282    // Check params
283       
284    if(PyArray_Check(pars)==1) {
285               
286            // Length of list should 1 or 2
287            npars = pars->nd; 
288            if(npars==1) {
289                // input is a numpy array
290                if (PyArray_Check(pars)) {
291                        return evaluateOneDim(self->model, (PyArrayObject*)pars); 
292                    }
293                }else{
294                    PyErr_SetString(CCoreShellSpheroidModelError, 
295                   "CCoreShellSpheroidModel.evalDistribution expect numpy array of one dimension.");
296                return NULL;
297                }
298    }else if( PyList_Check(pars)==1) {
299        // Length of list should be 2 for I(qx,qy)
300            mpars = PyList_GET_SIZE(pars); 
301            if(mpars!=2) {
302                PyErr_SetString(CCoreShellSpheroidModelError, 
303                        "CCoreShellSpheroidModel.evalDistribution expects a list of dimension 2.");
304                return NULL;
305            }
306             qx = PyList_GET_ITEM(pars,0);
307             qy = PyList_GET_ITEM(pars,1);
308             if (PyArray_Check(qx) && PyArray_Check(qy)) {
309                 return evaluateTwoDimXY(self->model, (PyArrayObject*)qx,
310                           (PyArrayObject*)qy);
311                 }else{
312                    PyErr_SetString(CCoreShellSpheroidModelError, 
313                   "CCoreShellSpheroidModel.evalDistribution expect 2 numpy arrays in list.");
314                return NULL;
315             }
316        }else{
317            PyErr_SetString(CCoreShellSpheroidModelError, 
318                   "CCoreShellSpheroidModel.evalDistribution couln't be run.");
319            return NULL;
320        }
321}
322
323/**
324 * Function to call to evaluate model
325 * @param args: input q or [q,phi]
326 * @return: function value
327 */
328static PyObject * run(CCoreShellSpheroidModel *self, PyObject *args) {
329        double q_value, phi_value;
330        PyObject* pars;
331        int npars;
332       
333        // Get parameters
334       
335            // Reader parameter dictionary
336    self->model->scale = PyFloat_AsDouble( PyDict_GetItemString(self->params, "scale") );
337    self->model->axis_theta = PyFloat_AsDouble( PyDict_GetItemString(self->params, "axis_theta") );
338    self->model->polar_shell = PyFloat_AsDouble( PyDict_GetItemString(self->params, "polar_shell") );
339    self->model->sld_solvent = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_solvent") );
340    self->model->equat_shell = PyFloat_AsDouble( PyDict_GetItemString(self->params, "equat_shell") );
341    self->model->axis_phi = PyFloat_AsDouble( PyDict_GetItemString(self->params, "axis_phi") );
342    self->model->background = PyFloat_AsDouble( PyDict_GetItemString(self->params, "background") );
343    self->model->equat_core = PyFloat_AsDouble( PyDict_GetItemString(self->params, "equat_core") );
344    self->model->polar_core = PyFloat_AsDouble( PyDict_GetItemString(self->params, "polar_core") );
345    self->model->contrast = PyFloat_AsDouble( PyDict_GetItemString(self->params, "contrast") );
346    // Read in dispersion parameters
347    PyObject* disp_dict;
348    DispersionVisitor* visitor = new DispersionVisitor();
349    disp_dict = PyDict_GetItemString(self->dispersion, "equat_core");
350    self->model->equat_core.dispersion->accept_as_destination(visitor, self->model->equat_core.dispersion, disp_dict);
351    disp_dict = PyDict_GetItemString(self->dispersion, "polar_core");
352    self->model->polar_core.dispersion->accept_as_destination(visitor, self->model->polar_core.dispersion, disp_dict);
353    disp_dict = PyDict_GetItemString(self->dispersion, "equat_shell");
354    self->model->equat_shell.dispersion->accept_as_destination(visitor, self->model->equat_shell.dispersion, disp_dict);
355    disp_dict = PyDict_GetItemString(self->dispersion, "polar_shell");
356    self->model->polar_shell.dispersion->accept_as_destination(visitor, self->model->polar_shell.dispersion, disp_dict);
357    disp_dict = PyDict_GetItemString(self->dispersion, "axis_phi");
358    self->model->axis_phi.dispersion->accept_as_destination(visitor, self->model->axis_phi.dispersion, disp_dict);
359    disp_dict = PyDict_GetItemString(self->dispersion, "axis_theta");
360    self->model->axis_theta.dispersion->accept_as_destination(visitor, self->model->axis_theta.dispersion, disp_dict);
361
362       
363        // Get input and determine whether we have to supply a 1D or 2D return value.
364        if ( !PyArg_ParseTuple(args,"O",&pars) ) {
365            PyErr_SetString(CCoreShellSpheroidModelError, 
366                "CCoreShellSpheroidModel.run expects a q value.");
367                return NULL;
368        }
369         
370        // Check params
371        if( PyList_Check(pars)==1) {
372               
373                // Length of list should be 2 for I(q,phi)
374            npars = PyList_GET_SIZE(pars); 
375            if(npars!=2) {
376                PyErr_SetString(CCoreShellSpheroidModelError, 
377                        "CCoreShellSpheroidModel.run expects a double or a list of dimension 2.");
378                return NULL;
379            }
380            // We have a vector q, get the q and phi values at which
381            // to evaluate I(q,phi)
382            q_value = CCoreShellSpheroidModel_readDouble(PyList_GET_ITEM(pars,0));
383            phi_value = CCoreShellSpheroidModel_readDouble(PyList_GET_ITEM(pars,1));
384            // Skip zero
385            if (q_value==0) {
386                return Py_BuildValue("d",0.0);
387            }
388                return Py_BuildValue("d",(*(self->model)).evaluate_rphi(q_value,phi_value));
389
390        } else {
391
392                // We have a scalar q, we will evaluate I(q)
393                q_value = CCoreShellSpheroidModel_readDouble(pars);             
394               
395                return Py_BuildValue("d",(*(self->model))(q_value));
396        }       
397}
398
399/**
400 * Function to call to evaluate model in cartesian coordinates
401 * @param args: input q or [qx, qy]]
402 * @return: function value
403 */
404static PyObject * runXY(CCoreShellSpheroidModel *self, PyObject *args) {
405        double qx_value, qy_value;
406        PyObject* pars;
407        int npars;
408       
409        // Get parameters
410       
411            // Reader parameter dictionary
412    self->model->scale = PyFloat_AsDouble( PyDict_GetItemString(self->params, "scale") );
413    self->model->axis_theta = PyFloat_AsDouble( PyDict_GetItemString(self->params, "axis_theta") );
414    self->model->polar_shell = PyFloat_AsDouble( PyDict_GetItemString(self->params, "polar_shell") );
415    self->model->sld_solvent = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_solvent") );
416    self->model->equat_shell = PyFloat_AsDouble( PyDict_GetItemString(self->params, "equat_shell") );
417    self->model->axis_phi = PyFloat_AsDouble( PyDict_GetItemString(self->params, "axis_phi") );
418    self->model->background = PyFloat_AsDouble( PyDict_GetItemString(self->params, "background") );
419    self->model->equat_core = PyFloat_AsDouble( PyDict_GetItemString(self->params, "equat_core") );
420    self->model->polar_core = PyFloat_AsDouble( PyDict_GetItemString(self->params, "polar_core") );
421    self->model->contrast = PyFloat_AsDouble( PyDict_GetItemString(self->params, "contrast") );
422    // Read in dispersion parameters
423    PyObject* disp_dict;
424    DispersionVisitor* visitor = new DispersionVisitor();
425    disp_dict = PyDict_GetItemString(self->dispersion, "equat_core");
426    self->model->equat_core.dispersion->accept_as_destination(visitor, self->model->equat_core.dispersion, disp_dict);
427    disp_dict = PyDict_GetItemString(self->dispersion, "polar_core");
428    self->model->polar_core.dispersion->accept_as_destination(visitor, self->model->polar_core.dispersion, disp_dict);
429    disp_dict = PyDict_GetItemString(self->dispersion, "equat_shell");
430    self->model->equat_shell.dispersion->accept_as_destination(visitor, self->model->equat_shell.dispersion, disp_dict);
431    disp_dict = PyDict_GetItemString(self->dispersion, "polar_shell");
432    self->model->polar_shell.dispersion->accept_as_destination(visitor, self->model->polar_shell.dispersion, disp_dict);
433    disp_dict = PyDict_GetItemString(self->dispersion, "axis_phi");
434    self->model->axis_phi.dispersion->accept_as_destination(visitor, self->model->axis_phi.dispersion, disp_dict);
435    disp_dict = PyDict_GetItemString(self->dispersion, "axis_theta");
436    self->model->axis_theta.dispersion->accept_as_destination(visitor, self->model->axis_theta.dispersion, disp_dict);
437
438       
439        // Get input and determine whether we have to supply a 1D or 2D return value.
440        if ( !PyArg_ParseTuple(args,"O",&pars) ) {
441            PyErr_SetString(CCoreShellSpheroidModelError, 
442                "CCoreShellSpheroidModel.run expects a q value.");
443                return NULL;
444        }
445         
446        // Check params
447        if( PyList_Check(pars)==1) {
448               
449                // Length of list should be 2 for I(qx, qy))
450            npars = PyList_GET_SIZE(pars); 
451            if(npars!=2) {
452                PyErr_SetString(CCoreShellSpheroidModelError, 
453                        "CCoreShellSpheroidModel.run expects a double or a list of dimension 2.");
454                return NULL;
455            }
456            // We have a vector q, get the qx and qy values at which
457            // to evaluate I(qx,qy)
458            qx_value = CCoreShellSpheroidModel_readDouble(PyList_GET_ITEM(pars,0));
459            qy_value = CCoreShellSpheroidModel_readDouble(PyList_GET_ITEM(pars,1));
460            return Py_BuildValue("d",(*(self->model))(qx_value,qy_value));
461
462        } else {
463
464                // We have a scalar q, we will evaluate I(q)
465                qx_value = CCoreShellSpheroidModel_readDouble(pars);           
466               
467                return Py_BuildValue("d",(*(self->model))(qx_value));
468        }       
469}
470
471static PyObject * reset(CCoreShellSpheroidModel *self, PyObject *args) {
472   
473
474    return Py_BuildValue("d",0.0);
475}
476
477static PyObject * set_dispersion(CCoreShellSpheroidModel *self, PyObject *args) {
478        PyObject * disp;
479        const char * par_name;
480
481        if ( !PyArg_ParseTuple(args,"sO", &par_name, &disp) ) {
482            PyErr_SetString(CCoreShellSpheroidModelError,
483                "CCoreShellSpheroidModel.set_dispersion expects a DispersionModel object.");
484                return NULL;
485        }
486        void *temp = PyCObject_AsVoidPtr(disp);
487        DispersionModel * dispersion = static_cast<DispersionModel *>(temp);
488
489
490        // Ugliness necessary to go from python to C
491            // TODO: refactor this
492    if (!strcmp(par_name, "equat_core")) {
493        self->model->equat_core.dispersion = dispersion;
494    } else    if (!strcmp(par_name, "polar_core")) {
495        self->model->polar_core.dispersion = dispersion;
496    } else    if (!strcmp(par_name, "equat_shell")) {
497        self->model->equat_shell.dispersion = dispersion;
498    } else    if (!strcmp(par_name, "polar_shell")) {
499        self->model->polar_shell.dispersion = dispersion;
500    } else    if (!strcmp(par_name, "axis_phi")) {
501        self->model->axis_phi.dispersion = dispersion;
502    } else    if (!strcmp(par_name, "axis_theta")) {
503        self->model->axis_theta.dispersion = dispersion;
504    } else {
505            PyErr_SetString(CCoreShellSpheroidModelError,
506                "CCoreShellSpheroidModel.set_dispersion expects a valid parameter name.");
507                return NULL;
508        }
509
510        DispersionVisitor* visitor = new DispersionVisitor();
511        PyObject * disp_dict = PyDict_New();
512        dispersion->accept_as_source(visitor, dispersion, disp_dict);
513        PyDict_SetItemString(self->dispersion, par_name, disp_dict);
514    return Py_BuildValue("i",1);
515}
516
517
518static PyMethodDef CCoreShellSpheroidModel_methods[] = {
519    {"run",      (PyCFunction)run     , METH_VARARGS,
520      "Evaluate the model at a given Q or Q, phi"},
521    {"runXY",      (PyCFunction)runXY     , METH_VARARGS,
522      "Evaluate the model at a given Q or Qx, Qy"},
523     
524    {"evalDistribution",  (PyCFunction)evalDistribution , METH_VARARGS,
525      "Evaluate the model at a given Q or Qx, Qy vector "},
526    {"reset",    (PyCFunction)reset   , METH_VARARGS,
527      "Reset pair correlation"},
528    {"set_dispersion",      (PyCFunction)set_dispersion     , METH_VARARGS,
529      "Set the dispersion model for a given parameter"},
530   {NULL}
531};
532
533static PyTypeObject CCoreShellSpheroidModelType = {
534    PyObject_HEAD_INIT(NULL)
535    0,                         /*ob_size*/
536    "CCoreShellSpheroidModel",             /*tp_name*/
537    sizeof(CCoreShellSpheroidModel),             /*tp_basicsize*/
538    0,                         /*tp_itemsize*/
539    (destructor)CCoreShellSpheroidModel_dealloc, /*tp_dealloc*/
540    0,                         /*tp_print*/
541    0,                         /*tp_getattr*/
542    0,                         /*tp_setattr*/
543    0,                         /*tp_compare*/
544    0,                         /*tp_repr*/
545    0,                         /*tp_as_number*/
546    0,                         /*tp_as_sequence*/
547    0,                         /*tp_as_mapping*/
548    0,                         /*tp_hash */
549    0,                         /*tp_call*/
550    0,                         /*tp_str*/
551    0,                         /*tp_getattro*/
552    0,                         /*tp_setattro*/
553    0,                         /*tp_as_buffer*/
554    Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
555    "CCoreShellSpheroidModel objects",           /* tp_doc */
556    0,                         /* tp_traverse */
557    0,                         /* tp_clear */
558    0,                         /* tp_richcompare */
559    0,                         /* tp_weaklistoffset */
560    0,                         /* tp_iter */
561    0,                         /* tp_iternext */
562    CCoreShellSpheroidModel_methods,             /* tp_methods */
563    CCoreShellSpheroidModel_members,             /* tp_members */
564    0,                         /* tp_getset */
565    0,                         /* tp_base */
566    0,                         /* tp_dict */
567    0,                         /* tp_descr_get */
568    0,                         /* tp_descr_set */
569    0,                         /* tp_dictoffset */
570    (initproc)CCoreShellSpheroidModel_init,      /* tp_init */
571    0,                         /* tp_alloc */
572    CCoreShellSpheroidModel_new,                 /* tp_new */
573};
574
575
576//static PyMethodDef module_methods[] = {
577//    {NULL}
578//};
579
580/**
581 * Function used to add the model class to a module
582 * @param module: module to add the class to
583 */ 
584void addCCoreShellSpheroidModel(PyObject *module) {
585        PyObject *d;
586       
587    if (PyType_Ready(&CCoreShellSpheroidModelType) < 0)
588        return;
589
590    Py_INCREF(&CCoreShellSpheroidModelType);
591    PyModule_AddObject(module, "CCoreShellSpheroidModel", (PyObject *)&CCoreShellSpheroidModelType);
592   
593    d = PyModule_GetDict(module);
594    CCoreShellSpheroidModelError = PyErr_NewException("CCoreShellSpheroidModel.error", NULL, NULL);
595    PyDict_SetItemString(d, "CCoreShellSpheroidModelError", CCoreShellSpheroidModelError);
596}
597
Note: See TracBrowser for help on using the repository browser.