source: sasview/sansmodels/src/sans/models/c_models/CCoreShellCylinderModel.cpp @ c146f092

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 c146f092 was 71e2de7, checked in by Gervaise Alina <gervyh@…>, 15 years ago

change destructor for models

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