source: sasview/sansmodels/src/sans/models/c_models/COblateModel.cpp @ 1b001a7

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 1b001a7 was 8dc0b746, checked in by Jae Cho <jhjcho@…>, 15 years ago

added 2D and corrected polydisp. parameters…

  • Property mode set to 100644
File size: 15.5 KB
RevLine 
[27a0771]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/** COblateModel
16 *
17 * C extension
18 *
19 * WARNING: THIS FILE WAS GENERATED BY WRAPPERGENERATOR.PY
20 *          DO NOT MODIFY THIS FILE, MODIFY oblate.h
21 *          AND RE-RUN THE GENERATOR SCRIPT
22 *
23 */
24 
25extern "C" {
26#include <Python.h>
27#include "structmember.h"
28#include <stdio.h>
29#include <stdlib.h>
30#include <math.h>
31#include <time.h>
32#include "oblate.h"
33}
34
35#include "models.hh"
36#include "dispersion_visitor.hh"
37
38/// Error object for raised exceptions
39static PyObject * COblateModelError = NULL;
40
41
42// Class definition
43typedef struct {
44    PyObject_HEAD
45    /// Parameters
46    PyObject * params;
47    /// Dispersion parameters
48    PyObject * dispersion;
49    /// Underlying model object
50    OblateModel * model;
51    /// Log for unit testing
52    PyObject * log;
53} COblateModel;
54
55
56static void
57COblateModel_dealloc(COblateModel* self)
58{
59    self->ob_type->tp_free((PyObject*)self);
60   
61
62}
63
64static PyObject *
65COblateModel_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
66{
67    COblateModel *self;
68   
69    self = (COblateModel *)type->tp_alloc(type, 0);
70   
71    return (PyObject *)self;
72}
73
74static int
75COblateModel_init(COblateModel *self, PyObject *args, PyObject *kwds)
76{
77    if (self != NULL) {
78       
79        // Create parameters
80        self->params = PyDict_New();
81        self->dispersion = PyDict_New();
82        self->model = new OblateModel();
83       
84        // Initialize parameter dictionary
85        PyDict_SetItemString(self->params,"major_core",Py_BuildValue("d",200.000000));
86        PyDict_SetItemString(self->params,"scale",Py_BuildValue("d",1.000000));
87        PyDict_SetItemString(self->params,"minor_core",Py_BuildValue("d",20.000000));
88        PyDict_SetItemString(self->params,"axis_theta",Py_BuildValue("d",1.000000));
89        PyDict_SetItemString(self->params,"sld_solvent",Py_BuildValue("d",0.000006));
90        PyDict_SetItemString(self->params,"axis_phi",Py_BuildValue("d",1.000000));
91        PyDict_SetItemString(self->params,"background",Py_BuildValue("d",0.001000));
92        PyDict_SetItemString(self->params,"major_shell",Py_BuildValue("d",250.000000));
93        PyDict_SetItemString(self->params,"contrast",Py_BuildValue("d",0.000001));
94        PyDict_SetItemString(self->params,"minor_shell",Py_BuildValue("d",30.000000));
95        // Initialize dispersion / averaging parameter dict
96        DispersionVisitor* visitor = new DispersionVisitor();
97        PyObject * disp_dict;
98        disp_dict = PyDict_New();
99        self->model->major_core.dispersion->accept_as_source(visitor, self->model->major_core.dispersion, disp_dict);
100        PyDict_SetItemString(self->dispersion, "major_core", disp_dict);
101        disp_dict = PyDict_New();
102        self->model->minor_core.dispersion->accept_as_source(visitor, self->model->minor_core.dispersion, disp_dict);
103        PyDict_SetItemString(self->dispersion, "minor_core", disp_dict);
104        disp_dict = PyDict_New();
105        self->model->major_shell.dispersion->accept_as_source(visitor, self->model->major_shell.dispersion, disp_dict);
106        PyDict_SetItemString(self->dispersion, "major_shell", disp_dict);
107        disp_dict = PyDict_New();
108        self->model->minor_shell.dispersion->accept_as_source(visitor, self->model->minor_shell.dispersion, disp_dict);
109        PyDict_SetItemString(self->dispersion, "minor_shell", disp_dict);
110
111
112         
113        // Create empty log
114        self->log = PyDict_New();
115       
116       
117
118    }
119    return 0;
120}
121
122static PyMemberDef COblateModel_members[] = {
123    {"params", T_OBJECT, offsetof(COblateModel, params), 0,
124     "Parameters"},
125        {"dispersion", T_OBJECT, offsetof(COblateModel, dispersion), 0,
126          "Dispersion parameters"},     
127    {"log", T_OBJECT, offsetof(COblateModel, log), 0,
128     "Log"},
129    {NULL}  /* Sentinel */
130};
131
132/** Read double from PyObject
133    @param p PyObject
134    @return double
135*/
136double COblateModel_readDouble(PyObject *p) {
137    if (PyFloat_Check(p)==1) {
138        return (double)(((PyFloatObject *)(p))->ob_fval);
139    } else if (PyInt_Check(p)==1) {
140        return (double)(((PyIntObject *)(p))->ob_ival);
141    } else if (PyLong_Check(p)==1) {
142        return (double)PyLong_AsLong(p);
143    } else {
144        return 0.0;
145    }
146}
147
148
149/**
150 * Function to call to evaluate model
151 * @param args: input q or [q,phi]
152 * @return: function value
153 */
154static PyObject * run(COblateModel *self, PyObject *args) {
155        double q_value, phi_value;
156        PyObject* pars;
157        int npars;
158       
159        // Get parameters
160       
161            // Reader parameter dictionary
162    self->model->major_core = PyFloat_AsDouble( PyDict_GetItemString(self->params, "major_core") );
163    self->model->scale = PyFloat_AsDouble( PyDict_GetItemString(self->params, "scale") );
164    self->model->minor_core = PyFloat_AsDouble( PyDict_GetItemString(self->params, "minor_core") );
165    self->model->axis_theta = PyFloat_AsDouble( PyDict_GetItemString(self->params, "axis_theta") );
166    self->model->sld_solvent = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_solvent") );
167    self->model->axis_phi = PyFloat_AsDouble( PyDict_GetItemString(self->params, "axis_phi") );
168    self->model->background = PyFloat_AsDouble( PyDict_GetItemString(self->params, "background") );
169    self->model->major_shell = PyFloat_AsDouble( PyDict_GetItemString(self->params, "major_shell") );
170    self->model->contrast = PyFloat_AsDouble( PyDict_GetItemString(self->params, "contrast") );
171    self->model->minor_shell = PyFloat_AsDouble( PyDict_GetItemString(self->params, "minor_shell") );
172    // Read in dispersion parameters
173    PyObject* disp_dict;
174    DispersionVisitor* visitor = new DispersionVisitor();
175    disp_dict = PyDict_GetItemString(self->dispersion, "major_core");
176    self->model->major_core.dispersion->accept_as_destination(visitor, self->model->major_core.dispersion, disp_dict);
177    disp_dict = PyDict_GetItemString(self->dispersion, "minor_core");
178    self->model->minor_core.dispersion->accept_as_destination(visitor, self->model->minor_core.dispersion, disp_dict);
179    disp_dict = PyDict_GetItemString(self->dispersion, "major_shell");
180    self->model->major_shell.dispersion->accept_as_destination(visitor, self->model->major_shell.dispersion, disp_dict);
181    disp_dict = PyDict_GetItemString(self->dispersion, "minor_shell");
182    self->model->minor_shell.dispersion->accept_as_destination(visitor, self->model->minor_shell.dispersion, disp_dict);
183
184       
185        // Get input and determine whether we have to supply a 1D or 2D return value.
186        if ( !PyArg_ParseTuple(args,"O",&pars) ) {
187            PyErr_SetString(COblateModelError, 
188                "COblateModel.run expects a q value.");
189                return NULL;
190        }
191         
192        // Check params
193        if( PyList_Check(pars)==1) {
194               
195                // Length of list should be 2 for I(q,phi)
196            npars = PyList_GET_SIZE(pars); 
197            if(npars!=2) {
198                PyErr_SetString(COblateModelError, 
199                        "COblateModel.run expects a double or a list of dimension 2.");
200                return NULL;
201            }
202            // We have a vector q, get the q and phi values at which
203            // to evaluate I(q,phi)
204            q_value = COblateModel_readDouble(PyList_GET_ITEM(pars,0));
205            phi_value = COblateModel_readDouble(PyList_GET_ITEM(pars,1));
206            // Skip zero
207            if (q_value==0) {
208                return Py_BuildValue("d",0.0);
209            }
210                return Py_BuildValue("d",(*(self->model)).evaluate_rphi(q_value,phi_value));
211
212        } else {
213
214                // We have a scalar q, we will evaluate I(q)
215                q_value = COblateModel_readDouble(pars);               
216               
217                return Py_BuildValue("d",(*(self->model))(q_value));
218        }       
219}
220
221/**
222 * Function to call to evaluate model in cartesian coordinates
223 * @param args: input q or [qx, qy]]
224 * @return: function value
225 */
226static PyObject * runXY(COblateModel *self, PyObject *args) {
227        double qx_value, qy_value;
228        PyObject* pars;
229        int npars;
230       
231        // Get parameters
232       
233            // Reader parameter dictionary
234    self->model->major_core = PyFloat_AsDouble( PyDict_GetItemString(self->params, "major_core") );
235    self->model->scale = PyFloat_AsDouble( PyDict_GetItemString(self->params, "scale") );
236    self->model->minor_core = PyFloat_AsDouble( PyDict_GetItemString(self->params, "minor_core") );
237    self->model->axis_theta = PyFloat_AsDouble( PyDict_GetItemString(self->params, "axis_theta") );
238    self->model->sld_solvent = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_solvent") );
239    self->model->axis_phi = PyFloat_AsDouble( PyDict_GetItemString(self->params, "axis_phi") );
240    self->model->background = PyFloat_AsDouble( PyDict_GetItemString(self->params, "background") );
241    self->model->major_shell = PyFloat_AsDouble( PyDict_GetItemString(self->params, "major_shell") );
242    self->model->contrast = PyFloat_AsDouble( PyDict_GetItemString(self->params, "contrast") );
243    self->model->minor_shell = PyFloat_AsDouble( PyDict_GetItemString(self->params, "minor_shell") );
244    // Read in dispersion parameters
245    PyObject* disp_dict;
246    DispersionVisitor* visitor = new DispersionVisitor();
247    disp_dict = PyDict_GetItemString(self->dispersion, "major_core");
248    self->model->major_core.dispersion->accept_as_destination(visitor, self->model->major_core.dispersion, disp_dict);
249    disp_dict = PyDict_GetItemString(self->dispersion, "minor_core");
250    self->model->minor_core.dispersion->accept_as_destination(visitor, self->model->minor_core.dispersion, disp_dict);
251    disp_dict = PyDict_GetItemString(self->dispersion, "major_shell");
252    self->model->major_shell.dispersion->accept_as_destination(visitor, self->model->major_shell.dispersion, disp_dict);
253    disp_dict = PyDict_GetItemString(self->dispersion, "minor_shell");
254    self->model->minor_shell.dispersion->accept_as_destination(visitor, self->model->minor_shell.dispersion, disp_dict);
255
256       
257        // Get input and determine whether we have to supply a 1D or 2D return value.
258        if ( !PyArg_ParseTuple(args,"O",&pars) ) {
259            PyErr_SetString(COblateModelError, 
260                "COblateModel.run expects a q value.");
261                return NULL;
262        }
263         
264        // Check params
265        if( PyList_Check(pars)==1) {
266               
267                // Length of list should be 2 for I(qx, qy))
268            npars = PyList_GET_SIZE(pars); 
269            if(npars!=2) {
270                PyErr_SetString(COblateModelError, 
271                        "COblateModel.run expects a double or a list of dimension 2.");
272                return NULL;
273            }
274            // We have a vector q, get the qx and qy values at which
275            // to evaluate I(qx,qy)
276            qx_value = COblateModel_readDouble(PyList_GET_ITEM(pars,0));
277            qy_value = COblateModel_readDouble(PyList_GET_ITEM(pars,1));
278            return Py_BuildValue("d",(*(self->model))(qx_value,qy_value));
279
280        } else {
281
282                // We have a scalar q, we will evaluate I(q)
283                qx_value = COblateModel_readDouble(pars);               
284               
285                return Py_BuildValue("d",(*(self->model))(qx_value));
286        }       
287}
288
289static PyObject * reset(COblateModel *self, PyObject *args) {
290   
291
292    return Py_BuildValue("d",0.0);
293}
294
295static PyObject * set_dispersion(COblateModel *self, PyObject *args) {
296        PyObject * disp;
297        const char * par_name;
298
299        if ( !PyArg_ParseTuple(args,"sO", &par_name, &disp) ) {
300            PyErr_SetString(COblateModelError,
301                "COblateModel.set_dispersion expects a DispersionModel object.");
302                return NULL;
303        }
304        void *temp = PyCObject_AsVoidPtr(disp);
305        DispersionModel * dispersion = static_cast<DispersionModel *>(temp);
306
307
308        // Ugliness necessary to go from python to C
309            // TODO: refactor this
310    if (!strcmp(par_name, "major_core")) {
311        self->model->major_core.dispersion = dispersion;
312    } else    if (!strcmp(par_name, "minor_core")) {
313        self->model->minor_core.dispersion = dispersion;
314    } else    if (!strcmp(par_name, "major_shell")) {
315        self->model->major_shell.dispersion = dispersion;
316    } else    if (!strcmp(par_name, "minor_shell")) {
317        self->model->minor_shell.dispersion = dispersion;
318    } else {
319            PyErr_SetString(COblateModelError,
320                "COblateModel.set_dispersion expects a valid parameter name.");
321                return NULL;
322        }
323
324        DispersionVisitor* visitor = new DispersionVisitor();
325        PyObject * disp_dict = PyDict_New();
326        dispersion->accept_as_source(visitor, dispersion, disp_dict);
327        PyDict_SetItemString(self->dispersion, par_name, disp_dict);
328    return Py_BuildValue("i",1);
329}
330
331
332static PyMethodDef COblateModel_methods[] = {
333    {"run",      (PyCFunction)run     , METH_VARARGS,
334      "Evaluate the model at a given Q or Q, phi"},
335    {"runXY",      (PyCFunction)runXY     , METH_VARARGS,
336      "Evaluate the model at a given Q or Qx, Qy"},
337    {"reset",    (PyCFunction)reset   , METH_VARARGS,
338      "Reset pair correlation"},
339    {"set_dispersion",      (PyCFunction)set_dispersion     , METH_VARARGS,
340      "Set the dispersion model for a given parameter"},
341   {NULL}
342};
343
344static PyTypeObject COblateModelType = {
345    PyObject_HEAD_INIT(NULL)
346    0,                         /*ob_size*/
347    "COblateModel",             /*tp_name*/
348    sizeof(COblateModel),             /*tp_basicsize*/
349    0,                         /*tp_itemsize*/
350    (destructor)COblateModel_dealloc, /*tp_dealloc*/
351    0,                         /*tp_print*/
352    0,                         /*tp_getattr*/
353    0,                         /*tp_setattr*/
354    0,                         /*tp_compare*/
355    0,                         /*tp_repr*/
356    0,                         /*tp_as_number*/
357    0,                         /*tp_as_sequence*/
358    0,                         /*tp_as_mapping*/
359    0,                         /*tp_hash */
360    0,                         /*tp_call*/
361    0,                         /*tp_str*/
362    0,                         /*tp_getattro*/
363    0,                         /*tp_setattro*/
364    0,                         /*tp_as_buffer*/
365    Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
366    "COblateModel objects",           /* tp_doc */
367    0,                         /* tp_traverse */
368    0,                         /* tp_clear */
369    0,                         /* tp_richcompare */
370    0,                         /* tp_weaklistoffset */
371    0,                         /* tp_iter */
372    0,                         /* tp_iternext */
373    COblateModel_methods,             /* tp_methods */
374    COblateModel_members,             /* tp_members */
375    0,                         /* tp_getset */
376    0,                         /* tp_base */
377    0,                         /* tp_dict */
378    0,                         /* tp_descr_get */
379    0,                         /* tp_descr_set */
380    0,                         /* tp_dictoffset */
381    (initproc)COblateModel_init,      /* tp_init */
382    0,                         /* tp_alloc */
383    COblateModel_new,                 /* tp_new */
384};
385
386
387static PyMethodDef module_methods[] = {
388    {NULL} 
389};
390
391/**
392 * Function used to add the model class to a module
393 * @param module: module to add the class to
394 */ 
395void addCOblateModel(PyObject *module) {
396        PyObject *d;
397       
398    if (PyType_Ready(&COblateModelType) < 0)
399        return;
400
401    Py_INCREF(&COblateModelType);
402    PyModule_AddObject(module, "COblateModel", (PyObject *)&COblateModelType);
403   
404    d = PyModule_GetDict(module);
405    COblateModelError = PyErr_NewException("COblateModel.error", NULL, NULL);
406    PyDict_SetItemString(d, "COblateModelError", COblateModelError);
407}
408
Note: See TracBrowser for help on using the repository browser.