source: sasview/sansmodels/prototypes/src/CSimCylinderF.c @ ee0f3fc

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 ee0f3fc was 7df1a50, checked in by Jae Cho <jhjcho@…>, 13 years ago

moving a file

  • Property mode set to 100644
File size: 7.9 KB
Line 
1/** CSimCylinderF
2 *
3 * C extension
4 *
5 * WARNING: THIS FILE WAS GENERATED BY WRAPPERGENERATOR.PY
6 *          DO NOT MODIFY THIS FILE, MODIFY ..\prototypes\src\simcylinder_fast.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 "simcylinder_fast.h"
20
21/// Error object for raised exceptions
22static PyObject * CSimCylinderFError = 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        SimCylinderFParameters model_pars;
34} CSimCylinderF;
35
36
37static void
38CSimCylinderF_dealloc(CSimCylinderF* self)
39{
40    self->ob_type->tp_free((PyObject*)self);
41        modelcalculations_dealloc(&(self->model_pars.calcPars));
42
43}
44
45static PyObject *
46CSimCylinderF_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
47{
48    CSimCylinderF *self;
49   
50    self = (CSimCylinderF *)type->tp_alloc(type, 0);
51   
52    return (PyObject *)self;
53}
54
55static int
56CSimCylinderF_init(CSimCylinderF *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,"theta",Py_BuildValue("d",0.000000));
65        PyDict_SetItemString(self->params,"length",Py_BuildValue("d",400.000000));
66        PyDict_SetItemString(self->params,"scale",Py_BuildValue("d",1.000000));
67        PyDict_SetItemString(self->params,"radius",Py_BuildValue("d",20.000000));
68        PyDict_SetItemString(self->params,"phi",Py_BuildValue("d",0.000000));
69        PyDict_SetItemString(self->params,"npoints",Py_BuildValue("d",50000.000000));
70        PyDict_SetItemString(self->params,"seed",Py_BuildValue("d",1000.000000));
71
72         
73        // Create empty log
74        self->log = PyDict_New();
75       
76                modelcalculations_init(&(self->model_pars.calcPars));
77
78    }
79    return 0;
80}
81
82static PyMemberDef CSimCylinderF_members[] = {
83    {"params", T_OBJECT, offsetof(CSimCylinderF, params), 0,
84     "Parameters"},
85    {"log", T_OBJECT, offsetof(CSimCylinderF, log), 0,
86     "Log"},
87    {NULL}  /* Sentinel */
88};
89
90/** Read double from PyObject
91    @param p PyObject
92    @return double
93*/
94double CSimCylinderF_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(CSimCylinderF *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.theta = PyFloat_AsDouble( PyDict_GetItemString(self->params, "theta") );
121    self->model_pars.length = PyFloat_AsDouble( PyDict_GetItemString(self->params, "length") );
122    self->model_pars.scale = PyFloat_AsDouble( PyDict_GetItemString(self->params, "scale") );
123    self->model_pars.radius = PyFloat_AsDouble( PyDict_GetItemString(self->params, "radius") );
124    self->model_pars.phi = PyFloat_AsDouble( PyDict_GetItemString(self->params, "phi") );
125    self->model_pars.npoints = PyFloat_AsDouble( PyDict_GetItemString(self->params, "npoints") );
126    self->model_pars.seed = PyFloat_AsDouble( PyDict_GetItemString(self->params, "seed") );
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(CSimCylinderFError, 
132                "CSimCylinderF.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(CSimCylinderFError, 
143                        "CSimCylinderF.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 = CSimCylinderF_readDouble(PyList_GET_ITEM(pars,0));
149            phi_value = CSimCylinderF_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",simcylinder_fast_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 = CSimCylinderF_readDouble(pars);               
160               
161                return Py_BuildValue("d",simcylinder_fast_analytical_1D(&(self->model_pars),q_value));
162        }       
163}
164
165static PyObject * reset(CSimCylinderF *self, PyObject *args) {
166    modelcalculations_reset(&(self->model_pars.calcPars));
167
168    return Py_BuildValue("d",0.0);
169}
170
171
172static PyMethodDef CSimCylinderF_methods[] = {
173    {"run",      (PyCFunction)run     , METH_VARARGS,
174      "Evaluate the model at a given Q"},
175    {"reset",    (PyCFunction)reset   , METH_VARARGS,
176      "Reset pair correlation"},
177    //{"numerical_1D",      (PyCFunction)numerical_1D     , METH_VARARGS,
178    //  "Evaluate the 1D model at a given Q"},
179   {NULL}
180};
181
182static PyTypeObject CSimCylinderFType = {
183    PyObject_HEAD_INIT(NULL)
184    0,                         /*ob_size*/
185    "CSimCylinderF",             /*tp_name*/
186    sizeof(CSimCylinderF),             /*tp_basicsize*/
187    0,                         /*tp_itemsize*/
188    (destructor)CSimCylinderF_dealloc, /*tp_dealloc*/
189    0,                         /*tp_print*/
190    0,                         /*tp_getattr*/
191    0,                         /*tp_setattr*/
192    0,                         /*tp_compare*/
193    0,                         /*tp_repr*/
194    0,                         /*tp_as_number*/
195    0,                         /*tp_as_sequence*/
196    0,                         /*tp_as_mapping*/
197    0,                         /*tp_hash */
198    0,                         /*tp_call*/
199    0,                         /*tp_str*/
200    0,                         /*tp_getattro*/
201    0,                         /*tp_setattro*/
202    0,                         /*tp_as_buffer*/
203    Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
204    "CSimCylinderF objects",           /* tp_doc */
205    0,                         /* tp_traverse */
206    0,                         /* tp_clear */
207    0,                         /* tp_richcompare */
208    0,                         /* tp_weaklistoffset */
209    0,                         /* tp_iter */
210    0,                         /* tp_iternext */
211    CSimCylinderF_methods,             /* tp_methods */
212    CSimCylinderF_members,             /* tp_members */
213    0,                         /* tp_getset */
214    0,                         /* tp_base */
215    0,                         /* tp_dict */
216    0,                         /* tp_descr_get */
217    0,                         /* tp_descr_set */
218    0,                         /* tp_dictoffset */
219    (initproc)CSimCylinderF_init,      /* tp_init */
220    0,                         /* tp_alloc */
221    CSimCylinderF_new,                 /* tp_new */
222};
223
224
225static PyMethodDef module_methods[] = {
226    {NULL} 
227};
228
229/**
230 * Function used to add the model class to a module
231 * @param module: module to add the class to
232 */ 
233void addCSimCylinderF(PyObject *module) {
234        PyObject *d;
235       
236    if (PyType_Ready(&CSimCylinderFType) < 0)
237        return;
238
239    Py_INCREF(&CSimCylinderFType);
240    PyModule_AddObject(module, "CSimCylinderF", (PyObject *)&CSimCylinderFType);
241   
242    d = PyModule_GetDict(module);
243    CSimCylinderFError = PyErr_NewException("CSimCylinderF.error", NULL, NULL);
244    PyDict_SetItemString(d, "CSimCylinderFError", CSimCylinderFError);
245}
246
Note: See TracBrowser for help on using the repository browser.