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