source: sasview/sansmodels/src/sans/models/c_models/CLamellarPSHGModel.cpp @ e4193d9

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

add more models

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