source: sasview/sansmodels/src/sans/models/c_models/classTemplate.txt @ 5f89fb8

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

Model C extension update

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