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

ESS_GUIESS_GUI_DocsESS_GUI_batch_fittingESS_GUI_bumps_abstractionESS_GUI_iss1116ESS_GUI_iss879ESS_GUI_iss959ESS_GUI_openclESS_GUI_orderingESS_GUI_sync_sascalccostrafo411magnetic_scattrelease-4.1.1release-4.1.2release-4.2.2release_4.0.1ticket-1009ticket-1094-headlessticket-1242-2d-resolutionticket-1243ticket-1249ticket885unittest-saveload
Last change on this file since 400155b was 400155b, checked in by gonzalezm, 9 years ago

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

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