source: sasview/sansmodels/src/sans/models/c_extensions/CCoreShellCylinderModel.c @ 1aaa579

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 1aaa579 was a8d6888, checked in by Gervaise Alina <gervyh@…>, 15 years ago

change the orientation of vector for 2D models

  • Property mode set to 100644
File size: 11.3 KB
Line 
1/** CCoreShellCylinderModel
2 *
3 * C extension
4 *
5 * WARNING: THIS FILE WAS GENERATED BY WRAPPERGENERATOR.PY
6 *          DO NOT MODIFY THIS FILE, MODIFY core_shell_cylinder.h
7 *          AND RE-RUN THE GENERATOR SCRIPT
8 *
9 * @author   M.Doucet / UTK
10 */
11 
12#include <Python.h>
13#include "structmember.h"
14#include <stdio.h>
15#include <stdlib.h>
16#include <math.h>
17#include <time.h>
18
19#include "core_shell_cylinder.h"
20
21/// Error object for raised exceptions
22static PyObject * CCoreShellCylinderModelError = NULL;
23
24
25// Class definition
26typedef struct {
27    PyObject_HEAD
28    /// Parameters
29    PyObject * params;
30    /// Log for unit testing
31    PyObject * log;
32    /// Model parameters
33        CoreShellCylinderParameters model_pars;
34} CCoreShellCylinderModel;
35
36
37static void
38CCoreShellCylinderModel_dealloc(CCoreShellCylinderModel* self)
39{
40    self->ob_type->tp_free((PyObject*)self);
41   
42
43}
44
45static PyObject *
46CCoreShellCylinderModel_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
47{
48    CCoreShellCylinderModel *self;
49   
50    self = (CCoreShellCylinderModel *)type->tp_alloc(type, 0);
51   
52    return (PyObject *)self;
53}
54
55static int
56CCoreShellCylinderModel_init(CCoreShellCylinderModel *self, PyObject *args, PyObject *kwds)
57{
58    if (self != NULL) {
59       
60        // Create parameters
61        self->params = PyDict_New();
62       
63        // Initialize parameter dictionary
64        PyDict_SetItemString(self->params,"scale",Py_BuildValue("d",1.000000));
65        PyDict_SetItemString(self->params,"axis_theta",Py_BuildValue("d",1.570000));
66        PyDict_SetItemString(self->params,"solvent_sld",Py_BuildValue("d",0.000001));
67        PyDict_SetItemString(self->params,"thickness",Py_BuildValue("d",10.000000));
68        PyDict_SetItemString(self->params,"axis_phi",Py_BuildValue("d",0.000000));
69        PyDict_SetItemString(self->params,"length",Py_BuildValue("d",400.000000));
70        PyDict_SetItemString(self->params,"core_sld",Py_BuildValue("d",0.000001));
71        PyDict_SetItemString(self->params,"radius",Py_BuildValue("d",20.000000));
72        PyDict_SetItemString(self->params,"background",Py_BuildValue("d",0.000000));
73        PyDict_SetItemString(self->params,"shell_sld",Py_BuildValue("d",0.000004));
74
75         
76        // Create empty log
77        self->log = PyDict_New();
78       
79       
80
81    }
82    return 0;
83}
84
85static PyMemberDef CCoreShellCylinderModel_members[] = {
86    {"params", T_OBJECT, offsetof(CCoreShellCylinderModel, params), 0,
87     "Parameters"},
88    {"log", T_OBJECT, offsetof(CCoreShellCylinderModel, log), 0,
89     "Log"},
90    {NULL}  /* Sentinel */
91};
92
93/** Read double from PyObject
94    @param p PyObject
95    @return double
96*/
97double CCoreShellCylinderModel_readDouble(PyObject *p) {
98    if (PyFloat_Check(p)==1) {
99        return (double)(((PyFloatObject *)(p))->ob_fval);
100    } else if (PyInt_Check(p)==1) {
101        return (double)(((PyIntObject *)(p))->ob_ival);
102    } else if (PyLong_Check(p)==1) {
103        return (double)PyLong_AsLong(p);
104    } else {
105        return 0.0;
106    }
107}
108
109
110/**
111 * Function to call to evaluate model
112 * @param args: input q or [q,phi]
113 * @return: function value
114 */
115static PyObject * run(CCoreShellCylinderModel *self, PyObject *args) {
116        double q_value, phi_value;
117        PyObject* pars;
118        int npars;
119       
120        // Get parameters
121       
122        // Reader parameter dictionary
123    self->model_pars.scale = PyFloat_AsDouble( PyDict_GetItemString(self->params, "scale") );
124    self->model_pars.axis_theta = PyFloat_AsDouble( PyDict_GetItemString(self->params, "axis_theta") );
125    self->model_pars.solvent_sld = PyFloat_AsDouble( PyDict_GetItemString(self->params, "solvent_sld") );
126    self->model_pars.thickness = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thickness") );
127    self->model_pars.axis_phi = PyFloat_AsDouble( PyDict_GetItemString(self->params, "axis_phi") );
128    self->model_pars.length = PyFloat_AsDouble( PyDict_GetItemString(self->params, "length") );
129    self->model_pars.core_sld = PyFloat_AsDouble( PyDict_GetItemString(self->params, "core_sld") );
130    self->model_pars.radius = PyFloat_AsDouble( PyDict_GetItemString(self->params, "radius") );
131    self->model_pars.background = PyFloat_AsDouble( PyDict_GetItemString(self->params, "background") );
132    self->model_pars.shell_sld = PyFloat_AsDouble( PyDict_GetItemString(self->params, "shell_sld") );
133
134       
135        // Get input and determine whether we have to supply a 1D or 2D return value.
136        if ( !PyArg_ParseTuple(args,"O",&pars) ) {
137            PyErr_SetString(CCoreShellCylinderModelError, 
138                "CCoreShellCylinderModel.run expects a q value.");
139                return NULL;
140        }
141         
142        // Check params
143        if( PyList_Check(pars)==1) {
144               
145                // Length of list should be 2 for I(q,phi)
146            npars = PyList_GET_SIZE(pars); 
147            if(npars!=2) {
148                PyErr_SetString(CCoreShellCylinderModelError, 
149                        "CCoreShellCylinderModel.run expects a double or a list of dimension 2.");
150                return NULL;
151            }
152            // We have a vector q, get the q and phi values at which
153            // to evaluate I(q,phi)
154            q_value = CCoreShellCylinderModel_readDouble(PyList_GET_ITEM(pars,0));
155            phi_value = CCoreShellCylinderModel_readDouble(PyList_GET_ITEM(pars,1));
156            // Skip zero
157            if (q_value==0) {
158                return Py_BuildValue("d",0.0);
159            }
160                return Py_BuildValue("d",core_shell_cylinder_analytical_2D(&(self->model_pars),q_value,phi_value));
161
162        } else {
163
164                // We have a scalar q, we will evaluate I(q)
165                q_value = CCoreShellCylinderModel_readDouble(pars);             
166               
167                return Py_BuildValue("d",core_shell_cylinder_analytical_1D(&(self->model_pars),q_value));
168        }       
169}
170
171/**
172 * Function to call to evaluate model in cartesian coordinates
173 * @param args: input q or [qx, qy]]
174 * @return: function value
175 */
176static PyObject * runXY(CCoreShellCylinderModel *self, PyObject *args) {
177        double qx_value, qy_value;
178        PyObject* pars;
179        int npars;
180       
181        // Get parameters
182       
183        // Reader parameter dictionary
184    self->model_pars.scale = PyFloat_AsDouble( PyDict_GetItemString(self->params, "scale") );
185    self->model_pars.axis_theta = PyFloat_AsDouble( PyDict_GetItemString(self->params, "axis_theta") );
186    self->model_pars.solvent_sld = PyFloat_AsDouble( PyDict_GetItemString(self->params, "solvent_sld") );
187    self->model_pars.thickness = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thickness") );
188    self->model_pars.axis_phi = PyFloat_AsDouble( PyDict_GetItemString(self->params, "axis_phi") );
189    self->model_pars.length = PyFloat_AsDouble( PyDict_GetItemString(self->params, "length") );
190    self->model_pars.core_sld = PyFloat_AsDouble( PyDict_GetItemString(self->params, "core_sld") );
191    self->model_pars.radius = PyFloat_AsDouble( PyDict_GetItemString(self->params, "radius") );
192    self->model_pars.background = PyFloat_AsDouble( PyDict_GetItemString(self->params, "background") );
193    self->model_pars.shell_sld = PyFloat_AsDouble( PyDict_GetItemString(self->params, "shell_sld") );
194
195       
196        // Get input and determine whether we have to supply a 1D or 2D return value.
197        if ( !PyArg_ParseTuple(args,"O",&pars) ) {
198            PyErr_SetString(CCoreShellCylinderModelError, 
199                "CCoreShellCylinderModel.run expects a q value.");
200                return NULL;
201        }
202         
203        // Check params
204        if( PyList_Check(pars)==1) {
205               
206                // Length of list should be 2 for I(qx, qy))
207            npars = PyList_GET_SIZE(pars); 
208            if(npars!=2) {
209                PyErr_SetString(CCoreShellCylinderModelError, 
210                        "CCoreShellCylinderModel.run expects a double or a list of dimension 2.");
211                return NULL;
212            }
213            // We have a vector q, get the qx and qy values at which
214            // to evaluate I(qx,qy)
215            qx_value = CCoreShellCylinderModel_readDouble(PyList_GET_ITEM(pars,0));
216            qy_value = CCoreShellCylinderModel_readDouble(PyList_GET_ITEM(pars,1));
217                return Py_BuildValue("d",core_shell_cylinder_analytical_2DXY(&(self->model_pars),qx_value,qy_value));
218
219        } else {
220
221                // We have a scalar q, we will evaluate I(q)
222                qx_value = CCoreShellCylinderModel_readDouble(pars);           
223               
224                return Py_BuildValue("d",core_shell_cylinder_analytical_1D(&(self->model_pars),qx_value));
225        }       
226}
227
228static PyObject * reset(CCoreShellCylinderModel *self, PyObject *args) {
229   
230
231    return Py_BuildValue("d",0.0);
232}
233
234
235static PyMethodDef CCoreShellCylinderModel_methods[] = {
236    {"run",      (PyCFunction)run     , METH_VARARGS,
237      "Evaluate the model at a given Q or Q, phi"},
238    {"runXY",      (PyCFunction)runXY     , METH_VARARGS,
239      "Evaluate the model at a given Q or Qx, Qy"},
240    {"reset",    (PyCFunction)reset   , METH_VARARGS,
241      "Reset pair correlation"},
242    //{"numerical_1D",      (PyCFunction)numerical_1D     , METH_VARARGS,
243    //  "Evaluate the 1D model at a given Q"},
244   {NULL}
245};
246
247static PyTypeObject CCoreShellCylinderModelType = {
248    PyObject_HEAD_INIT(NULL)
249    0,                         /*ob_size*/
250    "CCoreShellCylinderModel",             /*tp_name*/
251    sizeof(CCoreShellCylinderModel),             /*tp_basicsize*/
252    0,                         /*tp_itemsize*/
253    (destructor)CCoreShellCylinderModel_dealloc, /*tp_dealloc*/
254    0,                         /*tp_print*/
255    0,                         /*tp_getattr*/
256    0,                         /*tp_setattr*/
257    0,                         /*tp_compare*/
258    0,                         /*tp_repr*/
259    0,                         /*tp_as_number*/
260    0,                         /*tp_as_sequence*/
261    0,                         /*tp_as_mapping*/
262    0,                         /*tp_hash */
263    0,                         /*tp_call*/
264    0,                         /*tp_str*/
265    0,                         /*tp_getattro*/
266    0,                         /*tp_setattro*/
267    0,                         /*tp_as_buffer*/
268    Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
269    "CCoreShellCylinderModel objects",           /* tp_doc */
270    0,                         /* tp_traverse */
271    0,                         /* tp_clear */
272    0,                         /* tp_richcompare */
273    0,                         /* tp_weaklistoffset */
274    0,                         /* tp_iter */
275    0,                         /* tp_iternext */
276    CCoreShellCylinderModel_methods,             /* tp_methods */
277    CCoreShellCylinderModel_members,             /* tp_members */
278    0,                         /* tp_getset */
279    0,                         /* tp_base */
280    0,                         /* tp_dict */
281    0,                         /* tp_descr_get */
282    0,                         /* tp_descr_set */
283    0,                         /* tp_dictoffset */
284    (initproc)CCoreShellCylinderModel_init,      /* tp_init */
285    0,                         /* tp_alloc */
286    CCoreShellCylinderModel_new,                 /* tp_new */
287};
288
289
290static PyMethodDef module_methods[] = {
291    {NULL} 
292};
293
294/**
295 * Function used to add the model class to a module
296 * @param module: module to add the class to
297 */ 
298void addCCoreShellCylinderModel(PyObject *module) {
299        PyObject *d;
300       
301    if (PyType_Ready(&CCoreShellCylinderModelType) < 0)
302        return;
303
304    Py_INCREF(&CCoreShellCylinderModelType);
305    PyModule_AddObject(module, "CCoreShellCylinderModel", (PyObject *)&CCoreShellCylinderModelType);
306   
307    d = PyModule_GetDict(module);
308    CCoreShellCylinderModelError = PyErr_NewException("CCoreShellCylinderModel.error", NULL, NULL);
309    PyDict_SetItemString(d, "CCoreShellCylinderModelError", CCoreShellCylinderModelError);
310}
311
Note: See TracBrowser for help on using the repository browser.