source: sasview/sansmodels/src/sans/models/c_models/CHayterMSAStructure.cpp @ eba9885

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 eba9885 was 25579e8, checked in by Jae Cho <jhjcho@…>, 16 years ago

Enable structure factors

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