[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 | |
---|
[af03ddd] | 12 | |
---|
| 13 | void addCCylinderModel(PyObject *module); |
---|
[fbd09fa0] | 14 | void addCBarBellModel(PyObject *module); |
---|
| 15 | void addCCappedCylinderModel(PyObject *module); |
---|
[27a0771] | 16 | void addCTriaxialEllipsoidModel(PyObject *module); |
---|
[8a48713] | 17 | void addCParallelepipedModel(PyObject *module); |
---|
[18f2ca1] | 18 | void addCCSParallelepipedModel(PyObject *module); |
---|
[0f5bc9f] | 19 | void addCSphereModel(PyObject *module); |
---|
[96d19c6] | 20 | void addCSphereSLDModel(PyObject *module); |
---|
[e096270] | 21 | void addCOnionModel(PyObject *module); |
---|
[8c8cb05] | 22 | void addCPearlNecklaceModel(PyObject *module); |
---|
[35aface] | 23 | void addCReflModel(PyObject *module); |
---|
[96656e3] | 24 | void addCReflAdvModel(PyObject *module); |
---|
[c724ccd] | 25 | void addCFuzzySphereModel(PyObject *module); |
---|
[25579e8] | 26 | void addCHardsphereStructure(PyObject *module); |
---|
| 27 | void addCStickyHSStructure(PyObject *module); |
---|
[94a3f8f] | 28 | void addCSCCrystalModel(PyObject *module); |
---|
[d5b6a9d] | 29 | void addCFCCrystalModel(PyObject *module); |
---|
| 30 | void addCBCCrystalModel(PyObject *module); |
---|
[25579e8] | 31 | void addCSquareWellStructure(PyObject *module); |
---|
| 32 | void addCHayterMSAStructure(PyObject *module); |
---|
[0f5bc9f] | 33 | void addCCoreShellModel(PyObject *module); |
---|
[c724ccd] | 34 | void addCCoreFourShellModel(PyObject *module); |
---|
[0f5bc9f] | 35 | void addCCoreShellCylinderModel(PyObject *module); |
---|
| 36 | void addCEllipsoidModel(PyObject *module); |
---|
| 37 | void addCEllipticalCylinderModel(PyObject *module); |
---|
[5068697] | 38 | void addCTriaxialEllipsoidModel(PyObject *module); |
---|
| 39 | void addCFlexibleCylinderModel(PyObject *module); |
---|
[c724ccd] | 40 | void addCFlexCylEllipXModel(PyObject *module); |
---|
[5068697] | 41 | void addCStackedDisksModel(PyObject *module); |
---|
[27a0771] | 42 | void addCLamellarPSModel(PyObject *module); |
---|
| 43 | void addCLamellarPSHGModel(PyObject *module); |
---|
[fbd09fa0] | 44 | void addCLamellarPCrystalModel(PyObject *module); |
---|
[eddff027] | 45 | void addCCoreShellEllipsoidModel(PyObject *module); |
---|
[5eb9154] | 46 | void addCDiamEllipFunc(PyObject *module); |
---|
| 47 | void addCDiamCylFunc(PyObject *module); |
---|
[96d19c6] | 48 | void addCSLDCalFunc(PyObject *module); |
---|
[5eb9154] | 49 | |
---|
[34c3020] | 50 | void addCLamellarModel(PyObject *module); |
---|
| 51 | void addCLamellarFFHGModel(PyObject *module); |
---|
[27fea3f] | 52 | void addCHollowCylinderModel(PyObject *module); |
---|
[3d25331f] | 53 | void addCMultiShellModel(PyObject *module); |
---|
| 54 | void addCVesicleModel(PyObject *module); |
---|
| 55 | void addCBinaryHSModel(PyObject *module); |
---|
[35aface] | 56 | void addCRPAModel(PyObject *module); |
---|
[c724ccd] | 57 | void addCFractalModel(PyObject *module); |
---|
| 58 | void addCPoly_GaussCoil(PyObject *module); |
---|
[27a0771] | 59 | |
---|
[0f5bc9f] | 60 | |
---|
[af03ddd] | 61 | extern "C" { |
---|
[fd080830] | 62 | void addDisperser(PyObject *module); |
---|
[af03ddd] | 63 | } |
---|
[82c11d3] | 64 | void addCLorentzian(PyObject *module); |
---|
[20d91bd] | 65 | void addCSchulz(PyObject *module); |
---|
[101065a] | 66 | void addCGaussian(PyObject *module); |
---|
| 67 | void addCLogNormal(PyObject *module); |
---|
[af03ddd] | 68 | |
---|
| 69 | /** |
---|
| 70 | * Delete a dispersion model object |
---|
| 71 | */ |
---|
| 72 | void del_dispersion_model(void *ptr){ |
---|
[fd080830] | 73 | DispersionModel * disp = static_cast<DispersionModel *>(ptr); |
---|
| 74 | delete disp; |
---|
| 75 | return; |
---|
[af03ddd] | 76 | } |
---|
| 77 | |
---|
| 78 | /** |
---|
| 79 | * Create a dispersion model as a python object |
---|
| 80 | */ |
---|
| 81 | PyObject * new_dispersion_model(PyObject *, PyObject *args) { |
---|
[fd080830] | 82 | DispersionModel *disp = new DispersionModel(); |
---|
| 83 | return PyCObject_FromVoidPtr(disp, del_dispersion_model); |
---|
[af03ddd] | 84 | } |
---|
| 85 | |
---|
| 86 | |
---|
| 87 | /** |
---|
[eba9885] | 88 | * Delete a lognormal dispersion model object |
---|
| 89 | */ |
---|
| 90 | void del_lognormal_dispersion(void *ptr){ |
---|
[fd080830] | 91 | LogNormalDispersion * disp = static_cast<LogNormalDispersion *>(ptr); |
---|
| 92 | delete disp; |
---|
| 93 | return; |
---|
[eba9885] | 94 | } |
---|
| 95 | |
---|
| 96 | /** |
---|
| 97 | * Create a lognormal dispersion model as a python object |
---|
| 98 | */ |
---|
| 99 | PyObject * new_lognormal_dispersion(PyObject *, PyObject *args) { |
---|
[fd080830] | 100 | LogNormalDispersion *disp = new LogNormalDispersion(); |
---|
| 101 | return PyCObject_FromVoidPtr(disp, del_lognormal_dispersion); |
---|
[eba9885] | 102 | } |
---|
| 103 | |
---|
| 104 | /** |
---|
[af03ddd] | 105 | * Delete a gaussian dispersion model object |
---|
| 106 | */ |
---|
| 107 | void del_gaussian_dispersion(void *ptr){ |
---|
[fd080830] | 108 | GaussianDispersion * disp = static_cast<GaussianDispersion *>(ptr); |
---|
| 109 | delete disp; |
---|
| 110 | return; |
---|
[af03ddd] | 111 | } |
---|
| 112 | |
---|
| 113 | /** |
---|
| 114 | * Create a gaussian dispersion model as a python object |
---|
| 115 | */ |
---|
| 116 | PyObject * new_gaussian_dispersion(PyObject *, PyObject *args) { |
---|
[fd080830] | 117 | GaussianDispersion *disp = new GaussianDispersion(); |
---|
| 118 | return PyCObject_FromVoidPtr(disp, del_gaussian_dispersion); |
---|
[af03ddd] | 119 | } |
---|
| 120 | |
---|
[8dc02d8b] | 121 | |
---|
| 122 | /** |
---|
| 123 | * Delete a rectangle dispersion model object |
---|
| 124 | */ |
---|
| 125 | void del_rectangle_dispersion(void *ptr){ |
---|
[fd080830] | 126 | RectangleDispersion * disp = static_cast<RectangleDispersion *>(ptr); |
---|
| 127 | delete disp; |
---|
| 128 | return; |
---|
[8dc02d8b] | 129 | } |
---|
| 130 | |
---|
| 131 | /** |
---|
| 132 | * Create a rectangle dispersion model as a python object |
---|
| 133 | */ |
---|
| 134 | PyObject * new_rectangle_dispersion(PyObject *, PyObject *args) { |
---|
[fd080830] | 135 | RectangleDispersion *disp = new RectangleDispersion(); |
---|
| 136 | return PyCObject_FromVoidPtr(disp, del_rectangle_dispersion); |
---|
[8dc02d8b] | 137 | } |
---|
| 138 | |
---|
| 139 | |
---|
[af03ddd] | 140 | /** |
---|
[eba9885] | 141 | * Delete a schulz dispersion model object |
---|
| 142 | */ |
---|
| 143 | void del_schulz_dispersion(void *ptr){ |
---|
[fd080830] | 144 | SchulzDispersion * disp = static_cast<SchulzDispersion *>(ptr); |
---|
| 145 | delete disp; |
---|
| 146 | return; |
---|
[eba9885] | 147 | } |
---|
| 148 | /** |
---|
| 149 | * Create a schulz dispersion model as a python object |
---|
| 150 | */ |
---|
| 151 | PyObject * new_schulz_dispersion(PyObject *, PyObject *args) { |
---|
[fd080830] | 152 | SchulzDispersion *disp = new SchulzDispersion(); |
---|
| 153 | return PyCObject_FromVoidPtr(disp, del_schulz_dispersion); |
---|
[eba9885] | 154 | } |
---|
| 155 | |
---|
| 156 | |
---|
| 157 | /** |
---|
[af03ddd] | 158 | * Delete an array dispersion model object |
---|
| 159 | */ |
---|
| 160 | void del_array_dispersion(void *ptr){ |
---|
[fd080830] | 161 | ArrayDispersion * disp = static_cast<ArrayDispersion *>(ptr); |
---|
| 162 | delete disp; |
---|
| 163 | return; |
---|
[af03ddd] | 164 | } |
---|
| 165 | |
---|
| 166 | /** |
---|
| 167 | * Create an array dispersion model as a python object |
---|
| 168 | */ |
---|
| 169 | PyObject * new_array_dispersion(PyObject *, PyObject *args) { |
---|
[fd080830] | 170 | ArrayDispersion *disp = new ArrayDispersion(); |
---|
| 171 | return PyCObject_FromVoidPtr(disp, del_array_dispersion); |
---|
[af03ddd] | 172 | } |
---|
| 173 | |
---|
| 174 | #define INVECTOR(obj,buf,len) \ |
---|
| 175 | do { \ |
---|
[fd080830] | 176 | int err = PyObject_AsReadBuffer(obj, (const void **)(&buf), &len); \ |
---|
| 177 | if (err < 0) return NULL; \ |
---|
| 178 | len /= sizeof(*buf); \ |
---|
[af03ddd] | 179 | } while (0) |
---|
| 180 | |
---|
| 181 | /** |
---|
| 182 | * Sets weights from a numpy array |
---|
| 183 | */ |
---|
| 184 | PyObject * set_weights(PyObject *, PyObject *args) { |
---|
[fd080830] | 185 | PyObject *val_obj; |
---|
| 186 | PyObject *wei_obj; |
---|
| 187 | PyObject *disp; |
---|
| 188 | Py_ssize_t nval; |
---|
| 189 | Py_ssize_t nwei; |
---|
| 190 | double *values; |
---|
| 191 | double *weights; |
---|
| 192 | //int i; |
---|
| 193 | |
---|
| 194 | if (!PyArg_ParseTuple(args, "OOO", &disp, &val_obj, &wei_obj)) return NULL; |
---|
| 195 | INVECTOR(val_obj, values, nval); |
---|
| 196 | INVECTOR(wei_obj, weights, nwei); |
---|
| 197 | |
---|
| 198 | // Sanity check |
---|
| 199 | if(nval!=nwei) return NULL; |
---|
| 200 | |
---|
| 201 | // Set the array pointers |
---|
| 202 | void *temp = PyCObject_AsVoidPtr(disp); |
---|
| 203 | DispersionModel * dispersion = static_cast<DispersionModel *>(temp); |
---|
| 204 | dispersion->set_weights(nval, values, weights); |
---|
| 205 | |
---|
| 206 | return Py_BuildValue("i",1); |
---|
[af03ddd] | 207 | } |
---|
| 208 | |
---|
| 209 | |
---|
| 210 | |
---|
| 211 | /** |
---|
| 212 | * Define empty module |
---|
| 213 | */ |
---|
| 214 | static PyMethodDef module_methods[] = { |
---|
[fd080830] | 215 | {"new_dispersion_model", (PyCFunction)new_dispersion_model , METH_VARARGS, |
---|
| 216 | "Create a new DispersionModel object"}, |
---|
| 217 | {"new_gaussian_model", (PyCFunction)new_gaussian_dispersion, METH_VARARGS, |
---|
| 218 | "Create a new GaussianDispersion object"}, |
---|
| 219 | {"new_rectangle_model", (PyCFunction)new_rectangle_dispersion, METH_VARARGS, |
---|
| 220 | "Create a new RectangleDispersion object"}, |
---|
| 221 | {"new_lognormal_model", (PyCFunction)new_lognormal_dispersion, METH_VARARGS, |
---|
| 222 | "Create a new LogNormalDispersion object"}, |
---|
| 223 | {"new_schulz_model", (PyCFunction)new_schulz_dispersion, METH_VARARGS, |
---|
| 224 | "Create a new SchulzDispersion object"}, |
---|
| 225 | {"new_array_model", (PyCFunction)new_array_dispersion , METH_VARARGS, |
---|
| 226 | "Create a new ArrayDispersion object"}, |
---|
| 227 | {"set_dispersion_weights",(PyCFunction)set_weights , METH_VARARGS, |
---|
| 228 | "Create the dispersion weight arrays for an Array Dispersion object"}, |
---|
| 229 | {NULL} |
---|
[af03ddd] | 230 | }; |
---|
| 231 | |
---|
| 232 | |
---|
| 233 | #ifndef PyMODINIT_FUNC /* declarations for DLL import/export */ |
---|
| 234 | #define PyMODINIT_FUNC void |
---|
| 235 | #endif |
---|
| 236 | PyMODINIT_FUNC |
---|
| 237 | initc_models(void) |
---|
| 238 | { |
---|
[fd080830] | 239 | PyObject* m; |
---|
| 240 | |
---|
| 241 | m = Py_InitModule3("c_models", module_methods, |
---|
| 242 | "C extension module for SANS scattering models."); |
---|
| 243 | import_array(); |
---|
| 244 | |
---|
| 245 | addCCylinderModel(m); |
---|
| 246 | addCBarBellModel(m); |
---|
| 247 | addCCappedCylinderModel(m); |
---|
| 248 | addCParallelepipedModel(m); |
---|
| 249 | addCCSParallelepipedModel(m); |
---|
| 250 | addCCoreShellCylinderModel(m); |
---|
| 251 | addCCoreShellModel(m); |
---|
| 252 | addCCoreFourShellModel(m); |
---|
| 253 | addCEllipsoidModel(m); |
---|
| 254 | addCSphereModel(m); |
---|
| 255 | addCSphereSLDModel(m); |
---|
| 256 | addCOnionModel(m); |
---|
| 257 | addCReflModel(m); |
---|
| 258 | addCReflAdvModel(m); |
---|
| 259 | addCFuzzySphereModel(m); |
---|
| 260 | addCHardsphereStructure(m); |
---|
| 261 | addCStickyHSStructure(m); |
---|
| 262 | addCSCCrystalModel(m); |
---|
| 263 | addCFCCrystalModel(m); |
---|
| 264 | addCBCCrystalModel(m); |
---|
| 265 | addCSquareWellStructure(m); |
---|
| 266 | addCHayterMSAStructure(m); |
---|
| 267 | addCEllipticalCylinderModel(m); |
---|
| 268 | addCTriaxialEllipsoidModel(m); |
---|
| 269 | addCFlexibleCylinderModel(m); |
---|
| 270 | addCFlexCylEllipXModel(m); |
---|
| 271 | addCStackedDisksModel(m); |
---|
| 272 | addCLamellarPSModel(m); |
---|
| 273 | addCLamellarPSHGModel(m); |
---|
| 274 | addCLamellarPCrystalModel(m); |
---|
| 275 | addCCoreShellEllipsoidModel(m); |
---|
| 276 | addCDiamEllipFunc(m); |
---|
| 277 | addCDiamCylFunc(m); |
---|
| 278 | addCSLDCalFunc(m); |
---|
| 279 | addCPearlNecklaceModel(m); |
---|
| 280 | addCLamellarModel(m); |
---|
| 281 | addCLamellarFFHGModel(m); |
---|
| 282 | addCHollowCylinderModel(m); |
---|
| 283 | addCMultiShellModel(m); |
---|
| 284 | addCBinaryHSModel(m); |
---|
| 285 | addDisperser(m); |
---|
| 286 | addCGaussian(m); |
---|
| 287 | addCSchulz(m); |
---|
| 288 | addCLogNormal(m); |
---|
| 289 | addCLorentzian(m); |
---|
| 290 | addCVesicleModel(m); |
---|
| 291 | addCPoly_GaussCoil(m); |
---|
| 292 | addCRPAModel(m); |
---|
| 293 | addCFractalModel(m); |
---|
[af03ddd] | 294 | } |
---|