source: sasview/sansmodels/src/sans/models/c_models/CParallelepipedModel.cpp @ 0b082f3

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 0b082f3 was 0b082f3, checked in by Mathieu Doucet <doucetm@…>, 12 years ago

Re #7 Enable openmp for all models

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