source: sasview/sansmodels/prototypes/src/CSimSphereF.c @ 5548954

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

moving a file

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