source: sasview/sansmodels/src/sans/models/c_models/CCylinder.cpp @ fca6936

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 fca6936 was fca6936, checked in by Mathieu Doucet <doucetm@…>, 16 years ago

First cut at better polydisp and averaging implementation for sans models.

  • Property mode set to 100644
File size: 12.8 KB
Line 
1/** CCylinder
2 *
3 * C extension
4 *
5 */
6extern "C" {
7#include <Python.h>
8#include "structmember.h"
9#include <stdio.h>
10#include <stdlib.h>
11#include <math.h>
12#include <time.h>
13#include "cylinder.h"
14}
15
16
17#include "models.hh"
18#include "dispersion_visitor.hh"
19
20/// Error object for raised exceptions
21static PyObject * CCylinderModelError = NULL;
22
23
24// Class definition
25typedef struct {
26    PyObject_HEAD
27    /// Parameters
28    PyObject * params;
29    PyObject * dispersion;
30    Cylinder * model;
31    /// Log for unit testing
32    PyObject * log;
33    /// Model parameters
34        CylinderParameters model_pars;
35} CCylinderModel;
36
37
38static void
39CCylinderModel_dealloc(CCylinderModel* self)
40{
41    self->ob_type->tp_free((PyObject*)self);
42
43
44}
45
46static PyObject *
47CCylinderModel_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
48{
49    CCylinderModel *self;
50
51    self = (CCylinderModel *)type->tp_alloc(type, 0);
52
53    return (PyObject *)self;
54}
55
56static int
57CCylinderModel_init(CCylinderModel *self, PyObject *args, PyObject *kwds)
58{
59    if (self != NULL) {
60        printf("Hello cylinder\n");
61        // Create parameters
62        self->params = PyDict_New();
63        self->dispersion = PyDict_New();
64        self->model = new Cylinder();
65
66        // Initialize parameter dictionary
67        PyDict_SetItemString(self->params,"scale",     Py_BuildValue("d", self->model->scale()));
68        PyDict_SetItemString(self->params,"length",    Py_BuildValue("d", self->model->length()));
69        PyDict_SetItemString(self->params,"cyl_theta", Py_BuildValue("d", self->model->cyl_theta()));
70        PyDict_SetItemString(self->params,"background",Py_BuildValue("d", self->model->background()));
71        PyDict_SetItemString(self->params,"radius",    Py_BuildValue("d", self->model->radius()));
72        PyDict_SetItemString(self->params,"contrast",  Py_BuildValue("d", self->model->contrast()));
73        PyDict_SetItemString(self->params,"cyl_phi",   Py_BuildValue("d", self->model->cyl_phi()));
74
75        // Initialize dispersion / averaging parameter dict
76        DispersionVisitor* visitor = new DispersionVisitor();
77        PyObject * disp_dict = PyDict_New();
78        self->model->radius.dispersion->accept_as_source(visitor, self->model->radius.dispersion, disp_dict);
79        PyDict_SetItemString(self->dispersion, "disp_radius", disp_dict);
80
81        disp_dict = PyDict_New();
82        self->model->length.dispersion->accept_as_source(visitor, self->model->length.dispersion, disp_dict);
83        PyDict_SetItemString(self->dispersion, "disp_length", disp_dict);
84
85        disp_dict = PyDict_New();
86        self->model->cyl_phi.dispersion->accept_as_source(visitor, self->model->cyl_phi.dispersion, disp_dict);
87        PyDict_SetItemString(self->dispersion, "disp_cyl_phi", disp_dict);
88
89        disp_dict = PyDict_New();
90        self->model->cyl_theta.dispersion->accept_as_source(visitor, self->model->cyl_theta.dispersion, disp_dict);
91        PyDict_SetItemString(self->dispersion, "disp_cyl_theta", disp_dict);
92
93        // Create empty log
94        self->log = PyDict_New();
95
96    }
97    return 0;
98}
99
100static PyMemberDef CCylinderModel_members[] = {
101        {"params", T_OBJECT, offsetof(CCylinderModel, params), 0,
102         "Parameters"},
103        {"dispersion", T_OBJECT, offsetof(CCylinderModel, dispersion), 0,
104          "Dispersion parameters"},
105    {"log", T_OBJECT, offsetof(CCylinderModel, log), 0,
106     "Log"},
107    {NULL}  /* Sentinel */
108};
109
110/** Read double from PyObject
111    @param p PyObject
112    @return double
113*/
114double CCylinderModel_readDouble(PyObject *p) {
115    if (PyFloat_Check(p)==1) {
116        return (double)(((PyFloatObject *)(p))->ob_fval);
117    } else if (PyInt_Check(p)==1) {
118        return (double)(((PyIntObject *)(p))->ob_ival);
119    } else if (PyLong_Check(p)==1) {
120        return (double)PyLong_AsLong(p);
121    } else {
122        return 0.0;
123    }
124}
125
126/**
127 * Method to set the dispersion model for a parameter.
128 * We need to update the dispersion object of the parameter
129 * on the C++ side and update the dispersion dictionary on the python side.
130 *
131 * TODO: define read-only integers that will be data members of this
132 * class and allow the user to specify which model is needed with a code name.
133 *
134 * This method should take in a code name (FLAT, GAUSSIAN) and a string
135 * that represent the parameter to attach the dispersion model to.
136 *
137 */
138static PyObject * set_dispersion_model(CCylinderModel *self, PyObject *args) {
139
140}
141
142
143/**
144 * Function to call to evaluate model
145 * @param args: input q or [q,phi]
146 * @return: function value
147 */
148static PyObject * run(CCylinderModel *self, PyObject *args) {
149        double q_value, phi_value;
150        PyObject* pars;
151        int npars;
152
153        // Get parameters
154
155        // Reader parameter dictionary
156    self->model->scale = PyFloat_AsDouble( PyDict_GetItemString(self->params, "scale") );
157    self->model->length = PyFloat_AsDouble( PyDict_GetItemString(self->params, "length") );
158    self->model->cyl_theta = PyFloat_AsDouble( PyDict_GetItemString(self->params, "cyl_theta") );
159    self->model->background = PyFloat_AsDouble( PyDict_GetItemString(self->params, "background") );
160    self->model->radius = PyFloat_AsDouble( PyDict_GetItemString(self->params, "radius") );
161    self->model->contrast = PyFloat_AsDouble( PyDict_GetItemString(self->params, "contrast") );
162    self->model->cyl_phi = PyFloat_AsDouble( PyDict_GetItemString(self->params, "cyl_phi") );
163
164    // Read in dispersion parameters
165    DispersionVisitor* visitor = new DispersionVisitor();
166    PyObject* disp_dict = PyDict_New();
167    disp_dict = PyDict_GetItemString(self->dispersion, "disp_radius");
168    self->model->radius.dispersion->accept_as_destination(visitor, self->model->radius.dispersion, disp_dict);
169    self->model->length.dispersion->accept_as_destination(visitor, self->model->length.dispersion, disp_dict);
170    self->model->cyl_theta.dispersion->accept_as_destination(visitor, self->model->cyl_theta.dispersion, disp_dict);
171    self->model->cyl_phi.dispersion->accept_as_destination(visitor, self->model->cyl_phi.dispersion, disp_dict);
172
173        // Get input and determine whether we have to supply a 1D or 2D return value.
174        if ( !PyArg_ParseTuple(args,"O",&pars) ) {
175            PyErr_SetString(CCylinderModelError,
176                "CCylinderModel.run expects a q value.");
177                return NULL;
178        }
179
180        // Check params
181        if( PyList_Check(pars)==1) {
182
183                // Length of list should be 2 for I(q,phi)
184            npars = PyList_GET_SIZE(pars);
185            if(npars!=2) {
186                PyErr_SetString(CCylinderModelError,
187                        "CCylinderModel.run expects a double or a list of dimension 2.");
188                return NULL;
189            }
190            // We have a vector q, get the q and phi values at which
191            // to evaluate I(q,phi)
192            q_value = CCylinderModel_readDouble(PyList_GET_ITEM(pars,0));
193            phi_value = CCylinderModel_readDouble(PyList_GET_ITEM(pars,1));
194            // Skip zero
195            if (q_value==0) {
196                return Py_BuildValue("d",0.0);
197            }
198                return Py_BuildValue("d",(*(self->model)).evaluate_rphi(q_value,phi_value));
199
200        } else {
201
202                // We have a scalar q, we will evaluate I(q)
203                q_value = CCylinderModel_readDouble(pars);
204
205                return Py_BuildValue("d",(*(self->model))(q_value));
206        }
207}
208
209/**
210 * Function to call to evaluate model in cartesian coordinates
211 * @param args: input q or [qx, qy]]
212 * @return: function value
213 */
214static PyObject * runXY(CCylinderModel *self, PyObject *args) {
215        double qx_value, qy_value;
216        PyObject* pars;
217        int npars;
218
219        // Get parameters
220
221        // Reader parameter dictionary
222    self->model->scale = PyFloat_AsDouble( PyDict_GetItemString(self->params, "scale") );
223    self->model->length = PyFloat_AsDouble( PyDict_GetItemString(self->params, "length") );
224    self->model->cyl_theta = PyFloat_AsDouble( PyDict_GetItemString(self->params, "cyl_theta") );
225    self->model->background = PyFloat_AsDouble( PyDict_GetItemString(self->params, "background") );
226    self->model->radius = PyFloat_AsDouble( PyDict_GetItemString(self->params, "radius") );
227    self->model->contrast = PyFloat_AsDouble( PyDict_GetItemString(self->params, "contrast") );
228    self->model->cyl_phi = PyFloat_AsDouble( PyDict_GetItemString(self->params, "cyl_phi") );
229
230    // Read in dispersion parameters
231    DispersionVisitor* visitor = new DispersionVisitor();
232    PyObject* disp_dict = PyDict_New();
233    disp_dict = PyDict_GetItemString(self->dispersion, "disp_radius");
234    self->model->radius.dispersion->accept_as_destination(visitor, self->model->radius.dispersion, disp_dict);
235    self->model->length.dispersion->accept_as_destination(visitor, self->model->length.dispersion, disp_dict);
236    self->model->cyl_theta.dispersion->accept_as_destination(visitor, self->model->cyl_theta.dispersion, disp_dict);
237    self->model->cyl_phi.dispersion->accept_as_destination(visitor, self->model->cyl_phi.dispersion, disp_dict);
238
239        // Get input and determine whether we have to supply a 1D or 2D return value.
240        if ( !PyArg_ParseTuple(args,"O",&pars) ) {
241            PyErr_SetString(CCylinderModelError,
242                "CCylinderModel.run expects a q value.");
243                return NULL;
244        }
245
246        // Check params
247        if( PyList_Check(pars)==1) {
248
249                // Length of list should be 2 for I(qx, qy))
250            npars = PyList_GET_SIZE(pars);
251            if(npars!=2) {
252                PyErr_SetString(CCylinderModelError,
253                        "CCylinderModel.run expects a double or a list of dimension 2.");
254                return NULL;
255            }
256            // We have a vector q, get the qx and qy values at which
257            // to evaluate I(qx,qy)
258            qx_value = CCylinderModel_readDouble(PyList_GET_ITEM(pars,0));
259            qy_value = CCylinderModel_readDouble(PyList_GET_ITEM(pars,1));
260            return Py_BuildValue("d",(*(self->model))(qx_value,qy_value));
261
262        } else {
263
264                // We have a scalar q, we will evaluate I(q)
265                qx_value = CCylinderModel_readDouble(pars);
266
267                return Py_BuildValue("d",(*(self->model))(qx_value));
268        }
269}
270
271static PyObject * reset(CCylinderModel *self, PyObject *args) {
272
273
274    return Py_BuildValue("d",0.0);
275}
276
277
278static PyMethodDef CCylinderModel_methods[] = {
279    {"run",      (PyCFunction)run     , METH_VARARGS,
280      "Evaluate the model at a given Q or Q, phi"},
281    {"runXY",      (PyCFunction)runXY     , METH_VARARGS,
282      "Evaluate the model at a given Q or Qx, Qy"},
283    {"reset",    (PyCFunction)reset   , METH_VARARGS,
284      "Reset pair correlation"},
285    //{"numerical_1D",      (PyCFunction)numerical_1D     , METH_VARARGS,
286    //  "Evaluate the 1D model at a given Q"},
287   {NULL}
288};
289
290static PyTypeObject CCylinderModelType = {
291    PyObject_HEAD_INIT(NULL)
292    0,                         /*ob_size*/
293    "CCylinderModel",             /*tp_name*/
294    sizeof(CCylinderModel),             /*tp_basicsize*/
295    0,                         /*tp_itemsize*/
296    (destructor)CCylinderModel_dealloc, /*tp_dealloc*/
297    0,                         /*tp_print*/
298    0,                         /*tp_getattr*/
299    0,                         /*tp_setattr*/
300    0,                         /*tp_compare*/
301    0,                         /*tp_repr*/
302    0,                         /*tp_as_number*/
303    0,                         /*tp_as_sequence*/
304    0,                         /*tp_as_mapping*/
305    0,                         /*tp_hash */
306    0,                         /*tp_call*/
307    0,                         /*tp_str*/
308    0,                         /*tp_getattro*/
309    0,                         /*tp_setattro*/
310    0,                         /*tp_as_buffer*/
311    Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
312    "CCylinderModel objects",           /* tp_doc */
313    0,                         /* tp_traverse */
314    0,                         /* tp_clear */
315    0,                         /* tp_richcompare */
316    0,                         /* tp_weaklistoffset */
317    0,                         /* tp_iter */
318    0,                         /* tp_iternext */
319    CCylinderModel_methods,             /* tp_methods */
320    CCylinderModel_members,             /* tp_members */
321    0,                         /* tp_getset */
322    0,                         /* tp_base */
323    0,                         /* tp_dict */
324    0,                         /* tp_descr_get */
325    0,                         /* tp_descr_set */
326    0,                         /* tp_dictoffset */
327    (initproc)CCylinderModel_init,      /* tp_init */
328    0,                         /* tp_alloc */
329    CCylinderModel_new,                 /* tp_new */
330};
331
332
333static PyMethodDef module_methods[] = {
334    {NULL}
335};
336
337/**
338 * Function used to add the model class to a module
339 * @param module: module to add the class to
340 */
341void addCCylinderModel(PyObject *module) {
342        PyObject *d;
343
344    if (PyType_Ready(&CCylinderModelType) < 0)
345        return;
346
347    Py_INCREF(&CCylinderModelType);
348    PyModule_AddObject(module, "CCylinderModel", (PyObject *)&CCylinderModelType);
349
350    d = PyModule_GetDict(module);
351    CCylinderModelError = PyErr_NewException("CCylinderModel.error", NULL, NULL);
352    PyDict_SetItemString(d, "CCylinderModelError", CCylinderModelError);
353}
354
Note: See TracBrowser for help on using the repository browser.