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