1 | /** c_models |
---|
2 | * |
---|
3 | * Module containing all SANS model extensions |
---|
4 | * |
---|
5 | * @author M.Doucet / UTK |
---|
6 | */ |
---|
7 | #include <Python.h> |
---|
8 | #include <parameters.hh> |
---|
9 | |
---|
10 | void addCCylinderModel(PyObject *module); |
---|
11 | void addCSphereModel(PyObject *module); |
---|
12 | void addCCoreShellModel(PyObject *module); |
---|
13 | void addCCoreShellCylinderModel(PyObject *module); |
---|
14 | void addCEllipsoidModel(PyObject *module); |
---|
15 | void addCEllipticalCylinderModel(PyObject *module); |
---|
16 | |
---|
17 | extern "C" { |
---|
18 | //void addCCoreShellCylinderModel(PyObject *module); |
---|
19 | //void addCCoreShellModel(PyObject *module); |
---|
20 | //void addCEllipsoidModel(PyObject *module); |
---|
21 | //void addCSphereModel(PyObject *module); |
---|
22 | //void addCEllipticalCylinderModel(PyObject *module); |
---|
23 | void addDisperser(PyObject *module); |
---|
24 | void addCGaussian(PyObject *module); |
---|
25 | void addCLorentzian(PyObject *module); |
---|
26 | } |
---|
27 | |
---|
28 | /** |
---|
29 | * Delete a dispersion model object |
---|
30 | */ |
---|
31 | void del_dispersion_model(void *ptr){ |
---|
32 | DispersionModel * disp = static_cast<DispersionModel *>(ptr); |
---|
33 | delete disp; |
---|
34 | return; |
---|
35 | } |
---|
36 | |
---|
37 | /** |
---|
38 | * Create a dispersion model as a python object |
---|
39 | */ |
---|
40 | PyObject * new_dispersion_model(PyObject *, PyObject *args) { |
---|
41 | DispersionModel *disp = new DispersionModel(); |
---|
42 | return PyCObject_FromVoidPtr(disp, del_dispersion_model); |
---|
43 | } |
---|
44 | |
---|
45 | |
---|
46 | /** |
---|
47 | * Delete a gaussian dispersion model object |
---|
48 | */ |
---|
49 | void del_gaussian_dispersion(void *ptr){ |
---|
50 | GaussianDispersion * disp = static_cast<GaussianDispersion *>(ptr); |
---|
51 | delete disp; |
---|
52 | return; |
---|
53 | } |
---|
54 | |
---|
55 | /** |
---|
56 | * Create a gaussian dispersion model as a python object |
---|
57 | */ |
---|
58 | PyObject * new_gaussian_dispersion(PyObject *, PyObject *args) { |
---|
59 | GaussianDispersion *disp = new GaussianDispersion(); |
---|
60 | return PyCObject_FromVoidPtr(disp, del_gaussian_dispersion); |
---|
61 | } |
---|
62 | |
---|
63 | /** |
---|
64 | * Delete an array dispersion model object |
---|
65 | */ |
---|
66 | void del_array_dispersion(void *ptr){ |
---|
67 | ArrayDispersion * disp = static_cast<ArrayDispersion *>(ptr); |
---|
68 | delete disp; |
---|
69 | return; |
---|
70 | } |
---|
71 | |
---|
72 | /** |
---|
73 | * Create an array dispersion model as a python object |
---|
74 | */ |
---|
75 | PyObject * new_array_dispersion(PyObject *, PyObject *args) { |
---|
76 | ArrayDispersion *disp = new ArrayDispersion(); |
---|
77 | return PyCObject_FromVoidPtr(disp, del_array_dispersion); |
---|
78 | } |
---|
79 | |
---|
80 | #define INVECTOR(obj,buf,len) \ |
---|
81 | do { \ |
---|
82 | int err = PyObject_AsReadBuffer(obj, (const void **)(&buf), &len); \ |
---|
83 | if (err < 0) return NULL; \ |
---|
84 | len /= sizeof(*buf); \ |
---|
85 | } while (0) |
---|
86 | |
---|
87 | /** |
---|
88 | * Sets weights from a numpy array |
---|
89 | */ |
---|
90 | PyObject * set_weights(PyObject *, PyObject *args) { |
---|
91 | PyObject *val_obj; |
---|
92 | PyObject *wei_obj; |
---|
93 | PyObject *disp; |
---|
94 | Py_ssize_t nval; |
---|
95 | Py_ssize_t nwei; |
---|
96 | double *values; |
---|
97 | double *weights; |
---|
98 | int i; |
---|
99 | |
---|
100 | if (!PyArg_ParseTuple(args, "OOO", &disp, &val_obj, &wei_obj)) return NULL; |
---|
101 | INVECTOR(val_obj, values, nval); |
---|
102 | INVECTOR(wei_obj, weights, nwei); |
---|
103 | |
---|
104 | // Sanity check |
---|
105 | if(nval!=nwei) return NULL; |
---|
106 | |
---|
107 | // Set the array pointers |
---|
108 | void *temp = PyCObject_AsVoidPtr(disp); |
---|
109 | DispersionModel * dispersion = static_cast<DispersionModel *>(temp); |
---|
110 | dispersion->set_weights(nval, values, weights); |
---|
111 | |
---|
112 | return Py_BuildValue("i",1); |
---|
113 | } |
---|
114 | |
---|
115 | |
---|
116 | |
---|
117 | /** |
---|
118 | * Define empty module |
---|
119 | */ |
---|
120 | static PyMethodDef module_methods[] = { |
---|
121 | {"new_dispersion_model", (PyCFunction)new_dispersion_model , METH_VARARGS, |
---|
122 | "Create a new DispersionModel object"}, |
---|
123 | {"new_gaussian_model", (PyCFunction)new_gaussian_dispersion, METH_VARARGS, |
---|
124 | "Create a new GaussianDispersion object"}, |
---|
125 | {"new_array_model", (PyCFunction)new_array_dispersion , METH_VARARGS, |
---|
126 | "Create a new ArrayDispersion object"}, |
---|
127 | {"set_dispersion_weights",(PyCFunction)set_weights , METH_VARARGS, |
---|
128 | "Create the dispersion weight arrays for an Array Dispersion object"}, |
---|
129 | {NULL} |
---|
130 | }; |
---|
131 | |
---|
132 | |
---|
133 | #ifndef PyMODINIT_FUNC /* declarations for DLL import/export */ |
---|
134 | #define PyMODINIT_FUNC void |
---|
135 | #endif |
---|
136 | PyMODINIT_FUNC |
---|
137 | initc_models(void) |
---|
138 | { |
---|
139 | PyObject* m; |
---|
140 | |
---|
141 | m = Py_InitModule3("c_models", module_methods, |
---|
142 | "C extension module for SANS scattering models."); |
---|
143 | |
---|
144 | addCCylinderModel(m); |
---|
145 | addCCoreShellCylinderModel(m); |
---|
146 | addCCoreShellModel(m); |
---|
147 | addCEllipsoidModel(m); |
---|
148 | addCSphereModel(m); |
---|
149 | addCEllipticalCylinderModel(m); |
---|
150 | addDisperser(m); |
---|
151 | addCGaussian(m); |
---|
152 | addCLorentzian(m); |
---|
153 | |
---|
154 | } |
---|