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