source: sasview/sansmodels/src/sans/models/c_models/CEllipticalCylinderModel.cpp @ d8a2e31

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

Update of all C models to the new style of C++ models

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