source: sasview/src/sas/sascalc/calculator/c_extensions/sld2i_module.c @ 3010f68

ESS_GUIESS_GUI_batch_fittingESS_GUI_bumps_abstractionESS_GUI_iss1116ESS_GUI_iss879ESS_GUI_iss959ESS_GUI_openclESS_GUI_orderingESS_GUI_sync_sascalc
Last change on this file since 3010f68 was 3010f68, checked in by Piotr Rozyczko <rozyczko@…>, 6 years ago

Cherrypicked (from master) commits for C++ → C conversion in the SLD calculator.

  • Property mode set to 100755
File size: 5.2 KB
Line 
1/**
2  SLD2I module to perform point and I calculations
3 */
4#include <Python.h>
5#include <stdio.h>
6#include <sld2i.h>
7
8#if PY_MAJOR_VERSION < 3
9typedef void (*PyCapsule_Destructor)(PyObject *);
10typedef void (*PyCObject_Destructor)(void *);
11#define PyCapsule_New(pointer, name, destructor) (PyCObject_FromVoidPtr(pointer, (PyCObject_Destructor)destructor))
12#define PyCapsule_GetPointer(capsule, name) (PyCObject_AsVoidPtr(capsule))
13#endif
14
15
16// Utilities
17#define INVECTOR(obj,buf,len)                                                                           \
18    do { \
19        int err = PyObject_AsReadBuffer(obj, (const void **)(&buf), &len); \
20        if (err < 0) return NULL; \
21        len /= sizeof(*buf); \
22    } while (0)
23
24#define OUTVECTOR(obj,buf,len) \
25    do { \
26        int err = PyObject_AsWriteBuffer(obj, (void **)(&buf), &len); \
27        if (err < 0) return NULL; \
28        len /= sizeof(*buf); \
29    } while (0)
30
31
32/**
33 * Delete a GenI object
34 */
35void
36del_sld2i(PyObject *obj){
37        GenI* sld2i = (GenI *)(PyCapsule_GetPointer(obj, "GenI"));
38        PyMem_Free((void *)sld2i);
39}
40
41/**
42 * Create a GenI as a python object by supplying arrays
43 */
44PyObject * new_GenI(PyObject *self, PyObject *args) {
45        PyObject *x_val_obj;
46        PyObject *y_val_obj;
47        PyObject *z_val_obj;
48        PyObject *sldn_val_obj;
49        PyObject *mx_val_obj;
50        PyObject *my_val_obj;
51        PyObject *mz_val_obj;
52        PyObject *vol_pix_obj;
53        Py_ssize_t n_x;
54        //PyObject rlimit_obj;
55        //PyObject npoints_obj;
56        //PyObject nrbins_obj;
57        //PyObject nphibins_obj;
58        int n_pix;
59        double* x_val;
60        double* y_val;
61        double* z_val;
62        double* sldn_val;
63        double* mx_val;
64        double* my_val;
65        double* mz_val;
66        double* vol_pix;
67        double inspin;
68        double outspin;
69        double stheta;
70
71        if (!PyArg_ParseTuple(args, "iOOOOOOOOddd", &n_pix, &x_val_obj, &y_val_obj, &z_val_obj, &sldn_val_obj, &mx_val_obj, &my_val_obj, &mz_val_obj, &vol_pix_obj, &inspin, &outspin, &stheta)) return NULL;
72        OUTVECTOR(x_val_obj, x_val, n_x);
73        OUTVECTOR(y_val_obj, y_val, n_x);
74        OUTVECTOR(z_val_obj, z_val, n_x);
75        OUTVECTOR(sldn_val_obj, sldn_val, n_x);
76        OUTVECTOR(mx_val_obj, mx_val, n_x);
77        OUTVECTOR(my_val_obj, my_val, n_x);
78        OUTVECTOR(mz_val_obj, mz_val, n_x);
79        OUTVECTOR(vol_pix_obj, vol_pix, n_x);
80        GenI* sld2i =  PyMem_Malloc(sizeof(GenI));
81        if (sld2i != NULL) {
82                initGenI(sld2i, n_pix,x_val,y_val,z_val,sldn_val,mx_val,my_val,mz_val,vol_pix,inspin,outspin,stheta);
83        }
84        return PyCapsule_New(sld2i, "GenI", del_sld2i);
85}
86
87/**
88 * GenI the given input (2D) according to a given object
89 */
90PyObject * genicom_inputXY(PyObject *self, PyObject *args) {
91        int npoints;
92        PyObject *qx_obj;
93        double *qx;
94        PyObject *qy_obj;
95        double *qy;
96        PyObject *I_out_obj;
97        Py_ssize_t n_out;
98        double *I_out;
99        PyObject *gen_obj;
100
101        if (!PyArg_ParseTuple(args, "OiOOO",  &gen_obj, &npoints, &qx_obj, &qy_obj, &I_out_obj)) return NULL;
102        OUTVECTOR(qx_obj, qx, n_out);
103        OUTVECTOR(qy_obj, qy, n_out);
104        OUTVECTOR(I_out_obj, I_out, n_out);
105
106        // Sanity check
107        //if(n_in!=n_out) return Py_BuildValue("i",-1);
108
109        // Set the array pointers
110        GenI* sld2i = (GenI *)PyCapsule_GetPointer(gen_obj, "GenI");
111
112        genicomXY(sld2i, npoints, qx, qy, I_out);
113        //return PyCObject_FromVoidPtr(s, del_genicom);
114        return Py_BuildValue("i",1);
115}
116
117/**
118 * GenI the given 1D input according to a given object
119 */
120PyObject * genicom_input(PyObject *self, PyObject *args) {
121        int npoints;
122        PyObject *q_obj;
123        double *q;
124        PyObject *I_out_obj;
125        Py_ssize_t n_out;
126        double *I_out;
127        PyObject *gen_obj;
128
129        if (!PyArg_ParseTuple(args, "OiOO",  &gen_obj, &npoints, &q_obj, &I_out_obj)) return NULL;
130        OUTVECTOR(q_obj, q, n_out);
131        OUTVECTOR(I_out_obj, I_out, n_out);
132
133        // Sanity check
134        //if(n_in!=n_out) return Py_BuildValue("i",-1);
135
136        // Set the array pointers
137        GenI *sld2i = (GenI *)PyCapsule_GetPointer(gen_obj, "GenI");
138
139        genicom(sld2i, npoints, q, I_out);
140        //return PyCObject_FromVoidPtr(s, del_genicom);
141        return Py_BuildValue("i",1);
142}
143
144/**
145 * Define module methods
146 */
147static PyMethodDef module_methods[] = {
148        {"new_GenI", (PyCFunction)new_GenI, METH_VARARGS,
149                  "Create a new GenI object"},
150        {"genicom",(PyCFunction)genicom_input, METH_VARARGS,
151                  "genicom the given 1d input arrays"},
152        {"genicomXY",(PyCFunction)genicom_inputXY, METH_VARARGS,
153                  "genicomXY the given 2d input arrays"},
154    {NULL}
155};
156
157#define MODULE_DOC "Sld2i C Library"
158#define MODULE_NAME "sld2i"
159#define MODULE_INIT2 initsld2i
160#define MODULE_INIT3 PyInit_sld2i
161#define MODULE_METHODS module_methods
162
163/* ==== boilerplate python 2/3 interface bootstrap ==== */
164
165
166#if defined(WIN32) && !defined(__MINGW32__)
167    #define DLL_EXPORT __declspec(dllexport)
168#else
169    #define DLL_EXPORT
170#endif
171
172#if PY_MAJOR_VERSION >= 3
173
174  DLL_EXPORT PyMODINIT_FUNC MODULE_INIT3(void)
175  {
176    static struct PyModuleDef moduledef = {
177      PyModuleDef_HEAD_INIT,
178      MODULE_NAME,         /* m_name */
179      MODULE_DOC,          /* m_doc */
180      -1,                  /* m_size */
181      MODULE_METHODS,      /* m_methods */
182      NULL,                /* m_reload */
183      NULL,                /* m_traverse */
184      NULL,                /* m_clear */
185      NULL,                /* m_free */
186    };
187    return PyModule_Create(&moduledef);
188  }
189
190#else /* !PY_MAJOR_VERSION >= 3 */
191
192  DLL_EXPORT PyMODINIT_FUNC MODULE_INIT2(void)
193  {
194    Py_InitModule4(MODULE_NAME,
195                 MODULE_METHODS,
196                 MODULE_DOC,
197                 0,
198                 PYTHON_API_VERSION
199                 );
200  }
201
202#endif /* !PY_MAJOR_VERSION >= 3 */
Note: See TracBrowser for help on using the repository browser.