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

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

Re #4 This should clean up a whole bunch of C++ warnings.

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