source: sasview/sansmodels/src/sans/models/c_models/CProlateModel.cpp @ d8a2e31

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