source: sasview/sansmodels/src/sans/models/c_models/CVesicleModel.cpp @ 8e91f01

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 8e91f01 was 42f193a, checked in by Jae Cho <jhjcho@…>, 15 years ago

some corrections on dips-parameters and adding 2D cal

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