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