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