source: sasview/sansmodels/src/sans/models/c_models/CCylinderModel.cpp @ acd91458

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 acd91458 was 0f5bc9f, checked in by Mathieu Doucet <doucetm@…>, 16 years ago

Update of all C models to the new style of C++ models

  • Property mode set to 100644
File size: 14.5 KB
RevLine 
[af03ddd]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/** CCylinderModel
[fca6936]16 *
[af03ddd]17 * C extension
18 *
19 * WARNING: THIS FILE WAS GENERATED BY WRAPPERGENERATOR.PY
20 *          DO NOT MODIFY THIS FILE, MODIFY cylinder.h
21 *          AND RE-RUN THE GENERATOR SCRIPT
[fca6936]22 *
23 */
[af03ddd]24 
[fca6936]25extern "C" {
26#include <Python.h>
27#include "structmember.h"
28#include <stdio.h>
29#include <stdlib.h>
30#include <math.h>
31#include <time.h>
32#include "cylinder.h"
33}
34
35#include "models.hh"
36#include "dispersion_visitor.hh"
37
38/// Error object for raised exceptions
39static PyObject * CCylinderModelError = NULL;
40
41
42// Class definition
43typedef struct {
44    PyObject_HEAD
45    /// Parameters
46    PyObject * params;
[af03ddd]47    /// Dispersion parameters
[fca6936]48    PyObject * dispersion;
[af03ddd]49    /// Underlying model object
50    CylinderModel * model;
[fca6936]51    /// Log for unit testing
52    PyObject * log;
53} CCylinderModel;
54
55
56static void
57CCylinderModel_dealloc(CCylinderModel* self)
58{
59    self->ob_type->tp_free((PyObject*)self);
[af03ddd]60   
[fca6936]61
62}
63
64static PyObject *
65CCylinderModel_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
66{
67    CCylinderModel *self;
[af03ddd]68   
[fca6936]69    self = (CCylinderModel *)type->tp_alloc(type, 0);
[af03ddd]70   
[fca6936]71    return (PyObject *)self;
72}
73
74static int
75CCylinderModel_init(CCylinderModel *self, PyObject *args, PyObject *kwds)
76{
77    if (self != NULL) {
[af03ddd]78       
[fca6936]79        // Create parameters
80        self->params = PyDict_New();
81        self->dispersion = PyDict_New();
[af03ddd]82        self->model = new CylinderModel();
83       
[fca6936]84        // Initialize parameter dictionary
[0f5bc9f]85        PyDict_SetItemString(self->params,"scale",Py_BuildValue("d",1.000000));
86        PyDict_SetItemString(self->params,"length",Py_BuildValue("d",400.000000));
87        PyDict_SetItemString(self->params,"cyl_theta",Py_BuildValue("d",1.000000));
88        PyDict_SetItemString(self->params,"background",Py_BuildValue("d",0.000000));
89        PyDict_SetItemString(self->params,"radius",Py_BuildValue("d",20.000000));
90        PyDict_SetItemString(self->params,"contrast",Py_BuildValue("d",0.000003));
91        PyDict_SetItemString(self->params,"cyl_phi",Py_BuildValue("d",1.000000));
[fca6936]92        // Initialize dispersion / averaging parameter dict
93        DispersionVisitor* visitor = new DispersionVisitor();
[af03ddd]94        PyObject * disp_dict;
95        disp_dict = PyDict_New();
[fca6936]96        self->model->radius.dispersion->accept_as_source(visitor, self->model->radius.dispersion, disp_dict);
[af03ddd]97        PyDict_SetItemString(self->dispersion, "radius", disp_dict);
[fca6936]98        disp_dict = PyDict_New();
99        self->model->length.dispersion->accept_as_source(visitor, self->model->length.dispersion, disp_dict);
[af03ddd]100        PyDict_SetItemString(self->dispersion, "length", disp_dict);
101        disp_dict = PyDict_New();
102        self->model->cyl_theta.dispersion->accept_as_source(visitor, self->model->cyl_theta.dispersion, disp_dict);
103        PyDict_SetItemString(self->dispersion, "cyl_theta", disp_dict);
[fca6936]104        disp_dict = PyDict_New();
105        self->model->cyl_phi.dispersion->accept_as_source(visitor, self->model->cyl_phi.dispersion, disp_dict);
[af03ddd]106        PyDict_SetItemString(self->dispersion, "cyl_phi", disp_dict);
[fca6936]107
108
[af03ddd]109         
[fca6936]110        // Create empty log
111        self->log = PyDict_New();
[af03ddd]112       
113       
[fca6936]114
115    }
116    return 0;
117}
118
119static PyMemberDef CCylinderModel_members[] = {
[af03ddd]120    {"params", T_OBJECT, offsetof(CCylinderModel, params), 0,
121     "Parameters"},
[fca6936]122        {"dispersion", T_OBJECT, offsetof(CCylinderModel, dispersion), 0,
[af03ddd]123          "Dispersion parameters"},     
[fca6936]124    {"log", T_OBJECT, offsetof(CCylinderModel, log), 0,
125     "Log"},
126    {NULL}  /* Sentinel */
127};
128
129/** Read double from PyObject
130    @param p PyObject
131    @return double
132*/
133double CCylinderModel_readDouble(PyObject *p) {
134    if (PyFloat_Check(p)==1) {
135        return (double)(((PyFloatObject *)(p))->ob_fval);
136    } else if (PyInt_Check(p)==1) {
137        return (double)(((PyIntObject *)(p))->ob_ival);
138    } else if (PyLong_Check(p)==1) {
139        return (double)PyLong_AsLong(p);
140    } else {
141        return 0.0;
142    }
143}
144
145
146/**
147 * Function to call to evaluate model
148 * @param args: input q or [q,phi]
149 * @return: function value
150 */
151static PyObject * run(CCylinderModel *self, PyObject *args) {
152        double q_value, phi_value;
153        PyObject* pars;
154        int npars;
[af03ddd]155       
[fca6936]156        // Get parameters
[af03ddd]157       
158            // Reader parameter dictionary
[fca6936]159    self->model->scale = PyFloat_AsDouble( PyDict_GetItemString(self->params, "scale") );
160    self->model->length = PyFloat_AsDouble( PyDict_GetItemString(self->params, "length") );
161    self->model->cyl_theta = PyFloat_AsDouble( PyDict_GetItemString(self->params, "cyl_theta") );
162    self->model->background = PyFloat_AsDouble( PyDict_GetItemString(self->params, "background") );
163    self->model->radius = PyFloat_AsDouble( PyDict_GetItemString(self->params, "radius") );
164    self->model->contrast = PyFloat_AsDouble( PyDict_GetItemString(self->params, "contrast") );
165    self->model->cyl_phi = PyFloat_AsDouble( PyDict_GetItemString(self->params, "cyl_phi") );
166    // Read in dispersion parameters
[af03ddd]167    PyObject* disp_dict;
[fca6936]168    DispersionVisitor* visitor = new DispersionVisitor();
[af03ddd]169    disp_dict = PyDict_GetItemString(self->dispersion, "radius");
[fca6936]170    self->model->radius.dispersion->accept_as_destination(visitor, self->model->radius.dispersion, disp_dict);
[af03ddd]171    disp_dict = PyDict_GetItemString(self->dispersion, "length");
[fca6936]172    self->model->length.dispersion->accept_as_destination(visitor, self->model->length.dispersion, disp_dict);
[af03ddd]173    disp_dict = PyDict_GetItemString(self->dispersion, "cyl_theta");
[fca6936]174    self->model->cyl_theta.dispersion->accept_as_destination(visitor, self->model->cyl_theta.dispersion, disp_dict);
[af03ddd]175    disp_dict = PyDict_GetItemString(self->dispersion, "cyl_phi");
[fca6936]176    self->model->cyl_phi.dispersion->accept_as_destination(visitor, self->model->cyl_phi.dispersion, disp_dict);
177
[af03ddd]178       
[fca6936]179        // Get input and determine whether we have to supply a 1D or 2D return value.
180        if ( !PyArg_ParseTuple(args,"O",&pars) ) {
[af03ddd]181            PyErr_SetString(CCylinderModelError, 
[fca6936]182                "CCylinderModel.run expects a q value.");
183                return NULL;
184        }
[af03ddd]185         
[fca6936]186        // Check params
187        if( PyList_Check(pars)==1) {
[af03ddd]188               
[fca6936]189                // Length of list should be 2 for I(q,phi)
[af03ddd]190            npars = PyList_GET_SIZE(pars); 
[fca6936]191            if(npars!=2) {
[af03ddd]192                PyErr_SetString(CCylinderModelError, 
[fca6936]193                        "CCylinderModel.run expects a double or a list of dimension 2.");
194                return NULL;
195            }
196            // We have a vector q, get the q and phi values at which
197            // to evaluate I(q,phi)
198            q_value = CCylinderModel_readDouble(PyList_GET_ITEM(pars,0));
199            phi_value = CCylinderModel_readDouble(PyList_GET_ITEM(pars,1));
200            // Skip zero
201            if (q_value==0) {
202                return Py_BuildValue("d",0.0);
203            }
204                return Py_BuildValue("d",(*(self->model)).evaluate_rphi(q_value,phi_value));
205
206        } else {
207
208                // We have a scalar q, we will evaluate I(q)
[af03ddd]209                q_value = CCylinderModel_readDouble(pars);             
210               
[fca6936]211                return Py_BuildValue("d",(*(self->model))(q_value));
[af03ddd]212        }       
[fca6936]213}
214
215/**
216 * Function to call to evaluate model in cartesian coordinates
217 * @param args: input q or [qx, qy]]
218 * @return: function value
219 */
220static PyObject * runXY(CCylinderModel *self, PyObject *args) {
221        double qx_value, qy_value;
222        PyObject* pars;
223        int npars;
[af03ddd]224       
[fca6936]225        // Get parameters
[af03ddd]226       
227            // Reader parameter dictionary
[fca6936]228    self->model->scale = PyFloat_AsDouble( PyDict_GetItemString(self->params, "scale") );
229    self->model->length = PyFloat_AsDouble( PyDict_GetItemString(self->params, "length") );
230    self->model->cyl_theta = PyFloat_AsDouble( PyDict_GetItemString(self->params, "cyl_theta") );
231    self->model->background = PyFloat_AsDouble( PyDict_GetItemString(self->params, "background") );
232    self->model->radius = PyFloat_AsDouble( PyDict_GetItemString(self->params, "radius") );
233    self->model->contrast = PyFloat_AsDouble( PyDict_GetItemString(self->params, "contrast") );
234    self->model->cyl_phi = PyFloat_AsDouble( PyDict_GetItemString(self->params, "cyl_phi") );
235    // Read in dispersion parameters
[af03ddd]236    PyObject* disp_dict;
[fca6936]237    DispersionVisitor* visitor = new DispersionVisitor();
[af03ddd]238    disp_dict = PyDict_GetItemString(self->dispersion, "radius");
[fca6936]239    self->model->radius.dispersion->accept_as_destination(visitor, self->model->radius.dispersion, disp_dict);
[af03ddd]240    disp_dict = PyDict_GetItemString(self->dispersion, "length");
[fca6936]241    self->model->length.dispersion->accept_as_destination(visitor, self->model->length.dispersion, disp_dict);
[af03ddd]242    disp_dict = PyDict_GetItemString(self->dispersion, "cyl_theta");
[fca6936]243    self->model->cyl_theta.dispersion->accept_as_destination(visitor, self->model->cyl_theta.dispersion, disp_dict);
[af03ddd]244    disp_dict = PyDict_GetItemString(self->dispersion, "cyl_phi");
[fca6936]245    self->model->cyl_phi.dispersion->accept_as_destination(visitor, self->model->cyl_phi.dispersion, disp_dict);
246
[af03ddd]247       
[fca6936]248        // Get input and determine whether we have to supply a 1D or 2D return value.
249        if ( !PyArg_ParseTuple(args,"O",&pars) ) {
[af03ddd]250            PyErr_SetString(CCylinderModelError, 
[fca6936]251                "CCylinderModel.run expects a q value.");
252                return NULL;
253        }
[af03ddd]254         
[fca6936]255        // Check params
256        if( PyList_Check(pars)==1) {
[af03ddd]257               
[fca6936]258                // Length of list should be 2 for I(qx, qy))
[af03ddd]259            npars = PyList_GET_SIZE(pars); 
[fca6936]260            if(npars!=2) {
[af03ddd]261                PyErr_SetString(CCylinderModelError, 
[fca6936]262                        "CCylinderModel.run expects a double or a list of dimension 2.");
263                return NULL;
264            }
265            // We have a vector q, get the qx and qy values at which
266            // to evaluate I(qx,qy)
267            qx_value = CCylinderModel_readDouble(PyList_GET_ITEM(pars,0));
268            qy_value = CCylinderModel_readDouble(PyList_GET_ITEM(pars,1));
269            return Py_BuildValue("d",(*(self->model))(qx_value,qy_value));
270
271        } else {
272
273                // We have a scalar q, we will evaluate I(q)
[af03ddd]274                qx_value = CCylinderModel_readDouble(pars);             
275               
[fca6936]276                return Py_BuildValue("d",(*(self->model))(qx_value));
[af03ddd]277        }       
[fca6936]278}
279
280static PyObject * reset(CCylinderModel *self, PyObject *args) {
[af03ddd]281   
[fca6936]282
283    return Py_BuildValue("d",0.0);
284}
285
[af03ddd]286static PyObject * set_dispersion(CCylinderModel *self, PyObject *args) {
287        PyObject * disp;
288        const char * par_name;
289
290        if ( !PyArg_ParseTuple(args,"sO", &par_name, &disp) ) {
291            PyErr_SetString(CCylinderModelError,
292                "CCylinderModel.set_dispersion expects a DispersionModel object.");
293                return NULL;
294        }
295        void *temp = PyCObject_AsVoidPtr(disp);
296        DispersionModel * dispersion = static_cast<DispersionModel *>(temp);
297
298
299        // Ugliness necessary to go from python to C
300            // TODO: refactor this
301    if (!strcmp(par_name, "radius")) {
302        self->model->radius.dispersion = dispersion;
303    } else    if (!strcmp(par_name, "length")) {
304        self->model->length.dispersion = dispersion;
305    } else    if (!strcmp(par_name, "cyl_theta")) {
306        self->model->cyl_theta.dispersion = dispersion;
307    } else    if (!strcmp(par_name, "cyl_phi")) {
308        self->model->cyl_phi.dispersion = dispersion;
309    } else {
310            PyErr_SetString(CCylinderModelError,
311                "CCylinderModel.set_dispersion expects a valid parameter name.");
312                return NULL;
313        }
314
315        DispersionVisitor* visitor = new DispersionVisitor();
316        PyObject * disp_dict = PyDict_New();
317        dispersion->accept_as_source(visitor, dispersion, disp_dict);
318        PyDict_SetItemString(self->dispersion, par_name, disp_dict);
319    return Py_BuildValue("i",1);
320}
321
[fca6936]322
323static PyMethodDef CCylinderModel_methods[] = {
324    {"run",      (PyCFunction)run     , METH_VARARGS,
325      "Evaluate the model at a given Q or Q, phi"},
326    {"runXY",      (PyCFunction)runXY     , METH_VARARGS,
327      "Evaluate the model at a given Q or Qx, Qy"},
328    {"reset",    (PyCFunction)reset   , METH_VARARGS,
329      "Reset pair correlation"},
[af03ddd]330    {"set_dispersion",      (PyCFunction)set_dispersion     , METH_VARARGS,
331      "Set the dispersion model for a given parameter"},
[fca6936]332   {NULL}
333};
334
335static PyTypeObject CCylinderModelType = {
336    PyObject_HEAD_INIT(NULL)
337    0,                         /*ob_size*/
338    "CCylinderModel",             /*tp_name*/
339    sizeof(CCylinderModel),             /*tp_basicsize*/
340    0,                         /*tp_itemsize*/
341    (destructor)CCylinderModel_dealloc, /*tp_dealloc*/
342    0,                         /*tp_print*/
343    0,                         /*tp_getattr*/
344    0,                         /*tp_setattr*/
345    0,                         /*tp_compare*/
346    0,                         /*tp_repr*/
347    0,                         /*tp_as_number*/
348    0,                         /*tp_as_sequence*/
349    0,                         /*tp_as_mapping*/
350    0,                         /*tp_hash */
351    0,                         /*tp_call*/
352    0,                         /*tp_str*/
353    0,                         /*tp_getattro*/
354    0,                         /*tp_setattro*/
355    0,                         /*tp_as_buffer*/
356    Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
357    "CCylinderModel objects",           /* tp_doc */
358    0,                         /* tp_traverse */
359    0,                         /* tp_clear */
360    0,                         /* tp_richcompare */
361    0,                         /* tp_weaklistoffset */
362    0,                         /* tp_iter */
363    0,                         /* tp_iternext */
364    CCylinderModel_methods,             /* tp_methods */
365    CCylinderModel_members,             /* tp_members */
366    0,                         /* tp_getset */
367    0,                         /* tp_base */
368    0,                         /* tp_dict */
369    0,                         /* tp_descr_get */
370    0,                         /* tp_descr_set */
371    0,                         /* tp_dictoffset */
372    (initproc)CCylinderModel_init,      /* tp_init */
373    0,                         /* tp_alloc */
374    CCylinderModel_new,                 /* tp_new */
375};
376
377
378static PyMethodDef module_methods[] = {
[af03ddd]379    {NULL} 
[fca6936]380};
381
382/**
383 * Function used to add the model class to a module
384 * @param module: module to add the class to
[af03ddd]385 */ 
[fca6936]386void addCCylinderModel(PyObject *module) {
387        PyObject *d;
[af03ddd]388       
[fca6936]389    if (PyType_Ready(&CCylinderModelType) < 0)
390        return;
391
392    Py_INCREF(&CCylinderModelType);
393    PyModule_AddObject(module, "CCylinderModel", (PyObject *)&CCylinderModelType);
[af03ddd]394   
[fca6936]395    d = PyModule_GetDict(module);
396    CCylinderModelError = PyErr_NewException("CCylinderModel.error", NULL, NULL);
397    PyDict_SetItemString(d, "CCylinderModelError", CCylinderModelError);
398}
399
Note: See TracBrowser for help on using the repository browser.