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 | #define PY_ARRAY_UNIQUE_SYMBOL PyArray_API_sans |
---|
10 | #include "arrayobject.h" |
---|
11 | |
---|
12 | |
---|
13 | void addCCylinderModel(PyObject *module); |
---|
14 | void addCTriaxialEllipsoidModel(PyObject *module); |
---|
15 | void addCParallelepipedModel(PyObject *module); |
---|
16 | void addCSphereModel(PyObject *module); |
---|
17 | void addCHardsphereStructure(PyObject *module); |
---|
18 | void addCStickyHSStructure(PyObject *module); |
---|
19 | void addCSquareWellStructure(PyObject *module); |
---|
20 | void addCHayterMSAStructure(PyObject *module); |
---|
21 | void addCDiamEllipFunc(PyObject *module); |
---|
22 | void addCDiamCylFunc(PyObject *module); |
---|
23 | void addCCoreShellModel(PyObject *module); |
---|
24 | void addCCoreShellCylinderModel(PyObject *module); |
---|
25 | void addCEllipsoidModel(PyObject *module); |
---|
26 | void addCEllipticalCylinderModel(PyObject *module); |
---|
27 | void addCTriaxialEllipsoidModel(PyObject *module); |
---|
28 | void addCFlexibleCylinderModel(PyObject *module); |
---|
29 | void addCStackedDisksModel(PyObject *module); |
---|
30 | void addCLamellarPSModel(PyObject *module); |
---|
31 | void addCLamellarPSHGModel(PyObject *module); |
---|
32 | void addCCoreShellEllipsoidModel(PyObject *module); |
---|
33 | void addCOblateModel(PyObject *module); |
---|
34 | void addCProlateModel(PyObject *module); |
---|
35 | void addCLamellarModel(PyObject *module); |
---|
36 | void addCLamellarFFHGModel(PyObject *module); |
---|
37 | void addCHollowCylinderModel(PyObject *module); |
---|
38 | void addCMultiShellModel(PyObject *module); |
---|
39 | void addCVesicleModel(PyObject *module); |
---|
40 | void addCBinaryHSModel(PyObject *module); |
---|
41 | |
---|
42 | |
---|
43 | extern "C" { |
---|
44 | //void addCCoreShellCylinderModel(PyObject *module); |
---|
45 | //void addCCoreShellModel(PyObject *module); |
---|
46 | //void addCEllipsoidModel(PyObject *module); |
---|
47 | //void addCEllipticalCylinderModel(PyObject *module); |
---|
48 | void addDisperser(PyObject *module); |
---|
49 | void addCGaussian(PyObject *module); |
---|
50 | void addCLorentzian(PyObject *module); |
---|
51 | void addCLogNormal(PyObject *module); |
---|
52 | void addCSchulz(PyObject *module); |
---|
53 | } |
---|
54 | |
---|
55 | /** |
---|
56 | * Delete a dispersion model object |
---|
57 | */ |
---|
58 | void del_dispersion_model(void *ptr){ |
---|
59 | DispersionModel * disp = static_cast<DispersionModel *>(ptr); |
---|
60 | delete disp; |
---|
61 | return; |
---|
62 | } |
---|
63 | |
---|
64 | /** |
---|
65 | * Create a dispersion model as a python object |
---|
66 | */ |
---|
67 | PyObject * new_dispersion_model(PyObject *, PyObject *args) { |
---|
68 | DispersionModel *disp = new DispersionModel(); |
---|
69 | return PyCObject_FromVoidPtr(disp, del_dispersion_model); |
---|
70 | } |
---|
71 | |
---|
72 | |
---|
73 | /** |
---|
74 | * Delete a lognormal dispersion model object |
---|
75 | */ |
---|
76 | void del_lognormal_dispersion(void *ptr){ |
---|
77 | LogNormalDispersion * disp = static_cast<LogNormalDispersion *>(ptr); |
---|
78 | delete disp; |
---|
79 | return; |
---|
80 | } |
---|
81 | |
---|
82 | /** |
---|
83 | * Create a lognormal dispersion model as a python object |
---|
84 | */ |
---|
85 | PyObject * new_lognormal_dispersion(PyObject *, PyObject *args) { |
---|
86 | LogNormalDispersion *disp = new LogNormalDispersion(); |
---|
87 | return PyCObject_FromVoidPtr(disp, del_lognormal_dispersion); |
---|
88 | } |
---|
89 | |
---|
90 | /** |
---|
91 | * Delete a gaussian dispersion model object |
---|
92 | */ |
---|
93 | void del_gaussian_dispersion(void *ptr){ |
---|
94 | GaussianDispersion * disp = static_cast<GaussianDispersion *>(ptr); |
---|
95 | delete disp; |
---|
96 | return; |
---|
97 | } |
---|
98 | |
---|
99 | /** |
---|
100 | * Create a gaussian dispersion model as a python object |
---|
101 | */ |
---|
102 | PyObject * new_gaussian_dispersion(PyObject *, PyObject *args) { |
---|
103 | GaussianDispersion *disp = new GaussianDispersion(); |
---|
104 | return PyCObject_FromVoidPtr(disp, del_gaussian_dispersion); |
---|
105 | } |
---|
106 | |
---|
107 | /** |
---|
108 | * Delete a schulz dispersion model object |
---|
109 | */ |
---|
110 | void del_schulz_dispersion(void *ptr){ |
---|
111 | SchulzDispersion * disp = static_cast<SchulzDispersion *>(ptr); |
---|
112 | delete disp; |
---|
113 | return; |
---|
114 | } |
---|
115 | /** |
---|
116 | * Create a schulz dispersion model as a python object |
---|
117 | */ |
---|
118 | PyObject * new_schulz_dispersion(PyObject *, PyObject *args) { |
---|
119 | SchulzDispersion *disp = new SchulzDispersion(); |
---|
120 | return PyCObject_FromVoidPtr(disp, del_schulz_dispersion); |
---|
121 | } |
---|
122 | |
---|
123 | |
---|
124 | /** |
---|
125 | * Delete an array dispersion model object |
---|
126 | */ |
---|
127 | void del_array_dispersion(void *ptr){ |
---|
128 | ArrayDispersion * disp = static_cast<ArrayDispersion *>(ptr); |
---|
129 | delete disp; |
---|
130 | return; |
---|
131 | } |
---|
132 | |
---|
133 | /** |
---|
134 | * Create an array dispersion model as a python object |
---|
135 | */ |
---|
136 | PyObject * new_array_dispersion(PyObject *, PyObject *args) { |
---|
137 | ArrayDispersion *disp = new ArrayDispersion(); |
---|
138 | return PyCObject_FromVoidPtr(disp, del_array_dispersion); |
---|
139 | } |
---|
140 | |
---|
141 | #define INVECTOR(obj,buf,len) \ |
---|
142 | do { \ |
---|
143 | int err = PyObject_AsReadBuffer(obj, (const void **)(&buf), &len); \ |
---|
144 | if (err < 0) return NULL; \ |
---|
145 | len /= sizeof(*buf); \ |
---|
146 | } while (0) |
---|
147 | |
---|
148 | /** |
---|
149 | * Sets weights from a numpy array |
---|
150 | */ |
---|
151 | PyObject * set_weights(PyObject *, PyObject *args) { |
---|
152 | PyObject *val_obj; |
---|
153 | PyObject *wei_obj; |
---|
154 | PyObject *disp; |
---|
155 | Py_ssize_t nval; |
---|
156 | Py_ssize_t nwei; |
---|
157 | double *values; |
---|
158 | double *weights; |
---|
159 | //int i; |
---|
160 | |
---|
161 | if (!PyArg_ParseTuple(args, "OOO", &disp, &val_obj, &wei_obj)) return NULL; |
---|
162 | INVECTOR(val_obj, values, nval); |
---|
163 | INVECTOR(wei_obj, weights, nwei); |
---|
164 | |
---|
165 | // Sanity check |
---|
166 | if(nval!=nwei) return NULL; |
---|
167 | |
---|
168 | // Set the array pointers |
---|
169 | void *temp = PyCObject_AsVoidPtr(disp); |
---|
170 | DispersionModel * dispersion = static_cast<DispersionModel *>(temp); |
---|
171 | dispersion->set_weights(nval, values, weights); |
---|
172 | |
---|
173 | return Py_BuildValue("i",1); |
---|
174 | } |
---|
175 | |
---|
176 | |
---|
177 | |
---|
178 | /** |
---|
179 | * Define empty module |
---|
180 | */ |
---|
181 | static PyMethodDef module_methods[] = { |
---|
182 | {"new_dispersion_model", (PyCFunction)new_dispersion_model , METH_VARARGS, |
---|
183 | "Create a new DispersionModel object"}, |
---|
184 | {"new_gaussian_model", (PyCFunction)new_gaussian_dispersion, METH_VARARGS, |
---|
185 | "Create a new GaussianDispersion object"}, |
---|
186 | {"new_lognormal_model", (PyCFunction)new_lognormal_dispersion, METH_VARARGS, |
---|
187 | "Create a new LogNormalDispersion object"}, |
---|
188 | {"new_schulz_model", (PyCFunction)new_schulz_dispersion, METH_VARARGS, |
---|
189 | "Create a new SchulzDispersion object"}, |
---|
190 | {"new_array_model", (PyCFunction)new_array_dispersion , METH_VARARGS, |
---|
191 | "Create a new ArrayDispersion object"}, |
---|
192 | {"set_dispersion_weights",(PyCFunction)set_weights , METH_VARARGS, |
---|
193 | "Create the dispersion weight arrays for an Array Dispersion object"}, |
---|
194 | {NULL} |
---|
195 | }; |
---|
196 | |
---|
197 | |
---|
198 | #ifndef PyMODINIT_FUNC /* declarations for DLL import/export */ |
---|
199 | #define PyMODINIT_FUNC void |
---|
200 | #endif |
---|
201 | PyMODINIT_FUNC |
---|
202 | initc_models(void) |
---|
203 | { |
---|
204 | PyObject* m; |
---|
205 | |
---|
206 | m = Py_InitModule3("c_models", module_methods, |
---|
207 | "C extension module for SANS scattering models."); |
---|
208 | import_array(); |
---|
209 | |
---|
210 | addCCylinderModel(m); |
---|
211 | addCParallelepipedModel(m); |
---|
212 | addCCoreShellCylinderModel(m); |
---|
213 | addCCoreShellModel(m); |
---|
214 | addCEllipsoidModel(m); |
---|
215 | addCSphereModel(m); |
---|
216 | addCHardsphereStructure(m); |
---|
217 | addCStickyHSStructure(m); |
---|
218 | addCSquareWellStructure(m); |
---|
219 | addCHayterMSAStructure(m); |
---|
220 | addCDiamEllipFunc(m); |
---|
221 | addCDiamCylFunc(m); |
---|
222 | addCEllipticalCylinderModel(m); |
---|
223 | addCTriaxialEllipsoidModel(m); |
---|
224 | addCFlexibleCylinderModel(m); |
---|
225 | addCStackedDisksModel(m); |
---|
226 | addCLamellarPSModel(m); |
---|
227 | addCLamellarPSHGModel(m); |
---|
228 | addCCoreShellEllipsoidModel(m); |
---|
229 | addCOblateModel(m); |
---|
230 | addCProlateModel(m); |
---|
231 | addCLamellarModel(m); |
---|
232 | addCLamellarFFHGModel(m); |
---|
233 | addCHollowCylinderModel(m); |
---|
234 | addCMultiShellModel(m); |
---|
235 | addCBinaryHSModel(m); |
---|
236 | addDisperser(m); |
---|
237 | addCGaussian(m); |
---|
238 | addCSchulz(m); |
---|
239 | addCLogNormal(m); |
---|
240 | addCLorentzian(m); |
---|
241 | addCVesicleModel(m); |
---|
242 | |
---|
243 | } |
---|