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

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 27953d1 was 96b59384, checked in by Jae Cho <jhjcho@…>, 15 years ago

added 2D and model descpt.

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