source: sasview/sansmodels/src/sans/models/c_models/CStakedDisksModel.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 5068697, checked in by Gervaise Alina <gervyh@…>, 15 years ago

add 1d models

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