source: sasview/sansmodels/src/sans/models/c_models/CCSParallelepipedModel.cpp @ 916f5c0

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 916f5c0 was 2605da22, checked in by Mathieu Doucet <doucetm@…>, 13 years ago

Re #4 Still a few more warnings

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