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