[e0a8a3c] | 1 | /** CBinaryHSModel |
---|
| 2 | * |
---|
| 3 | * C extension |
---|
| 4 | * |
---|
| 5 | * WARNING: THIS FILE WAS GENERATED BY WRAPPERGENERATOR.PY |
---|
| 6 | * DO NOT MODIFY THIS FILE, MODIFY binaryHs.h |
---|
| 7 | * AND RE-RUN THE GENERATOR SCRIPT |
---|
| 8 | * |
---|
| 9 | * @author M.Doucet / UTK |
---|
| 10 | */ |
---|
| 11 | |
---|
| 12 | #include <Python.h> |
---|
| 13 | #include "structmember.h" |
---|
| 14 | #include <stdio.h> |
---|
| 15 | #include <stdlib.h> |
---|
| 16 | #include <math.h> |
---|
| 17 | #include <time.h> |
---|
| 18 | |
---|
| 19 | #include "binaryHs.h" |
---|
| 20 | |
---|
| 21 | /// Error object for raised exceptions |
---|
| 22 | static PyObject * CBinaryHSModelError = NULL; |
---|
| 23 | |
---|
| 24 | |
---|
| 25 | // Class definition |
---|
| 26 | typedef struct { |
---|
| 27 | PyObject_HEAD |
---|
| 28 | /// Parameters |
---|
| 29 | PyObject * params; |
---|
| 30 | /// Log for unit testing |
---|
| 31 | PyObject * log; |
---|
| 32 | /// Model parameters |
---|
| 33 | BinaryHSParameters model_pars; |
---|
| 34 | } CBinaryHSModel; |
---|
| 35 | |
---|
| 36 | |
---|
| 37 | static void |
---|
| 38 | CBinaryHSModel_dealloc(CBinaryHSModel* self) |
---|
| 39 | { |
---|
| 40 | self->ob_type->tp_free((PyObject*)self); |
---|
| 41 | |
---|
| 42 | |
---|
| 43 | } |
---|
| 44 | |
---|
| 45 | static PyObject * |
---|
| 46 | CBinaryHSModel_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
---|
| 47 | { |
---|
| 48 | CBinaryHSModel *self; |
---|
| 49 | |
---|
| 50 | self = (CBinaryHSModel *)type->tp_alloc(type, 0); |
---|
| 51 | |
---|
| 52 | return (PyObject *)self; |
---|
| 53 | } |
---|
| 54 | |
---|
| 55 | static int |
---|
| 56 | CBinaryHSModel_init(CBinaryHSModel *self, PyObject *args, PyObject *kwds) |
---|
| 57 | { |
---|
| 58 | if (self != NULL) { |
---|
| 59 | |
---|
| 60 | // Create parameters |
---|
| 61 | self->params = PyDict_New(); |
---|
| 62 | |
---|
| 63 | // Initialize parameter dictionary |
---|
| 64 | PyDict_SetItemString(self->params,"vol_frac_ls",Py_BuildValue("d",0.100000)); |
---|
| 65 | PyDict_SetItemString(self->params,"background",Py_BuildValue("d",0.001000)); |
---|
| 66 | PyDict_SetItemString(self->params,"vol_frac_ss",Py_BuildValue("d",0.200000)); |
---|
| 67 | PyDict_SetItemString(self->params,"solvent_sld",Py_BuildValue("d",0.000006)); |
---|
| 68 | PyDict_SetItemString(self->params,"ls_sld",Py_BuildValue("d",0.000003)); |
---|
| 69 | PyDict_SetItemString(self->params,"ss_sld",Py_BuildValue("d",0.000000)); |
---|
| 70 | PyDict_SetItemString(self->params,"s_radius",Py_BuildValue("d",25.000000)); |
---|
| 71 | PyDict_SetItemString(self->params,"l_radius",Py_BuildValue("d",100.000000)); |
---|
| 72 | |
---|
| 73 | |
---|
| 74 | // Create empty log |
---|
| 75 | self->log = PyDict_New(); |
---|
| 76 | |
---|
| 77 | |
---|
| 78 | |
---|
| 79 | } |
---|
| 80 | return 0; |
---|
| 81 | } |
---|
| 82 | |
---|
| 83 | static PyMemberDef CBinaryHSModel_members[] = { |
---|
| 84 | {"params", T_OBJECT, offsetof(CBinaryHSModel, params), 0, |
---|
| 85 | "Parameters"}, |
---|
| 86 | {"log", T_OBJECT, offsetof(CBinaryHSModel, log), 0, |
---|
| 87 | "Log"}, |
---|
| 88 | {NULL} /* Sentinel */ |
---|
| 89 | }; |
---|
| 90 | |
---|
| 91 | /** Read double from PyObject |
---|
| 92 | @param p PyObject |
---|
| 93 | @return double |
---|
| 94 | */ |
---|
| 95 | double CBinaryHSModel_readDouble(PyObject *p) { |
---|
| 96 | if (PyFloat_Check(p)==1) { |
---|
| 97 | return (double)(((PyFloatObject *)(p))->ob_fval); |
---|
| 98 | } else if (PyInt_Check(p)==1) { |
---|
| 99 | return (double)(((PyIntObject *)(p))->ob_ival); |
---|
| 100 | } else if (PyLong_Check(p)==1) { |
---|
| 101 | return (double)PyLong_AsLong(p); |
---|
| 102 | } else { |
---|
| 103 | return 0.0; |
---|
| 104 | } |
---|
| 105 | } |
---|
| 106 | |
---|
| 107 | |
---|
| 108 | /** |
---|
| 109 | * Function to call to evaluate model |
---|
| 110 | * @param args: input q or [q,phi] |
---|
| 111 | * @return: function value |
---|
| 112 | */ |
---|
| 113 | static PyObject * run(CBinaryHSModel *self, PyObject *args) { |
---|
| 114 | double q_value, phi_value; |
---|
| 115 | PyObject* pars; |
---|
| 116 | int npars; |
---|
| 117 | |
---|
| 118 | // Get parameters |
---|
| 119 | |
---|
| 120 | // Reader parameter dictionary |
---|
| 121 | self->model_pars.vol_frac_ls = PyFloat_AsDouble( PyDict_GetItemString(self->params, "vol_frac_ls") ); |
---|
| 122 | self->model_pars.background = PyFloat_AsDouble( PyDict_GetItemString(self->params, "background") ); |
---|
| 123 | self->model_pars.vol_frac_ss = PyFloat_AsDouble( PyDict_GetItemString(self->params, "vol_frac_ss") ); |
---|
| 124 | self->model_pars.solvent_sld = PyFloat_AsDouble( PyDict_GetItemString(self->params, "solvent_sld") ); |
---|
| 125 | self->model_pars.ls_sld = PyFloat_AsDouble( PyDict_GetItemString(self->params, "ls_sld") ); |
---|
| 126 | self->model_pars.ss_sld = PyFloat_AsDouble( PyDict_GetItemString(self->params, "ss_sld") ); |
---|
| 127 | self->model_pars.s_radius = PyFloat_AsDouble( PyDict_GetItemString(self->params, "s_radius") ); |
---|
| 128 | self->model_pars.l_radius = PyFloat_AsDouble( PyDict_GetItemString(self->params, "l_radius") ); |
---|
| 129 | |
---|
| 130 | |
---|
| 131 | // Get input and determine whether we have to supply a 1D or 2D return value. |
---|
| 132 | if ( !PyArg_ParseTuple(args,"O",&pars) ) { |
---|
| 133 | PyErr_SetString(CBinaryHSModelError, |
---|
| 134 | "CBinaryHSModel.run expects a q value."); |
---|
| 135 | return NULL; |
---|
| 136 | } |
---|
| 137 | |
---|
| 138 | // Check params |
---|
| 139 | if( PyList_Check(pars)==1) { |
---|
| 140 | |
---|
| 141 | // Length of list should be 2 for I(q,phi) |
---|
| 142 | npars = PyList_GET_SIZE(pars); |
---|
| 143 | if(npars!=2) { |
---|
| 144 | PyErr_SetString(CBinaryHSModelError, |
---|
| 145 | "CBinaryHSModel.run expects a double or a list of dimension 2."); |
---|
| 146 | return NULL; |
---|
| 147 | } |
---|
| 148 | // We have a vector q, get the q and phi values at which |
---|
| 149 | // to evaluate I(q,phi) |
---|
| 150 | q_value = CBinaryHSModel_readDouble(PyList_GET_ITEM(pars,0)); |
---|
| 151 | phi_value = CBinaryHSModel_readDouble(PyList_GET_ITEM(pars,1)); |
---|
| 152 | // Skip zero |
---|
| 153 | if (q_value==0) { |
---|
| 154 | return Py_BuildValue("d",0.0); |
---|
| 155 | } |
---|
| 156 | return Py_BuildValue("d",binaryHs_analytical_2D(&(self->model_pars),q_value,phi_value)); |
---|
| 157 | |
---|
| 158 | } else { |
---|
| 159 | |
---|
| 160 | // We have a scalar q, we will evaluate I(q) |
---|
| 161 | q_value = CBinaryHSModel_readDouble(pars); |
---|
| 162 | |
---|
| 163 | return Py_BuildValue("d",binaryHs_analytical_1D(&(self->model_pars),q_value)); |
---|
| 164 | } |
---|
| 165 | } |
---|
| 166 | |
---|
| 167 | /** |
---|
| 168 | * Function to call to evaluate model in cartesian coordinates |
---|
| 169 | * @param args: input q or [qx, qy]] |
---|
| 170 | * @return: function value |
---|
| 171 | */ |
---|
| 172 | static PyObject * runXY(CBinaryHSModel *self, PyObject *args) { |
---|
| 173 | double qx_value, qy_value; |
---|
| 174 | PyObject* pars; |
---|
| 175 | int npars; |
---|
| 176 | |
---|
| 177 | // Get parameters |
---|
| 178 | |
---|
| 179 | // Reader parameter dictionary |
---|
| 180 | self->model_pars.vol_frac_ls = PyFloat_AsDouble( PyDict_GetItemString(self->params, "vol_frac_ls") ); |
---|
| 181 | self->model_pars.background = PyFloat_AsDouble( PyDict_GetItemString(self->params, "background") ); |
---|
| 182 | self->model_pars.vol_frac_ss = PyFloat_AsDouble( PyDict_GetItemString(self->params, "vol_frac_ss") ); |
---|
| 183 | self->model_pars.solvent_sld = PyFloat_AsDouble( PyDict_GetItemString(self->params, "solvent_sld") ); |
---|
| 184 | self->model_pars.ls_sld = PyFloat_AsDouble( PyDict_GetItemString(self->params, "ls_sld") ); |
---|
| 185 | self->model_pars.ss_sld = PyFloat_AsDouble( PyDict_GetItemString(self->params, "ss_sld") ); |
---|
| 186 | self->model_pars.s_radius = PyFloat_AsDouble( PyDict_GetItemString(self->params, "s_radius") ); |
---|
| 187 | self->model_pars.l_radius = PyFloat_AsDouble( PyDict_GetItemString(self->params, "l_radius") ); |
---|
| 188 | |
---|
| 189 | |
---|
| 190 | // Get input and determine whether we have to supply a 1D or 2D return value. |
---|
| 191 | if ( !PyArg_ParseTuple(args,"O",&pars) ) { |
---|
| 192 | PyErr_SetString(CBinaryHSModelError, |
---|
| 193 | "CBinaryHSModel.run expects a q value."); |
---|
| 194 | return NULL; |
---|
| 195 | } |
---|
| 196 | |
---|
| 197 | // Check params |
---|
| 198 | if( PyList_Check(pars)==1) { |
---|
| 199 | |
---|
| 200 | // Length of list should be 2 for I(qx, qy)) |
---|
| 201 | npars = PyList_GET_SIZE(pars); |
---|
| 202 | if(npars!=2) { |
---|
| 203 | PyErr_SetString(CBinaryHSModelError, |
---|
| 204 | "CBinaryHSModel.run expects a double or a list of dimension 2."); |
---|
| 205 | return NULL; |
---|
| 206 | } |
---|
| 207 | // We have a vector q, get the qx and qy values at which |
---|
| 208 | // to evaluate I(qx,qy) |
---|
| 209 | qx_value = CBinaryHSModel_readDouble(PyList_GET_ITEM(pars,0)); |
---|
| 210 | qy_value = CBinaryHSModel_readDouble(PyList_GET_ITEM(pars,1)); |
---|
| 211 | return Py_BuildValue("d",binaryHs_analytical_2DXY(&(self->model_pars),qx_value,qy_value)); |
---|
| 212 | |
---|
| 213 | } else { |
---|
| 214 | |
---|
| 215 | // We have a scalar q, we will evaluate I(q) |
---|
| 216 | qx_value = CBinaryHSModel_readDouble(pars); |
---|
| 217 | |
---|
| 218 | return Py_BuildValue("d",binaryHs_analytical_1D(&(self->model_pars),qx_value)); |
---|
| 219 | } |
---|
| 220 | } |
---|
| 221 | |
---|
| 222 | static PyObject * reset(CBinaryHSModel *self, PyObject *args) { |
---|
| 223 | |
---|
| 224 | |
---|
| 225 | return Py_BuildValue("d",0.0); |
---|
| 226 | } |
---|
| 227 | |
---|
| 228 | |
---|
| 229 | static PyMethodDef CBinaryHSModel_methods[] = { |
---|
| 230 | {"run", (PyCFunction)run , METH_VARARGS, |
---|
| 231 | "Evaluate the model at a given Q or Q, phi"}, |
---|
| 232 | {"runXY", (PyCFunction)runXY , METH_VARARGS, |
---|
| 233 | "Evaluate the model at a given Q or Qx, Qy"}, |
---|
| 234 | {"reset", (PyCFunction)reset , METH_VARARGS, |
---|
| 235 | "Reset pair correlation"}, |
---|
| 236 | //{"numerical_1D", (PyCFunction)numerical_1D , METH_VARARGS, |
---|
| 237 | // "Evaluate the 1D model at a given Q"}, |
---|
| 238 | {NULL} |
---|
| 239 | }; |
---|
| 240 | |
---|
| 241 | static PyTypeObject CBinaryHSModelType = { |
---|
| 242 | PyObject_HEAD_INIT(NULL) |
---|
| 243 | 0, /*ob_size*/ |
---|
| 244 | "CBinaryHSModel", /*tp_name*/ |
---|
| 245 | sizeof(CBinaryHSModel), /*tp_basicsize*/ |
---|
| 246 | 0, /*tp_itemsize*/ |
---|
| 247 | (destructor)CBinaryHSModel_dealloc, /*tp_dealloc*/ |
---|
| 248 | 0, /*tp_print*/ |
---|
| 249 | 0, /*tp_getattr*/ |
---|
| 250 | 0, /*tp_setattr*/ |
---|
| 251 | 0, /*tp_compare*/ |
---|
| 252 | 0, /*tp_repr*/ |
---|
| 253 | 0, /*tp_as_number*/ |
---|
| 254 | 0, /*tp_as_sequence*/ |
---|
| 255 | 0, /*tp_as_mapping*/ |
---|
| 256 | 0, /*tp_hash */ |
---|
| 257 | 0, /*tp_call*/ |
---|
| 258 | 0, /*tp_str*/ |
---|
| 259 | 0, /*tp_getattro*/ |
---|
| 260 | 0, /*tp_setattro*/ |
---|
| 261 | 0, /*tp_as_buffer*/ |
---|
| 262 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/ |
---|
| 263 | "CBinaryHSModel objects", /* tp_doc */ |
---|
| 264 | 0, /* tp_traverse */ |
---|
| 265 | 0, /* tp_clear */ |
---|
| 266 | 0, /* tp_richcompare */ |
---|
| 267 | 0, /* tp_weaklistoffset */ |
---|
| 268 | 0, /* tp_iter */ |
---|
| 269 | 0, /* tp_iternext */ |
---|
| 270 | CBinaryHSModel_methods, /* tp_methods */ |
---|
| 271 | CBinaryHSModel_members, /* tp_members */ |
---|
| 272 | 0, /* tp_getset */ |
---|
| 273 | 0, /* tp_base */ |
---|
| 274 | 0, /* tp_dict */ |
---|
| 275 | 0, /* tp_descr_get */ |
---|
| 276 | 0, /* tp_descr_set */ |
---|
| 277 | 0, /* tp_dictoffset */ |
---|
| 278 | (initproc)CBinaryHSModel_init, /* tp_init */ |
---|
| 279 | 0, /* tp_alloc */ |
---|
| 280 | CBinaryHSModel_new, /* tp_new */ |
---|
| 281 | }; |
---|
| 282 | |
---|
| 283 | |
---|
| 284 | static PyMethodDef module_methods[] = { |
---|
| 285 | {NULL} |
---|
| 286 | }; |
---|
| 287 | |
---|
| 288 | /** |
---|
| 289 | * Function used to add the model class to a module |
---|
| 290 | * @param module: module to add the class to |
---|
| 291 | */ |
---|
| 292 | void addCBinaryHSModel(PyObject *module) { |
---|
| 293 | PyObject *d; |
---|
| 294 | |
---|
| 295 | if (PyType_Ready(&CBinaryHSModelType) < 0) |
---|
| 296 | return; |
---|
| 297 | |
---|
| 298 | Py_INCREF(&CBinaryHSModelType); |
---|
| 299 | PyModule_AddObject(module, "CBinaryHSModel", (PyObject *)&CBinaryHSModelType); |
---|
| 300 | |
---|
| 301 | d = PyModule_GetDict(module); |
---|
| 302 | CBinaryHSModelError = PyErr_NewException("CBinaryHSModel.error", NULL, NULL); |
---|
| 303 | PyDict_SetItemString(d, "CBinaryHSModelError", CBinaryHSModelError); |
---|
| 304 | } |
---|
| 305 | |
---|