[af03ddd] | 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> |
---|
[812b901] | 9 | #define PY_ARRAY_UNIQUE_SYMBOL PyArray_API_sans |
---|
| 10 | #include "arrayobject.h" |
---|
| 11 | |
---|
[0a9686d] | 12 | // [TAG_1] |
---|
[af03ddd] | 13 | |
---|
[27a0771] | 14 | |
---|
[0f5bc9f] | 15 | |
---|
[af03ddd] | 16 | extern "C" { |
---|
[fd080830] | 17 | void addDisperser(PyObject *module); |
---|
[af03ddd] | 18 | } |
---|
| 19 | |
---|
| 20 | /** |
---|
| 21 | * Delete a dispersion model object |
---|
| 22 | */ |
---|
| 23 | void del_dispersion_model(void *ptr){ |
---|
[fd080830] | 24 | DispersionModel * disp = static_cast<DispersionModel *>(ptr); |
---|
| 25 | delete disp; |
---|
| 26 | return; |
---|
[af03ddd] | 27 | } |
---|
| 28 | |
---|
| 29 | /** |
---|
| 30 | * Create a dispersion model as a python object |
---|
| 31 | */ |
---|
| 32 | PyObject * new_dispersion_model(PyObject *, PyObject *args) { |
---|
[fd080830] | 33 | DispersionModel *disp = new DispersionModel(); |
---|
| 34 | return PyCObject_FromVoidPtr(disp, del_dispersion_model); |
---|
[af03ddd] | 35 | } |
---|
| 36 | |
---|
| 37 | |
---|
| 38 | /** |
---|
[eba9885] | 39 | * Delete a lognormal dispersion model object |
---|
| 40 | */ |
---|
| 41 | void del_lognormal_dispersion(void *ptr){ |
---|
[fd080830] | 42 | LogNormalDispersion * disp = static_cast<LogNormalDispersion *>(ptr); |
---|
| 43 | delete disp; |
---|
| 44 | return; |
---|
[eba9885] | 45 | } |
---|
| 46 | |
---|
| 47 | /** |
---|
| 48 | * Create a lognormal dispersion model as a python object |
---|
| 49 | */ |
---|
| 50 | PyObject * new_lognormal_dispersion(PyObject *, PyObject *args) { |
---|
[fd080830] | 51 | LogNormalDispersion *disp = new LogNormalDispersion(); |
---|
| 52 | return PyCObject_FromVoidPtr(disp, del_lognormal_dispersion); |
---|
[eba9885] | 53 | } |
---|
| 54 | |
---|
| 55 | /** |
---|
[af03ddd] | 56 | * Delete a gaussian dispersion model object |
---|
| 57 | */ |
---|
| 58 | void del_gaussian_dispersion(void *ptr){ |
---|
[fd080830] | 59 | GaussianDispersion * disp = static_cast<GaussianDispersion *>(ptr); |
---|
| 60 | delete disp; |
---|
| 61 | return; |
---|
[af03ddd] | 62 | } |
---|
| 63 | |
---|
| 64 | /** |
---|
| 65 | * Create a gaussian dispersion model as a python object |
---|
| 66 | */ |
---|
| 67 | PyObject * new_gaussian_dispersion(PyObject *, PyObject *args) { |
---|
[fd080830] | 68 | GaussianDispersion *disp = new GaussianDispersion(); |
---|
| 69 | return PyCObject_FromVoidPtr(disp, del_gaussian_dispersion); |
---|
[af03ddd] | 70 | } |
---|
| 71 | |
---|
[8dc02d8b] | 72 | |
---|
| 73 | /** |
---|
| 74 | * Delete a rectangle dispersion model object |
---|
| 75 | */ |
---|
| 76 | void del_rectangle_dispersion(void *ptr){ |
---|
[fd080830] | 77 | RectangleDispersion * disp = static_cast<RectangleDispersion *>(ptr); |
---|
| 78 | delete disp; |
---|
| 79 | return; |
---|
[8dc02d8b] | 80 | } |
---|
| 81 | |
---|
| 82 | /** |
---|
| 83 | * Create a rectangle dispersion model as a python object |
---|
| 84 | */ |
---|
| 85 | PyObject * new_rectangle_dispersion(PyObject *, PyObject *args) { |
---|
[fd080830] | 86 | RectangleDispersion *disp = new RectangleDispersion(); |
---|
| 87 | return PyCObject_FromVoidPtr(disp, del_rectangle_dispersion); |
---|
[8dc02d8b] | 88 | } |
---|
| 89 | |
---|
| 90 | |
---|
[af03ddd] | 91 | /** |
---|
[eba9885] | 92 | * Delete a schulz dispersion model object |
---|
| 93 | */ |
---|
| 94 | void del_schulz_dispersion(void *ptr){ |
---|
[fd080830] | 95 | SchulzDispersion * disp = static_cast<SchulzDispersion *>(ptr); |
---|
| 96 | delete disp; |
---|
| 97 | return; |
---|
[eba9885] | 98 | } |
---|
| 99 | /** |
---|
| 100 | * Create a schulz dispersion model as a python object |
---|
| 101 | */ |
---|
| 102 | PyObject * new_schulz_dispersion(PyObject *, PyObject *args) { |
---|
[fd080830] | 103 | SchulzDispersion *disp = new SchulzDispersion(); |
---|
| 104 | return PyCObject_FromVoidPtr(disp, del_schulz_dispersion); |
---|
[eba9885] | 105 | } |
---|
| 106 | |
---|
| 107 | |
---|
| 108 | /** |
---|
[af03ddd] | 109 | * Delete an array dispersion model object |
---|
| 110 | */ |
---|
| 111 | void del_array_dispersion(void *ptr){ |
---|
[fd080830] | 112 | ArrayDispersion * disp = static_cast<ArrayDispersion *>(ptr); |
---|
| 113 | delete disp; |
---|
| 114 | return; |
---|
[af03ddd] | 115 | } |
---|
| 116 | |
---|
| 117 | /** |
---|
| 118 | * Create an array dispersion model as a python object |
---|
| 119 | */ |
---|
| 120 | PyObject * new_array_dispersion(PyObject *, PyObject *args) { |
---|
[fd080830] | 121 | ArrayDispersion *disp = new ArrayDispersion(); |
---|
| 122 | return PyCObject_FromVoidPtr(disp, del_array_dispersion); |
---|
[af03ddd] | 123 | } |
---|
| 124 | |
---|
| 125 | #define INVECTOR(obj,buf,len) \ |
---|
| 126 | do { \ |
---|
[fd080830] | 127 | int err = PyObject_AsReadBuffer(obj, (const void **)(&buf), &len); \ |
---|
| 128 | if (err < 0) return NULL; \ |
---|
| 129 | len /= sizeof(*buf); \ |
---|
[af03ddd] | 130 | } while (0) |
---|
| 131 | |
---|
| 132 | /** |
---|
| 133 | * Sets weights from a numpy array |
---|
| 134 | */ |
---|
| 135 | PyObject * set_weights(PyObject *, PyObject *args) { |
---|
[fd080830] | 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); |
---|
[af03ddd] | 158 | } |
---|
| 159 | |
---|
| 160 | |
---|
| 161 | |
---|
| 162 | /** |
---|
| 163 | * Define empty module |
---|
| 164 | */ |
---|
| 165 | static PyMethodDef module_methods[] = { |
---|
[fd080830] | 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} |
---|
[af03ddd] | 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 | { |
---|
[fd080830] | 190 | PyObject* m; |
---|
| 191 | |
---|
| 192 | m = Py_InitModule3("c_models", module_methods, |
---|
| 193 | "C extension module for SANS scattering models."); |
---|
| 194 | import_array(); |
---|
| 195 | |
---|
[0a9686d] | 196 | // [TAG_2] |
---|
| 197 | |
---|
[af03ddd] | 198 | } |
---|