source: sasview/sansmodels/src/sans/models/c_models/CFlexibleCylinderModel.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 ea07075, checked in by Jae Cho <jhjcho@…>, 15 years ago

done corrections and 2d extension on this model function

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