source: sasview/sansmodels/src/sans/models/c_models/CHollowCylinderModel.cpp @ eba9885

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 eba9885 was 793c988, checked in by Gervaise Alina <gervyh@…>, 15 years ago

add more models

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