source: sasview/sansmodels/src/sans/models/c_models/CBinaryHSPSF11Model.cpp @ d8a2e31

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 d8a2e31 was 2c4b289, checked in by Gervaise Alina <gervyh@…>, 15 years ago
  • Property mode set to 100644
File size: 13.8 KB
Line 
1/**
2        This software was developed by the University of Tennessee as part of the
3        Distributed Data Analysis of Neutron Scattering Experiments (DANSE)
4        project funded by the US National Science Foundation.
5
6        If you use DANSE applications to do scientific research that leads to
7        publication, we ask that you acknowledge the use of the software with the
8        following sentence:
9
10        "This work benefited from DANSE software developed under NSF award DMR-0520547."
11
12        copyright 2008, University of Tennessee
13 */
14
15/** CBinaryHSPSF11Model
16 *
17 * C extension
18 *
19 * WARNING: THIS FILE WAS GENERATED BY WRAPPERGENERATOR.PY
20 *          DO NOT MODIFY THIS FILE, MODIFY binaryHS_PSF11.h
21 *          AND RE-RUN THE GENERATOR SCRIPT
22 *
23 */
24 
25extern "C" {
26#include <Python.h>
27#include "structmember.h"
28#include <stdio.h>
29#include <stdlib.h>
30#include <math.h>
31#include <time.h>
32#include "binaryHS_PSF11.h"
33}
34
35#include "models.hh"
36#include "dispersion_visitor.hh"
37
38/// Error object for raised exceptions
39static PyObject * CBinaryHSPSF11ModelError = NULL;
40
41
42// Class definition
43typedef struct {
44    PyObject_HEAD
45    /// Parameters
46    PyObject * params;
47    /// Dispersion parameters
48    PyObject * dispersion;
49    /// Underlying model object
50    BinaryHSPSF11Model * model;
51    /// Log for unit testing
52    PyObject * log;
53} CBinaryHSPSF11Model;
54
55
56static void
57CBinaryHSPSF11Model_dealloc(CBinaryHSPSF11Model* self)
58{
59    self->ob_type->tp_free((PyObject*)self);
60   
61
62}
63
64static PyObject *
65CBinaryHSPSF11Model_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
66{
67    CBinaryHSPSF11Model *self;
68   
69    self = (CBinaryHSPSF11Model *)type->tp_alloc(type, 0);
70   
71    return (PyObject *)self;
72}
73
74static int
75CBinaryHSPSF11Model_init(CBinaryHSPSF11Model *self, PyObject *args, PyObject *kwds)
76{
77    if (self != NULL) {
78       
79        // Create parameters
80        self->params = PyDict_New();
81        self->dispersion = PyDict_New();
82        self->model = new BinaryHSPSF11Model();
83       
84        // Initialize parameter dictionary
85        PyDict_SetItemString(self->params,"vol_frac_ls",Py_BuildValue("d",0.200000));
86        PyDict_SetItemString(self->params,"background",Py_BuildValue("d",0.001000));
87        PyDict_SetItemString(self->params,"vol_frac_ss",Py_BuildValue("d",0.200000));
88        PyDict_SetItemString(self->params,"solvent_sld",Py_BuildValue("d",0.000006));
89        PyDict_SetItemString(self->params,"ls_sld",Py_BuildValue("d",0.000003));
90        PyDict_SetItemString(self->params,"ss_sld",Py_BuildValue("d",0.000000));
91        PyDict_SetItemString(self->params,"s_radius",Py_BuildValue("d",25.000000));
92        PyDict_SetItemString(self->params,"l_radius",Py_BuildValue("d",160.000000));
93        // Initialize dispersion / averaging parameter dict
94        DispersionVisitor* visitor = new DispersionVisitor();
95        PyObject * disp_dict;
96        disp_dict = PyDict_New();
97        self->model->l_radius.dispersion->accept_as_source(visitor, self->model->l_radius.dispersion, disp_dict);
98        PyDict_SetItemString(self->dispersion, "l_radius", disp_dict);
99        disp_dict = PyDict_New();
100        self->model->s_radius.dispersion->accept_as_source(visitor, self->model->s_radius.dispersion, disp_dict);
101        PyDict_SetItemString(self->dispersion, "s_radius", disp_dict);
102
103
104         
105        // Create empty log
106        self->log = PyDict_New();
107       
108       
109
110    }
111    return 0;
112}
113
114static PyMemberDef CBinaryHSPSF11Model_members[] = {
115    {"params", T_OBJECT, offsetof(CBinaryHSPSF11Model, params), 0,
116     "Parameters"},
117        {"dispersion", T_OBJECT, offsetof(CBinaryHSPSF11Model, dispersion), 0,
118          "Dispersion parameters"},     
119    {"log", T_OBJECT, offsetof(CBinaryHSPSF11Model, log), 0,
120     "Log"},
121    {NULL}  /* Sentinel */
122};
123
124/** Read double from PyObject
125    @param p PyObject
126    @return double
127*/
128double CBinaryHSPSF11Model_readDouble(PyObject *p) {
129    if (PyFloat_Check(p)==1) {
130        return (double)(((PyFloatObject *)(p))->ob_fval);
131    } else if (PyInt_Check(p)==1) {
132        return (double)(((PyIntObject *)(p))->ob_ival);
133    } else if (PyLong_Check(p)==1) {
134        return (double)PyLong_AsLong(p);
135    } else {
136        return 0.0;
137    }
138}
139
140
141/**
142 * Function to call to evaluate model
143 * @param args: input q or [q,phi]
144 * @return: function value
145 */
146static PyObject * run(CBinaryHSPSF11Model *self, PyObject *args) {
147        double q_value, phi_value;
148        PyObject* pars;
149        int npars;
150       
151        // Get parameters
152       
153            // Reader parameter dictionary
154    self->model->vol_frac_ls = PyFloat_AsDouble( PyDict_GetItemString(self->params, "vol_frac_ls") );
155    self->model->background = PyFloat_AsDouble( PyDict_GetItemString(self->params, "background") );
156    self->model->vol_frac_ss = PyFloat_AsDouble( PyDict_GetItemString(self->params, "vol_frac_ss") );
157    self->model->solvent_sld = PyFloat_AsDouble( PyDict_GetItemString(self->params, "solvent_sld") );
158    self->model->ls_sld = PyFloat_AsDouble( PyDict_GetItemString(self->params, "ls_sld") );
159    self->model->ss_sld = PyFloat_AsDouble( PyDict_GetItemString(self->params, "ss_sld") );
160    self->model->s_radius = PyFloat_AsDouble( PyDict_GetItemString(self->params, "s_radius") );
161    self->model->l_radius = PyFloat_AsDouble( PyDict_GetItemString(self->params, "l_radius") );
162    // Read in dispersion parameters
163    PyObject* disp_dict;
164    DispersionVisitor* visitor = new DispersionVisitor();
165    disp_dict = PyDict_GetItemString(self->dispersion, "l_radius");
166    self->model->l_radius.dispersion->accept_as_destination(visitor, self->model->l_radius.dispersion, disp_dict);
167    disp_dict = PyDict_GetItemString(self->dispersion, "s_radius");
168    self->model->s_radius.dispersion->accept_as_destination(visitor, self->model->s_radius.dispersion, disp_dict);
169
170       
171        // Get input and determine whether we have to supply a 1D or 2D return value.
172        if ( !PyArg_ParseTuple(args,"O",&pars) ) {
173            PyErr_SetString(CBinaryHSPSF11ModelError, 
174                "CBinaryHSPSF11Model.run expects a q value.");
175                return NULL;
176        }
177         
178        // Check params
179        if( PyList_Check(pars)==1) {
180               
181                // Length of list should be 2 for I(q,phi)
182            npars = PyList_GET_SIZE(pars); 
183            if(npars!=2) {
184                PyErr_SetString(CBinaryHSPSF11ModelError, 
185                        "CBinaryHSPSF11Model.run expects a double or a list of dimension 2.");
186                return NULL;
187            }
188            // We have a vector q, get the q and phi values at which
189            // to evaluate I(q,phi)
190            q_value = CBinaryHSPSF11Model_readDouble(PyList_GET_ITEM(pars,0));
191            phi_value = CBinaryHSPSF11Model_readDouble(PyList_GET_ITEM(pars,1));
192            // Skip zero
193            if (q_value==0) {
194                return Py_BuildValue("d",0.0);
195            }
196                return Py_BuildValue("d",(*(self->model)).evaluate_rphi(q_value,phi_value));
197
198        } else {
199
200                // We have a scalar q, we will evaluate I(q)
201                q_value = CBinaryHSPSF11Model_readDouble(pars);         
202               
203                return Py_BuildValue("d",(*(self->model))(q_value));
204        }       
205}
206
207/**
208 * Function to call to evaluate model in cartesian coordinates
209 * @param args: input q or [qx, qy]]
210 * @return: function value
211 */
212static PyObject * runXY(CBinaryHSPSF11Model *self, PyObject *args) {
213        double qx_value, qy_value;
214        PyObject* pars;
215        int npars;
216       
217        // Get parameters
218       
219            // Reader parameter dictionary
220    self->model->vol_frac_ls = PyFloat_AsDouble( PyDict_GetItemString(self->params, "vol_frac_ls") );
221    self->model->background = PyFloat_AsDouble( PyDict_GetItemString(self->params, "background") );
222    self->model->vol_frac_ss = PyFloat_AsDouble( PyDict_GetItemString(self->params, "vol_frac_ss") );
223    self->model->solvent_sld = PyFloat_AsDouble( PyDict_GetItemString(self->params, "solvent_sld") );
224    self->model->ls_sld = PyFloat_AsDouble( PyDict_GetItemString(self->params, "ls_sld") );
225    self->model->ss_sld = PyFloat_AsDouble( PyDict_GetItemString(self->params, "ss_sld") );
226    self->model->s_radius = PyFloat_AsDouble( PyDict_GetItemString(self->params, "s_radius") );
227    self->model->l_radius = PyFloat_AsDouble( PyDict_GetItemString(self->params, "l_radius") );
228    // Read in dispersion parameters
229    PyObject* disp_dict;
230    DispersionVisitor* visitor = new DispersionVisitor();
231    disp_dict = PyDict_GetItemString(self->dispersion, "l_radius");
232    self->model->l_radius.dispersion->accept_as_destination(visitor, self->model->l_radius.dispersion, disp_dict);
233    disp_dict = PyDict_GetItemString(self->dispersion, "s_radius");
234    self->model->s_radius.dispersion->accept_as_destination(visitor, self->model->s_radius.dispersion, disp_dict);
235
236       
237        // Get input and determine whether we have to supply a 1D or 2D return value.
238        if ( !PyArg_ParseTuple(args,"O",&pars) ) {
239            PyErr_SetString(CBinaryHSPSF11ModelError, 
240                "CBinaryHSPSF11Model.run expects a q value.");
241                return NULL;
242        }
243         
244        // Check params
245        if( PyList_Check(pars)==1) {
246               
247                // Length of list should be 2 for I(qx, qy))
248            npars = PyList_GET_SIZE(pars); 
249            if(npars!=2) {
250                PyErr_SetString(CBinaryHSPSF11ModelError, 
251                        "CBinaryHSPSF11Model.run expects a double or a list of dimension 2.");
252                return NULL;
253            }
254            // We have a vector q, get the qx and qy values at which
255            // to evaluate I(qx,qy)
256            qx_value = CBinaryHSPSF11Model_readDouble(PyList_GET_ITEM(pars,0));
257            qy_value = CBinaryHSPSF11Model_readDouble(PyList_GET_ITEM(pars,1));
258            return Py_BuildValue("d",(*(self->model))(qx_value,qy_value));
259
260        } else {
261
262                // We have a scalar q, we will evaluate I(q)
263                qx_value = CBinaryHSPSF11Model_readDouble(pars);               
264               
265                return Py_BuildValue("d",(*(self->model))(qx_value));
266        }       
267}
268
269static PyObject * reset(CBinaryHSPSF11Model *self, PyObject *args) {
270   
271
272    return Py_BuildValue("d",0.0);
273}
274
275static PyObject * set_dispersion(CBinaryHSPSF11Model *self, PyObject *args) {
276        PyObject * disp;
277        const char * par_name;
278
279        if ( !PyArg_ParseTuple(args,"sO", &par_name, &disp) ) {
280            PyErr_SetString(CBinaryHSPSF11ModelError,
281                "CBinaryHSPSF11Model.set_dispersion expects a DispersionModel object.");
282                return NULL;
283        }
284        void *temp = PyCObject_AsVoidPtr(disp);
285        DispersionModel * dispersion = static_cast<DispersionModel *>(temp);
286
287
288        // Ugliness necessary to go from python to C
289            // TODO: refactor this
290    if (!strcmp(par_name, "l_radius")) {
291        self->model->l_radius.dispersion = dispersion;
292    } else    if (!strcmp(par_name, "s_radius")) {
293        self->model->s_radius.dispersion = dispersion;
294    } else {
295            PyErr_SetString(CBinaryHSPSF11ModelError,
296                "CBinaryHSPSF11Model.set_dispersion expects a valid parameter name.");
297                return NULL;
298        }
299
300        DispersionVisitor* visitor = new DispersionVisitor();
301        PyObject * disp_dict = PyDict_New();
302        dispersion->accept_as_source(visitor, dispersion, disp_dict);
303        PyDict_SetItemString(self->dispersion, par_name, disp_dict);
304    return Py_BuildValue("i",1);
305}
306
307
308static PyMethodDef CBinaryHSPSF11Model_methods[] = {
309    {"run",      (PyCFunction)run     , METH_VARARGS,
310      "Evaluate the model at a given Q or Q, phi"},
311    {"runXY",      (PyCFunction)runXY     , METH_VARARGS,
312      "Evaluate the model at a given Q or Qx, Qy"},
313    {"reset",    (PyCFunction)reset   , METH_VARARGS,
314      "Reset pair correlation"},
315    {"set_dispersion",      (PyCFunction)set_dispersion     , METH_VARARGS,
316      "Set the dispersion model for a given parameter"},
317   {NULL}
318};
319
320static PyTypeObject CBinaryHSPSF11ModelType = {
321    PyObject_HEAD_INIT(NULL)
322    0,                         /*ob_size*/
323    "CBinaryHSPSF11Model",             /*tp_name*/
324    sizeof(CBinaryHSPSF11Model),             /*tp_basicsize*/
325    0,                         /*tp_itemsize*/
326    (destructor)CBinaryHSPSF11Model_dealloc, /*tp_dealloc*/
327    0,                         /*tp_print*/
328    0,                         /*tp_getattr*/
329    0,                         /*tp_setattr*/
330    0,                         /*tp_compare*/
331    0,                         /*tp_repr*/
332    0,                         /*tp_as_number*/
333    0,                         /*tp_as_sequence*/
334    0,                         /*tp_as_mapping*/
335    0,                         /*tp_hash */
336    0,                         /*tp_call*/
337    0,                         /*tp_str*/
338    0,                         /*tp_getattro*/
339    0,                         /*tp_setattro*/
340    0,                         /*tp_as_buffer*/
341    Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
342    "CBinaryHSPSF11Model objects",           /* tp_doc */
343    0,                         /* tp_traverse */
344    0,                         /* tp_clear */
345    0,                         /* tp_richcompare */
346    0,                         /* tp_weaklistoffset */
347    0,                         /* tp_iter */
348    0,                         /* tp_iternext */
349    CBinaryHSPSF11Model_methods,             /* tp_methods */
350    CBinaryHSPSF11Model_members,             /* tp_members */
351    0,                         /* tp_getset */
352    0,                         /* tp_base */
353    0,                         /* tp_dict */
354    0,                         /* tp_descr_get */
355    0,                         /* tp_descr_set */
356    0,                         /* tp_dictoffset */
357    (initproc)CBinaryHSPSF11Model_init,      /* tp_init */
358    0,                         /* tp_alloc */
359    CBinaryHSPSF11Model_new,                 /* tp_new */
360};
361
362
363static PyMethodDef module_methods[] = {
364    {NULL} 
365};
366
367/**
368 * Function used to add the model class to a module
369 * @param module: module to add the class to
370 */ 
371void addCBinaryHSPSF11Model(PyObject *module) {
372        PyObject *d;
373       
374    if (PyType_Ready(&CBinaryHSPSF11ModelType) < 0)
375        return;
376
377    Py_INCREF(&CBinaryHSPSF11ModelType);
378    PyModule_AddObject(module, "CBinaryHSPSF11Model", (PyObject *)&CBinaryHSPSF11ModelType);
379   
380    d = PyModule_GetDict(module);
381    CBinaryHSPSF11ModelError = PyErr_NewException("CBinaryHSPSF11Model.error", NULL, NULL);
382    PyDict_SetItemString(d, "CBinaryHSPSF11ModelError", CBinaryHSPSF11ModelError);
383}
384
Note: See TracBrowser for help on using the repository browser.