[34c3020] | 1 | /** |
---|
| 2 | This software was developed by the University of Tennessee as part of the |
---|
| 3 | Distributed Data Analysis of Neutron Scattering Experiments (DANSE) |
---|
| 4 | project funded by the US National Science Foundation. |
---|
| 5 | |
---|
| 6 | If you use DANSE applications to do scientific research that leads to |
---|
| 7 | publication, we ask that you acknowledge the use of the software with the |
---|
| 8 | following sentence: |
---|
| 9 | |
---|
| 10 | "This work benefited from DANSE software developed under NSF award DMR-0520547." |
---|
| 11 | |
---|
| 12 | copyright 2008, University of Tennessee |
---|
| 13 | */ |
---|
| 14 | |
---|
| 15 | /** CLamellarFFHGModel |
---|
| 16 | * |
---|
| 17 | * C extension |
---|
| 18 | * |
---|
| 19 | * WARNING: THIS FILE WAS GENERATED BY WRAPPERGENERATOR.PY |
---|
| 20 | * DO NOT MODIFY THIS FILE, MODIFY lamellarFF_HG.h |
---|
| 21 | * AND RE-RUN THE GENERATOR SCRIPT |
---|
| 22 | * |
---|
| 23 | */ |
---|
[9bd69098] | 24 | #define NO_IMPORT_ARRAY |
---|
| 25 | #define PY_ARRAY_UNIQUE_SYMBOL PyArray_API_sans |
---|
[34c3020] | 26 | |
---|
| 27 | extern "C" { |
---|
| 28 | #include <Python.h> |
---|
[9bd69098] | 29 | #include <arrayobject.h> |
---|
[34c3020] | 30 | #include "structmember.h" |
---|
| 31 | #include <stdio.h> |
---|
| 32 | #include <stdlib.h> |
---|
| 33 | #include <math.h> |
---|
| 34 | #include <time.h> |
---|
| 35 | #include "lamellarFF_HG.h" |
---|
| 36 | } |
---|
| 37 | |
---|
| 38 | #include "models.hh" |
---|
| 39 | #include "dispersion_visitor.hh" |
---|
| 40 | |
---|
| 41 | /// Error object for raised exceptions |
---|
| 42 | static PyObject * CLamellarFFHGModelError = NULL; |
---|
| 43 | |
---|
| 44 | |
---|
| 45 | // Class definition |
---|
| 46 | typedef struct { |
---|
| 47 | PyObject_HEAD |
---|
| 48 | /// Parameters |
---|
| 49 | PyObject * params; |
---|
| 50 | /// Dispersion parameters |
---|
| 51 | PyObject * dispersion; |
---|
| 52 | /// Underlying model object |
---|
| 53 | LamellarFFHGModel * model; |
---|
| 54 | /// Log for unit testing |
---|
| 55 | PyObject * log; |
---|
| 56 | } CLamellarFFHGModel; |
---|
| 57 | |
---|
| 58 | |
---|
| 59 | static void |
---|
| 60 | CLamellarFFHGModel_dealloc(CLamellarFFHGModel* self) |
---|
| 61 | { |
---|
[71e2de7] | 62 | Py_DECREF(self->params); |
---|
| 63 | Py_DECREF(self->dispersion); |
---|
| 64 | Py_DECREF(self->log); |
---|
| 65 | delete self->model; |
---|
[34c3020] | 66 | self->ob_type->tp_free((PyObject*)self); |
---|
[b1c3295] | 67 | |
---|
[34c3020] | 68 | |
---|
| 69 | } |
---|
| 70 | |
---|
| 71 | static PyObject * |
---|
| 72 | CLamellarFFHGModel_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
---|
| 73 | { |
---|
| 74 | CLamellarFFHGModel *self; |
---|
| 75 | |
---|
| 76 | self = (CLamellarFFHGModel *)type->tp_alloc(type, 0); |
---|
| 77 | |
---|
| 78 | return (PyObject *)self; |
---|
| 79 | } |
---|
| 80 | |
---|
| 81 | static int |
---|
| 82 | CLamellarFFHGModel_init(CLamellarFFHGModel *self, PyObject *args, PyObject *kwds) |
---|
| 83 | { |
---|
| 84 | if (self != NULL) { |
---|
| 85 | |
---|
| 86 | // Create parameters |
---|
| 87 | self->params = PyDict_New(); |
---|
| 88 | self->dispersion = PyDict_New(); |
---|
| 89 | self->model = new LamellarFFHGModel(); |
---|
| 90 | |
---|
[b1c3295] | 91 | // Initialize parameter dictionary |
---|
| 92 | PyDict_SetItemString(self->params,"scale",Py_BuildValue("d",1.000000000000)); |
---|
| 93 | PyDict_SetItemString(self->params,"h_thickness",Py_BuildValue("d",10.000000000000)); |
---|
| 94 | PyDict_SetItemString(self->params,"sld_tail",Py_BuildValue("d",0.000000400000)); |
---|
| 95 | PyDict_SetItemString(self->params,"sld_solvent",Py_BuildValue("d",0.000006000000)); |
---|
| 96 | PyDict_SetItemString(self->params,"t_length",Py_BuildValue("d",15.000000000000)); |
---|
| 97 | PyDict_SetItemString(self->params,"sld_head",Py_BuildValue("d",0.000003000000)); |
---|
| 98 | PyDict_SetItemString(self->params,"background",Py_BuildValue("d",0.000000000000)); |
---|
| 99 | // Initialize dispersion / averaging parameter dict |
---|
| 100 | DispersionVisitor* visitor = new DispersionVisitor(); |
---|
| 101 | PyObject * disp_dict; |
---|
| 102 | disp_dict = PyDict_New(); |
---|
| 103 | self->model->t_length.dispersion->accept_as_source(visitor, self->model->t_length.dispersion, disp_dict); |
---|
| 104 | PyDict_SetItemString(self->dispersion, "t_length", disp_dict); |
---|
| 105 | disp_dict = PyDict_New(); |
---|
| 106 | self->model->h_thickness.dispersion->accept_as_source(visitor, self->model->h_thickness.dispersion, disp_dict); |
---|
| 107 | PyDict_SetItemString(self->dispersion, "h_thickness", disp_dict); |
---|
| 108 | |
---|
[34c3020] | 109 | |
---|
| 110 | |
---|
| 111 | // Create empty log |
---|
| 112 | self->log = PyDict_New(); |
---|
| 113 | |
---|
[b1c3295] | 114 | |
---|
[34c3020] | 115 | |
---|
| 116 | } |
---|
| 117 | return 0; |
---|
| 118 | } |
---|
| 119 | |
---|
[b1c3295] | 120 | static char name_params[] = "params"; |
---|
| 121 | static char def_params[] = "Parameters"; |
---|
| 122 | static char name_dispersion[] = "dispersion"; |
---|
| 123 | static char def_dispersion[] = "Dispersion parameters"; |
---|
| 124 | static char name_log[] = "log"; |
---|
| 125 | static char def_log[] = "Log"; |
---|
| 126 | |
---|
[34c3020] | 127 | static PyMemberDef CLamellarFFHGModel_members[] = { |
---|
[b1c3295] | 128 | {name_params, T_OBJECT, offsetof(CLamellarFFHGModel, params), 0, def_params}, |
---|
| 129 | {name_dispersion, T_OBJECT, offsetof(CLamellarFFHGModel, dispersion), 0, def_dispersion}, |
---|
| 130 | {name_log, T_OBJECT, offsetof(CLamellarFFHGModel, log), 0, def_log}, |
---|
[34c3020] | 131 | {NULL} /* Sentinel */ |
---|
| 132 | }; |
---|
| 133 | |
---|
| 134 | /** Read double from PyObject |
---|
| 135 | @param p PyObject |
---|
| 136 | @return double |
---|
| 137 | */ |
---|
| 138 | double CLamellarFFHGModel_readDouble(PyObject *p) { |
---|
| 139 | if (PyFloat_Check(p)==1) { |
---|
| 140 | return (double)(((PyFloatObject *)(p))->ob_fval); |
---|
| 141 | } else if (PyInt_Check(p)==1) { |
---|
| 142 | return (double)(((PyIntObject *)(p))->ob_ival); |
---|
| 143 | } else if (PyLong_Check(p)==1) { |
---|
| 144 | return (double)PyLong_AsLong(p); |
---|
| 145 | } else { |
---|
| 146 | return 0.0; |
---|
| 147 | } |
---|
| 148 | } |
---|
[9bd69098] | 149 | /** |
---|
| 150 | * Function to call to evaluate model |
---|
| 151 | * @param args: input numpy array q[] |
---|
| 152 | * @return: numpy array object |
---|
| 153 | */ |
---|
| 154 | |
---|
| 155 | static PyObject *evaluateOneDim(LamellarFFHGModel* model, PyArrayObject *q){ |
---|
| 156 | PyArrayObject *result; |
---|
| 157 | |
---|
| 158 | // Check validity of array q , q must be of dimension 1, an array of double |
---|
| 159 | if (q->nd != 1 || q->descr->type_num != PyArray_DOUBLE) |
---|
| 160 | { |
---|
| 161 | //const char * message= "Invalid array: q->nd=%d,type_num=%d\n",q->nd,q->descr->type_num; |
---|
| 162 | //PyErr_SetString(PyExc_ValueError , message); |
---|
| 163 | return NULL; |
---|
| 164 | } |
---|
[8f5b34a] | 165 | result = (PyArrayObject *)PyArray_FromDims(q->nd, (int *)(q->dimensions), PyArray_DOUBLE); |
---|
[9bd69098] | 166 | if (result == NULL) { |
---|
| 167 | const char * message= "Could not create result "; |
---|
| 168 | PyErr_SetString(PyExc_RuntimeError , message); |
---|
| 169 | return NULL; |
---|
| 170 | } |
---|
[0b082f3] | 171 | #pragma omp parallel for |
---|
[9bd69098] | 172 | for (int i = 0; i < q->dimensions[0]; i++){ |
---|
| 173 | double q_value = *(double *)(q->data + i*q->strides[0]); |
---|
| 174 | double *result_value = (double *)(result->data + i*result->strides[0]); |
---|
| 175 | *result_value =(*model)(q_value); |
---|
| 176 | } |
---|
| 177 | return PyArray_Return(result); |
---|
| 178 | } |
---|
| 179 | |
---|
| 180 | /** |
---|
| 181 | * Function to call to evaluate model |
---|
| 182 | * @param args: input numpy array [x[],y[]] |
---|
| 183 | * @return: numpy array object |
---|
| 184 | */ |
---|
| 185 | static PyObject * evaluateTwoDimXY( LamellarFFHGModel* model, |
---|
| 186 | PyArrayObject *x, PyArrayObject *y) |
---|
| 187 | { |
---|
| 188 | PyArrayObject *result; |
---|
[0b082f3] | 189 | int x_len, y_len, dims[1]; |
---|
[9bd69098] | 190 | //check validity of input vectors |
---|
[3080527] | 191 | if (x->nd != 1 || x->descr->type_num != PyArray_DOUBLE |
---|
| 192 | || y->nd != 1 || y->descr->type_num != PyArray_DOUBLE |
---|
| 193 | || y->dimensions[0] != x->dimensions[0]){ |
---|
[9bd69098] | 194 | const char * message= "evaluateTwoDimXY expect 2 numpy arrays"; |
---|
| 195 | PyErr_SetString(PyExc_ValueError , message); |
---|
| 196 | return NULL; |
---|
| 197 | } |
---|
| 198 | |
---|
| 199 | if (PyArray_Check(x) && PyArray_Check(y)) { |
---|
[a8d6888] | 200 | |
---|
[3080527] | 201 | x_len = dims[0]= x->dimensions[0]; |
---|
[9ce41c6] | 202 | y_len = dims[0]= y->dimensions[0]; |
---|
[9bd69098] | 203 | |
---|
| 204 | // Make a new double matrix of same dims |
---|
[8f5b34a] | 205 | result=(PyArrayObject *) PyArray_FromDims(1,dims,NPY_DOUBLE); |
---|
[9bd69098] | 206 | if (result == NULL){ |
---|
| 207 | const char * message= "Could not create result "; |
---|
| 208 | PyErr_SetString(PyExc_RuntimeError , message); |
---|
| 209 | return NULL; |
---|
| 210 | } |
---|
| 211 | |
---|
| 212 | /* Do the calculation. */ |
---|
[0b082f3] | 213 | #pragma omp parallel for |
---|
| 214 | for (int i=0; i< x_len; i++) { |
---|
[3080527] | 215 | double x_value = *(double *)(x->data + i*x->strides[0]); |
---|
| 216 | double y_value = *(double *)(y->data + i*y->strides[0]); |
---|
| 217 | double *result_value = (double *)(result->data + |
---|
| 218 | i*result->strides[0]); |
---|
| 219 | *result_value = (*model)(x_value, y_value); |
---|
| 220 | } |
---|
[9bd69098] | 221 | return PyArray_Return(result); |
---|
| 222 | |
---|
| 223 | }else{ |
---|
| 224 | PyErr_SetString(CLamellarFFHGModelError, |
---|
| 225 | "CLamellarFFHGModel.evaluateTwoDimXY couldn't run."); |
---|
| 226 | return NULL; |
---|
| 227 | } |
---|
| 228 | } |
---|
| 229 | /** |
---|
| 230 | * evalDistribution function evaluate a model function with input vector |
---|
| 231 | * @param args: input q as vector or [qx, qy] where qx, qy are vectors |
---|
| 232 | * |
---|
| 233 | */ |
---|
| 234 | static PyObject * evalDistribution(CLamellarFFHGModel *self, PyObject *args){ |
---|
| 235 | PyObject *qx, *qy; |
---|
| 236 | PyArrayObject * pars; |
---|
| 237 | int npars ,mpars; |
---|
| 238 | |
---|
| 239 | // Get parameters |
---|
| 240 | |
---|
[b1c3295] | 241 | // Reader parameter dictionary |
---|
| 242 | self->model->scale = PyFloat_AsDouble( PyDict_GetItemString(self->params, "scale") ); |
---|
| 243 | self->model->h_thickness = PyFloat_AsDouble( PyDict_GetItemString(self->params, "h_thickness") ); |
---|
| 244 | self->model->sld_tail = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_tail") ); |
---|
| 245 | self->model->sld_solvent = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_solvent") ); |
---|
| 246 | self->model->t_length = PyFloat_AsDouble( PyDict_GetItemString(self->params, "t_length") ); |
---|
| 247 | self->model->sld_head = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_head") ); |
---|
| 248 | self->model->background = PyFloat_AsDouble( PyDict_GetItemString(self->params, "background") ); |
---|
| 249 | // Read in dispersion parameters |
---|
| 250 | PyObject* disp_dict; |
---|
| 251 | DispersionVisitor* visitor = new DispersionVisitor(); |
---|
| 252 | disp_dict = PyDict_GetItemString(self->dispersion, "t_length"); |
---|
| 253 | self->model->t_length.dispersion->accept_as_destination(visitor, self->model->t_length.dispersion, disp_dict); |
---|
| 254 | disp_dict = PyDict_GetItemString(self->dispersion, "h_thickness"); |
---|
| 255 | self->model->h_thickness.dispersion->accept_as_destination(visitor, self->model->h_thickness.dispersion, disp_dict); |
---|
[34c3020] | 256 | |
---|
[9bd69098] | 257 | |
---|
| 258 | // Get input and determine whether we have to supply a 1D or 2D return value. |
---|
| 259 | if ( !PyArg_ParseTuple(args,"O",&pars) ) { |
---|
| 260 | PyErr_SetString(CLamellarFFHGModelError, |
---|
| 261 | "CLamellarFFHGModel.evalDistribution expects a q value."); |
---|
| 262 | return NULL; |
---|
| 263 | } |
---|
| 264 | // Check params |
---|
| 265 | |
---|
| 266 | if(PyArray_Check(pars)==1) { |
---|
| 267 | |
---|
| 268 | // Length of list should 1 or 2 |
---|
| 269 | npars = pars->nd; |
---|
| 270 | if(npars==1) { |
---|
| 271 | // input is a numpy array |
---|
| 272 | if (PyArray_Check(pars)) { |
---|
| 273 | return evaluateOneDim(self->model, (PyArrayObject*)pars); |
---|
| 274 | } |
---|
| 275 | }else{ |
---|
| 276 | PyErr_SetString(CLamellarFFHGModelError, |
---|
| 277 | "CLamellarFFHGModel.evalDistribution expect numpy array of one dimension."); |
---|
| 278 | return NULL; |
---|
| 279 | } |
---|
| 280 | }else if( PyList_Check(pars)==1) { |
---|
| 281 | // Length of list should be 2 for I(qx,qy) |
---|
| 282 | mpars = PyList_GET_SIZE(pars); |
---|
| 283 | if(mpars!=2) { |
---|
| 284 | PyErr_SetString(CLamellarFFHGModelError, |
---|
| 285 | "CLamellarFFHGModel.evalDistribution expects a list of dimension 2."); |
---|
| 286 | return NULL; |
---|
| 287 | } |
---|
| 288 | qx = PyList_GET_ITEM(pars,0); |
---|
| 289 | qy = PyList_GET_ITEM(pars,1); |
---|
| 290 | if (PyArray_Check(qx) && PyArray_Check(qy)) { |
---|
| 291 | return evaluateTwoDimXY(self->model, (PyArrayObject*)qx, |
---|
| 292 | (PyArrayObject*)qy); |
---|
| 293 | }else{ |
---|
| 294 | PyErr_SetString(CLamellarFFHGModelError, |
---|
| 295 | "CLamellarFFHGModel.evalDistribution expect 2 numpy arrays in list."); |
---|
| 296 | return NULL; |
---|
| 297 | } |
---|
| 298 | } |
---|
[e0a8a3c] | 299 | PyErr_SetString(CLamellarFFHGModelError, |
---|
| 300 | "CLamellarFFHGModel.evalDistribution couln't be run."); |
---|
| 301 | return NULL; |
---|
| 302 | |
---|
[9bd69098] | 303 | } |
---|
[34c3020] | 304 | |
---|
| 305 | /** |
---|
| 306 | * Function to call to evaluate model |
---|
| 307 | * @param args: input q or [q,phi] |
---|
| 308 | * @return: function value |
---|
| 309 | */ |
---|
| 310 | static PyObject * run(CLamellarFFHGModel *self, PyObject *args) { |
---|
| 311 | double q_value, phi_value; |
---|
| 312 | PyObject* pars; |
---|
| 313 | int npars; |
---|
| 314 | |
---|
| 315 | // Get parameters |
---|
| 316 | |
---|
[b1c3295] | 317 | // Reader parameter dictionary |
---|
| 318 | self->model->scale = PyFloat_AsDouble( PyDict_GetItemString(self->params, "scale") ); |
---|
| 319 | self->model->h_thickness = PyFloat_AsDouble( PyDict_GetItemString(self->params, "h_thickness") ); |
---|
| 320 | self->model->sld_tail = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_tail") ); |
---|
| 321 | self->model->sld_solvent = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_solvent") ); |
---|
| 322 | self->model->t_length = PyFloat_AsDouble( PyDict_GetItemString(self->params, "t_length") ); |
---|
| 323 | self->model->sld_head = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_head") ); |
---|
| 324 | self->model->background = PyFloat_AsDouble( PyDict_GetItemString(self->params, "background") ); |
---|
| 325 | // Read in dispersion parameters |
---|
| 326 | PyObject* disp_dict; |
---|
| 327 | DispersionVisitor* visitor = new DispersionVisitor(); |
---|
| 328 | disp_dict = PyDict_GetItemString(self->dispersion, "t_length"); |
---|
| 329 | self->model->t_length.dispersion->accept_as_destination(visitor, self->model->t_length.dispersion, disp_dict); |
---|
| 330 | disp_dict = PyDict_GetItemString(self->dispersion, "h_thickness"); |
---|
| 331 | self->model->h_thickness.dispersion->accept_as_destination(visitor, self->model->h_thickness.dispersion, disp_dict); |
---|
[34c3020] | 332 | |
---|
| 333 | |
---|
| 334 | // Get input and determine whether we have to supply a 1D or 2D return value. |
---|
| 335 | if ( !PyArg_ParseTuple(args,"O",&pars) ) { |
---|
| 336 | PyErr_SetString(CLamellarFFHGModelError, |
---|
| 337 | "CLamellarFFHGModel.run expects a q value."); |
---|
| 338 | return NULL; |
---|
| 339 | } |
---|
| 340 | |
---|
| 341 | // Check params |
---|
| 342 | if( PyList_Check(pars)==1) { |
---|
| 343 | |
---|
| 344 | // Length of list should be 2 for I(q,phi) |
---|
| 345 | npars = PyList_GET_SIZE(pars); |
---|
| 346 | if(npars!=2) { |
---|
| 347 | PyErr_SetString(CLamellarFFHGModelError, |
---|
| 348 | "CLamellarFFHGModel.run expects a double or a list of dimension 2."); |
---|
| 349 | return NULL; |
---|
| 350 | } |
---|
| 351 | // We have a vector q, get the q and phi values at which |
---|
| 352 | // to evaluate I(q,phi) |
---|
| 353 | q_value = CLamellarFFHGModel_readDouble(PyList_GET_ITEM(pars,0)); |
---|
| 354 | phi_value = CLamellarFFHGModel_readDouble(PyList_GET_ITEM(pars,1)); |
---|
| 355 | // Skip zero |
---|
| 356 | if (q_value==0) { |
---|
| 357 | return Py_BuildValue("d",0.0); |
---|
| 358 | } |
---|
| 359 | return Py_BuildValue("d",(*(self->model)).evaluate_rphi(q_value,phi_value)); |
---|
| 360 | |
---|
| 361 | } else { |
---|
| 362 | |
---|
| 363 | // We have a scalar q, we will evaluate I(q) |
---|
| 364 | q_value = CLamellarFFHGModel_readDouble(pars); |
---|
| 365 | |
---|
| 366 | return Py_BuildValue("d",(*(self->model))(q_value)); |
---|
| 367 | } |
---|
| 368 | } |
---|
[5eb9154] | 369 | /** |
---|
| 370 | * Function to call to calculate_ER |
---|
| 371 | * @return: effective radius value |
---|
| 372 | */ |
---|
| 373 | static PyObject * calculate_ER(CLamellarFFHGModel *self) { |
---|
[34c3020] | 374 | |
---|
[5eb9154] | 375 | // Get parameters |
---|
| 376 | |
---|
[b1c3295] | 377 | // Reader parameter dictionary |
---|
| 378 | self->model->scale = PyFloat_AsDouble( PyDict_GetItemString(self->params, "scale") ); |
---|
| 379 | self->model->h_thickness = PyFloat_AsDouble( PyDict_GetItemString(self->params, "h_thickness") ); |
---|
| 380 | self->model->sld_tail = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_tail") ); |
---|
| 381 | self->model->sld_solvent = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_solvent") ); |
---|
| 382 | self->model->t_length = PyFloat_AsDouble( PyDict_GetItemString(self->params, "t_length") ); |
---|
| 383 | self->model->sld_head = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_head") ); |
---|
| 384 | self->model->background = PyFloat_AsDouble( PyDict_GetItemString(self->params, "background") ); |
---|
| 385 | // Read in dispersion parameters |
---|
| 386 | PyObject* disp_dict; |
---|
| 387 | DispersionVisitor* visitor = new DispersionVisitor(); |
---|
| 388 | disp_dict = PyDict_GetItemString(self->dispersion, "t_length"); |
---|
| 389 | self->model->t_length.dispersion->accept_as_destination(visitor, self->model->t_length.dispersion, disp_dict); |
---|
| 390 | disp_dict = PyDict_GetItemString(self->dispersion, "h_thickness"); |
---|
| 391 | self->model->h_thickness.dispersion->accept_as_destination(visitor, self->model->h_thickness.dispersion, disp_dict); |
---|
[5eb9154] | 392 | |
---|
| 393 | |
---|
| 394 | return Py_BuildValue("d",(*(self->model)).calculate_ER()); |
---|
| 395 | |
---|
| 396 | } |
---|
[34c3020] | 397 | /** |
---|
| 398 | * Function to call to evaluate model in cartesian coordinates |
---|
| 399 | * @param args: input q or [qx, qy]] |
---|
| 400 | * @return: function value |
---|
| 401 | */ |
---|
| 402 | static PyObject * runXY(CLamellarFFHGModel *self, PyObject *args) { |
---|
| 403 | double qx_value, qy_value; |
---|
| 404 | PyObject* pars; |
---|
| 405 | int npars; |
---|
| 406 | |
---|
| 407 | // Get parameters |
---|
| 408 | |
---|
[b1c3295] | 409 | // Reader parameter dictionary |
---|
| 410 | self->model->scale = PyFloat_AsDouble( PyDict_GetItemString(self->params, "scale") ); |
---|
| 411 | self->model->h_thickness = PyFloat_AsDouble( PyDict_GetItemString(self->params, "h_thickness") ); |
---|
| 412 | self->model->sld_tail = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_tail") ); |
---|
| 413 | self->model->sld_solvent = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_solvent") ); |
---|
| 414 | self->model->t_length = PyFloat_AsDouble( PyDict_GetItemString(self->params, "t_length") ); |
---|
| 415 | self->model->sld_head = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_head") ); |
---|
| 416 | self->model->background = PyFloat_AsDouble( PyDict_GetItemString(self->params, "background") ); |
---|
| 417 | // Read in dispersion parameters |
---|
| 418 | PyObject* disp_dict; |
---|
| 419 | DispersionVisitor* visitor = new DispersionVisitor(); |
---|
| 420 | disp_dict = PyDict_GetItemString(self->dispersion, "t_length"); |
---|
| 421 | self->model->t_length.dispersion->accept_as_destination(visitor, self->model->t_length.dispersion, disp_dict); |
---|
| 422 | disp_dict = PyDict_GetItemString(self->dispersion, "h_thickness"); |
---|
| 423 | self->model->h_thickness.dispersion->accept_as_destination(visitor, self->model->h_thickness.dispersion, disp_dict); |
---|
[34c3020] | 424 | |
---|
| 425 | |
---|
| 426 | // Get input and determine whether we have to supply a 1D or 2D return value. |
---|
| 427 | if ( !PyArg_ParseTuple(args,"O",&pars) ) { |
---|
| 428 | PyErr_SetString(CLamellarFFHGModelError, |
---|
| 429 | "CLamellarFFHGModel.run expects a q value."); |
---|
| 430 | return NULL; |
---|
| 431 | } |
---|
| 432 | |
---|
| 433 | // Check params |
---|
| 434 | if( PyList_Check(pars)==1) { |
---|
| 435 | |
---|
| 436 | // Length of list should be 2 for I(qx, qy)) |
---|
| 437 | npars = PyList_GET_SIZE(pars); |
---|
| 438 | if(npars!=2) { |
---|
| 439 | PyErr_SetString(CLamellarFFHGModelError, |
---|
| 440 | "CLamellarFFHGModel.run expects a double or a list of dimension 2."); |
---|
| 441 | return NULL; |
---|
| 442 | } |
---|
| 443 | // We have a vector q, get the qx and qy values at which |
---|
| 444 | // to evaluate I(qx,qy) |
---|
| 445 | qx_value = CLamellarFFHGModel_readDouble(PyList_GET_ITEM(pars,0)); |
---|
| 446 | qy_value = CLamellarFFHGModel_readDouble(PyList_GET_ITEM(pars,1)); |
---|
| 447 | return Py_BuildValue("d",(*(self->model))(qx_value,qy_value)); |
---|
| 448 | |
---|
| 449 | } else { |
---|
| 450 | |
---|
| 451 | // We have a scalar q, we will evaluate I(q) |
---|
| 452 | qx_value = CLamellarFFHGModel_readDouble(pars); |
---|
| 453 | |
---|
| 454 | return Py_BuildValue("d",(*(self->model))(qx_value)); |
---|
| 455 | } |
---|
| 456 | } |
---|
| 457 | |
---|
| 458 | static PyObject * reset(CLamellarFFHGModel *self, PyObject *args) { |
---|
[b1c3295] | 459 | |
---|
[34c3020] | 460 | |
---|
| 461 | return Py_BuildValue("d",0.0); |
---|
| 462 | } |
---|
| 463 | |
---|
| 464 | static PyObject * set_dispersion(CLamellarFFHGModel *self, PyObject *args) { |
---|
| 465 | PyObject * disp; |
---|
| 466 | const char * par_name; |
---|
| 467 | |
---|
| 468 | if ( !PyArg_ParseTuple(args,"sO", &par_name, &disp) ) { |
---|
| 469 | PyErr_SetString(CLamellarFFHGModelError, |
---|
| 470 | "CLamellarFFHGModel.set_dispersion expects a DispersionModel object."); |
---|
| 471 | return NULL; |
---|
| 472 | } |
---|
| 473 | void *temp = PyCObject_AsVoidPtr(disp); |
---|
| 474 | DispersionModel * dispersion = static_cast<DispersionModel *>(temp); |
---|
| 475 | |
---|
| 476 | |
---|
| 477 | // Ugliness necessary to go from python to C |
---|
[b1c3295] | 478 | // TODO: refactor this |
---|
| 479 | if (!strcmp(par_name, "t_length")) { |
---|
| 480 | self->model->t_length.dispersion = dispersion; |
---|
| 481 | } else if (!strcmp(par_name, "h_thickness")) { |
---|
| 482 | self->model->h_thickness.dispersion = dispersion; |
---|
[34c3020] | 483 | } else { |
---|
| 484 | PyErr_SetString(CLamellarFFHGModelError, |
---|
| 485 | "CLamellarFFHGModel.set_dispersion expects a valid parameter name."); |
---|
| 486 | return NULL; |
---|
| 487 | } |
---|
| 488 | |
---|
| 489 | DispersionVisitor* visitor = new DispersionVisitor(); |
---|
| 490 | PyObject * disp_dict = PyDict_New(); |
---|
| 491 | dispersion->accept_as_source(visitor, dispersion, disp_dict); |
---|
| 492 | PyDict_SetItemString(self->dispersion, par_name, disp_dict); |
---|
| 493 | return Py_BuildValue("i",1); |
---|
| 494 | } |
---|
| 495 | |
---|
| 496 | |
---|
| 497 | static PyMethodDef CLamellarFFHGModel_methods[] = { |
---|
| 498 | {"run", (PyCFunction)run , METH_VARARGS, |
---|
| 499 | "Evaluate the model at a given Q or Q, phi"}, |
---|
| 500 | {"runXY", (PyCFunction)runXY , METH_VARARGS, |
---|
| 501 | "Evaluate the model at a given Q or Qx, Qy"}, |
---|
[5eb9154] | 502 | {"calculate_ER", (PyCFunction)calculate_ER , METH_VARARGS, |
---|
| 503 | "Evaluate the model at a given Q or Q, phi"}, |
---|
[9bd69098] | 504 | |
---|
| 505 | {"evalDistribution", (PyCFunction)evalDistribution , METH_VARARGS, |
---|
| 506 | "Evaluate the model at a given Q or Qx, Qy vector "}, |
---|
[34c3020] | 507 | {"reset", (PyCFunction)reset , METH_VARARGS, |
---|
| 508 | "Reset pair correlation"}, |
---|
| 509 | {"set_dispersion", (PyCFunction)set_dispersion , METH_VARARGS, |
---|
| 510 | "Set the dispersion model for a given parameter"}, |
---|
| 511 | {NULL} |
---|
| 512 | }; |
---|
| 513 | |
---|
| 514 | static PyTypeObject CLamellarFFHGModelType = { |
---|
| 515 | PyObject_HEAD_INIT(NULL) |
---|
| 516 | 0, /*ob_size*/ |
---|
| 517 | "CLamellarFFHGModel", /*tp_name*/ |
---|
| 518 | sizeof(CLamellarFFHGModel), /*tp_basicsize*/ |
---|
| 519 | 0, /*tp_itemsize*/ |
---|
| 520 | (destructor)CLamellarFFHGModel_dealloc, /*tp_dealloc*/ |
---|
| 521 | 0, /*tp_print*/ |
---|
| 522 | 0, /*tp_getattr*/ |
---|
| 523 | 0, /*tp_setattr*/ |
---|
| 524 | 0, /*tp_compare*/ |
---|
| 525 | 0, /*tp_repr*/ |
---|
| 526 | 0, /*tp_as_number*/ |
---|
| 527 | 0, /*tp_as_sequence*/ |
---|
| 528 | 0, /*tp_as_mapping*/ |
---|
| 529 | 0, /*tp_hash */ |
---|
| 530 | 0, /*tp_call*/ |
---|
| 531 | 0, /*tp_str*/ |
---|
| 532 | 0, /*tp_getattro*/ |
---|
| 533 | 0, /*tp_setattro*/ |
---|
| 534 | 0, /*tp_as_buffer*/ |
---|
| 535 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/ |
---|
| 536 | "CLamellarFFHGModel objects", /* tp_doc */ |
---|
| 537 | 0, /* tp_traverse */ |
---|
| 538 | 0, /* tp_clear */ |
---|
| 539 | 0, /* tp_richcompare */ |
---|
| 540 | 0, /* tp_weaklistoffset */ |
---|
| 541 | 0, /* tp_iter */ |
---|
| 542 | 0, /* tp_iternext */ |
---|
| 543 | CLamellarFFHGModel_methods, /* tp_methods */ |
---|
| 544 | CLamellarFFHGModel_members, /* tp_members */ |
---|
| 545 | 0, /* tp_getset */ |
---|
| 546 | 0, /* tp_base */ |
---|
| 547 | 0, /* tp_dict */ |
---|
| 548 | 0, /* tp_descr_get */ |
---|
| 549 | 0, /* tp_descr_set */ |
---|
| 550 | 0, /* tp_dictoffset */ |
---|
| 551 | (initproc)CLamellarFFHGModel_init, /* tp_init */ |
---|
| 552 | 0, /* tp_alloc */ |
---|
| 553 | CLamellarFFHGModel_new, /* tp_new */ |
---|
| 554 | }; |
---|
| 555 | |
---|
| 556 | |
---|
[9bd69098] | 557 | //static PyMethodDef module_methods[] = { |
---|
| 558 | // {NULL} |
---|
| 559 | //}; |
---|
[34c3020] | 560 | |
---|
| 561 | /** |
---|
| 562 | * Function used to add the model class to a module |
---|
| 563 | * @param module: module to add the class to |
---|
| 564 | */ |
---|
| 565 | void addCLamellarFFHGModel(PyObject *module) { |
---|
| 566 | PyObject *d; |
---|
| 567 | |
---|
| 568 | if (PyType_Ready(&CLamellarFFHGModelType) < 0) |
---|
| 569 | return; |
---|
| 570 | |
---|
| 571 | Py_INCREF(&CLamellarFFHGModelType); |
---|
| 572 | PyModule_AddObject(module, "CLamellarFFHGModel", (PyObject *)&CLamellarFFHGModelType); |
---|
| 573 | |
---|
| 574 | d = PyModule_GetDict(module); |
---|
[2605da22] | 575 | static char error_name[] = "CLamellarFFHGModel.error"; |
---|
| 576 | CLamellarFFHGModelError = PyErr_NewException(error_name, NULL, NULL); |
---|
[34c3020] | 577 | PyDict_SetItemString(d, "CLamellarFFHGModelError", CLamellarFFHGModelError); |
---|
| 578 | } |
---|
[b1c3295] | 579 | |
---|