source: sasview/sansmodels/src/sans/models/c_models/CLamellarModel.cpp @ 34c3020

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

add model1D

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