source: sasview/sansmodels/src/sans/models/c_models/CFlexibleCylinderModel.cpp @ e4193d9

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

add 1d models

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