source: sasview/sansmodels/src/sans/models/c_models/CVesicleModel.cpp @ 3d25331f

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

add more model 1d and 2 D

  • Property mode set to 100644
File size: 12.3 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/** CVesicleModel
16 *
17 * C extension
18 *
19 * WARNING: THIS FILE WAS GENERATED BY WRAPPERGENERATOR.PY
20 *          DO NOT MODIFY THIS FILE, MODIFY vesicle.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 "vesicle.h"
33}
34
35#include "models.hh"
36#include "dispersion_visitor.hh"
37
38/// Error object for raised exceptions
39static PyObject * CVesicleModelError = 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    VesicleModel * model;
51    /// Log for unit testing
52    PyObject * log;
53} CVesicleModel;
54
55
56static void
57CVesicleModel_dealloc(CVesicleModel* self)
58{
59    self->ob_type->tp_free((PyObject*)self);
60   
61
62}
63
64static PyObject *
65CVesicleModel_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
66{
67    CVesicleModel *self;
68   
69    self = (CVesicleModel *)type->tp_alloc(type, 0);
70   
71    return (PyObject *)self;
72}
73
74static int
75CVesicleModel_init(CVesicleModel *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 VesicleModel();
83       
84        // Initialize parameter dictionary
85        PyDict_SetItemString(self->params,"core_sld",Py_BuildValue("d",0.000006));
86        PyDict_SetItemString(self->params,"core_radius",Py_BuildValue("d",100.000000));
87        PyDict_SetItemString(self->params,"thickness",Py_BuildValue("d",30.000000));
88        PyDict_SetItemString(self->params,"scale",Py_BuildValue("d",1.000000));
89        PyDict_SetItemString(self->params,"background",Py_BuildValue("d",0.000000));
90        PyDict_SetItemString(self->params,"shell_sld",Py_BuildValue("d",0.000000));
91        // Initialize dispersion / averaging parameter dict
92        DispersionVisitor* visitor = new DispersionVisitor();
93        PyObject * disp_dict;
94        disp_dict = PyDict_New();
95        self->model->core_radius.dispersion->accept_as_source(visitor, self->model->core_radius.dispersion, disp_dict);
96        PyDict_SetItemString(self->dispersion, "core_radius", disp_dict);
97
98
99         
100        // Create empty log
101        self->log = PyDict_New();
102       
103       
104
105    }
106    return 0;
107}
108
109static PyMemberDef CVesicleModel_members[] = {
110    {"params", T_OBJECT, offsetof(CVesicleModel, params), 0,
111     "Parameters"},
112        {"dispersion", T_OBJECT, offsetof(CVesicleModel, dispersion), 0,
113          "Dispersion parameters"},     
114    {"log", T_OBJECT, offsetof(CVesicleModel, log), 0,
115     "Log"},
116    {NULL}  /* Sentinel */
117};
118
119/** Read double from PyObject
120    @param p PyObject
121    @return double
122*/
123double CVesicleModel_readDouble(PyObject *p) {
124    if (PyFloat_Check(p)==1) {
125        return (double)(((PyFloatObject *)(p))->ob_fval);
126    } else if (PyInt_Check(p)==1) {
127        return (double)(((PyIntObject *)(p))->ob_ival);
128    } else if (PyLong_Check(p)==1) {
129        return (double)PyLong_AsLong(p);
130    } else {
131        return 0.0;
132    }
133}
134
135
136/**
137 * Function to call to evaluate model
138 * @param args: input q or [q,phi]
139 * @return: function value
140 */
141static PyObject * run(CVesicleModel *self, PyObject *args) {
142        double q_value, phi_value;
143        PyObject* pars;
144        int npars;
145       
146        // Get parameters
147       
148            // Reader parameter dictionary
149    self->model->core_sld = PyFloat_AsDouble( PyDict_GetItemString(self->params, "core_sld") );
150    self->model->core_radius = PyFloat_AsDouble( PyDict_GetItemString(self->params, "core_radius") );
151    self->model->thickness = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thickness") );
152    self->model->scale = PyFloat_AsDouble( PyDict_GetItemString(self->params, "scale") );
153    self->model->background = PyFloat_AsDouble( PyDict_GetItemString(self->params, "background") );
154    self->model->shell_sld = PyFloat_AsDouble( PyDict_GetItemString(self->params, "shell_sld") );
155    // Read in dispersion parameters
156    PyObject* disp_dict;
157    DispersionVisitor* visitor = new DispersionVisitor();
158    disp_dict = PyDict_GetItemString(self->dispersion, "core_radius");
159    self->model->core_radius.dispersion->accept_as_destination(visitor, self->model->core_radius.dispersion, disp_dict);
160
161       
162        // Get input and determine whether we have to supply a 1D or 2D return value.
163        if ( !PyArg_ParseTuple(args,"O",&pars) ) {
164            PyErr_SetString(CVesicleModelError, 
165                "CVesicleModel.run expects a q value.");
166                return NULL;
167        }
168         
169        // Check params
170        if( PyList_Check(pars)==1) {
171               
172                // Length of list should be 2 for I(q,phi)
173            npars = PyList_GET_SIZE(pars); 
174            if(npars!=2) {
175                PyErr_SetString(CVesicleModelError, 
176                        "CVesicleModel.run expects a double or a list of dimension 2.");
177                return NULL;
178            }
179            // We have a vector q, get the q and phi values at which
180            // to evaluate I(q,phi)
181            q_value = CVesicleModel_readDouble(PyList_GET_ITEM(pars,0));
182            phi_value = CVesicleModel_readDouble(PyList_GET_ITEM(pars,1));
183            // Skip zero
184            if (q_value==0) {
185                return Py_BuildValue("d",0.0);
186            }
187                return Py_BuildValue("d",(*(self->model)).evaluate_rphi(q_value,phi_value));
188
189        } else {
190
191                // We have a scalar q, we will evaluate I(q)
192                q_value = CVesicleModel_readDouble(pars);               
193               
194                return Py_BuildValue("d",(*(self->model))(q_value));
195        }       
196}
197
198/**
199 * Function to call to evaluate model in cartesian coordinates
200 * @param args: input q or [qx, qy]]
201 * @return: function value
202 */
203static PyObject * runXY(CVesicleModel *self, PyObject *args) {
204        double qx_value, qy_value;
205        PyObject* pars;
206        int npars;
207       
208        // Get parameters
209       
210            // Reader parameter dictionary
211    self->model->core_sld = PyFloat_AsDouble( PyDict_GetItemString(self->params, "core_sld") );
212    self->model->core_radius = PyFloat_AsDouble( PyDict_GetItemString(self->params, "core_radius") );
213    self->model->thickness = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thickness") );
214    self->model->scale = PyFloat_AsDouble( PyDict_GetItemString(self->params, "scale") );
215    self->model->background = PyFloat_AsDouble( PyDict_GetItemString(self->params, "background") );
216    self->model->shell_sld = PyFloat_AsDouble( PyDict_GetItemString(self->params, "shell_sld") );
217    // Read in dispersion parameters
218    PyObject* disp_dict;
219    DispersionVisitor* visitor = new DispersionVisitor();
220    disp_dict = PyDict_GetItemString(self->dispersion, "core_radius");
221    self->model->core_radius.dispersion->accept_as_destination(visitor, self->model->core_radius.dispersion, disp_dict);
222
223       
224        // Get input and determine whether we have to supply a 1D or 2D return value.
225        if ( !PyArg_ParseTuple(args,"O",&pars) ) {
226            PyErr_SetString(CVesicleModelError, 
227                "CVesicleModel.run expects a q value.");
228                return NULL;
229        }
230         
231        // Check params
232        if( PyList_Check(pars)==1) {
233               
234                // Length of list should be 2 for I(qx, qy))
235            npars = PyList_GET_SIZE(pars); 
236            if(npars!=2) {
237                PyErr_SetString(CVesicleModelError, 
238                        "CVesicleModel.run expects a double or a list of dimension 2.");
239                return NULL;
240            }
241            // We have a vector q, get the qx and qy values at which
242            // to evaluate I(qx,qy)
243            qx_value = CVesicleModel_readDouble(PyList_GET_ITEM(pars,0));
244            qy_value = CVesicleModel_readDouble(PyList_GET_ITEM(pars,1));
245            return Py_BuildValue("d",(*(self->model))(qx_value,qy_value));
246
247        } else {
248
249                // We have a scalar q, we will evaluate I(q)
250                qx_value = CVesicleModel_readDouble(pars);             
251               
252                return Py_BuildValue("d",(*(self->model))(qx_value));
253        }       
254}
255
256static PyObject * reset(CVesicleModel *self, PyObject *args) {
257   
258
259    return Py_BuildValue("d",0.0);
260}
261
262static PyObject * set_dispersion(CVesicleModel *self, PyObject *args) {
263        PyObject * disp;
264        const char * par_name;
265
266        if ( !PyArg_ParseTuple(args,"sO", &par_name, &disp) ) {
267            PyErr_SetString(CVesicleModelError,
268                "CVesicleModel.set_dispersion expects a DispersionModel object.");
269                return NULL;
270        }
271        void *temp = PyCObject_AsVoidPtr(disp);
272        DispersionModel * dispersion = static_cast<DispersionModel *>(temp);
273
274
275        // Ugliness necessary to go from python to C
276            // TODO: refactor this
277    if (!strcmp(par_name, "core_radius")) {
278        self->model->core_radius.dispersion = dispersion;
279    } else {
280            PyErr_SetString(CVesicleModelError,
281                "CVesicleModel.set_dispersion expects a valid parameter name.");
282                return NULL;
283        }
284
285        DispersionVisitor* visitor = new DispersionVisitor();
286        PyObject * disp_dict = PyDict_New();
287        dispersion->accept_as_source(visitor, dispersion, disp_dict);
288        PyDict_SetItemString(self->dispersion, par_name, disp_dict);
289    return Py_BuildValue("i",1);
290}
291
292
293static PyMethodDef CVesicleModel_methods[] = {
294    {"run",      (PyCFunction)run     , METH_VARARGS,
295      "Evaluate the model at a given Q or Q, phi"},
296    {"runXY",      (PyCFunction)runXY     , METH_VARARGS,
297      "Evaluate the model at a given Q or Qx, Qy"},
298    {"reset",    (PyCFunction)reset   , METH_VARARGS,
299      "Reset pair correlation"},
300    {"set_dispersion",      (PyCFunction)set_dispersion     , METH_VARARGS,
301      "Set the dispersion model for a given parameter"},
302   {NULL}
303};
304
305static PyTypeObject CVesicleModelType = {
306    PyObject_HEAD_INIT(NULL)
307    0,                         /*ob_size*/
308    "CVesicleModel",             /*tp_name*/
309    sizeof(CVesicleModel),             /*tp_basicsize*/
310    0,                         /*tp_itemsize*/
311    (destructor)CVesicleModel_dealloc, /*tp_dealloc*/
312    0,                         /*tp_print*/
313    0,                         /*tp_getattr*/
314    0,                         /*tp_setattr*/
315    0,                         /*tp_compare*/
316    0,                         /*tp_repr*/
317    0,                         /*tp_as_number*/
318    0,                         /*tp_as_sequence*/
319    0,                         /*tp_as_mapping*/
320    0,                         /*tp_hash */
321    0,                         /*tp_call*/
322    0,                         /*tp_str*/
323    0,                         /*tp_getattro*/
324    0,                         /*tp_setattro*/
325    0,                         /*tp_as_buffer*/
326    Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
327    "CVesicleModel objects",           /* tp_doc */
328    0,                         /* tp_traverse */
329    0,                         /* tp_clear */
330    0,                         /* tp_richcompare */
331    0,                         /* tp_weaklistoffset */
332    0,                         /* tp_iter */
333    0,                         /* tp_iternext */
334    CVesicleModel_methods,             /* tp_methods */
335    CVesicleModel_members,             /* tp_members */
336    0,                         /* tp_getset */
337    0,                         /* tp_base */
338    0,                         /* tp_dict */
339    0,                         /* tp_descr_get */
340    0,                         /* tp_descr_set */
341    0,                         /* tp_dictoffset */
342    (initproc)CVesicleModel_init,      /* tp_init */
343    0,                         /* tp_alloc */
344    CVesicleModel_new,                 /* tp_new */
345};
346
347
348static PyMethodDef module_methods[] = {
349    {NULL} 
350};
351
352/**
353 * Function used to add the model class to a module
354 * @param module: module to add the class to
355 */ 
356void addCVesicleModel(PyObject *module) {
357        PyObject *d;
358       
359    if (PyType_Ready(&CVesicleModelType) < 0)
360        return;
361
362    Py_INCREF(&CVesicleModelType);
363    PyModule_AddObject(module, "CVesicleModel", (PyObject *)&CVesicleModelType);
364   
365    d = PyModule_GetDict(module);
366    CVesicleModelError = PyErr_NewException("CVesicleModel.error", NULL, NULL);
367    PyDict_SetItemString(d, "CVesicleModelError", CVesicleModelError);
368}
369
Note: See TracBrowser for help on using the repository browser.