source: sasview/sansmodels/src/sans/models/c_models/CCSParallelepipedModel.cpp @ 0b082f3

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 0b082f3 was 0b082f3, checked in by Mathieu Doucet <doucetm@…>, 12 years ago

Re #7 Enable openmp for all models

  • Property mode set to 100644
File size: 30.1 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/** 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);
67   
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       
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
130
131         
132        // Create empty log
133        self->log = PyDict_New();
134       
135       
136
137    }
138    return 0;
139}
140
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
148static PyMemberDef CCSParallelepipedModel_members[] = {
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},
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#pragma omp parallel for
194         for (int i = 0; i < q->dimensions[0]; i++){
195      double q_value  = *(double *)(q->data + i*q->strides[0]);
196      double *result_value = (double *)(result->data + i*result->strides[0]);
197      *result_value =(*model)(q_value);
198        }
199    return PyArray_Return(result); 
200 }
201
202 /**
203 * Function to call to evaluate model
204 * @param args: input numpy array  [x[],y[]]
205 * @return: numpy array object
206 */
207 static PyObject * evaluateTwoDimXY( CSParallelepipedModel* model, 
208                              PyArrayObject *x, PyArrayObject *y)
209 {
210    PyArrayObject *result;
211    int x_len, y_len, dims[1];
212    //check validity of input vectors
213    if (x->nd != 1 || x->descr->type_num != PyArray_DOUBLE
214        || y->nd != 1 || y->descr->type_num != PyArray_DOUBLE
215        || y->dimensions[0] != x->dimensions[0]){
216        const char * message= "evaluateTwoDimXY  expect 2 numpy arrays";
217        PyErr_SetString(PyExc_ValueError , message); 
218        return NULL;
219    }
220   
221        if (PyArray_Check(x) && PyArray_Check(y)) {
222               
223            x_len = dims[0]= x->dimensions[0];
224        y_len = dims[0]= y->dimensions[0];
225           
226            // Make a new double matrix of same dims
227        result=(PyArrayObject *) PyArray_FromDims(1,dims,NPY_DOUBLE);
228        if (result == NULL){
229            const char * message= "Could not create result ";
230        PyErr_SetString(PyExc_RuntimeError , message);
231            return NULL;
232            }
233       
234        /* Do the calculation. */
235#pragma omp parallel for
236        for (int i=0; i< x_len; i++) {
237            double x_value = *(double *)(x->data + i*x->strides[0]);
238                    double y_value = *(double *)(y->data + i*y->strides[0]);
239                        double *result_value = (double *)(result->data +
240                              i*result->strides[0]);
241                        *result_value = (*model)(x_value, y_value);
242        }           
243        return PyArray_Return(result); 
244       
245        }else{
246                    PyErr_SetString(CCSParallelepipedModelError, 
247                   "CCSParallelepipedModel.evaluateTwoDimXY couldn't run.");
248                return NULL;
249                }       
250}
251/**
252 *  evalDistribution function evaluate a model function with input vector
253 *  @param args: input q as vector or [qx, qy] where qx, qy are vectors
254 *
255 */ 
256static PyObject * evalDistribution(CCSParallelepipedModel *self, PyObject *args){
257        PyObject *qx, *qy;
258        PyArrayObject * pars;
259        int npars ,mpars;
260       
261        // Get parameters
262       
263            // Reader parameter dictionary
264    self->model->sld_rimB = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_rimB") );
265    self->model->sld_rimC = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_rimC") );
266    self->model->scale = PyFloat_AsDouble( PyDict_GetItemString(self->params, "scale") );
267    self->model->background = PyFloat_AsDouble( PyDict_GetItemString(self->params, "background") );
268    self->model->parallel_psi = PyFloat_AsDouble( PyDict_GetItemString(self->params, "parallel_psi") );
269    self->model->midB = PyFloat_AsDouble( PyDict_GetItemString(self->params, "midB") );
270    self->model->parallel_theta = PyFloat_AsDouble( PyDict_GetItemString(self->params, "parallel_theta") );
271    self->model->parallel_phi = PyFloat_AsDouble( PyDict_GetItemString(self->params, "parallel_phi") );
272    self->model->sld_pcore = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_pcore") );
273    self->model->sld_rimA = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_rimA") );
274    self->model->shortA = PyFloat_AsDouble( PyDict_GetItemString(self->params, "shortA") );
275    self->model->rimB = PyFloat_AsDouble( PyDict_GetItemString(self->params, "rimB") );
276    self->model->rimC = PyFloat_AsDouble( PyDict_GetItemString(self->params, "rimC") );
277    self->model->longC = PyFloat_AsDouble( PyDict_GetItemString(self->params, "longC") );
278    self->model->sld_solv = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_solv") );
279    self->model->rimA = PyFloat_AsDouble( PyDict_GetItemString(self->params, "rimA") );
280    // Read in dispersion parameters
281    PyObject* disp_dict;
282    DispersionVisitor* visitor = new DispersionVisitor();
283    disp_dict = PyDict_GetItemString(self->dispersion, "shortA");
284    self->model->shortA.dispersion->accept_as_destination(visitor, self->model->shortA.dispersion, disp_dict);
285    disp_dict = PyDict_GetItemString(self->dispersion, "midB");
286    self->model->midB.dispersion->accept_as_destination(visitor, self->model->midB.dispersion, disp_dict);
287    disp_dict = PyDict_GetItemString(self->dispersion, "longC");
288    self->model->longC.dispersion->accept_as_destination(visitor, self->model->longC.dispersion, disp_dict);
289    disp_dict = PyDict_GetItemString(self->dispersion, "parallel_phi");
290    self->model->parallel_phi.dispersion->accept_as_destination(visitor, self->model->parallel_phi.dispersion, disp_dict);
291    disp_dict = PyDict_GetItemString(self->dispersion, "parallel_psi");
292    self->model->parallel_psi.dispersion->accept_as_destination(visitor, self->model->parallel_psi.dispersion, disp_dict);
293    disp_dict = PyDict_GetItemString(self->dispersion, "parallel_theta");
294    self->model->parallel_theta.dispersion->accept_as_destination(visitor, self->model->parallel_theta.dispersion, disp_dict);
295
296       
297        // Get input and determine whether we have to supply a 1D or 2D return value.
298        if ( !PyArg_ParseTuple(args,"O",&pars) ) {
299            PyErr_SetString(CCSParallelepipedModelError, 
300                "CCSParallelepipedModel.evalDistribution expects a q value.");
301                return NULL;
302        }
303    // Check params
304       
305    if(PyArray_Check(pars)==1) {
306               
307            // Length of list should 1 or 2
308            npars = pars->nd; 
309            if(npars==1) {
310                // input is a numpy array
311                if (PyArray_Check(pars)) {
312                        return evaluateOneDim(self->model, (PyArrayObject*)pars); 
313                    }
314                }else{
315                    PyErr_SetString(CCSParallelepipedModelError, 
316                   "CCSParallelepipedModel.evalDistribution expect numpy array of one dimension.");
317                return NULL;
318                }
319    }else if( PyList_Check(pars)==1) {
320        // Length of list should be 2 for I(qx,qy)
321            mpars = PyList_GET_SIZE(pars); 
322            if(mpars!=2) {
323                PyErr_SetString(CCSParallelepipedModelError, 
324                        "CCSParallelepipedModel.evalDistribution expects a list of dimension 2.");
325                return NULL;
326            }
327             qx = PyList_GET_ITEM(pars,0);
328             qy = PyList_GET_ITEM(pars,1);
329             if (PyArray_Check(qx) && PyArray_Check(qy)) {
330                 return evaluateTwoDimXY(self->model, (PyArrayObject*)qx,
331                           (PyArrayObject*)qy);
332                 }else{
333                    PyErr_SetString(CCSParallelepipedModelError, 
334                   "CCSParallelepipedModel.evalDistribution expect 2 numpy arrays in list.");
335                return NULL;
336             }
337        }
338        PyErr_SetString(CCSParallelepipedModelError, 
339                   "CCSParallelepipedModel.evalDistribution couln't be run.");
340        return NULL;
341       
342}
343
344/**
345 * Function to call to evaluate model
346 * @param args: input q or [q,phi]
347 * @return: function value
348 */
349static PyObject * run(CCSParallelepipedModel *self, PyObject *args) {
350        double q_value, phi_value;
351        PyObject* pars;
352        int npars;
353       
354        // Get parameters
355       
356            // Reader parameter dictionary
357    self->model->sld_rimB = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_rimB") );
358    self->model->sld_rimC = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_rimC") );
359    self->model->scale = PyFloat_AsDouble( PyDict_GetItemString(self->params, "scale") );
360    self->model->background = PyFloat_AsDouble( PyDict_GetItemString(self->params, "background") );
361    self->model->parallel_psi = PyFloat_AsDouble( PyDict_GetItemString(self->params, "parallel_psi") );
362    self->model->midB = PyFloat_AsDouble( PyDict_GetItemString(self->params, "midB") );
363    self->model->parallel_theta = PyFloat_AsDouble( PyDict_GetItemString(self->params, "parallel_theta") );
364    self->model->parallel_phi = PyFloat_AsDouble( PyDict_GetItemString(self->params, "parallel_phi") );
365    self->model->sld_pcore = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_pcore") );
366    self->model->sld_rimA = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_rimA") );
367    self->model->shortA = PyFloat_AsDouble( PyDict_GetItemString(self->params, "shortA") );
368    self->model->rimB = PyFloat_AsDouble( PyDict_GetItemString(self->params, "rimB") );
369    self->model->rimC = PyFloat_AsDouble( PyDict_GetItemString(self->params, "rimC") );
370    self->model->longC = PyFloat_AsDouble( PyDict_GetItemString(self->params, "longC") );
371    self->model->sld_solv = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_solv") );
372    self->model->rimA = PyFloat_AsDouble( PyDict_GetItemString(self->params, "rimA") );
373    // Read in dispersion parameters
374    PyObject* disp_dict;
375    DispersionVisitor* visitor = new DispersionVisitor();
376    disp_dict = PyDict_GetItemString(self->dispersion, "shortA");
377    self->model->shortA.dispersion->accept_as_destination(visitor, self->model->shortA.dispersion, disp_dict);
378    disp_dict = PyDict_GetItemString(self->dispersion, "midB");
379    self->model->midB.dispersion->accept_as_destination(visitor, self->model->midB.dispersion, disp_dict);
380    disp_dict = PyDict_GetItemString(self->dispersion, "longC");
381    self->model->longC.dispersion->accept_as_destination(visitor, self->model->longC.dispersion, disp_dict);
382    disp_dict = PyDict_GetItemString(self->dispersion, "parallel_phi");
383    self->model->parallel_phi.dispersion->accept_as_destination(visitor, self->model->parallel_phi.dispersion, disp_dict);
384    disp_dict = PyDict_GetItemString(self->dispersion, "parallel_psi");
385    self->model->parallel_psi.dispersion->accept_as_destination(visitor, self->model->parallel_psi.dispersion, disp_dict);
386    disp_dict = PyDict_GetItemString(self->dispersion, "parallel_theta");
387    self->model->parallel_theta.dispersion->accept_as_destination(visitor, self->model->parallel_theta.dispersion, disp_dict);
388
389       
390        // Get input and determine whether we have to supply a 1D or 2D return value.
391        if ( !PyArg_ParseTuple(args,"O",&pars) ) {
392            PyErr_SetString(CCSParallelepipedModelError, 
393                "CCSParallelepipedModel.run expects a q value.");
394                return NULL;
395        }
396         
397        // Check params
398        if( PyList_Check(pars)==1) {
399               
400                // Length of list should be 2 for I(q,phi)
401            npars = PyList_GET_SIZE(pars); 
402            if(npars!=2) {
403                PyErr_SetString(CCSParallelepipedModelError, 
404                        "CCSParallelepipedModel.run expects a double or a list of dimension 2.");
405                return NULL;
406            }
407            // We have a vector q, get the q and phi values at which
408            // to evaluate I(q,phi)
409            q_value = CCSParallelepipedModel_readDouble(PyList_GET_ITEM(pars,0));
410            phi_value = CCSParallelepipedModel_readDouble(PyList_GET_ITEM(pars,1));
411            // Skip zero
412            if (q_value==0) {
413                return Py_BuildValue("d",0.0);
414            }
415                return Py_BuildValue("d",(*(self->model)).evaluate_rphi(q_value,phi_value));
416
417        } else {
418
419                // We have a scalar q, we will evaluate I(q)
420                q_value = CCSParallelepipedModel_readDouble(pars);             
421               
422                return Py_BuildValue("d",(*(self->model))(q_value));
423        }       
424}
425/**
426 * Function to call to calculate_ER
427 * @return: effective radius value
428 */
429static PyObject * calculate_ER(CCSParallelepipedModel *self) {
430
431        // Get parameters
432       
433            // Reader parameter dictionary
434    self->model->sld_rimB = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_rimB") );
435    self->model->sld_rimC = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_rimC") );
436    self->model->scale = PyFloat_AsDouble( PyDict_GetItemString(self->params, "scale") );
437    self->model->background = PyFloat_AsDouble( PyDict_GetItemString(self->params, "background") );
438    self->model->parallel_psi = PyFloat_AsDouble( PyDict_GetItemString(self->params, "parallel_psi") );
439    self->model->midB = PyFloat_AsDouble( PyDict_GetItemString(self->params, "midB") );
440    self->model->parallel_theta = PyFloat_AsDouble( PyDict_GetItemString(self->params, "parallel_theta") );
441    self->model->parallel_phi = PyFloat_AsDouble( PyDict_GetItemString(self->params, "parallel_phi") );
442    self->model->sld_pcore = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_pcore") );
443    self->model->sld_rimA = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_rimA") );
444    self->model->shortA = PyFloat_AsDouble( PyDict_GetItemString(self->params, "shortA") );
445    self->model->rimB = PyFloat_AsDouble( PyDict_GetItemString(self->params, "rimB") );
446    self->model->rimC = PyFloat_AsDouble( PyDict_GetItemString(self->params, "rimC") );
447    self->model->longC = PyFloat_AsDouble( PyDict_GetItemString(self->params, "longC") );
448    self->model->sld_solv = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_solv") );
449    self->model->rimA = PyFloat_AsDouble( PyDict_GetItemString(self->params, "rimA") );
450    // Read in dispersion parameters
451    PyObject* disp_dict;
452    DispersionVisitor* visitor = new DispersionVisitor();
453    disp_dict = PyDict_GetItemString(self->dispersion, "shortA");
454    self->model->shortA.dispersion->accept_as_destination(visitor, self->model->shortA.dispersion, disp_dict);
455    disp_dict = PyDict_GetItemString(self->dispersion, "midB");
456    self->model->midB.dispersion->accept_as_destination(visitor, self->model->midB.dispersion, disp_dict);
457    disp_dict = PyDict_GetItemString(self->dispersion, "longC");
458    self->model->longC.dispersion->accept_as_destination(visitor, self->model->longC.dispersion, disp_dict);
459    disp_dict = PyDict_GetItemString(self->dispersion, "parallel_phi");
460    self->model->parallel_phi.dispersion->accept_as_destination(visitor, self->model->parallel_phi.dispersion, disp_dict);
461    disp_dict = PyDict_GetItemString(self->dispersion, "parallel_psi");
462    self->model->parallel_psi.dispersion->accept_as_destination(visitor, self->model->parallel_psi.dispersion, disp_dict);
463    disp_dict = PyDict_GetItemString(self->dispersion, "parallel_theta");
464    self->model->parallel_theta.dispersion->accept_as_destination(visitor, self->model->parallel_theta.dispersion, disp_dict);
465
466               
467        return Py_BuildValue("d",(*(self->model)).calculate_ER());
468
469}
470/**
471 * Function to call to evaluate model in cartesian coordinates
472 * @param args: input q or [qx, qy]]
473 * @return: function value
474 */
475static PyObject * runXY(CCSParallelepipedModel *self, PyObject *args) {
476        double qx_value, qy_value;
477        PyObject* pars;
478        int npars;
479       
480        // Get parameters
481       
482            // Reader parameter dictionary
483    self->model->sld_rimB = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_rimB") );
484    self->model->sld_rimC = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_rimC") );
485    self->model->scale = PyFloat_AsDouble( PyDict_GetItemString(self->params, "scale") );
486    self->model->background = PyFloat_AsDouble( PyDict_GetItemString(self->params, "background") );
487    self->model->parallel_psi = PyFloat_AsDouble( PyDict_GetItemString(self->params, "parallel_psi") );
488    self->model->midB = PyFloat_AsDouble( PyDict_GetItemString(self->params, "midB") );
489    self->model->parallel_theta = PyFloat_AsDouble( PyDict_GetItemString(self->params, "parallel_theta") );
490    self->model->parallel_phi = PyFloat_AsDouble( PyDict_GetItemString(self->params, "parallel_phi") );
491    self->model->sld_pcore = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_pcore") );
492    self->model->sld_rimA = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_rimA") );
493    self->model->shortA = PyFloat_AsDouble( PyDict_GetItemString(self->params, "shortA") );
494    self->model->rimB = PyFloat_AsDouble( PyDict_GetItemString(self->params, "rimB") );
495    self->model->rimC = PyFloat_AsDouble( PyDict_GetItemString(self->params, "rimC") );
496    self->model->longC = PyFloat_AsDouble( PyDict_GetItemString(self->params, "longC") );
497    self->model->sld_solv = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_solv") );
498    self->model->rimA = PyFloat_AsDouble( PyDict_GetItemString(self->params, "rimA") );
499    // Read in dispersion parameters
500    PyObject* disp_dict;
501    DispersionVisitor* visitor = new DispersionVisitor();
502    disp_dict = PyDict_GetItemString(self->dispersion, "shortA");
503    self->model->shortA.dispersion->accept_as_destination(visitor, self->model->shortA.dispersion, disp_dict);
504    disp_dict = PyDict_GetItemString(self->dispersion, "midB");
505    self->model->midB.dispersion->accept_as_destination(visitor, self->model->midB.dispersion, disp_dict);
506    disp_dict = PyDict_GetItemString(self->dispersion, "longC");
507    self->model->longC.dispersion->accept_as_destination(visitor, self->model->longC.dispersion, disp_dict);
508    disp_dict = PyDict_GetItemString(self->dispersion, "parallel_phi");
509    self->model->parallel_phi.dispersion->accept_as_destination(visitor, self->model->parallel_phi.dispersion, disp_dict);
510    disp_dict = PyDict_GetItemString(self->dispersion, "parallel_psi");
511    self->model->parallel_psi.dispersion->accept_as_destination(visitor, self->model->parallel_psi.dispersion, disp_dict);
512    disp_dict = PyDict_GetItemString(self->dispersion, "parallel_theta");
513    self->model->parallel_theta.dispersion->accept_as_destination(visitor, self->model->parallel_theta.dispersion, disp_dict);
514
515       
516        // Get input and determine whether we have to supply a 1D or 2D return value.
517        if ( !PyArg_ParseTuple(args,"O",&pars) ) {
518            PyErr_SetString(CCSParallelepipedModelError, 
519                "CCSParallelepipedModel.run expects a q value.");
520                return NULL;
521        }
522         
523        // Check params
524        if( PyList_Check(pars)==1) {
525               
526                // Length of list should be 2 for I(qx, qy))
527            npars = PyList_GET_SIZE(pars); 
528            if(npars!=2) {
529                PyErr_SetString(CCSParallelepipedModelError, 
530                        "CCSParallelepipedModel.run expects a double or a list of dimension 2.");
531                return NULL;
532            }
533            // We have a vector q, get the qx and qy values at which
534            // to evaluate I(qx,qy)
535            qx_value = CCSParallelepipedModel_readDouble(PyList_GET_ITEM(pars,0));
536            qy_value = CCSParallelepipedModel_readDouble(PyList_GET_ITEM(pars,1));
537            return Py_BuildValue("d",(*(self->model))(qx_value,qy_value));
538
539        } else {
540
541                // We have a scalar q, we will evaluate I(q)
542                qx_value = CCSParallelepipedModel_readDouble(pars);             
543               
544                return Py_BuildValue("d",(*(self->model))(qx_value));
545        }       
546}
547
548static PyObject * reset(CCSParallelepipedModel *self, PyObject *args) {
549   
550
551    return Py_BuildValue("d",0.0);
552}
553
554static PyObject * set_dispersion(CCSParallelepipedModel *self, PyObject *args) {
555        PyObject * disp;
556        const char * par_name;
557
558        if ( !PyArg_ParseTuple(args,"sO", &par_name, &disp) ) {
559            PyErr_SetString(CCSParallelepipedModelError,
560                "CCSParallelepipedModel.set_dispersion expects a DispersionModel object.");
561                return NULL;
562        }
563        void *temp = PyCObject_AsVoidPtr(disp);
564        DispersionModel * dispersion = static_cast<DispersionModel *>(temp);
565
566
567        // Ugliness necessary to go from python to C
568            // TODO: refactor this
569    if (!strcmp(par_name, "shortA")) {
570        self->model->shortA.dispersion = dispersion;
571    } else    if (!strcmp(par_name, "midB")) {
572        self->model->midB.dispersion = dispersion;
573    } else    if (!strcmp(par_name, "longC")) {
574        self->model->longC.dispersion = dispersion;
575    } else    if (!strcmp(par_name, "parallel_phi")) {
576        self->model->parallel_phi.dispersion = dispersion;
577    } else    if (!strcmp(par_name, "parallel_psi")) {
578        self->model->parallel_psi.dispersion = dispersion;
579    } else    if (!strcmp(par_name, "parallel_theta")) {
580        self->model->parallel_theta.dispersion = dispersion;
581    } else {
582            PyErr_SetString(CCSParallelepipedModelError,
583                "CCSParallelepipedModel.set_dispersion expects a valid parameter name.");
584                return NULL;
585        }
586
587        DispersionVisitor* visitor = new DispersionVisitor();
588        PyObject * disp_dict = PyDict_New();
589        dispersion->accept_as_source(visitor, dispersion, disp_dict);
590        PyDict_SetItemString(self->dispersion, par_name, disp_dict);
591    return Py_BuildValue("i",1);
592}
593
594
595static PyMethodDef CCSParallelepipedModel_methods[] = {
596    {"run",      (PyCFunction)run     , METH_VARARGS,
597      "Evaluate the model at a given Q or Q, phi"},
598    {"runXY",      (PyCFunction)runXY     , METH_VARARGS,
599      "Evaluate the model at a given Q or Qx, Qy"},
600    {"calculate_ER",      (PyCFunction)calculate_ER     , METH_VARARGS,
601      "Evaluate the model at a given Q or Q, phi"},
602     
603    {"evalDistribution",  (PyCFunction)evalDistribution , METH_VARARGS,
604      "Evaluate the model at a given Q or Qx, Qy vector "},
605    {"reset",    (PyCFunction)reset   , METH_VARARGS,
606      "Reset pair correlation"},
607    {"set_dispersion",      (PyCFunction)set_dispersion     , METH_VARARGS,
608      "Set the dispersion model for a given parameter"},
609   {NULL}
610};
611
612static PyTypeObject CCSParallelepipedModelType = {
613    PyObject_HEAD_INIT(NULL)
614    0,                         /*ob_size*/
615    "CCSParallelepipedModel",             /*tp_name*/
616    sizeof(CCSParallelepipedModel),             /*tp_basicsize*/
617    0,                         /*tp_itemsize*/
618    (destructor)CCSParallelepipedModel_dealloc, /*tp_dealloc*/
619    0,                         /*tp_print*/
620    0,                         /*tp_getattr*/
621    0,                         /*tp_setattr*/
622    0,                         /*tp_compare*/
623    0,                         /*tp_repr*/
624    0,                         /*tp_as_number*/
625    0,                         /*tp_as_sequence*/
626    0,                         /*tp_as_mapping*/
627    0,                         /*tp_hash */
628    0,                         /*tp_call*/
629    0,                         /*tp_str*/
630    0,                         /*tp_getattro*/
631    0,                         /*tp_setattro*/
632    0,                         /*tp_as_buffer*/
633    Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
634    "CCSParallelepipedModel objects",           /* tp_doc */
635    0,                         /* tp_traverse */
636    0,                         /* tp_clear */
637    0,                         /* tp_richcompare */
638    0,                         /* tp_weaklistoffset */
639    0,                         /* tp_iter */
640    0,                         /* tp_iternext */
641    CCSParallelepipedModel_methods,             /* tp_methods */
642    CCSParallelepipedModel_members,             /* tp_members */
643    0,                         /* tp_getset */
644    0,                         /* tp_base */
645    0,                         /* tp_dict */
646    0,                         /* tp_descr_get */
647    0,                         /* tp_descr_set */
648    0,                         /* tp_dictoffset */
649    (initproc)CCSParallelepipedModel_init,      /* tp_init */
650    0,                         /* tp_alloc */
651    CCSParallelepipedModel_new,                 /* tp_new */
652};
653
654
655//static PyMethodDef module_methods[] = {
656//    {NULL}
657//};
658
659/**
660 * Function used to add the model class to a module
661 * @param module: module to add the class to
662 */ 
663void addCCSParallelepipedModel(PyObject *module) {
664        PyObject *d;
665       
666    if (PyType_Ready(&CCSParallelepipedModelType) < 0)
667        return;
668
669    Py_INCREF(&CCSParallelepipedModelType);
670    PyModule_AddObject(module, "CCSParallelepipedModel", (PyObject *)&CCSParallelepipedModelType);
671   
672    d = PyModule_GetDict(module);
673    static char error_name[] = "CCSParallelepipedModel.error";
674    CCSParallelepipedModelError = PyErr_NewException(error_name, NULL, NULL);
675    PyDict_SetItemString(d, "CCSParallelepipedModelError", CCSParallelepipedModelError);
676}
677
Note: See TracBrowser for help on using the repository browser.