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