source: sasview/sansmodels/src/sans/models/c_models/CMultiShellModel.cpp @ 8e91f01

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 8e91f01 was 2cc633b, checked in by Jae Cho <jhjcho@…>, 15 years ago

FIXED A BUG

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