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

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 19aa896 was 4628e31, checked in by Jae Cho <jhjcho@…>, 14 years ago

changed the unit of angles into degrees

  • Property mode set to 100644
File size: 26.2 KB
Line 
1/**
2        This software was developed by the University of Tennessee as part of the
3        Distributed Data Analysis of Neutron Scattering Experiments (DANSE)
4        project funded by the US National Science Foundation.
5
6        If you use DANSE applications to do scientific research that leads to
7        publication, we ask that you acknowledge the use of the software with the
8        following sentence:
9
10        "This work benefited from DANSE software developed under NSF award DMR-0520547."
11
12        copyright 2008, University of Tennessee
13 */
14
15/** 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.000000000000));
93        PyDict_SetItemString(self->params,"axis_theta",Py_BuildValue("d",90.000000000000));
94        PyDict_SetItemString(self->params,"solvent_sld",Py_BuildValue("d",0.000001000000));
95        PyDict_SetItemString(self->params,"thickness",Py_BuildValue("d",10.000000000000));
96        PyDict_SetItemString(self->params,"axis_phi",Py_BuildValue("d",0.000000000000));
97        PyDict_SetItemString(self->params,"length",Py_BuildValue("d",400.000000000000));
98        PyDict_SetItemString(self->params,"core_sld",Py_BuildValue("d",0.000001000000));
99        PyDict_SetItemString(self->params,"radius",Py_BuildValue("d",20.000000000000));
100        PyDict_SetItemString(self->params,"background",Py_BuildValue("d",0.000000000000));
101        PyDict_SetItemString(self->params,"shell_sld",Py_BuildValue("d",0.000004000000));
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[1];
198    //check validity of input vectors
199    if (x->nd != 1 || x->descr->type_num != PyArray_DOUBLE
200        || y->nd != 1 || y->descr->type_num != PyArray_DOUBLE
201        || y->dimensions[0] != 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[0]= x->dimensions[0];
210        y_len = dims[0]= y->dimensions[0];
211           
212            // Make a new double matrix of same dims
213        result=(PyArrayObject *) PyArray_FromDims(1,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 ( i=0; i< x_len; i++) {
222            double x_value = *(double *)(x->data + i*x->strides[0]);
223                    double y_value = *(double *)(y->data + i*y->strides[0]);
224                        double *result_value = (double *)(result->data +
225                              i*result->strides[0]);
226                        *result_value = (*model)(x_value, y_value);
227        }           
228        return PyArray_Return(result); 
229       
230        }else{
231                    PyErr_SetString(CCoreShellCylinderModelError, 
232                   "CCoreShellCylinderModel.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(CCoreShellCylinderModel *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->solvent_sld = PyFloat_AsDouble( PyDict_GetItemString(self->params, "solvent_sld") );
252    self->model->thickness = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thickness") );
253    self->model->axis_phi = PyFloat_AsDouble( PyDict_GetItemString(self->params, "axis_phi") );
254    self->model->length = PyFloat_AsDouble( PyDict_GetItemString(self->params, "length") );
255    self->model->core_sld = PyFloat_AsDouble( PyDict_GetItemString(self->params, "core_sld") );
256    self->model->radius = PyFloat_AsDouble( PyDict_GetItemString(self->params, "radius") );
257    self->model->background = PyFloat_AsDouble( PyDict_GetItemString(self->params, "background") );
258    self->model->shell_sld = PyFloat_AsDouble( PyDict_GetItemString(self->params, "shell_sld") );
259    // Read in dispersion parameters
260    PyObject* disp_dict;
261    DispersionVisitor* visitor = new DispersionVisitor();
262    disp_dict = PyDict_GetItemString(self->dispersion, "radius");
263    self->model->radius.dispersion->accept_as_destination(visitor, self->model->radius.dispersion, disp_dict);
264    disp_dict = PyDict_GetItemString(self->dispersion, "thickness");
265    self->model->thickness.dispersion->accept_as_destination(visitor, self->model->thickness.dispersion, disp_dict);
266    disp_dict = PyDict_GetItemString(self->dispersion, "length");
267    self->model->length.dispersion->accept_as_destination(visitor, self->model->length.dispersion, disp_dict);
268    disp_dict = PyDict_GetItemString(self->dispersion, "axis_theta");
269    self->model->axis_theta.dispersion->accept_as_destination(visitor, self->model->axis_theta.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
273       
274        // Get input and determine whether we have to supply a 1D or 2D return value.
275        if ( !PyArg_ParseTuple(args,"O",&pars) ) {
276            PyErr_SetString(CCoreShellCylinderModelError, 
277                "CCoreShellCylinderModel.evalDistribution expects a q value.");
278                return NULL;
279        }
280    // Check params
281       
282    if(PyArray_Check(pars)==1) {
283               
284            // Length of list should 1 or 2
285            npars = pars->nd; 
286            if(npars==1) {
287                // input is a numpy array
288                if (PyArray_Check(pars)) {
289                        return evaluateOneDim(self->model, (PyArrayObject*)pars); 
290                    }
291                }else{
292                    PyErr_SetString(CCoreShellCylinderModelError, 
293                   "CCoreShellCylinderModel.evalDistribution expect numpy array of one dimension.");
294                return NULL;
295                }
296    }else if( PyList_Check(pars)==1) {
297        // Length of list should be 2 for I(qx,qy)
298            mpars = PyList_GET_SIZE(pars); 
299            if(mpars!=2) {
300                PyErr_SetString(CCoreShellCylinderModelError, 
301                        "CCoreShellCylinderModel.evalDistribution expects a list of dimension 2.");
302                return NULL;
303            }
304             qx = PyList_GET_ITEM(pars,0);
305             qy = PyList_GET_ITEM(pars,1);
306             if (PyArray_Check(qx) && PyArray_Check(qy)) {
307                 return evaluateTwoDimXY(self->model, (PyArrayObject*)qx,
308                           (PyArrayObject*)qy);
309                 }else{
310                    PyErr_SetString(CCoreShellCylinderModelError, 
311                   "CCoreShellCylinderModel.evalDistribution expect 2 numpy arrays in list.");
312                return NULL;
313             }
314        }
315        PyErr_SetString(CCoreShellCylinderModelError, 
316                   "CCoreShellCylinderModel.evalDistribution couln't be run.");
317        return NULL;
318       
319}
320
321/**
322 * Function to call to evaluate model
323 * @param args: input q or [q,phi]
324 * @return: function value
325 */
326static PyObject * run(CCoreShellCylinderModel *self, PyObject *args) {
327        double q_value, phi_value;
328        PyObject* pars;
329        int npars;
330       
331        // Get parameters
332       
333            // Reader parameter dictionary
334    self->model->scale = PyFloat_AsDouble( PyDict_GetItemString(self->params, "scale") );
335    self->model->axis_theta = PyFloat_AsDouble( PyDict_GetItemString(self->params, "axis_theta") );
336    self->model->solvent_sld = PyFloat_AsDouble( PyDict_GetItemString(self->params, "solvent_sld") );
337    self->model->thickness = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thickness") );
338    self->model->axis_phi = PyFloat_AsDouble( PyDict_GetItemString(self->params, "axis_phi") );
339    self->model->length = PyFloat_AsDouble( PyDict_GetItemString(self->params, "length") );
340    self->model->core_sld = PyFloat_AsDouble( PyDict_GetItemString(self->params, "core_sld") );
341    self->model->radius = PyFloat_AsDouble( PyDict_GetItemString(self->params, "radius") );
342    self->model->background = PyFloat_AsDouble( PyDict_GetItemString(self->params, "background") );
343    self->model->shell_sld = PyFloat_AsDouble( PyDict_GetItemString(self->params, "shell_sld") );
344    // Read in dispersion parameters
345    PyObject* disp_dict;
346    DispersionVisitor* visitor = new DispersionVisitor();
347    disp_dict = PyDict_GetItemString(self->dispersion, "radius");
348    self->model->radius.dispersion->accept_as_destination(visitor, self->model->radius.dispersion, disp_dict);
349    disp_dict = PyDict_GetItemString(self->dispersion, "thickness");
350    self->model->thickness.dispersion->accept_as_destination(visitor, self->model->thickness.dispersion, disp_dict);
351    disp_dict = PyDict_GetItemString(self->dispersion, "length");
352    self->model->length.dispersion->accept_as_destination(visitor, self->model->length.dispersion, disp_dict);
353    disp_dict = PyDict_GetItemString(self->dispersion, "axis_theta");
354    self->model->axis_theta.dispersion->accept_as_destination(visitor, self->model->axis_theta.dispersion, disp_dict);
355    disp_dict = PyDict_GetItemString(self->dispersion, "axis_phi");
356    self->model->axis_phi.dispersion->accept_as_destination(visitor, self->model->axis_phi.dispersion, disp_dict);
357
358       
359        // Get input and determine whether we have to supply a 1D or 2D return value.
360        if ( !PyArg_ParseTuple(args,"O",&pars) ) {
361            PyErr_SetString(CCoreShellCylinderModelError, 
362                "CCoreShellCylinderModel.run expects a q value.");
363                return NULL;
364        }
365         
366        // Check params
367        if( PyList_Check(pars)==1) {
368               
369                // Length of list should be 2 for I(q,phi)
370            npars = PyList_GET_SIZE(pars); 
371            if(npars!=2) {
372                PyErr_SetString(CCoreShellCylinderModelError, 
373                        "CCoreShellCylinderModel.run expects a double or a list of dimension 2.");
374                return NULL;
375            }
376            // We have a vector q, get the q and phi values at which
377            // to evaluate I(q,phi)
378            q_value = CCoreShellCylinderModel_readDouble(PyList_GET_ITEM(pars,0));
379            phi_value = CCoreShellCylinderModel_readDouble(PyList_GET_ITEM(pars,1));
380            // Skip zero
381            if (q_value==0) {
382                return Py_BuildValue("d",0.0);
383            }
384                return Py_BuildValue("d",(*(self->model)).evaluate_rphi(q_value,phi_value));
385
386        } else {
387
388                // We have a scalar q, we will evaluate I(q)
389                q_value = CCoreShellCylinderModel_readDouble(pars);             
390               
391                return Py_BuildValue("d",(*(self->model))(q_value));
392        }       
393}
394/**
395 * Function to call to calculate_ER
396 * @return: effective radius value
397 */
398static PyObject * calculate_ER(CCoreShellCylinderModel *self) {
399
400        PyObject* pars;
401        int npars;
402       
403        // Get parameters
404       
405            // Reader parameter dictionary
406    self->model->scale = PyFloat_AsDouble( PyDict_GetItemString(self->params, "scale") );
407    self->model->axis_theta = PyFloat_AsDouble( PyDict_GetItemString(self->params, "axis_theta") );
408    self->model->solvent_sld = PyFloat_AsDouble( PyDict_GetItemString(self->params, "solvent_sld") );
409    self->model->thickness = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thickness") );
410    self->model->axis_phi = PyFloat_AsDouble( PyDict_GetItemString(self->params, "axis_phi") );
411    self->model->length = PyFloat_AsDouble( PyDict_GetItemString(self->params, "length") );
412    self->model->core_sld = PyFloat_AsDouble( PyDict_GetItemString(self->params, "core_sld") );
413    self->model->radius = PyFloat_AsDouble( PyDict_GetItemString(self->params, "radius") );
414    self->model->background = PyFloat_AsDouble( PyDict_GetItemString(self->params, "background") );
415    self->model->shell_sld = PyFloat_AsDouble( PyDict_GetItemString(self->params, "shell_sld") );
416    // Read in dispersion parameters
417    PyObject* disp_dict;
418    DispersionVisitor* visitor = new DispersionVisitor();
419    disp_dict = PyDict_GetItemString(self->dispersion, "radius");
420    self->model->radius.dispersion->accept_as_destination(visitor, self->model->radius.dispersion, disp_dict);
421    disp_dict = PyDict_GetItemString(self->dispersion, "thickness");
422    self->model->thickness.dispersion->accept_as_destination(visitor, self->model->thickness.dispersion, disp_dict);
423    disp_dict = PyDict_GetItemString(self->dispersion, "length");
424    self->model->length.dispersion->accept_as_destination(visitor, self->model->length.dispersion, disp_dict);
425    disp_dict = PyDict_GetItemString(self->dispersion, "axis_theta");
426    self->model->axis_theta.dispersion->accept_as_destination(visitor, self->model->axis_theta.dispersion, disp_dict);
427    disp_dict = PyDict_GetItemString(self->dispersion, "axis_phi");
428    self->model->axis_phi.dispersion->accept_as_destination(visitor, self->model->axis_phi.dispersion, disp_dict);
429
430               
431        return Py_BuildValue("d",(*(self->model)).calculate_ER());
432
433}
434/**
435 * Function to call to evaluate model in cartesian coordinates
436 * @param args: input q or [qx, qy]]
437 * @return: function value
438 */
439static PyObject * runXY(CCoreShellCylinderModel *self, PyObject *args) {
440        double qx_value, qy_value;
441        PyObject* pars;
442        int npars;
443       
444        // Get parameters
445       
446            // Reader parameter dictionary
447    self->model->scale = PyFloat_AsDouble( PyDict_GetItemString(self->params, "scale") );
448    self->model->axis_theta = PyFloat_AsDouble( PyDict_GetItemString(self->params, "axis_theta") );
449    self->model->solvent_sld = PyFloat_AsDouble( PyDict_GetItemString(self->params, "solvent_sld") );
450    self->model->thickness = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thickness") );
451    self->model->axis_phi = PyFloat_AsDouble( PyDict_GetItemString(self->params, "axis_phi") );
452    self->model->length = PyFloat_AsDouble( PyDict_GetItemString(self->params, "length") );
453    self->model->core_sld = PyFloat_AsDouble( PyDict_GetItemString(self->params, "core_sld") );
454    self->model->radius = PyFloat_AsDouble( PyDict_GetItemString(self->params, "radius") );
455    self->model->background = PyFloat_AsDouble( PyDict_GetItemString(self->params, "background") );
456    self->model->shell_sld = PyFloat_AsDouble( PyDict_GetItemString(self->params, "shell_sld") );
457    // Read in dispersion parameters
458    PyObject* disp_dict;
459    DispersionVisitor* visitor = new DispersionVisitor();
460    disp_dict = PyDict_GetItemString(self->dispersion, "radius");
461    self->model->radius.dispersion->accept_as_destination(visitor, self->model->radius.dispersion, disp_dict);
462    disp_dict = PyDict_GetItemString(self->dispersion, "thickness");
463    self->model->thickness.dispersion->accept_as_destination(visitor, self->model->thickness.dispersion, disp_dict);
464    disp_dict = PyDict_GetItemString(self->dispersion, "length");
465    self->model->length.dispersion->accept_as_destination(visitor, self->model->length.dispersion, disp_dict);
466    disp_dict = PyDict_GetItemString(self->dispersion, "axis_theta");
467    self->model->axis_theta.dispersion->accept_as_destination(visitor, self->model->axis_theta.dispersion, disp_dict);
468    disp_dict = PyDict_GetItemString(self->dispersion, "axis_phi");
469    self->model->axis_phi.dispersion->accept_as_destination(visitor, self->model->axis_phi.dispersion, disp_dict);
470
471       
472        // Get input and determine whether we have to supply a 1D or 2D return value.
473        if ( !PyArg_ParseTuple(args,"O",&pars) ) {
474            PyErr_SetString(CCoreShellCylinderModelError, 
475                "CCoreShellCylinderModel.run expects a q value.");
476                return NULL;
477        }
478         
479        // Check params
480        if( PyList_Check(pars)==1) {
481               
482                // Length of list should be 2 for I(qx, qy))
483            npars = PyList_GET_SIZE(pars); 
484            if(npars!=2) {
485                PyErr_SetString(CCoreShellCylinderModelError, 
486                        "CCoreShellCylinderModel.run expects a double or a list of dimension 2.");
487                return NULL;
488            }
489            // We have a vector q, get the qx and qy values at which
490            // to evaluate I(qx,qy)
491            qx_value = CCoreShellCylinderModel_readDouble(PyList_GET_ITEM(pars,0));
492            qy_value = CCoreShellCylinderModel_readDouble(PyList_GET_ITEM(pars,1));
493            return Py_BuildValue("d",(*(self->model))(qx_value,qy_value));
494
495        } else {
496
497                // We have a scalar q, we will evaluate I(q)
498                qx_value = CCoreShellCylinderModel_readDouble(pars);           
499               
500                return Py_BuildValue("d",(*(self->model))(qx_value));
501        }       
502}
503
504static PyObject * reset(CCoreShellCylinderModel *self, PyObject *args) {
505   
506
507    return Py_BuildValue("d",0.0);
508}
509
510static PyObject * set_dispersion(CCoreShellCylinderModel *self, PyObject *args) {
511        PyObject * disp;
512        const char * par_name;
513
514        if ( !PyArg_ParseTuple(args,"sO", &par_name, &disp) ) {
515            PyErr_SetString(CCoreShellCylinderModelError,
516                "CCoreShellCylinderModel.set_dispersion expects a DispersionModel object.");
517                return NULL;
518        }
519        void *temp = PyCObject_AsVoidPtr(disp);
520        DispersionModel * dispersion = static_cast<DispersionModel *>(temp);
521
522
523        // Ugliness necessary to go from python to C
524            // TODO: refactor this
525    if (!strcmp(par_name, "radius")) {
526        self->model->radius.dispersion = dispersion;
527    } else    if (!strcmp(par_name, "thickness")) {
528        self->model->thickness.dispersion = dispersion;
529    } else    if (!strcmp(par_name, "length")) {
530        self->model->length.dispersion = dispersion;
531    } else    if (!strcmp(par_name, "axis_theta")) {
532        self->model->axis_theta.dispersion = dispersion;
533    } else    if (!strcmp(par_name, "axis_phi")) {
534        self->model->axis_phi.dispersion = dispersion;
535    } else {
536            PyErr_SetString(CCoreShellCylinderModelError,
537                "CCoreShellCylinderModel.set_dispersion expects a valid parameter name.");
538                return NULL;
539        }
540
541        DispersionVisitor* visitor = new DispersionVisitor();
542        PyObject * disp_dict = PyDict_New();
543        dispersion->accept_as_source(visitor, dispersion, disp_dict);
544        PyDict_SetItemString(self->dispersion, par_name, disp_dict);
545    return Py_BuildValue("i",1);
546}
547
548
549static PyMethodDef CCoreShellCylinderModel_methods[] = {
550    {"run",      (PyCFunction)run     , METH_VARARGS,
551      "Evaluate the model at a given Q or Q, phi"},
552    {"runXY",      (PyCFunction)runXY     , METH_VARARGS,
553      "Evaluate the model at a given Q or Qx, Qy"},
554    {"calculate_ER",      (PyCFunction)calculate_ER     , METH_VARARGS,
555      "Evaluate the model at a given Q or Q, phi"},
556     
557    {"evalDistribution",  (PyCFunction)evalDistribution , METH_VARARGS,
558      "Evaluate the model at a given Q or Qx, Qy vector "},
559    {"reset",    (PyCFunction)reset   , METH_VARARGS,
560      "Reset pair correlation"},
561    {"set_dispersion",      (PyCFunction)set_dispersion     , METH_VARARGS,
562      "Set the dispersion model for a given parameter"},
563   {NULL}
564};
565
566static PyTypeObject CCoreShellCylinderModelType = {
567    PyObject_HEAD_INIT(NULL)
568    0,                         /*ob_size*/
569    "CCoreShellCylinderModel",             /*tp_name*/
570    sizeof(CCoreShellCylinderModel),             /*tp_basicsize*/
571    0,                         /*tp_itemsize*/
572    (destructor)CCoreShellCylinderModel_dealloc, /*tp_dealloc*/
573    0,                         /*tp_print*/
574    0,                         /*tp_getattr*/
575    0,                         /*tp_setattr*/
576    0,                         /*tp_compare*/
577    0,                         /*tp_repr*/
578    0,                         /*tp_as_number*/
579    0,                         /*tp_as_sequence*/
580    0,                         /*tp_as_mapping*/
581    0,                         /*tp_hash */
582    0,                         /*tp_call*/
583    0,                         /*tp_str*/
584    0,                         /*tp_getattro*/
585    0,                         /*tp_setattro*/
586    0,                         /*tp_as_buffer*/
587    Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
588    "CCoreShellCylinderModel objects",           /* tp_doc */
589    0,                         /* tp_traverse */
590    0,                         /* tp_clear */
591    0,                         /* tp_richcompare */
592    0,                         /* tp_weaklistoffset */
593    0,                         /* tp_iter */
594    0,                         /* tp_iternext */
595    CCoreShellCylinderModel_methods,             /* tp_methods */
596    CCoreShellCylinderModel_members,             /* tp_members */
597    0,                         /* tp_getset */
598    0,                         /* tp_base */
599    0,                         /* tp_dict */
600    0,                         /* tp_descr_get */
601    0,                         /* tp_descr_set */
602    0,                         /* tp_dictoffset */
603    (initproc)CCoreShellCylinderModel_init,      /* tp_init */
604    0,                         /* tp_alloc */
605    CCoreShellCylinderModel_new,                 /* tp_new */
606};
607
608
609//static PyMethodDef module_methods[] = {
610//    {NULL}
611//};
612
613/**
614 * Function used to add the model class to a module
615 * @param module: module to add the class to
616 */ 
617void addCCoreShellCylinderModel(PyObject *module) {
618        PyObject *d;
619       
620    if (PyType_Ready(&CCoreShellCylinderModelType) < 0)
621        return;
622
623    Py_INCREF(&CCoreShellCylinderModelType);
624    PyModule_AddObject(module, "CCoreShellCylinderModel", (PyObject *)&CCoreShellCylinderModelType);
625   
626    d = PyModule_GetDict(module);
627    CCoreShellCylinderModelError = PyErr_NewException("CCoreShellCylinderModel.error", NULL, NULL);
628    PyDict_SetItemString(d, "CCoreShellCylinderModelError", CCoreShellCylinderModelError);
629}
630
Note: See TracBrowser for help on using the repository browser.