source: sasview/sansmodels/src/sans/models/c_extensions/classTemplate.txt @ cabe74b

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

Moving sansmodels to trunk

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