source: sasview/sansmodels/src/sans/models/c_extensions/CEllipticalCylinderModel.c @ f629e346

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

Moving sansmodels to trunk

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