source: sasview/sansmodels/src/sans/models/c_extensions/CBinaryHSPSF11Model.c @ d6da3b1

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 d6da3b1 was e0a8a3c, checked in by Gervaise Alina <gervyh@…>, 15 years ago

change the orientation of models

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