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

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 916f5c0 was 2605da22, checked in by Mathieu Doucet <doucetm@…>, 13 years ago

Re #4 Still a few more warnings

  • 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.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 char name_params[] = "params";
133static char def_params[] = "Parameters";
134static char name_dispersion[] = "dispersion";
135static char def_dispersion[] = "Dispersion parameters";
136static char name_log[] = "log";
137static char def_log[] = "Log";
138
139static PyMemberDef CCoreShellCylinderModel_members[] = {
140    {name_params, T_OBJECT, offsetof(CCoreShellCylinderModel, params), 0, def_params},
141        {name_dispersion, T_OBJECT, offsetof(CCoreShellCylinderModel, dispersion), 0, def_dispersion},     
142    {name_log, T_OBJECT, offsetof(CCoreShellCylinderModel, log), 0, def_log},
143    {NULL}  /* Sentinel */
144};
145
146/** Read double from PyObject
147    @param p PyObject
148    @return double
149*/
150double CCoreShellCylinderModel_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(CoreShellCylinderModel* 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( CoreShellCylinderModel* model, 
198                              PyArrayObject *x, PyArrayObject *y)
199 {
200    PyArrayObject *result;
201    int i, 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(CCoreShellCylinderModelError, 
236                   "CCoreShellCylinderModel.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(CCoreShellCylinderModel *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->axis_theta = PyFloat_AsDouble( PyDict_GetItemString(self->params, "axis_theta") );
255    self->model->solvent_sld = PyFloat_AsDouble( PyDict_GetItemString(self->params, "solvent_sld") );
256    self->model->thickness = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thickness") );
257    self->model->axis_phi = PyFloat_AsDouble( PyDict_GetItemString(self->params, "axis_phi") );
258    self->model->length = PyFloat_AsDouble( PyDict_GetItemString(self->params, "length") );
259    self->model->core_sld = PyFloat_AsDouble( PyDict_GetItemString(self->params, "core_sld") );
260    self->model->radius = PyFloat_AsDouble( PyDict_GetItemString(self->params, "radius") );
261    self->model->background = PyFloat_AsDouble( PyDict_GetItemString(self->params, "background") );
262    self->model->shell_sld = PyFloat_AsDouble( PyDict_GetItemString(self->params, "shell_sld") );
263    // Read in dispersion parameters
264    PyObject* disp_dict;
265    DispersionVisitor* visitor = new DispersionVisitor();
266    disp_dict = PyDict_GetItemString(self->dispersion, "radius");
267    self->model->radius.dispersion->accept_as_destination(visitor, self->model->radius.dispersion, disp_dict);
268    disp_dict = PyDict_GetItemString(self->dispersion, "thickness");
269    self->model->thickness.dispersion->accept_as_destination(visitor, self->model->thickness.dispersion, disp_dict);
270    disp_dict = PyDict_GetItemString(self->dispersion, "length");
271    self->model->length.dispersion->accept_as_destination(visitor, self->model->length.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    disp_dict = PyDict_GetItemString(self->dispersion, "axis_phi");
275    self->model->axis_phi.dispersion->accept_as_destination(visitor, self->model->axis_phi.dispersion, disp_dict);
276
277       
278        // Get input and determine whether we have to supply a 1D or 2D return value.
279        if ( !PyArg_ParseTuple(args,"O",&pars) ) {
280            PyErr_SetString(CCoreShellCylinderModelError, 
281                "CCoreShellCylinderModel.evalDistribution expects a q value.");
282                return NULL;
283        }
284    // Check params
285       
286    if(PyArray_Check(pars)==1) {
287               
288            // Length of list should 1 or 2
289            npars = pars->nd; 
290            if(npars==1) {
291                // input is a numpy array
292                if (PyArray_Check(pars)) {
293                        return evaluateOneDim(self->model, (PyArrayObject*)pars); 
294                    }
295                }else{
296                    PyErr_SetString(CCoreShellCylinderModelError, 
297                   "CCoreShellCylinderModel.evalDistribution expect numpy array of one dimension.");
298                return NULL;
299                }
300    }else if( PyList_Check(pars)==1) {
301        // Length of list should be 2 for I(qx,qy)
302            mpars = PyList_GET_SIZE(pars); 
303            if(mpars!=2) {
304                PyErr_SetString(CCoreShellCylinderModelError, 
305                        "CCoreShellCylinderModel.evalDistribution expects a list of dimension 2.");
306                return NULL;
307            }
308             qx = PyList_GET_ITEM(pars,0);
309             qy = PyList_GET_ITEM(pars,1);
310             if (PyArray_Check(qx) && PyArray_Check(qy)) {
311                 return evaluateTwoDimXY(self->model, (PyArrayObject*)qx,
312                           (PyArrayObject*)qy);
313                 }else{
314                    PyErr_SetString(CCoreShellCylinderModelError, 
315                   "CCoreShellCylinderModel.evalDistribution expect 2 numpy arrays in list.");
316                return NULL;
317             }
318        }
319        PyErr_SetString(CCoreShellCylinderModelError, 
320                   "CCoreShellCylinderModel.evalDistribution couln't be run.");
321        return NULL;
322       
323}
324
325/**
326 * Function to call to evaluate model
327 * @param args: input q or [q,phi]
328 * @return: function value
329 */
330static PyObject * run(CCoreShellCylinderModel *self, PyObject *args) {
331        double q_value, phi_value;
332        PyObject* pars;
333        int npars;
334       
335        // Get parameters
336       
337            // Reader parameter dictionary
338    self->model->scale = PyFloat_AsDouble( PyDict_GetItemString(self->params, "scale") );
339    self->model->axis_theta = PyFloat_AsDouble( PyDict_GetItemString(self->params, "axis_theta") );
340    self->model->solvent_sld = PyFloat_AsDouble( PyDict_GetItemString(self->params, "solvent_sld") );
341    self->model->thickness = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thickness") );
342    self->model->axis_phi = PyFloat_AsDouble( PyDict_GetItemString(self->params, "axis_phi") );
343    self->model->length = PyFloat_AsDouble( PyDict_GetItemString(self->params, "length") );
344    self->model->core_sld = PyFloat_AsDouble( PyDict_GetItemString(self->params, "core_sld") );
345    self->model->radius = PyFloat_AsDouble( PyDict_GetItemString(self->params, "radius") );
346    self->model->background = PyFloat_AsDouble( PyDict_GetItemString(self->params, "background") );
347    self->model->shell_sld = PyFloat_AsDouble( PyDict_GetItemString(self->params, "shell_sld") );
348    // Read in dispersion parameters
349    PyObject* disp_dict;
350    DispersionVisitor* visitor = new DispersionVisitor();
351    disp_dict = PyDict_GetItemString(self->dispersion, "radius");
352    self->model->radius.dispersion->accept_as_destination(visitor, self->model->radius.dispersion, disp_dict);
353    disp_dict = PyDict_GetItemString(self->dispersion, "thickness");
354    self->model->thickness.dispersion->accept_as_destination(visitor, self->model->thickness.dispersion, disp_dict);
355    disp_dict = PyDict_GetItemString(self->dispersion, "length");
356    self->model->length.dispersion->accept_as_destination(visitor, self->model->length.dispersion, disp_dict);
357    disp_dict = PyDict_GetItemString(self->dispersion, "axis_theta");
358    self->model->axis_theta.dispersion->accept_as_destination(visitor, self->model->axis_theta.dispersion, disp_dict);
359    disp_dict = PyDict_GetItemString(self->dispersion, "axis_phi");
360    self->model->axis_phi.dispersion->accept_as_destination(visitor, self->model->axis_phi.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(CCoreShellCylinderModelError, 
366                "CCoreShellCylinderModel.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(CCoreShellCylinderModelError, 
377                        "CCoreShellCylinderModel.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 = CCoreShellCylinderModel_readDouble(PyList_GET_ITEM(pars,0));
383            phi_value = CCoreShellCylinderModel_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 = CCoreShellCylinderModel_readDouble(pars);             
394               
395                return Py_BuildValue("d",(*(self->model))(q_value));
396        }       
397}
398/**
399 * Function to call to calculate_ER
400 * @return: effective radius value
401 */
402static PyObject * calculate_ER(CCoreShellCylinderModel *self) {
403
404        // Get parameters
405       
406            // Reader parameter dictionary
407    self->model->scale = PyFloat_AsDouble( PyDict_GetItemString(self->params, "scale") );
408    self->model->axis_theta = PyFloat_AsDouble( PyDict_GetItemString(self->params, "axis_theta") );
409    self->model->solvent_sld = PyFloat_AsDouble( PyDict_GetItemString(self->params, "solvent_sld") );
410    self->model->thickness = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thickness") );
411    self->model->axis_phi = PyFloat_AsDouble( PyDict_GetItemString(self->params, "axis_phi") );
412    self->model->length = PyFloat_AsDouble( PyDict_GetItemString(self->params, "length") );
413    self->model->core_sld = PyFloat_AsDouble( PyDict_GetItemString(self->params, "core_sld") );
414    self->model->radius = PyFloat_AsDouble( PyDict_GetItemString(self->params, "radius") );
415    self->model->background = PyFloat_AsDouble( PyDict_GetItemString(self->params, "background") );
416    self->model->shell_sld = PyFloat_AsDouble( PyDict_GetItemString(self->params, "shell_sld") );
417    // Read in dispersion parameters
418    PyObject* disp_dict;
419    DispersionVisitor* visitor = new DispersionVisitor();
420    disp_dict = PyDict_GetItemString(self->dispersion, "radius");
421    self->model->radius.dispersion->accept_as_destination(visitor, self->model->radius.dispersion, disp_dict);
422    disp_dict = PyDict_GetItemString(self->dispersion, "thickness");
423    self->model->thickness.dispersion->accept_as_destination(visitor, self->model->thickness.dispersion, disp_dict);
424    disp_dict = PyDict_GetItemString(self->dispersion, "length");
425    self->model->length.dispersion->accept_as_destination(visitor, self->model->length.dispersion, disp_dict);
426    disp_dict = PyDict_GetItemString(self->dispersion, "axis_theta");
427    self->model->axis_theta.dispersion->accept_as_destination(visitor, self->model->axis_theta.dispersion, disp_dict);
428    disp_dict = PyDict_GetItemString(self->dispersion, "axis_phi");
429    self->model->axis_phi.dispersion->accept_as_destination(visitor, self->model->axis_phi.dispersion, disp_dict);
430
431               
432        return Py_BuildValue("d",(*(self->model)).calculate_ER());
433
434}
435/**
436 * Function to call to evaluate model in cartesian coordinates
437 * @param args: input q or [qx, qy]]
438 * @return: function value
439 */
440static PyObject * runXY(CCoreShellCylinderModel *self, PyObject *args) {
441        double qx_value, qy_value;
442        PyObject* pars;
443        int npars;
444       
445        // Get parameters
446       
447            // Reader parameter dictionary
448    self->model->scale = PyFloat_AsDouble( PyDict_GetItemString(self->params, "scale") );
449    self->model->axis_theta = PyFloat_AsDouble( PyDict_GetItemString(self->params, "axis_theta") );
450    self->model->solvent_sld = PyFloat_AsDouble( PyDict_GetItemString(self->params, "solvent_sld") );
451    self->model->thickness = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thickness") );
452    self->model->axis_phi = PyFloat_AsDouble( PyDict_GetItemString(self->params, "axis_phi") );
453    self->model->length = PyFloat_AsDouble( PyDict_GetItemString(self->params, "length") );
454    self->model->core_sld = PyFloat_AsDouble( PyDict_GetItemString(self->params, "core_sld") );
455    self->model->radius = PyFloat_AsDouble( PyDict_GetItemString(self->params, "radius") );
456    self->model->background = PyFloat_AsDouble( PyDict_GetItemString(self->params, "background") );
457    self->model->shell_sld = PyFloat_AsDouble( PyDict_GetItemString(self->params, "shell_sld") );
458    // Read in dispersion parameters
459    PyObject* disp_dict;
460    DispersionVisitor* visitor = new DispersionVisitor();
461    disp_dict = PyDict_GetItemString(self->dispersion, "radius");
462    self->model->radius.dispersion->accept_as_destination(visitor, self->model->radius.dispersion, disp_dict);
463    disp_dict = PyDict_GetItemString(self->dispersion, "thickness");
464    self->model->thickness.dispersion->accept_as_destination(visitor, self->model->thickness.dispersion, disp_dict);
465    disp_dict = PyDict_GetItemString(self->dispersion, "length");
466    self->model->length.dispersion->accept_as_destination(visitor, self->model->length.dispersion, disp_dict);
467    disp_dict = PyDict_GetItemString(self->dispersion, "axis_theta");
468    self->model->axis_theta.dispersion->accept_as_destination(visitor, self->model->axis_theta.dispersion, disp_dict);
469    disp_dict = PyDict_GetItemString(self->dispersion, "axis_phi");
470    self->model->axis_phi.dispersion->accept_as_destination(visitor, self->model->axis_phi.dispersion, disp_dict);
471
472       
473        // Get input and determine whether we have to supply a 1D or 2D return value.
474        if ( !PyArg_ParseTuple(args,"O",&pars) ) {
475            PyErr_SetString(CCoreShellCylinderModelError, 
476                "CCoreShellCylinderModel.run expects a q value.");
477                return NULL;
478        }
479         
480        // Check params
481        if( PyList_Check(pars)==1) {
482               
483                // Length of list should be 2 for I(qx, qy))
484            npars = PyList_GET_SIZE(pars); 
485            if(npars!=2) {
486                PyErr_SetString(CCoreShellCylinderModelError, 
487                        "CCoreShellCylinderModel.run expects a double or a list of dimension 2.");
488                return NULL;
489            }
490            // We have a vector q, get the qx and qy values at which
491            // to evaluate I(qx,qy)
492            qx_value = CCoreShellCylinderModel_readDouble(PyList_GET_ITEM(pars,0));
493            qy_value = CCoreShellCylinderModel_readDouble(PyList_GET_ITEM(pars,1));
494            return Py_BuildValue("d",(*(self->model))(qx_value,qy_value));
495
496        } else {
497
498                // We have a scalar q, we will evaluate I(q)
499                qx_value = CCoreShellCylinderModel_readDouble(pars);           
500               
501                return Py_BuildValue("d",(*(self->model))(qx_value));
502        }       
503}
504
505static PyObject * reset(CCoreShellCylinderModel *self, PyObject *args) {
506   
507
508    return Py_BuildValue("d",0.0);
509}
510
511static PyObject * set_dispersion(CCoreShellCylinderModel *self, PyObject *args) {
512        PyObject * disp;
513        const char * par_name;
514
515        if ( !PyArg_ParseTuple(args,"sO", &par_name, &disp) ) {
516            PyErr_SetString(CCoreShellCylinderModelError,
517                "CCoreShellCylinderModel.set_dispersion expects a DispersionModel object.");
518                return NULL;
519        }
520        void *temp = PyCObject_AsVoidPtr(disp);
521        DispersionModel * dispersion = static_cast<DispersionModel *>(temp);
522
523
524        // Ugliness necessary to go from python to C
525            // TODO: refactor this
526    if (!strcmp(par_name, "radius")) {
527        self->model->radius.dispersion = dispersion;
528    } else    if (!strcmp(par_name, "thickness")) {
529        self->model->thickness.dispersion = dispersion;
530    } else    if (!strcmp(par_name, "length")) {
531        self->model->length.dispersion = dispersion;
532    } else    if (!strcmp(par_name, "axis_theta")) {
533        self->model->axis_theta.dispersion = dispersion;
534    } else    if (!strcmp(par_name, "axis_phi")) {
535        self->model->axis_phi.dispersion = dispersion;
536    } else {
537            PyErr_SetString(CCoreShellCylinderModelError,
538                "CCoreShellCylinderModel.set_dispersion expects a valid parameter name.");
539                return NULL;
540        }
541
542        DispersionVisitor* visitor = new DispersionVisitor();
543        PyObject * disp_dict = PyDict_New();
544        dispersion->accept_as_source(visitor, dispersion, disp_dict);
545        PyDict_SetItemString(self->dispersion, par_name, disp_dict);
546    return Py_BuildValue("i",1);
547}
548
549
550static PyMethodDef CCoreShellCylinderModel_methods[] = {
551    {"run",      (PyCFunction)run     , METH_VARARGS,
552      "Evaluate the model at a given Q or Q, phi"},
553    {"runXY",      (PyCFunction)runXY     , METH_VARARGS,
554      "Evaluate the model at a given Q or Qx, Qy"},
555    {"calculate_ER",      (PyCFunction)calculate_ER     , METH_VARARGS,
556      "Evaluate the model at a given Q or Q, phi"},
557     
558    {"evalDistribution",  (PyCFunction)evalDistribution , METH_VARARGS,
559      "Evaluate the model at a given Q or Qx, Qy vector "},
560    {"reset",    (PyCFunction)reset   , METH_VARARGS,
561      "Reset pair correlation"},
562    {"set_dispersion",      (PyCFunction)set_dispersion     , METH_VARARGS,
563      "Set the dispersion model for a given parameter"},
564   {NULL}
565};
566
567static PyTypeObject CCoreShellCylinderModelType = {
568    PyObject_HEAD_INIT(NULL)
569    0,                         /*ob_size*/
570    "CCoreShellCylinderModel",             /*tp_name*/
571    sizeof(CCoreShellCylinderModel),             /*tp_basicsize*/
572    0,                         /*tp_itemsize*/
573    (destructor)CCoreShellCylinderModel_dealloc, /*tp_dealloc*/
574    0,                         /*tp_print*/
575    0,                         /*tp_getattr*/
576    0,                         /*tp_setattr*/
577    0,                         /*tp_compare*/
578    0,                         /*tp_repr*/
579    0,                         /*tp_as_number*/
580    0,                         /*tp_as_sequence*/
581    0,                         /*tp_as_mapping*/
582    0,                         /*tp_hash */
583    0,                         /*tp_call*/
584    0,                         /*tp_str*/
585    0,                         /*tp_getattro*/
586    0,                         /*tp_setattro*/
587    0,                         /*tp_as_buffer*/
588    Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
589    "CCoreShellCylinderModel objects",           /* tp_doc */
590    0,                         /* tp_traverse */
591    0,                         /* tp_clear */
592    0,                         /* tp_richcompare */
593    0,                         /* tp_weaklistoffset */
594    0,                         /* tp_iter */
595    0,                         /* tp_iternext */
596    CCoreShellCylinderModel_methods,             /* tp_methods */
597    CCoreShellCylinderModel_members,             /* tp_members */
598    0,                         /* tp_getset */
599    0,                         /* tp_base */
600    0,                         /* tp_dict */
601    0,                         /* tp_descr_get */
602    0,                         /* tp_descr_set */
603    0,                         /* tp_dictoffset */
604    (initproc)CCoreShellCylinderModel_init,      /* tp_init */
605    0,                         /* tp_alloc */
606    CCoreShellCylinderModel_new,                 /* tp_new */
607};
608
609
610//static PyMethodDef module_methods[] = {
611//    {NULL}
612//};
613
614/**
615 * Function used to add the model class to a module
616 * @param module: module to add the class to
617 */ 
618void addCCoreShellCylinderModel(PyObject *module) {
619        PyObject *d;
620       
621    if (PyType_Ready(&CCoreShellCylinderModelType) < 0)
622        return;
623
624    Py_INCREF(&CCoreShellCylinderModelType);
625    PyModule_AddObject(module, "CCoreShellCylinderModel", (PyObject *)&CCoreShellCylinderModelType);
626   
627    d = PyModule_GetDict(module);
628    static char error_name[] = "CCoreShellCylinderModel.error";
629    CCoreShellCylinderModelError = PyErr_NewException(error_name, NULL, NULL);
630    PyDict_SetItemString(d, "CCoreShellCylinderModelError", CCoreShellCylinderModelError);
631}
632
Note: See TracBrowser for help on using the repository browser.