source: sasview/sansmodels/src/sans/models/c_extensions/CEllipsoidModel.c @ a55fac1

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

Moving sansmodels to trunk

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