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

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

code reverse initial definition of run and runXY restore

  • Property mode set to 100644
File size: 14.5 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/** CCylinderModel
16 *
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
22 *
23 */
24 
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;
47    /// Dispersion parameters
48    PyObject * dispersion;
49    /// Underlying model object
50    CylinderModel * model;
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);
60   
61
62}
63
64static PyObject *
65CCylinderModel_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
66{
67    CCylinderModel *self;
68   
69    self = (CCylinderModel *)type->tp_alloc(type, 0);
70   
71    return (PyObject *)self;
72}
73
74static int
75CCylinderModel_init(CCylinderModel *self, PyObject *args, PyObject *kwds)
76{
77    if (self != NULL) {
78       
79        // Create parameters
80        self->params = PyDict_New();
81        self->dispersion = PyDict_New();
82        self->model = new CylinderModel();
83       
84        // Initialize parameter dictionary
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));
92        // Initialize dispersion / averaging parameter dict
93        DispersionVisitor* visitor = new DispersionVisitor();
94        PyObject * disp_dict;
95        disp_dict = PyDict_New();
96        self->model->radius.dispersion->accept_as_source(visitor, self->model->radius.dispersion, disp_dict);
97        PyDict_SetItemString(self->dispersion, "radius", disp_dict);
98        disp_dict = PyDict_New();
99        self->model->length.dispersion->accept_as_source(visitor, self->model->length.dispersion, disp_dict);
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);
104        disp_dict = PyDict_New();
105        self->model->cyl_phi.dispersion->accept_as_source(visitor, self->model->cyl_phi.dispersion, disp_dict);
106        PyDict_SetItemString(self->dispersion, "cyl_phi", disp_dict);
107
108
109         
110        // Create empty log
111        self->log = PyDict_New();
112       
113       
114
115    }
116    return 0;
117}
118
119static PyMemberDef CCylinderModel_members[] = {
120    {"params", T_OBJECT, offsetof(CCylinderModel, params), 0,
121     "Parameters"},
122        {"dispersion", T_OBJECT, offsetof(CCylinderModel, dispersion), 0,
123          "Dispersion parameters"},     
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;
155       
156        // Get parameters
157       
158            // Reader parameter dictionary
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
167    PyObject* disp_dict;
168    DispersionVisitor* visitor = new DispersionVisitor();
169    disp_dict = PyDict_GetItemString(self->dispersion, "radius");
170    self->model->radius.dispersion->accept_as_destination(visitor, self->model->radius.dispersion, disp_dict);
171    disp_dict = PyDict_GetItemString(self->dispersion, "length");
172    self->model->length.dispersion->accept_as_destination(visitor, self->model->length.dispersion, disp_dict);
173    disp_dict = PyDict_GetItemString(self->dispersion, "cyl_theta");
174    self->model->cyl_theta.dispersion->accept_as_destination(visitor, self->model->cyl_theta.dispersion, disp_dict);
175    disp_dict = PyDict_GetItemString(self->dispersion, "cyl_phi");
176    self->model->cyl_phi.dispersion->accept_as_destination(visitor, self->model->cyl_phi.dispersion, disp_dict);
177
178       
179        // Get input and determine whether we have to supply a 1D or 2D return value.
180        if ( !PyArg_ParseTuple(args,"O",&pars) ) {
181            PyErr_SetString(CCylinderModelError, 
182                "CCylinderModel.run expects a q value.");
183                return NULL;
184        }
185         
186        // Check params
187        if( PyList_Check(pars)==1) {
188               
189                // Length of list should be 2 for I(q,phi)
190            npars = PyList_GET_SIZE(pars); 
191            if(npars!=2) {
192                PyErr_SetString(CCylinderModelError, 
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)
209                q_value = CCylinderModel_readDouble(pars);             
210               
211                return Py_BuildValue("d",(*(self->model))(q_value));
212        }       
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;
224       
225        // Get parameters
226       
227            // Reader parameter dictionary
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
236    PyObject* disp_dict;
237    DispersionVisitor* visitor = new DispersionVisitor();
238    disp_dict = PyDict_GetItemString(self->dispersion, "radius");
239    self->model->radius.dispersion->accept_as_destination(visitor, self->model->radius.dispersion, disp_dict);
240    disp_dict = PyDict_GetItemString(self->dispersion, "length");
241    self->model->length.dispersion->accept_as_destination(visitor, self->model->length.dispersion, disp_dict);
242    disp_dict = PyDict_GetItemString(self->dispersion, "cyl_theta");
243    self->model->cyl_theta.dispersion->accept_as_destination(visitor, self->model->cyl_theta.dispersion, disp_dict);
244    disp_dict = PyDict_GetItemString(self->dispersion, "cyl_phi");
245    self->model->cyl_phi.dispersion->accept_as_destination(visitor, self->model->cyl_phi.dispersion, disp_dict);
246
247       
248        // Get input and determine whether we have to supply a 1D or 2D return value.
249        if ( !PyArg_ParseTuple(args,"O",&pars) ) {
250            PyErr_SetString(CCylinderModelError, 
251                "CCylinderModel.run expects a q value.");
252                return NULL;
253        }
254         
255        // Check params
256        if( PyList_Check(pars)==1) {
257               
258                // Length of list should be 2 for I(qx, qy))
259            npars = PyList_GET_SIZE(pars); 
260            if(npars!=2) {
261                PyErr_SetString(CCylinderModelError, 
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)
274                qx_value = CCylinderModel_readDouble(pars);             
275               
276                return Py_BuildValue("d",(*(self->model))(qx_value));
277        }       
278}
279
280static PyObject * reset(CCylinderModel *self, PyObject *args) {
281   
282
283    return Py_BuildValue("d",0.0);
284}
285
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
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"},
330    {"set_dispersion",      (PyCFunction)set_dispersion     , METH_VARARGS,
331      "Set the dispersion model for a given parameter"},
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[] = {
379    {NULL} 
380};
381
382/**
383 * Function used to add the model class to a module
384 * @param module: module to add the class to
385 */ 
386void addCCylinderModel(PyObject *module) {
387        PyObject *d;
388       
389    if (PyType_Ready(&CCylinderModelType) < 0)
390        return;
391
392    Py_INCREF(&CCylinderModelType);
393    PyModule_AddObject(module, "CCylinderModel", (PyObject *)&CCylinderModelType);
394   
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.