source: sasview/sansmodels/src/sans/models/c_extensions/CDiamEllip.c @ 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 fccfd90, checked in by Jae Cho <jhjcho@…>, 15 years ago

Added 2nd virial coeff. functions

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