source: sasview/src/sans/models/c_extension/python_wrapper/generated/CDiamEllipFunc.cpp @ 400155b

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 400155b was 400155b, checked in by gonzalezm, 9 years ago

Implementing request from ticket 261 - default number of bins in Annulus [Phi View] is now 36 and the first bin is now centered at 0 degrees

  • Property mode set to 100644
File size: 19.5 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/** CDiamEllipFunc
16 *
17 * C extension
18 *
19 * WARNING: THIS FILE WAS GENERATED BY WRAPPERGENERATOR.PY
20 *          DO NOT MODIFY THIS FILE, MODIFY src\sans\models\include\DiamEllip.h
21 *          AND RE-RUN THE GENERATOR SCRIPT
22 *
23 */
24#define NO_IMPORT_ARRAY
25#define PY_ARRAY_UNIQUE_SYMBOL PyArray_API_sans
26 
27extern "C" {
28#include <Python.h>
29#include <arrayobject.h>
30#include "structmember.h"
31#include <stdio.h>
32#include <stdlib.h>
33#include <math.h>
34#include <time.h>
35
36}
37
38#include "DiamEllip.h"
39#include "dispersion_visitor.hh"
40
41/// Error object for raised exceptions
42static PyObject * CDiamEllipFuncError = NULL;
43
44
45// Class definition
46typedef struct {
47    PyObject_HEAD
48    /// Parameters
49    PyObject * params;
50    /// Dispersion parameters
51    PyObject * dispersion;
52    /// Underlying model object
53    DiamEllipFunc * model;
54    /// Log for unit testing
55    PyObject * log;
56} CDiamEllipFunc;
57
58
59static void
60CDiamEllipFunc_dealloc(CDiamEllipFunc* self)
61{
62    Py_DECREF(self->params);
63    Py_DECREF(self->dispersion);
64    Py_DECREF(self->log);
65    delete self->model;
66    self->ob_type->tp_free((PyObject*)self);
67   
68
69}
70
71static PyObject *
72CDiamEllipFunc_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
73{
74    CDiamEllipFunc *self;
75   
76    self = (CDiamEllipFunc *)type->tp_alloc(type, 0);
77   
78    return (PyObject *)self;
79}
80
81static int
82CDiamEllipFunc_init(CDiamEllipFunc *self, PyObject *args, PyObject *kwds)
83{
84    if (self != NULL) {
85       
86        // Create parameters
87        self->params = PyDict_New();
88        self->dispersion = PyDict_New();
89
90        self->model = new DiamEllipFunc();
91
92        // Initialize parameter dictionary
93        PyDict_SetItemString(self->params,"radius_b",Py_BuildValue("d",400.000000000000));
94        PyDict_SetItemString(self->params,"radius_a",Py_BuildValue("d",20.000000000000));
95        // Initialize dispersion / averaging parameter dict
96        DispersionVisitor* visitor = new DispersionVisitor();
97        PyObject * disp_dict;
98        disp_dict = PyDict_New();
99        self->model->radius_a.dispersion->accept_as_source(visitor, self->model->radius_a.dispersion, disp_dict);
100        PyDict_SetItemString(self->dispersion, "radius_a", disp_dict);
101        disp_dict = PyDict_New();
102        self->model->radius_b.dispersion->accept_as_source(visitor, self->model->radius_b.dispersion, disp_dict);
103        PyDict_SetItemString(self->dispersion, "radius_b", disp_dict);
104
105
106         
107        // Create empty log
108        self->log = PyDict_New();
109       
110       
111
112    }
113    return 0;
114}
115
116static char name_params[] = "params";
117static char def_params[] = "Parameters";
118static char name_dispersion[] = "dispersion";
119static char def_dispersion[] = "Dispersion parameters";
120static char name_log[] = "log";
121static char def_log[] = "Log";
122
123static PyMemberDef CDiamEllipFunc_members[] = {
124    {name_params, T_OBJECT, offsetof(CDiamEllipFunc, params), 0, def_params},
125        {name_dispersion, T_OBJECT, offsetof(CDiamEllipFunc, dispersion), 0, def_dispersion},     
126    {name_log, T_OBJECT, offsetof(CDiamEllipFunc, log), 0, def_log},
127    {NULL}  /* Sentinel */
128};
129
130/** Read double from PyObject
131    @param p PyObject
132    @return double
133*/
134double CDiamEllipFunc_readDouble(PyObject *p) {
135    if (PyFloat_Check(p)==1) {
136        return (double)(((PyFloatObject *)(p))->ob_fval);
137    } else if (PyInt_Check(p)==1) {
138        return (double)(((PyIntObject *)(p))->ob_ival);
139    } else if (PyLong_Check(p)==1) {
140        return (double)PyLong_AsLong(p);
141    } else {
142        return 0.0;
143    }
144}
145/**
146 * Function to call to evaluate model
147 * @param args: input numpy array q[]
148 * @return: numpy array object
149 */
150 
151static PyObject *evaluateOneDim(DiamEllipFunc* model, PyArrayObject *q){
152    PyArrayObject *result;
153   
154    // Check validity of array q , q must be of dimension 1, an array of double
155    if (q->nd != 1 || q->descr->type_num != PyArray_DOUBLE)
156    {
157        //const char * message= "Invalid array: q->nd=%d,type_num=%d\n",q->nd,q->descr->type_num;
158        //PyErr_SetString(PyExc_ValueError , message);
159        return NULL;
160    }
161    result = (PyArrayObject *)PyArray_FromDims(q->nd, (int *)(q->dimensions), PyArray_DOUBLE);
162        if (result == NULL) {
163        const char * message= "Could not create result ";
164        PyErr_SetString(PyExc_RuntimeError , message);
165                return NULL;
166        }
167#pragma omp parallel for
168         for (int i = 0; i < q->dimensions[0]; i++){
169      double q_value  = *(double *)(q->data + i*q->strides[0]);
170      double *result_value = (double *)(result->data + i*result->strides[0]);
171      *result_value =(*model)(q_value);
172        }
173    return PyArray_Return(result); 
174 }
175
176 /**
177 * Function to call to evaluate model
178 * @param args: input numpy array  [x[],y[]]
179 * @return: numpy array object
180 */
181 static PyObject * evaluateTwoDimXY( DiamEllipFunc* model, 
182                              PyArrayObject *x, PyArrayObject *y)
183 {
184    PyArrayObject *result;
185    int x_len, y_len, dims[1];
186    //check validity of input vectors
187    if (x->nd != 1 || x->descr->type_num != PyArray_DOUBLE
188        || y->nd != 1 || y->descr->type_num != PyArray_DOUBLE
189        || y->dimensions[0] != x->dimensions[0]){
190        const char * message= "evaluateTwoDimXY  expect 2 numpy arrays";
191        PyErr_SetString(PyExc_ValueError , message); 
192        return NULL;
193    }
194   
195        if (PyArray_Check(x) && PyArray_Check(y)) {
196               
197            x_len = dims[0]= x->dimensions[0];
198        y_len = dims[0]= y->dimensions[0];
199           
200            // Make a new double matrix of same dims
201        result=(PyArrayObject *) PyArray_FromDims(1,dims,NPY_DOUBLE);
202        if (result == NULL){
203            const char * message= "Could not create result ";
204        PyErr_SetString(PyExc_RuntimeError , message);
205            return NULL;
206            }
207       
208        /* Do the calculation. */
209#pragma omp parallel for
210        for (int i=0; i< x_len; i++) {
211            double x_value = *(double *)(x->data + i*x->strides[0]);
212                    double y_value = *(double *)(y->data + i*y->strides[0]);
213                        double *result_value = (double *)(result->data +
214                              i*result->strides[0]);
215                        *result_value = (*model)(x_value, y_value);
216        }           
217        return PyArray_Return(result); 
218       
219        }else{
220                    PyErr_SetString(CDiamEllipFuncError, 
221                   "CDiamEllipFunc.evaluateTwoDimXY couldn't run.");
222                return NULL;
223                }       
224}
225/**
226 *  evalDistribution function evaluate a model function with input vector
227 *  @param args: input q as vector or [qx, qy] where qx, qy are vectors
228 *
229 */ 
230static PyObject * evalDistribution(CDiamEllipFunc *self, PyObject *args){
231        PyObject *qx, *qy;
232        PyArrayObject * pars;
233        int npars ,mpars;
234       
235        // Get parameters
236       
237            // Reader parameter dictionary
238    self->model->radius_b = PyFloat_AsDouble( PyDict_GetItemString(self->params, "radius_b") );
239    self->model->radius_a = PyFloat_AsDouble( PyDict_GetItemString(self->params, "radius_a") );
240    // Read in dispersion parameters
241    PyObject* disp_dict;
242    DispersionVisitor* visitor = new DispersionVisitor();
243    disp_dict = PyDict_GetItemString(self->dispersion, "radius_a");
244    self->model->radius_a.dispersion->accept_as_destination(visitor, self->model->radius_a.dispersion, disp_dict);
245    disp_dict = PyDict_GetItemString(self->dispersion, "radius_b");
246    self->model->radius_b.dispersion->accept_as_destination(visitor, self->model->radius_b.dispersion, disp_dict);
247
248       
249        // Get input and determine whether we have to supply a 1D or 2D return value.
250        if ( !PyArg_ParseTuple(args,"O",&pars) ) {
251            PyErr_SetString(CDiamEllipFuncError, 
252                "CDiamEllipFunc.evalDistribution expects a q value.");
253                return NULL;
254        }
255    // Check params
256       
257    if(PyArray_Check(pars)==1) {
258               
259            // Length of list should 1 or 2
260            npars = pars->nd; 
261            if(npars==1) {
262                // input is a numpy array
263                if (PyArray_Check(pars)) {
264                        return evaluateOneDim(self->model, (PyArrayObject*)pars); 
265                    }
266                }else{
267                    PyErr_SetString(CDiamEllipFuncError, 
268                   "CDiamEllipFunc.evalDistribution expect numpy array of one dimension.");
269                return NULL;
270                }
271    }else if( PyList_Check(pars)==1) {
272        // Length of list should be 2 for I(qx,qy)
273            mpars = PyList_GET_SIZE(pars); 
274            if(mpars!=2) {
275                PyErr_SetString(CDiamEllipFuncError, 
276                        "CDiamEllipFunc.evalDistribution expects a list of dimension 2.");
277                return NULL;
278            }
279             qx = PyList_GET_ITEM(pars,0);
280             qy = PyList_GET_ITEM(pars,1);
281             if (PyArray_Check(qx) && PyArray_Check(qy)) {
282                 return evaluateTwoDimXY(self->model, (PyArrayObject*)qx,
283                           (PyArrayObject*)qy);
284                 }else{
285                    PyErr_SetString(CDiamEllipFuncError, 
286                   "CDiamEllipFunc.evalDistribution expect 2 numpy arrays in list.");
287                return NULL;
288             }
289        }
290        PyErr_SetString(CDiamEllipFuncError, 
291                   "CDiamEllipFunc.evalDistribution couln't be run.");
292        return NULL;
293       
294}
295
296/**
297 * Function to call to evaluate model
298 * @param args: input q or [q,phi]
299 * @return: function value
300 */
301static PyObject * run(CDiamEllipFunc *self, PyObject *args) {
302        double q_value, phi_value;
303        PyObject* pars;
304        int npars;
305       
306        // Get parameters
307       
308            // Reader parameter dictionary
309    self->model->radius_b = PyFloat_AsDouble( PyDict_GetItemString(self->params, "radius_b") );
310    self->model->radius_a = PyFloat_AsDouble( PyDict_GetItemString(self->params, "radius_a") );
311    // Read in dispersion parameters
312    PyObject* disp_dict;
313    DispersionVisitor* visitor = new DispersionVisitor();
314    disp_dict = PyDict_GetItemString(self->dispersion, "radius_a");
315    self->model->radius_a.dispersion->accept_as_destination(visitor, self->model->radius_a.dispersion, disp_dict);
316    disp_dict = PyDict_GetItemString(self->dispersion, "radius_b");
317    self->model->radius_b.dispersion->accept_as_destination(visitor, self->model->radius_b.dispersion, disp_dict);
318
319       
320        // Get input and determine whether we have to supply a 1D or 2D return value.
321        if ( !PyArg_ParseTuple(args,"O",&pars) ) {
322            PyErr_SetString(CDiamEllipFuncError, 
323                "CDiamEllipFunc.run expects a q value.");
324                return NULL;
325        }
326         
327        // Check params
328        if( PyList_Check(pars)==1) {
329               
330                // Length of list should be 2 for I(q,phi)
331            npars = PyList_GET_SIZE(pars); 
332            if(npars!=2) {
333                PyErr_SetString(CDiamEllipFuncError, 
334                        "CDiamEllipFunc.run expects a double or a list of dimension 2.");
335                return NULL;
336            }
337            // We have a vector q, get the q and phi values at which
338            // to evaluate I(q,phi)
339            q_value = CDiamEllipFunc_readDouble(PyList_GET_ITEM(pars,0));
340            phi_value = CDiamEllipFunc_readDouble(PyList_GET_ITEM(pars,1));
341            // Skip zero
342            if (q_value==0) {
343                return Py_BuildValue("d",0.0);
344            }
345                return Py_BuildValue("d",(*(self->model)).evaluate_rphi(q_value,phi_value));
346
347        } else {
348
349                // We have a scalar q, we will evaluate I(q)
350                q_value = CDiamEllipFunc_readDouble(pars);             
351               
352                return Py_BuildValue("d",(*(self->model))(q_value));
353        }       
354}
355/**
356 * Function to call to calculate_ER
357 * @return: effective radius value
358 */
359static PyObject * calculate_ER(CDiamEllipFunc *self) {
360
361        // Get parameters
362       
363            // Reader parameter dictionary
364    self->model->radius_b = PyFloat_AsDouble( PyDict_GetItemString(self->params, "radius_b") );
365    self->model->radius_a = PyFloat_AsDouble( PyDict_GetItemString(self->params, "radius_a") );
366    // Read in dispersion parameters
367    PyObject* disp_dict;
368    DispersionVisitor* visitor = new DispersionVisitor();
369    disp_dict = PyDict_GetItemString(self->dispersion, "radius_a");
370    self->model->radius_a.dispersion->accept_as_destination(visitor, self->model->radius_a.dispersion, disp_dict);
371    disp_dict = PyDict_GetItemString(self->dispersion, "radius_b");
372    self->model->radius_b.dispersion->accept_as_destination(visitor, self->model->radius_b.dispersion, disp_dict);
373
374               
375        return Py_BuildValue("d",(*(self->model)).calculate_ER());
376
377}
378/**
379 * Function to call to cal the ratio shell volume/ total volume
380 * @return: the ratio shell volume/ total volume
381 */
382static PyObject * calculate_VR(CDiamEllipFunc *self) {
383
384        // Get parameters
385       
386            // Reader parameter dictionary
387    self->model->radius_b = PyFloat_AsDouble( PyDict_GetItemString(self->params, "radius_b") );
388    self->model->radius_a = PyFloat_AsDouble( PyDict_GetItemString(self->params, "radius_a") );
389    // Read in dispersion parameters
390    PyObject* disp_dict;
391    DispersionVisitor* visitor = new DispersionVisitor();
392    disp_dict = PyDict_GetItemString(self->dispersion, "radius_a");
393    self->model->radius_a.dispersion->accept_as_destination(visitor, self->model->radius_a.dispersion, disp_dict);
394    disp_dict = PyDict_GetItemString(self->dispersion, "radius_b");
395    self->model->radius_b.dispersion->accept_as_destination(visitor, self->model->radius_b.dispersion, disp_dict);
396
397               
398        return Py_BuildValue("d",(*(self->model)).calculate_VR());
399
400}
401/**
402 * Function to call to evaluate model in cartesian coordinates
403 * @param args: input q or [qx, qy]]
404 * @return: function value
405 */
406static PyObject * runXY(CDiamEllipFunc *self, PyObject *args) {
407        double qx_value, qy_value;
408        PyObject* pars;
409        int npars;
410       
411        // Get parameters
412       
413            // Reader parameter dictionary
414    self->model->radius_b = PyFloat_AsDouble( PyDict_GetItemString(self->params, "radius_b") );
415    self->model->radius_a = PyFloat_AsDouble( PyDict_GetItemString(self->params, "radius_a") );
416    // Read in dispersion parameters
417    PyObject* disp_dict;
418    DispersionVisitor* visitor = new DispersionVisitor();
419    disp_dict = PyDict_GetItemString(self->dispersion, "radius_a");
420    self->model->radius_a.dispersion->accept_as_destination(visitor, self->model->radius_a.dispersion, disp_dict);
421    disp_dict = PyDict_GetItemString(self->dispersion, "radius_b");
422    self->model->radius_b.dispersion->accept_as_destination(visitor, self->model->radius_b.dispersion, disp_dict);
423
424       
425        // Get input and determine whether we have to supply a 1D or 2D return value.
426        if ( !PyArg_ParseTuple(args,"O",&pars) ) {
427            PyErr_SetString(CDiamEllipFuncError, 
428                "CDiamEllipFunc.run expects a q value.");
429                return NULL;
430        }
431         
432        // Check params
433        if( PyList_Check(pars)==1) {
434               
435                // Length of list should be 2 for I(qx, qy))
436            npars = PyList_GET_SIZE(pars); 
437            if(npars!=2) {
438                PyErr_SetString(CDiamEllipFuncError, 
439                        "CDiamEllipFunc.run expects a double or a list of dimension 2.");
440                return NULL;
441            }
442            // We have a vector q, get the qx and qy values at which
443            // to evaluate I(qx,qy)
444            qx_value = CDiamEllipFunc_readDouble(PyList_GET_ITEM(pars,0));
445            qy_value = CDiamEllipFunc_readDouble(PyList_GET_ITEM(pars,1));
446            return Py_BuildValue("d",(*(self->model))(qx_value,qy_value));
447
448        } else {
449
450                // We have a scalar q, we will evaluate I(q)
451                qx_value = CDiamEllipFunc_readDouble(pars);             
452               
453                return Py_BuildValue("d",(*(self->model))(qx_value));
454        }       
455}
456
457static PyObject * reset(CDiamEllipFunc *self, PyObject *args) {
458   
459
460    return Py_BuildValue("d",0.0);
461}
462
463static PyObject * set_dispersion(CDiamEllipFunc *self, PyObject *args) {
464        PyObject * disp;
465        const char * par_name;
466
467        if ( !PyArg_ParseTuple(args,"sO", &par_name, &disp) ) {
468            PyErr_SetString(CDiamEllipFuncError,
469                "CDiamEllipFunc.set_dispersion expects a DispersionModel object.");
470                return NULL;
471        }
472        void *temp = PyCObject_AsVoidPtr(disp);
473        DispersionModel * dispersion = static_cast<DispersionModel *>(temp);
474
475
476        // Ugliness necessary to go from python to C
477            // TODO: refactor this
478    if (!strcmp(par_name, "radius_a")) {
479        self->model->radius_a.dispersion = dispersion;
480    } else    if (!strcmp(par_name, "radius_b")) {
481        self->model->radius_b.dispersion = dispersion;
482    } else {
483            PyErr_SetString(CDiamEllipFuncError,
484                "CDiamEllipFunc.set_dispersion expects a valid parameter name.");
485                return NULL;
486        }
487
488        DispersionVisitor* visitor = new DispersionVisitor();
489        PyObject * disp_dict = PyDict_New();
490        dispersion->accept_as_source(visitor, dispersion, disp_dict);
491        PyDict_SetItemString(self->dispersion, par_name, disp_dict);
492    return Py_BuildValue("i",1);
493}
494
495
496static PyMethodDef CDiamEllipFunc_methods[] = {
497    {"run",      (PyCFunction)run     , METH_VARARGS,
498      "Evaluate the model at a given Q or Q, phi"},
499    {"runXY",      (PyCFunction)runXY     , METH_VARARGS,
500      "Evaluate the model at a given Q or Qx, Qy"},
501    {"calculate_ER",      (PyCFunction)calculate_ER     , METH_VARARGS,
502      "Evaluate the model at a given Q or Q, phi"},
503    {"calculate_VR",      (PyCFunction)calculate_VR     , METH_VARARGS,
504      "Evaluate VR"},   
505    {"evalDistribution",  (PyCFunction)evalDistribution , METH_VARARGS,
506      "Evaluate the model at a given Q or Qx, Qy vector "},
507    {"reset",    (PyCFunction)reset   , METH_VARARGS,
508      "Reset pair correlation"},
509    {"set_dispersion",      (PyCFunction)set_dispersion     , METH_VARARGS,
510      "Set the dispersion model for a given parameter"},
511   {NULL}
512};
513
514static PyTypeObject CDiamEllipFuncType = {
515    PyObject_HEAD_INIT(NULL)
516    0,                         /*ob_size*/
517    "CDiamEllipFunc",             /*tp_name*/
518    sizeof(CDiamEllipFunc),             /*tp_basicsize*/
519    0,                         /*tp_itemsize*/
520    (destructor)CDiamEllipFunc_dealloc, /*tp_dealloc*/
521    0,                         /*tp_print*/
522    0,                         /*tp_getattr*/
523    0,                         /*tp_setattr*/
524    0,                         /*tp_compare*/
525    0,                         /*tp_repr*/
526    0,                         /*tp_as_number*/
527    0,                         /*tp_as_sequence*/
528    0,                         /*tp_as_mapping*/
529    0,                         /*tp_hash */
530    0,                         /*tp_call*/
531    0,                         /*tp_str*/
532    0,                         /*tp_getattro*/
533    0,                         /*tp_setattro*/
534    0,                         /*tp_as_buffer*/
535    Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
536    "CDiamEllipFunc objects",           /* tp_doc */
537    0,                         /* tp_traverse */
538    0,                         /* tp_clear */
539    0,                         /* tp_richcompare */
540    0,                         /* tp_weaklistoffset */
541    0,                         /* tp_iter */
542    0,                         /* tp_iternext */
543    CDiamEllipFunc_methods,             /* tp_methods */
544    CDiamEllipFunc_members,             /* tp_members */
545    0,                         /* tp_getset */
546    0,                         /* tp_base */
547    0,                         /* tp_dict */
548    0,                         /* tp_descr_get */
549    0,                         /* tp_descr_set */
550    0,                         /* tp_dictoffset */
551    (initproc)CDiamEllipFunc_init,      /* tp_init */
552    0,                         /* tp_alloc */
553    CDiamEllipFunc_new,                 /* tp_new */
554};
555
556
557//static PyMethodDef module_methods[] = {
558//    {NULL}
559//};
560
561/**
562 * Function used to add the model class to a module
563 * @param module: module to add the class to
564 */ 
565void addCDiamEllipFunc(PyObject *module) {
566        PyObject *d;
567       
568    if (PyType_Ready(&CDiamEllipFuncType) < 0)
569        return;
570
571    Py_INCREF(&CDiamEllipFuncType);
572    PyModule_AddObject(module, "CDiamEllipFunc", (PyObject *)&CDiamEllipFuncType);
573   
574    d = PyModule_GetDict(module);
575    static char error_name[] = "CDiamEllipFunc.error";
576    CDiamEllipFuncError = PyErr_NewException(error_name, NULL, NULL);
577    PyDict_SetItemString(d, "CDiamEllipFuncError", CDiamEllipFuncError);
578}
579
Note: See TracBrowser for help on using the repository browser.