source: sasview/sansmodels/src/sans/models/c_extensions/CSphereModel.c @ 9316609

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

added description to model

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