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

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 7a69683 was 72a90bd, checked in by Gervaise Alina <gervyh@…>, 15 years ago

add more model 1D

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