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

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 0d86fecb was 18f2ca1, checked in by Jae Cho <jhjcho@…>, 14 years ago

hope this was the last model left to add

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