source: sasview/sansmodels/src/sans/models/c_models/CLamellarModel.cpp @ 42f193a

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 42f193a was 42f193a, checked in by Jae Cho <jhjcho@…>, 15 years ago

some corrections on dips-parameters and adding 2D cal

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