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