source: sasview/sansmodels/src/sans/models/c_models/CEllipsoidModel.cpp @ da3dae3

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 da3dae3 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.7 KB
RevLine 
[0f5bc9f]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/** CEllipsoidModel
16 *
17 * C extension
18 *
19 * WARNING: THIS FILE WAS GENERATED BY WRAPPERGENERATOR.PY
20 *          DO NOT MODIFY THIS FILE, MODIFY ellipsoid.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 "ellipsoid.h"
33}
34
35#include "models.hh"
36#include "dispersion_visitor.hh"
37
38/// Error object for raised exceptions
39static PyObject * CEllipsoidModelError = 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    EllipsoidModel * model;
51    /// Log for unit testing
52    PyObject * log;
53} CEllipsoidModel;
54
55
56static void
57CEllipsoidModel_dealloc(CEllipsoidModel* self)
58{
59    self->ob_type->tp_free((PyObject*)self);
60   
61
62}
63
64static PyObject *
65CEllipsoidModel_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
66{
67    CEllipsoidModel *self;
68   
69    self = (CEllipsoidModel *)type->tp_alloc(type, 0);
70   
71    return (PyObject *)self;
72}
73
74static int
75CEllipsoidModel_init(CEllipsoidModel *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 EllipsoidModel();
83       
84        // Initialize parameter dictionary
85        PyDict_SetItemString(self->params,"scale",Py_BuildValue("d",1.000000));
86        PyDict_SetItemString(self->params,"axis_theta",Py_BuildValue("d",1.570000));
87        PyDict_SetItemString(self->params,"radius_b",Py_BuildValue("d",400.000000));
88        PyDict_SetItemString(self->params,"radius_a",Py_BuildValue("d",20.000000));
89        PyDict_SetItemString(self->params,"axis_phi",Py_BuildValue("d",0.000000));
90        PyDict_SetItemString(self->params,"background",Py_BuildValue("d",0.000000));
91        PyDict_SetItemString(self->params,"contrast",Py_BuildValue("d",0.000003));
92        // Initialize dispersion / averaging parameter dict
93        DispersionVisitor* visitor = new DispersionVisitor();
94        PyObject * disp_dict;
95        disp_dict = PyDict_New();
96        self->model->radius_a.dispersion->accept_as_source(visitor, self->model->radius_a.dispersion, disp_dict);
97        PyDict_SetItemString(self->dispersion, "radius_a", disp_dict);
98        disp_dict = PyDict_New();
99        self->model->radius_b.dispersion->accept_as_source(visitor, self->model->radius_b.dispersion, disp_dict);
100        PyDict_SetItemString(self->dispersion, "radius_b", disp_dict);
101        disp_dict = PyDict_New();
102        self->model->axis_theta.dispersion->accept_as_source(visitor, self->model->axis_theta.dispersion, disp_dict);
103        PyDict_SetItemString(self->dispersion, "axis_theta", disp_dict);
104        disp_dict = PyDict_New();
105        self->model->axis_phi.dispersion->accept_as_source(visitor, self->model->axis_phi.dispersion, disp_dict);
106        PyDict_SetItemString(self->dispersion, "axis_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 CEllipsoidModel_members[] = {
120    {"params", T_OBJECT, offsetof(CEllipsoidModel, params), 0,
121     "Parameters"},
122        {"dispersion", T_OBJECT, offsetof(CEllipsoidModel, dispersion), 0,
123          "Dispersion parameters"},     
124    {"log", T_OBJECT, offsetof(CEllipsoidModel, log), 0,
125     "Log"},
126    {NULL}  /* Sentinel */
127};
128
129/** Read double from PyObject
130    @param p PyObject
131    @return double
132*/
133double CEllipsoidModel_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(CEllipsoidModel *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->axis_theta = PyFloat_AsDouble( PyDict_GetItemString(self->params, "axis_theta") );
161    self->model->radius_b = PyFloat_AsDouble( PyDict_GetItemString(self->params, "radius_b") );
162    self->model->radius_a = PyFloat_AsDouble( PyDict_GetItemString(self->params, "radius_a") );
163    self->model->axis_phi = PyFloat_AsDouble( PyDict_GetItemString(self->params, "axis_phi") );
164    self->model->background = PyFloat_AsDouble( PyDict_GetItemString(self->params, "background") );
165    self->model->contrast = PyFloat_AsDouble( PyDict_GetItemString(self->params, "contrast") );
166    // Read in dispersion parameters
167    PyObject* disp_dict;
168    DispersionVisitor* visitor = new DispersionVisitor();
169    disp_dict = PyDict_GetItemString(self->dispersion, "radius_a");
170    self->model->radius_a.dispersion->accept_as_destination(visitor, self->model->radius_a.dispersion, disp_dict);
171    disp_dict = PyDict_GetItemString(self->dispersion, "radius_b");
172    self->model->radius_b.dispersion->accept_as_destination(visitor, self->model->radius_b.dispersion, disp_dict);
173    disp_dict = PyDict_GetItemString(self->dispersion, "axis_theta");
174    self->model->axis_theta.dispersion->accept_as_destination(visitor, self->model->axis_theta.dispersion, disp_dict);
175    disp_dict = PyDict_GetItemString(self->dispersion, "axis_phi");
176    self->model->axis_phi.dispersion->accept_as_destination(visitor, self->model->axis_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(CEllipsoidModelError, 
182                "CEllipsoidModel.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(CEllipsoidModelError, 
193                        "CEllipsoidModel.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 = CEllipsoidModel_readDouble(PyList_GET_ITEM(pars,0));
199            phi_value = CEllipsoidModel_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 = CEllipsoidModel_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(CEllipsoidModel *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->axis_theta = PyFloat_AsDouble( PyDict_GetItemString(self->params, "axis_theta") );
230    self->model->radius_b = PyFloat_AsDouble( PyDict_GetItemString(self->params, "radius_b") );
231    self->model->radius_a = PyFloat_AsDouble( PyDict_GetItemString(self->params, "radius_a") );
232    self->model->axis_phi = PyFloat_AsDouble( PyDict_GetItemString(self->params, "axis_phi") );
233    self->model->background = PyFloat_AsDouble( PyDict_GetItemString(self->params, "background") );
234    self->model->contrast = PyFloat_AsDouble( PyDict_GetItemString(self->params, "contrast") );
235    // Read in dispersion parameters
236    PyObject* disp_dict;
237    DispersionVisitor* visitor = new DispersionVisitor();
238    disp_dict = PyDict_GetItemString(self->dispersion, "radius_a");
239    self->model->radius_a.dispersion->accept_as_destination(visitor, self->model->radius_a.dispersion, disp_dict);
240    disp_dict = PyDict_GetItemString(self->dispersion, "radius_b");
241    self->model->radius_b.dispersion->accept_as_destination(visitor, self->model->radius_b.dispersion, disp_dict);
242    disp_dict = PyDict_GetItemString(self->dispersion, "axis_theta");
243    self->model->axis_theta.dispersion->accept_as_destination(visitor, self->model->axis_theta.dispersion, disp_dict);
244    disp_dict = PyDict_GetItemString(self->dispersion, "axis_phi");
245    self->model->axis_phi.dispersion->accept_as_destination(visitor, self->model->axis_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(CEllipsoidModelError, 
251                "CEllipsoidModel.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(CEllipsoidModelError, 
262                        "CEllipsoidModel.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 = CEllipsoidModel_readDouble(PyList_GET_ITEM(pars,0));
268            qy_value = CEllipsoidModel_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 = CEllipsoidModel_readDouble(pars);           
275               
276                return Py_BuildValue("d",(*(self->model))(qx_value));
277        }       
278}
279
280static PyObject * reset(CEllipsoidModel *self, PyObject *args) {
281   
282
283    return Py_BuildValue("d",0.0);
284}
285
286static PyObject * set_dispersion(CEllipsoidModel *self, PyObject *args) {
287        PyObject * disp;
288        const char * par_name;
289
290        if ( !PyArg_ParseTuple(args,"sO", &par_name, &disp) ) {
291            PyErr_SetString(CEllipsoidModelError,
292                "CEllipsoidModel.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_a")) {
302        self->model->radius_a.dispersion = dispersion;
303    } else    if (!strcmp(par_name, "radius_b")) {
304        self->model->radius_b.dispersion = dispersion;
305    } else    if (!strcmp(par_name, "axis_theta")) {
306        self->model->axis_theta.dispersion = dispersion;
307    } else    if (!strcmp(par_name, "axis_phi")) {
308        self->model->axis_phi.dispersion = dispersion;
309    } else {
310            PyErr_SetString(CEllipsoidModelError,
311                "CEllipsoidModel.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 CEllipsoidModel_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 CEllipsoidModelType = {
336    PyObject_HEAD_INIT(NULL)
337    0,                         /*ob_size*/
338    "CEllipsoidModel",             /*tp_name*/
339    sizeof(CEllipsoidModel),             /*tp_basicsize*/
340    0,                         /*tp_itemsize*/
341    (destructor)CEllipsoidModel_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    "CEllipsoidModel 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    CEllipsoidModel_methods,             /* tp_methods */
365    CEllipsoidModel_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)CEllipsoidModel_init,      /* tp_init */
373    0,                         /* tp_alloc */
374    CEllipsoidModel_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 addCEllipsoidModel(PyObject *module) {
387        PyObject *d;
388       
389    if (PyType_Ready(&CEllipsoidModelType) < 0)
390        return;
391
392    Py_INCREF(&CEllipsoidModelType);
393    PyModule_AddObject(module, "CEllipsoidModel", (PyObject *)&CEllipsoidModelType);
394   
395    d = PyModule_GetDict(module);
396    CEllipsoidModelError = PyErr_NewException("CEllipsoidModel.error", NULL, NULL);
397    PyDict_SetItemString(d, "CEllipsoidModelError", CEllipsoidModelError);
398}
399
Note: See TracBrowser for help on using the repository browser.