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