1 | /** c_models |
---|
2 | * |
---|
3 | * Module containing all SAS model extensions |
---|
4 | * |
---|
5 | * @author M.Doucet / UTK |
---|
6 | */ |
---|
7 | #include <Python.h> |
---|
8 | #include <parameters.hh> |
---|
9 | #define PY_ARRAY_UNIQUE_SYMBOL PyArray_API_sas |
---|
10 | #include "arrayobject.h" |
---|
11 | |
---|
12 | // [TAG_1] |
---|
13 | |
---|
14 | |
---|
15 | |
---|
16 | extern "C" { |
---|
17 | void addDisperser(PyObject *module); |
---|
18 | } |
---|
19 | |
---|
20 | /** |
---|
21 | * Delete a dispersion model object |
---|
22 | */ |
---|
23 | void del_dispersion_model(void *ptr){ |
---|
24 | DispersionModel * disp = static_cast<DispersionModel *>(ptr); |
---|
25 | delete disp; |
---|
26 | return; |
---|
27 | } |
---|
28 | |
---|
29 | /** |
---|
30 | * Create a dispersion model as a python object |
---|
31 | */ |
---|
32 | PyObject * new_dispersion_model(PyObject *, PyObject *args) { |
---|
33 | DispersionModel *disp = new DispersionModel(); |
---|
34 | return PyCObject_FromVoidPtr(disp, del_dispersion_model); |
---|
35 | } |
---|
36 | |
---|
37 | |
---|
38 | /** |
---|
39 | * Delete a lognormal dispersion model object |
---|
40 | */ |
---|
41 | void del_lognormal_dispersion(void *ptr){ |
---|
42 | LogNormalDispersion * disp = static_cast<LogNormalDispersion *>(ptr); |
---|
43 | delete disp; |
---|
44 | return; |
---|
45 | } |
---|
46 | |
---|
47 | /** |
---|
48 | * Create a lognormal dispersion model as a python object |
---|
49 | */ |
---|
50 | PyObject * new_lognormal_dispersion(PyObject *, PyObject *args) { |
---|
51 | LogNormalDispersion *disp = new LogNormalDispersion(); |
---|
52 | return PyCObject_FromVoidPtr(disp, del_lognormal_dispersion); |
---|
53 | } |
---|
54 | |
---|
55 | /** |
---|
56 | * Delete a gaussian dispersion model object |
---|
57 | */ |
---|
58 | void del_gaussian_dispersion(void *ptr){ |
---|
59 | GaussianDispersion * disp = static_cast<GaussianDispersion *>(ptr); |
---|
60 | delete disp; |
---|
61 | return; |
---|
62 | } |
---|
63 | |
---|
64 | /** |
---|
65 | * Create a gaussian dispersion model as a python object |
---|
66 | */ |
---|
67 | PyObject * new_gaussian_dispersion(PyObject *, PyObject *args) { |
---|
68 | GaussianDispersion *disp = new GaussianDispersion(); |
---|
69 | return PyCObject_FromVoidPtr(disp, del_gaussian_dispersion); |
---|
70 | } |
---|
71 | |
---|
72 | |
---|
73 | /** |
---|
74 | * Delete a rectangle dispersion model object |
---|
75 | */ |
---|
76 | void del_rectangle_dispersion(void *ptr){ |
---|
77 | RectangleDispersion * disp = static_cast<RectangleDispersion *>(ptr); |
---|
78 | delete disp; |
---|
79 | return; |
---|
80 | } |
---|
81 | |
---|
82 | /** |
---|
83 | * Create a rectangle dispersion model as a python object |
---|
84 | */ |
---|
85 | PyObject * new_rectangle_dispersion(PyObject *, PyObject *args) { |
---|
86 | RectangleDispersion *disp = new RectangleDispersion(); |
---|
87 | return PyCObject_FromVoidPtr(disp, del_rectangle_dispersion); |
---|
88 | } |
---|
89 | |
---|
90 | |
---|
91 | /** |
---|
92 | * Delete a schulz dispersion model object |
---|
93 | */ |
---|
94 | void del_schulz_dispersion(void *ptr){ |
---|
95 | SchulzDispersion * disp = static_cast<SchulzDispersion *>(ptr); |
---|
96 | delete disp; |
---|
97 | return; |
---|
98 | } |
---|
99 | /** |
---|
100 | * Create a schulz dispersion model as a python object |
---|
101 | */ |
---|
102 | PyObject * new_schulz_dispersion(PyObject *, PyObject *args) { |
---|
103 | SchulzDispersion *disp = new SchulzDispersion(); |
---|
104 | return PyCObject_FromVoidPtr(disp, del_schulz_dispersion); |
---|
105 | } |
---|
106 | |
---|
107 | |
---|
108 | /** |
---|
109 | * Delete an array dispersion model object |
---|
110 | */ |
---|
111 | void del_array_dispersion(void *ptr){ |
---|
112 | ArrayDispersion * disp = static_cast<ArrayDispersion *>(ptr); |
---|
113 | delete disp; |
---|
114 | return; |
---|
115 | } |
---|
116 | |
---|
117 | /** |
---|
118 | * Create an array dispersion model as a python object |
---|
119 | */ |
---|
120 | PyObject * new_array_dispersion(PyObject *, PyObject *args) { |
---|
121 | ArrayDispersion *disp = new ArrayDispersion(); |
---|
122 | return PyCObject_FromVoidPtr(disp, del_array_dispersion); |
---|
123 | } |
---|
124 | |
---|
125 | #define INVECTOR(obj,buf,len) \ |
---|
126 | do { \ |
---|
127 | int err = PyObject_AsReadBuffer(obj, (const void **)(&buf), &len); \ |
---|
128 | if (err < 0) return NULL; \ |
---|
129 | len /= sizeof(*buf); \ |
---|
130 | } while (0) |
---|
131 | |
---|
132 | /** |
---|
133 | * Sets weights from a numpy array |
---|
134 | */ |
---|
135 | PyObject * set_weights(PyObject *, PyObject *args) { |
---|
136 | PyObject *val_obj; |
---|
137 | PyObject *wei_obj; |
---|
138 | PyObject *disp; |
---|
139 | Py_ssize_t nval; |
---|
140 | Py_ssize_t nwei; |
---|
141 | double *values; |
---|
142 | double *weights; |
---|
143 | //int i; |
---|
144 | |
---|
145 | if (!PyArg_ParseTuple(args, "OOO", &disp, &val_obj, &wei_obj)) return NULL; |
---|
146 | INVECTOR(val_obj, values, nval); |
---|
147 | INVECTOR(wei_obj, weights, nwei); |
---|
148 | |
---|
149 | // Sanity check |
---|
150 | if(nval!=nwei) return NULL; |
---|
151 | |
---|
152 | // Set the array pointers |
---|
153 | void *temp = PyCObject_AsVoidPtr(disp); |
---|
154 | DispersionModel * dispersion = static_cast<DispersionModel *>(temp); |
---|
155 | dispersion->set_weights(nval, values, weights); |
---|
156 | |
---|
157 | return Py_BuildValue("i",1); |
---|
158 | } |
---|
159 | |
---|
160 | |
---|
161 | |
---|
162 | /** |
---|
163 | * Define empty module |
---|
164 | */ |
---|
165 | static PyMethodDef module_methods[] = { |
---|
166 | {"new_dispersion_model", (PyCFunction)new_dispersion_model , METH_VARARGS, |
---|
167 | "Create a new DispersionModel object"}, |
---|
168 | {"new_gaussian_model", (PyCFunction)new_gaussian_dispersion, METH_VARARGS, |
---|
169 | "Create a new GaussianDispersion object"}, |
---|
170 | {"new_rectangle_model", (PyCFunction)new_rectangle_dispersion, METH_VARARGS, |
---|
171 | "Create a new RectangleDispersion object"}, |
---|
172 | {"new_lognormal_model", (PyCFunction)new_lognormal_dispersion, METH_VARARGS, |
---|
173 | "Create a new LogNormalDispersion object"}, |
---|
174 | {"new_schulz_model", (PyCFunction)new_schulz_dispersion, METH_VARARGS, |
---|
175 | "Create a new SchulzDispersion object"}, |
---|
176 | {"new_array_model", (PyCFunction)new_array_dispersion , METH_VARARGS, |
---|
177 | "Create a new ArrayDispersion object"}, |
---|
178 | {"set_dispersion_weights",(PyCFunction)set_weights , METH_VARARGS, |
---|
179 | "Create the dispersion weight arrays for an Array Dispersion object"}, |
---|
180 | {NULL} |
---|
181 | }; |
---|
182 | |
---|
183 | |
---|
184 | #ifndef PyMODINIT_FUNC /* declarations for DLL import/export */ |
---|
185 | #define PyMODINIT_FUNC void |
---|
186 | #endif |
---|
187 | PyMODINIT_FUNC |
---|
188 | initc_models(void) |
---|
189 | { |
---|
190 | PyObject* m; |
---|
191 | |
---|
192 | m = Py_InitModule3("c_models", module_methods, |
---|
193 | "C extension module for SAS scattering models."); |
---|
194 | import_array(); |
---|
195 | |
---|
196 | // [TAG_2] |
---|
197 | |
---|
198 | } |
---|