source: sasview/sansmodels/src/sans/models/c_extensions/CLamellarModel.c @ 0164899a

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 0164899a was e0a8a3c, checked in by Gervaise Alina <gervyh@…>, 15 years ago

change the orientation of models

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