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

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

change destructor for models

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