source: sasview/sansmodels/src/sans/models/c_models/CCoreShellModel.cpp @ 98bfd10

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 98bfd10 was 0f5bc9f, checked in by Mathieu Doucet <doucetm@…>, 16 years ago

Update of all C models to the new style of C++ models

  • Property mode set to 100644
File size: 13.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/** CCoreShellModel
16 *
17 * C extension
18 *
19 * WARNING: THIS FILE WAS GENERATED BY WRAPPERGENERATOR.PY
20 *          DO NOT MODIFY THIS FILE, MODIFY core_shell.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 "core_shell.h"
33}
34
35#include "models.hh"
36#include "dispersion_visitor.hh"
37
38/// Error object for raised exceptions
39static PyObject * CCoreShellModelError = 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    CoreShellModel * model;
51    /// Log for unit testing
52    PyObject * log;
53} CCoreShellModel;
54
55
56static void
57CCoreShellModel_dealloc(CCoreShellModel* self)
58{
59    self->ob_type->tp_free((PyObject*)self);
60   
61
62}
63
64static PyObject *
65CCoreShellModel_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
66{
67    CCoreShellModel *self;
68   
69    self = (CCoreShellModel *)type->tp_alloc(type, 0);
70   
71    return (PyObject *)self;
72}
73
74static int
75CCoreShellModel_init(CCoreShellModel *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 CoreShellModel();
83       
84        // Initialize parameter dictionary
85        PyDict_SetItemString(self->params,"core_sld",Py_BuildValue("d",0.000001));
86        PyDict_SetItemString(self->params,"thickness",Py_BuildValue("d",10.000000));
87        PyDict_SetItemString(self->params,"solvent_sld",Py_BuildValue("d",0.000003));
88        PyDict_SetItemString(self->params,"scale",Py_BuildValue("d",1.000000));
89        PyDict_SetItemString(self->params,"radius",Py_BuildValue("d",60.000000));
90        PyDict_SetItemString(self->params,"background",Py_BuildValue("d",0.000000));
91        PyDict_SetItemString(self->params,"shell_sld",Py_BuildValue("d",0.000002));
92        // Initialize dispersion / averaging parameter dict
93        DispersionVisitor* visitor = new DispersionVisitor();
94        PyObject * disp_dict;
95        disp_dict = PyDict_New();
96        self->model->radius.dispersion->accept_as_source(visitor, self->model->radius.dispersion, disp_dict);
97        PyDict_SetItemString(self->dispersion, "radius", disp_dict);
98        disp_dict = PyDict_New();
99        self->model->thickness.dispersion->accept_as_source(visitor, self->model->thickness.dispersion, disp_dict);
100        PyDict_SetItemString(self->dispersion, "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 CCoreShellModel_members[] = {
114    {"params", T_OBJECT, offsetof(CCoreShellModel, params), 0,
115     "Parameters"},
116        {"dispersion", T_OBJECT, offsetof(CCoreShellModel, dispersion), 0,
117          "Dispersion parameters"},     
118    {"log", T_OBJECT, offsetof(CCoreShellModel, log), 0,
119     "Log"},
120    {NULL}  /* Sentinel */
121};
122
123/** Read double from PyObject
124    @param p PyObject
125    @return double
126*/
127double CCoreShellModel_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(CCoreShellModel *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->core_sld = PyFloat_AsDouble( PyDict_GetItemString(self->params, "core_sld") );
154    self->model->thickness = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thickness") );
155    self->model->solvent_sld = PyFloat_AsDouble( PyDict_GetItemString(self->params, "solvent_sld") );
156    self->model->scale = PyFloat_AsDouble( PyDict_GetItemString(self->params, "scale") );
157    self->model->radius = PyFloat_AsDouble( PyDict_GetItemString(self->params, "radius") );
158    self->model->background = PyFloat_AsDouble( PyDict_GetItemString(self->params, "background") );
159    self->model->shell_sld = PyFloat_AsDouble( PyDict_GetItemString(self->params, "shell_sld") );
160    // Read in dispersion parameters
161    PyObject* disp_dict;
162    DispersionVisitor* visitor = new DispersionVisitor();
163    disp_dict = PyDict_GetItemString(self->dispersion, "radius");
164    self->model->radius.dispersion->accept_as_destination(visitor, self->model->radius.dispersion, disp_dict);
165    disp_dict = PyDict_GetItemString(self->dispersion, "thickness");
166    self->model->thickness.dispersion->accept_as_destination(visitor, self->model->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(CCoreShellModelError, 
172                "CCoreShellModel.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(CCoreShellModelError, 
183                        "CCoreShellModel.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 = CCoreShellModel_readDouble(PyList_GET_ITEM(pars,0));
189            phi_value = CCoreShellModel_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 = CCoreShellModel_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(CCoreShellModel *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->core_sld = PyFloat_AsDouble( PyDict_GetItemString(self->params, "core_sld") );
219    self->model->thickness = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thickness") );
220    self->model->solvent_sld = PyFloat_AsDouble( PyDict_GetItemString(self->params, "solvent_sld") );
221    self->model->scale = PyFloat_AsDouble( PyDict_GetItemString(self->params, "scale") );
222    self->model->radius = PyFloat_AsDouble( PyDict_GetItemString(self->params, "radius") );
223    self->model->background = PyFloat_AsDouble( PyDict_GetItemString(self->params, "background") );
224    self->model->shell_sld = PyFloat_AsDouble( PyDict_GetItemString(self->params, "shell_sld") );
225    // Read in dispersion parameters
226    PyObject* disp_dict;
227    DispersionVisitor* visitor = new DispersionVisitor();
228    disp_dict = PyDict_GetItemString(self->dispersion, "radius");
229    self->model->radius.dispersion->accept_as_destination(visitor, self->model->radius.dispersion, disp_dict);
230    disp_dict = PyDict_GetItemString(self->dispersion, "thickness");
231    self->model->thickness.dispersion->accept_as_destination(visitor, self->model->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(CCoreShellModelError, 
237                "CCoreShellModel.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(CCoreShellModelError, 
248                        "CCoreShellModel.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 = CCoreShellModel_readDouble(PyList_GET_ITEM(pars,0));
254            qy_value = CCoreShellModel_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 = CCoreShellModel_readDouble(pars);           
261               
262                return Py_BuildValue("d",(*(self->model))(qx_value));
263        }       
264}
265
266static PyObject * reset(CCoreShellModel *self, PyObject *args) {
267   
268
269    return Py_BuildValue("d",0.0);
270}
271
272static PyObject * set_dispersion(CCoreShellModel *self, PyObject *args) {
273        PyObject * disp;
274        const char * par_name;
275
276        if ( !PyArg_ParseTuple(args,"sO", &par_name, &disp) ) {
277            PyErr_SetString(CCoreShellModelError,
278                "CCoreShellModel.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, "radius")) {
288        self->model->radius.dispersion = dispersion;
289    } else    if (!strcmp(par_name, "thickness")) {
290        self->model->thickness.dispersion = dispersion;
291    } else {
292            PyErr_SetString(CCoreShellModelError,
293                "CCoreShellModel.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 CCoreShellModel_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 CCoreShellModelType = {
318    PyObject_HEAD_INIT(NULL)
319    0,                         /*ob_size*/
320    "CCoreShellModel",             /*tp_name*/
321    sizeof(CCoreShellModel),             /*tp_basicsize*/
322    0,                         /*tp_itemsize*/
323    (destructor)CCoreShellModel_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    "CCoreShellModel 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    CCoreShellModel_methods,             /* tp_methods */
347    CCoreShellModel_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)CCoreShellModel_init,      /* tp_init */
355    0,                         /* tp_alloc */
356    CCoreShellModel_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 addCCoreShellModel(PyObject *module) {
369        PyObject *d;
370       
371    if (PyType_Ready(&CCoreShellModelType) < 0)
372        return;
373
374    Py_INCREF(&CCoreShellModelType);
375    PyModule_AddObject(module, "CCoreShellModel", (PyObject *)&CCoreShellModelType);
376   
377    d = PyModule_GetDict(module);
378    CCoreShellModelError = PyErr_NewException("CCoreShellModel.error", NULL, NULL);
379    PyDict_SetItemString(d, "CCoreShellModelError", CCoreShellModelError);
380}
381
Note: See TracBrowser for help on using the repository browser.