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