source: sasview/sansmodels/src/c_extensions/CGaussian.c @ 67424cd

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 67424cd was 67424cd, checked in by Mathieu Doucet <doucetm@…>, 12 years ago

Reorganizing models in preparation of cpp cleanup

  • Property mode set to 100644
File size: 8.5 KB
Line 
1/** CGaussian
2 *
3 * C extension
4 *
5 * WARNING: THIS FILE WAS GENERATED BY WRAPPERGENERATOR.PY
6 *          DO NOT MODIFY THIS FILE, MODIFY gaussian.h
7 *          AND RE-RUN THE GENERATOR SCRIPT
8 *
9 * @author   M.Doucet / UTK
10 */
11 
12#include <Python.h>
13#include "structmember.h"
14#include <stdio.h>
15#include <stdlib.h>
16#include <math.h>
17#include <time.h>
18
19#include "gaussian.h"
20
21/// Error object for raised exceptions
22static PyObject * CGaussianError = NULL;
23
24
25// Class definition
26typedef struct {
27    PyObject_HEAD
28    /// Parameters
29    PyObject * params;
30    /// Log for unit testing
31    PyObject * log;
32    /// Model parameters
33        GaussianParameters model_pars;
34} CGaussian;
35
36
37static void
38CGaussian_dealloc(CGaussian* self)
39{
40    self->ob_type->tp_free((PyObject*)self);
41   
42
43}
44
45static PyObject *
46CGaussian_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
47{
48    CGaussian *self;
49   
50    self = (CGaussian *)type->tp_alloc(type, 0);
51   
52    return (PyObject *)self;
53}
54
55static int
56CGaussian_init(CGaussian *self, PyObject *args, PyObject *kwds)
57{
58    if (self != NULL) {
59       
60        // Create parameters
61        self->params = PyDict_New();
62       
63        // Initialize parameter dictionary
64        PyDict_SetItemString(self->params,"scale",Py_BuildValue("d",1.000000));
65        PyDict_SetItemString(self->params,"sigma",Py_BuildValue("d",1.000000));
66        PyDict_SetItemString(self->params,"center",Py_BuildValue("d",0.000000));
67
68         
69        // Create empty log
70        self->log = PyDict_New();
71       
72       
73
74    }
75    return 0;
76}
77
78static PyMemberDef CGaussian_members[] = {
79    {"params", T_OBJECT, offsetof(CGaussian, params), 0,
80     "Parameters"},
81    {"log", T_OBJECT, offsetof(CGaussian, log), 0,
82     "Log"},
83    {NULL}  /* Sentinel */
84};
85
86/** Read double from PyObject
87    @param p PyObject
88    @return double
89*/
90double CGaussian_readDouble(PyObject *p) {
91    if (PyFloat_Check(p)==1) {
92        return (double)(((PyFloatObject *)(p))->ob_fval);
93    } else if (PyInt_Check(p)==1) {
94        return (double)(((PyIntObject *)(p))->ob_ival);
95    } else if (PyLong_Check(p)==1) {
96        return (double)PyLong_AsLong(p);
97    } else {
98        return 0.0;
99    }
100}
101
102
103/**
104 * Function to call to evaluate model
105 * @param args: input q or [q,phi]
106 * @return: function value
107 */
108static PyObject * run(CGaussian *self, PyObject *args) {
109        double q_value, phi_value;
110        PyObject* pars;
111        int npars;
112       
113        // Get parameters
114       
115        // Reader parameter dictionary
116    self->model_pars.scale = PyFloat_AsDouble( PyDict_GetItemString(self->params, "scale") );
117    self->model_pars.sigma = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sigma") );
118    self->model_pars.center = PyFloat_AsDouble( PyDict_GetItemString(self->params, "center") );
119
120       
121        // Get input and determine whether we have to supply a 1D or 2D return value.
122        if ( !PyArg_ParseTuple(args,"O",&pars) ) {
123            PyErr_SetString(CGaussianError, 
124                "CGaussian.run expects a q value.");
125                return NULL;
126        }
127         
128        // Check params
129        if( PyList_Check(pars)==1) {
130               
131                // Length of list should be 2 for I(q,phi)
132            npars = PyList_GET_SIZE(pars); 
133            if(npars!=2) {
134                PyErr_SetString(CGaussianError, 
135                        "CGaussian.run expects a double or a list of dimension 2.");
136                return NULL;
137            }
138            // We have a vector q, get the q and phi values at which
139            // to evaluate I(q,phi)
140            q_value = CGaussian_readDouble(PyList_GET_ITEM(pars,0));
141            phi_value = CGaussian_readDouble(PyList_GET_ITEM(pars,1));
142            // Skip zero
143            if (q_value==0) {
144                return Py_BuildValue("d",0.0);
145            }
146                return Py_BuildValue("d",gaussian_analytical_2D(&(self->model_pars),q_value,phi_value));
147
148        } else {
149
150                // We have a scalar q, we will evaluate I(q)
151                q_value = CGaussian_readDouble(pars);           
152               
153                return Py_BuildValue("d",gaussian_analytical_1D(&(self->model_pars),q_value));
154        }       
155}
156
157/**
158 * Function to call to evaluate model in cartesian coordinates
159 * @param args: input q or [qx, qy]]
160 * @return: function value
161 */
162static PyObject * runXY(CGaussian *self, PyObject *args) {
163        double qx_value, qy_value;
164        PyObject* pars;
165        int npars;
166       
167        // Get parameters
168       
169        // Reader parameter dictionary
170    self->model_pars.scale = PyFloat_AsDouble( PyDict_GetItemString(self->params, "scale") );
171    self->model_pars.sigma = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sigma") );
172    self->model_pars.center = PyFloat_AsDouble( PyDict_GetItemString(self->params, "center") );
173
174       
175        // Get input and determine whether we have to supply a 1D or 2D return value.
176        if ( !PyArg_ParseTuple(args,"O",&pars) ) {
177            PyErr_SetString(CGaussianError, 
178                "CGaussian.run expects a q value.");
179                return NULL;
180        }
181         
182        // Check params
183        if( PyList_Check(pars)==1) {
184               
185                // Length of list should be 2 for I(qx, qy))
186            npars = PyList_GET_SIZE(pars); 
187            if(npars!=2) {
188                PyErr_SetString(CGaussianError, 
189                        "CGaussian.run expects a double or a list of dimension 2.");
190                return NULL;
191            }
192            // We have a vector q, get the qx and qy values at which
193            // to evaluate I(qx,qy)
194            qx_value = CGaussian_readDouble(PyList_GET_ITEM(pars,0));
195            qy_value = CGaussian_readDouble(PyList_GET_ITEM(pars,1));
196                return Py_BuildValue("d",gaussian_analytical_2DXY(&(self->model_pars),qx_value,qy_value));
197
198        } else {
199
200                // We have a scalar q, we will evaluate I(q)
201                qx_value = CGaussian_readDouble(pars);         
202               
203                return Py_BuildValue("d",gaussian_analytical_1D(&(self->model_pars),qx_value));
204        }       
205}
206
207static PyObject * reset(CGaussian *self, PyObject *args) {
208   
209
210    return Py_BuildValue("d",0.0);
211}
212
213
214static PyMethodDef CGaussian_methods[] = {
215    {"run",      (PyCFunction)run     , METH_VARARGS,
216      "Evaluate the model at a given Q or Q, phi"},
217    {"runXY",      (PyCFunction)runXY     , METH_VARARGS,
218      "Evaluate the model at a given Q or Qx, Qy"},
219    {"reset",    (PyCFunction)reset   , METH_VARARGS,
220      "Reset pair correlation"},
221    //{"numerical_1D",      (PyCFunction)numerical_1D     , METH_VARARGS,
222    //  "Evaluate the 1D model at a given Q"},
223   {NULL}
224};
225
226static PyTypeObject CGaussianType = {
227    PyObject_HEAD_INIT(NULL)
228    0,                         /*ob_size*/
229    "CGaussian",             /*tp_name*/
230    sizeof(CGaussian),             /*tp_basicsize*/
231    0,                         /*tp_itemsize*/
232    (destructor)CGaussian_dealloc, /*tp_dealloc*/
233    0,                         /*tp_print*/
234    0,                         /*tp_getattr*/
235    0,                         /*tp_setattr*/
236    0,                         /*tp_compare*/
237    0,                         /*tp_repr*/
238    0,                         /*tp_as_number*/
239    0,                         /*tp_as_sequence*/
240    0,                         /*tp_as_mapping*/
241    0,                         /*tp_hash */
242    0,                         /*tp_call*/
243    0,                         /*tp_str*/
244    0,                         /*tp_getattro*/
245    0,                         /*tp_setattro*/
246    0,                         /*tp_as_buffer*/
247    Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
248    "CGaussian objects",           /* tp_doc */
249    0,                         /* tp_traverse */
250    0,                         /* tp_clear */
251    0,                         /* tp_richcompare */
252    0,                         /* tp_weaklistoffset */
253    0,                         /* tp_iter */
254    0,                         /* tp_iternext */
255    CGaussian_methods,             /* tp_methods */
256    CGaussian_members,             /* tp_members */
257    0,                         /* tp_getset */
258    0,                         /* tp_base */
259    0,                         /* tp_dict */
260    0,                         /* tp_descr_get */
261    0,                         /* tp_descr_set */
262    0,                         /* tp_dictoffset */
263    (initproc)CGaussian_init,      /* tp_init */
264    0,                         /* tp_alloc */
265    CGaussian_new,                 /* tp_new */
266};
267
268
269//static PyMethodDef module_methods[] = {
270//    {NULL}
271//};
272
273/**
274 * Function used to add the model class to a module
275 * @param module: module to add the class to
276 */ 
277void addCGaussian(PyObject *module) {
278        PyObject *d;
279       
280    if (PyType_Ready(&CGaussianType) < 0)
281        return;
282
283    Py_INCREF(&CGaussianType);
284    PyModule_AddObject(module, "CGaussian", (PyObject *)&CGaussianType);
285   
286    d = PyModule_GetDict(module);
287    CGaussianError = PyErr_NewException("CGaussian.error", NULL, NULL);
288    PyDict_SetItemString(d, "CGaussianError", CGaussianError);
289}
290
Note: See TracBrowser for help on using the repository browser.