source: sasview/sansmodels/src/sans/models/c_models/CParallelepipedModel.cpp @ 8a48713

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

implement 1D for parallelepiped model

  • Property mode set to 100644
File size: 16.3 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/** CParallelepipedModel
16 *
17 * C extension
18 *
19 * WARNING: THIS FILE WAS GENERATED BY WRAPPERGENERATOR.PY
20 *          DO NOT MODIFY THIS FILE, MODIFY parallelepiped.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 "parallelepiped.h"
33}
34
35#include "models.hh"
36#include "dispersion_visitor.hh"
37
38/// Error object for raised exceptions
39static PyObject * CParallelepipedModelError = 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    ParallelepipedModel * model;
51    /// Log for unit testing
52    PyObject * log;
53} CParallelepipedModel;
54
55
56static void
57CParallelepipedModel_dealloc(CParallelepipedModel* self)
58{
59    self->ob_type->tp_free((PyObject*)self);
60   
61
62}
63
64static PyObject *
65CParallelepipedModel_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
66{
67    CParallelepipedModel *self;
68   
69    self = (CParallelepipedModel *)type->tp_alloc(type, 0);
70   
71    return (PyObject *)self;
72}
73
74static int
75CParallelepipedModel_init(CParallelepipedModel *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 ParallelepipedModel();
83       
84        // Initialize parameter dictionary
85        PyDict_SetItemString(self->params,"scale",Py_BuildValue("d",1.000000));
86        PyDict_SetItemString(self->params,"longer_edgeB",Py_BuildValue("d",75.000000));
87        PyDict_SetItemString(self->params,"longuest_edgeC",Py_BuildValue("d",400.000000));
88        PyDict_SetItemString(self->params,"parallel_phi",Py_BuildValue("d",1.000000));
89        PyDict_SetItemString(self->params,"parallel_theta",Py_BuildValue("d",1.000000));
90        PyDict_SetItemString(self->params,"background",Py_BuildValue("d",0.000000));
91        PyDict_SetItemString(self->params,"short_edgeA",Py_BuildValue("d",35.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->short_edgeA.dispersion->accept_as_source(visitor, self->model->short_edgeA.dispersion, disp_dict);
98        PyDict_SetItemString(self->dispersion, "short_edgeA", disp_dict);
99        disp_dict = PyDict_New();
100        self->model->longer_edgeB.dispersion->accept_as_source(visitor, self->model->longer_edgeB.dispersion, disp_dict);
101        PyDict_SetItemString(self->dispersion, "longer_edgeB", disp_dict);
102        disp_dict = PyDict_New();
103        self->model->longuest_edgeC.dispersion->accept_as_source(visitor, self->model->longuest_edgeC.dispersion, disp_dict);
104        PyDict_SetItemString(self->dispersion, "longuest_edgeC", disp_dict);
105        disp_dict = PyDict_New();
106        self->model->parallel_phi.dispersion->accept_as_source(visitor, self->model->parallel_phi.dispersion, disp_dict);
107        PyDict_SetItemString(self->dispersion, "parallel_phi", disp_dict);
108        disp_dict = PyDict_New();
109        self->model->parallel_theta.dispersion->accept_as_source(visitor, self->model->parallel_theta.dispersion, disp_dict);
110        PyDict_SetItemString(self->dispersion, "parallel_theta", 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 CParallelepipedModel_members[] = {
124    {"params", T_OBJECT, offsetof(CParallelepipedModel, params), 0,
125     "Parameters"},
126        {"dispersion", T_OBJECT, offsetof(CParallelepipedModel, dispersion), 0,
127          "Dispersion parameters"},     
128    {"log", T_OBJECT, offsetof(CParallelepipedModel, log), 0,
129     "Log"},
130    {NULL}  /* Sentinel */
131};
132
133/** Read double from PyObject
134    @param p PyObject
135    @return double
136*/
137double CParallelepipedModel_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(CParallelepipedModel *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->longer_edgeB = PyFloat_AsDouble( PyDict_GetItemString(self->params, "longer_edgeB") );
165    self->model->longuest_edgeC = PyFloat_AsDouble( PyDict_GetItemString(self->params, "longuest_edgeC") );
166    self->model->parallel_phi = PyFloat_AsDouble( PyDict_GetItemString(self->params, "parallel_phi") );
167    self->model->parallel_theta = PyFloat_AsDouble( PyDict_GetItemString(self->params, "parallel_theta") );
168    self->model->background = PyFloat_AsDouble( PyDict_GetItemString(self->params, "background") );
169    self->model->short_edgeA = PyFloat_AsDouble( PyDict_GetItemString(self->params, "short_edgeA") );
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, "short_edgeA");
175    self->model->short_edgeA.dispersion->accept_as_destination(visitor, self->model->short_edgeA.dispersion, disp_dict);
176    disp_dict = PyDict_GetItemString(self->dispersion, "longer_edgeB");
177    self->model->longer_edgeB.dispersion->accept_as_destination(visitor, self->model->longer_edgeB.dispersion, disp_dict);
178    disp_dict = PyDict_GetItemString(self->dispersion, "longuest_edgeC");
179    self->model->longuest_edgeC.dispersion->accept_as_destination(visitor, self->model->longuest_edgeC.dispersion, disp_dict);
180    disp_dict = PyDict_GetItemString(self->dispersion, "parallel_phi");
181    self->model->parallel_phi.dispersion->accept_as_destination(visitor, self->model->parallel_phi.dispersion, disp_dict);
182    disp_dict = PyDict_GetItemString(self->dispersion, "parallel_theta");
183    self->model->parallel_theta.dispersion->accept_as_destination(visitor, self->model->parallel_theta.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(CParallelepipedModelError, 
189                "CParallelepipedModel.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(CParallelepipedModelError, 
200                        "CParallelepipedModel.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 = CParallelepipedModel_readDouble(PyList_GET_ITEM(pars,0));
206            phi_value = CParallelepipedModel_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 = CParallelepipedModel_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(CParallelepipedModel *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->longer_edgeB = PyFloat_AsDouble( PyDict_GetItemString(self->params, "longer_edgeB") );
237    self->model->longuest_edgeC = PyFloat_AsDouble( PyDict_GetItemString(self->params, "longuest_edgeC") );
238    self->model->parallel_phi = PyFloat_AsDouble( PyDict_GetItemString(self->params, "parallel_phi") );
239    self->model->parallel_theta = PyFloat_AsDouble( PyDict_GetItemString(self->params, "parallel_theta") );
240    self->model->background = PyFloat_AsDouble( PyDict_GetItemString(self->params, "background") );
241    self->model->short_edgeA = PyFloat_AsDouble( PyDict_GetItemString(self->params, "short_edgeA") );
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, "short_edgeA");
247    self->model->short_edgeA.dispersion->accept_as_destination(visitor, self->model->short_edgeA.dispersion, disp_dict);
248    disp_dict = PyDict_GetItemString(self->dispersion, "longer_edgeB");
249    self->model->longer_edgeB.dispersion->accept_as_destination(visitor, self->model->longer_edgeB.dispersion, disp_dict);
250    disp_dict = PyDict_GetItemString(self->dispersion, "longuest_edgeC");
251    self->model->longuest_edgeC.dispersion->accept_as_destination(visitor, self->model->longuest_edgeC.dispersion, disp_dict);
252    disp_dict = PyDict_GetItemString(self->dispersion, "parallel_phi");
253    self->model->parallel_phi.dispersion->accept_as_destination(visitor, self->model->parallel_phi.dispersion, disp_dict);
254    disp_dict = PyDict_GetItemString(self->dispersion, "parallel_theta");
255    self->model->parallel_theta.dispersion->accept_as_destination(visitor, self->model->parallel_theta.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(CParallelepipedModelError, 
261                "CParallelepipedModel.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(CParallelepipedModelError, 
272                        "CParallelepipedModel.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 = CParallelepipedModel_readDouble(PyList_GET_ITEM(pars,0));
278            qy_value = CParallelepipedModel_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 = CParallelepipedModel_readDouble(pars);               
285               
286                return Py_BuildValue("d",(*(self->model))(qx_value));
287        }       
288}
289
290static PyObject * reset(CParallelepipedModel *self, PyObject *args) {
291   
292
293    return Py_BuildValue("d",0.0);
294}
295
296static PyObject * set_dispersion(CParallelepipedModel *self, PyObject *args) {
297        PyObject * disp;
298        const char * par_name;
299
300        if ( !PyArg_ParseTuple(args,"sO", &par_name, &disp) ) {
301            PyErr_SetString(CParallelepipedModelError,
302                "CParallelepipedModel.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, "short_edgeA")) {
312        self->model->short_edgeA.dispersion = dispersion;
313    } else    if (!strcmp(par_name, "longer_edgeB")) {
314        self->model->longer_edgeB.dispersion = dispersion;
315    } else    if (!strcmp(par_name, "longuest_edgeC")) {
316        self->model->longuest_edgeC.dispersion = dispersion;
317    } else    if (!strcmp(par_name, "parallel_phi")) {
318        self->model->parallel_phi.dispersion = dispersion;
319    } else    if (!strcmp(par_name, "parallel_theta")) {
320        self->model->parallel_theta.dispersion = dispersion;
321    } else {
322            PyErr_SetString(CParallelepipedModelError,
323                "CParallelepipedModel.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 CParallelepipedModel_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 CParallelepipedModelType = {
348    PyObject_HEAD_INIT(NULL)
349    0,                         /*ob_size*/
350    "CParallelepipedModel",             /*tp_name*/
351    sizeof(CParallelepipedModel),             /*tp_basicsize*/
352    0,                         /*tp_itemsize*/
353    (destructor)CParallelepipedModel_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    "CParallelepipedModel 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    CParallelepipedModel_methods,             /* tp_methods */
377    CParallelepipedModel_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)CParallelepipedModel_init,      /* tp_init */
385    0,                         /* tp_alloc */
386    CParallelepipedModel_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 addCParallelepipedModel(PyObject *module) {
399        PyObject *d;
400       
401    if (PyType_Ready(&CParallelepipedModelType) < 0)
402        return;
403
404    Py_INCREF(&CParallelepipedModelType);
405    PyModule_AddObject(module, "CParallelepipedModel", (PyObject *)&CParallelepipedModelType);
406   
407    d = PyModule_GetDict(module);
408    CParallelepipedModelError = PyErr_NewException("CParallelepipedModel.error", NULL, NULL);
409    PyDict_SetItemString(d, "CParallelepipedModelError", CParallelepipedModelError);
410}
411
Note: See TracBrowser for help on using the repository browser.