source: sasview/sansmodels/src/sans/models/c_models/CLamellarFFHGModel.cpp @ eba9885

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

add model1D

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