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