source: sasview/sansmodels/src/sans/models/c_models/CCoreShellCylinderModel.cpp @ f99a6b1

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