[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 |
---|
[975ec8e] | 92 | PyDict_SetItemString(self->params,"sld_sol",Py_BuildValue("d",0.000006)); |
---|
[34c3020] | 93 | PyDict_SetItemString(self->params,"scale",Py_BuildValue("d",1.000000)); |
---|
[975ec8e] | 94 | PyDict_SetItemString(self->params,"bi_thick",Py_BuildValue("d",50.000000)); |
---|
[34c3020] | 95 | PyDict_SetItemString(self->params,"background",Py_BuildValue("d",0.000000)); |
---|
[975ec8e] | 96 | PyDict_SetItemString(self->params,"sld_bi",Py_BuildValue("d",0.000001)); |
---|
[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; |
---|
| 180 | int i,j, x_len, y_len, dims[2]; |
---|
| 181 | //check validity of input vectors |
---|
| 182 | if (x->nd != 2 || x->descr->type_num != PyArray_DOUBLE |
---|
| 183 | || y->nd != 2 || y->descr->type_num != PyArray_DOUBLE |
---|
| 184 | || y->dimensions[1] != x->dimensions[0]){ |
---|
| 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 | |
---|
[9ce41c6] | 192 | x_len = dims[1]= x->dimensions[1]; |
---|
| 193 | y_len = dims[0]= y->dimensions[0]; |
---|
[9bd69098] | 194 | |
---|
| 195 | // Make a new double matrix of same dims |
---|
| 196 | result=(PyArrayObject *) PyArray_FromDims(2,dims,NPY_DOUBLE); |
---|
| 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. */ |
---|
[e0a8a3c] | 204 | for ( j=0; j< y_len; j++) { |
---|
| 205 | for ( i=0; i< x_len; i++) { |
---|
[9ce41c6] | 206 | double x_value = *(double *)(x->data + i*x->strides[1]); |
---|
| 207 | double y_value = *(double *)(y->data + j*y->strides[0]); |
---|
[9bd69098] | 208 | double *result_value = (double *)(result->data + |
---|
[870f131] | 209 | j*result->strides[0] + i*result->strides[1]); |
---|
[9bd69098] | 210 | *result_value = (*model)(x_value, y_value); |
---|
| 211 | } |
---|
| 212 | } |
---|
| 213 | return PyArray_Return(result); |
---|
| 214 | |
---|
| 215 | }else{ |
---|
| 216 | PyErr_SetString(CLamellarModelError, |
---|
| 217 | "CLamellarModel.evaluateTwoDimXY couldn't run."); |
---|
| 218 | return NULL; |
---|
| 219 | } |
---|
| 220 | } |
---|
| 221 | /** |
---|
| 222 | * evalDistribution function evaluate a model function with input vector |
---|
| 223 | * @param args: input q as vector or [qx, qy] where qx, qy are vectors |
---|
| 224 | * |
---|
| 225 | */ |
---|
| 226 | static PyObject * evalDistribution(CLamellarModel *self, PyObject *args){ |
---|
| 227 | PyObject *qx, *qy; |
---|
| 228 | PyArrayObject * pars; |
---|
| 229 | int npars ,mpars; |
---|
| 230 | |
---|
| 231 | // Get parameters |
---|
| 232 | |
---|
| 233 | // Reader parameter dictionary |
---|
[975ec8e] | 234 | self->model->sld_sol = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_sol") ); |
---|
[9bd69098] | 235 | self->model->scale = PyFloat_AsDouble( PyDict_GetItemString(self->params, "scale") ); |
---|
[975ec8e] | 236 | self->model->bi_thick = PyFloat_AsDouble( PyDict_GetItemString(self->params, "bi_thick") ); |
---|
[9bd69098] | 237 | self->model->background = PyFloat_AsDouble( PyDict_GetItemString(self->params, "background") ); |
---|
[975ec8e] | 238 | self->model->sld_bi = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_bi") ); |
---|
[9bd69098] | 239 | // Read in dispersion parameters |
---|
| 240 | PyObject* disp_dict; |
---|
| 241 | DispersionVisitor* visitor = new DispersionVisitor(); |
---|
[975ec8e] | 242 | disp_dict = PyDict_GetItemString(self->dispersion, "bi_thick"); |
---|
| 243 | self->model->bi_thick.dispersion->accept_as_destination(visitor, self->model->bi_thick.dispersion, disp_dict); |
---|
[34c3020] | 244 | |
---|
[9bd69098] | 245 | |
---|
| 246 | // Get input and determine whether we have to supply a 1D or 2D return value. |
---|
| 247 | if ( !PyArg_ParseTuple(args,"O",&pars) ) { |
---|
| 248 | PyErr_SetString(CLamellarModelError, |
---|
| 249 | "CLamellarModel.evalDistribution expects a q value."); |
---|
| 250 | return NULL; |
---|
| 251 | } |
---|
| 252 | // Check params |
---|
| 253 | |
---|
| 254 | if(PyArray_Check(pars)==1) { |
---|
| 255 | |
---|
| 256 | // Length of list should 1 or 2 |
---|
| 257 | npars = pars->nd; |
---|
| 258 | if(npars==1) { |
---|
| 259 | // input is a numpy array |
---|
| 260 | if (PyArray_Check(pars)) { |
---|
| 261 | return evaluateOneDim(self->model, (PyArrayObject*)pars); |
---|
| 262 | } |
---|
| 263 | }else{ |
---|
| 264 | PyErr_SetString(CLamellarModelError, |
---|
| 265 | "CLamellarModel.evalDistribution expect numpy array of one dimension."); |
---|
| 266 | return NULL; |
---|
| 267 | } |
---|
| 268 | }else if( PyList_Check(pars)==1) { |
---|
| 269 | // Length of list should be 2 for I(qx,qy) |
---|
| 270 | mpars = PyList_GET_SIZE(pars); |
---|
| 271 | if(mpars!=2) { |
---|
| 272 | PyErr_SetString(CLamellarModelError, |
---|
| 273 | "CLamellarModel.evalDistribution expects a list of dimension 2."); |
---|
| 274 | return NULL; |
---|
| 275 | } |
---|
| 276 | qx = PyList_GET_ITEM(pars,0); |
---|
| 277 | qy = PyList_GET_ITEM(pars,1); |
---|
| 278 | if (PyArray_Check(qx) && PyArray_Check(qy)) { |
---|
| 279 | return evaluateTwoDimXY(self->model, (PyArrayObject*)qx, |
---|
| 280 | (PyArrayObject*)qy); |
---|
| 281 | }else{ |
---|
| 282 | PyErr_SetString(CLamellarModelError, |
---|
| 283 | "CLamellarModel.evalDistribution expect 2 numpy arrays in list."); |
---|
| 284 | return NULL; |
---|
| 285 | } |
---|
| 286 | } |
---|
[e0a8a3c] | 287 | PyErr_SetString(CLamellarModelError, |
---|
| 288 | "CLamellarModel.evalDistribution couln't be run."); |
---|
| 289 | return NULL; |
---|
| 290 | |
---|
[9bd69098] | 291 | } |
---|
[34c3020] | 292 | |
---|
| 293 | /** |
---|
| 294 | * Function to call to evaluate model |
---|
| 295 | * @param args: input q or [q,phi] |
---|
| 296 | * @return: function value |
---|
| 297 | */ |
---|
| 298 | static PyObject * run(CLamellarModel *self, PyObject *args) { |
---|
| 299 | double q_value, phi_value; |
---|
| 300 | PyObject* pars; |
---|
| 301 | int npars; |
---|
| 302 | |
---|
| 303 | // Get parameters |
---|
| 304 | |
---|
| 305 | // Reader parameter dictionary |
---|
[975ec8e] | 306 | self->model->sld_sol = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_sol") ); |
---|
[34c3020] | 307 | self->model->scale = PyFloat_AsDouble( PyDict_GetItemString(self->params, "scale") ); |
---|
[975ec8e] | 308 | self->model->bi_thick = PyFloat_AsDouble( PyDict_GetItemString(self->params, "bi_thick") ); |
---|
[34c3020] | 309 | self->model->background = PyFloat_AsDouble( PyDict_GetItemString(self->params, "background") ); |
---|
[975ec8e] | 310 | self->model->sld_bi = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_bi") ); |
---|
[34c3020] | 311 | // Read in dispersion parameters |
---|
| 312 | PyObject* disp_dict; |
---|
| 313 | DispersionVisitor* visitor = new DispersionVisitor(); |
---|
[975ec8e] | 314 | disp_dict = PyDict_GetItemString(self->dispersion, "bi_thick"); |
---|
| 315 | self->model->bi_thick.dispersion->accept_as_destination(visitor, self->model->bi_thick.dispersion, disp_dict); |
---|
[34c3020] | 316 | |
---|
| 317 | |
---|
| 318 | // Get input and determine whether we have to supply a 1D or 2D return value. |
---|
| 319 | if ( !PyArg_ParseTuple(args,"O",&pars) ) { |
---|
| 320 | PyErr_SetString(CLamellarModelError, |
---|
| 321 | "CLamellarModel.run expects a q value."); |
---|
| 322 | return NULL; |
---|
| 323 | } |
---|
| 324 | |
---|
| 325 | // Check params |
---|
| 326 | if( PyList_Check(pars)==1) { |
---|
| 327 | |
---|
| 328 | // Length of list should be 2 for I(q,phi) |
---|
| 329 | npars = PyList_GET_SIZE(pars); |
---|
| 330 | if(npars!=2) { |
---|
| 331 | PyErr_SetString(CLamellarModelError, |
---|
| 332 | "CLamellarModel.run expects a double or a list of dimension 2."); |
---|
| 333 | return NULL; |
---|
| 334 | } |
---|
| 335 | // We have a vector q, get the q and phi values at which |
---|
| 336 | // to evaluate I(q,phi) |
---|
| 337 | q_value = CLamellarModel_readDouble(PyList_GET_ITEM(pars,0)); |
---|
| 338 | phi_value = CLamellarModel_readDouble(PyList_GET_ITEM(pars,1)); |
---|
| 339 | // Skip zero |
---|
| 340 | if (q_value==0) { |
---|
| 341 | return Py_BuildValue("d",0.0); |
---|
| 342 | } |
---|
| 343 | return Py_BuildValue("d",(*(self->model)).evaluate_rphi(q_value,phi_value)); |
---|
| 344 | |
---|
| 345 | } else { |
---|
| 346 | |
---|
| 347 | // We have a scalar q, we will evaluate I(q) |
---|
| 348 | q_value = CLamellarModel_readDouble(pars); |
---|
| 349 | |
---|
| 350 | return Py_BuildValue("d",(*(self->model))(q_value)); |
---|
| 351 | } |
---|
| 352 | } |
---|
[5eb9154] | 353 | /** |
---|
| 354 | * Function to call to calculate_ER |
---|
| 355 | * @return: effective radius value |
---|
| 356 | */ |
---|
| 357 | static PyObject * calculate_ER(CLamellarModel *self) { |
---|
[34c3020] | 358 | |
---|
[5eb9154] | 359 | PyObject* pars; |
---|
| 360 | int npars; |
---|
| 361 | |
---|
| 362 | // Get parameters |
---|
| 363 | |
---|
| 364 | // Reader parameter dictionary |
---|
| 365 | self->model->sld_sol = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_sol") ); |
---|
| 366 | self->model->scale = PyFloat_AsDouble( PyDict_GetItemString(self->params, "scale") ); |
---|
| 367 | self->model->bi_thick = PyFloat_AsDouble( PyDict_GetItemString(self->params, "bi_thick") ); |
---|
| 368 | self->model->background = PyFloat_AsDouble( PyDict_GetItemString(self->params, "background") ); |
---|
| 369 | self->model->sld_bi = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_bi") ); |
---|
| 370 | // Read in dispersion parameters |
---|
| 371 | PyObject* disp_dict; |
---|
| 372 | DispersionVisitor* visitor = new DispersionVisitor(); |
---|
| 373 | disp_dict = PyDict_GetItemString(self->dispersion, "bi_thick"); |
---|
| 374 | self->model->bi_thick.dispersion->accept_as_destination(visitor, self->model->bi_thick.dispersion, disp_dict); |
---|
| 375 | |
---|
| 376 | |
---|
| 377 | return Py_BuildValue("d",(*(self->model)).calculate_ER()); |
---|
| 378 | |
---|
| 379 | } |
---|
[34c3020] | 380 | /** |
---|
| 381 | * Function to call to evaluate model in cartesian coordinates |
---|
| 382 | * @param args: input q or [qx, qy]] |
---|
| 383 | * @return: function value |
---|
| 384 | */ |
---|
| 385 | static PyObject * runXY(CLamellarModel *self, PyObject *args) { |
---|
| 386 | double qx_value, qy_value; |
---|
| 387 | PyObject* pars; |
---|
| 388 | int npars; |
---|
| 389 | |
---|
| 390 | // Get parameters |
---|
| 391 | |
---|
| 392 | // Reader parameter dictionary |
---|
[975ec8e] | 393 | self->model->sld_sol = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_sol") ); |
---|
[34c3020] | 394 | self->model->scale = PyFloat_AsDouble( PyDict_GetItemString(self->params, "scale") ); |
---|
[975ec8e] | 395 | self->model->bi_thick = PyFloat_AsDouble( PyDict_GetItemString(self->params, "bi_thick") ); |
---|
[34c3020] | 396 | self->model->background = PyFloat_AsDouble( PyDict_GetItemString(self->params, "background") ); |
---|
[975ec8e] | 397 | self->model->sld_bi = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_bi") ); |
---|
[34c3020] | 398 | // Read in dispersion parameters |
---|
| 399 | PyObject* disp_dict; |
---|
| 400 | DispersionVisitor* visitor = new DispersionVisitor(); |
---|
[975ec8e] | 401 | disp_dict = PyDict_GetItemString(self->dispersion, "bi_thick"); |
---|
| 402 | self->model->bi_thick.dispersion->accept_as_destination(visitor, self->model->bi_thick.dispersion, disp_dict); |
---|
[34c3020] | 403 | |
---|
| 404 | |
---|
| 405 | // Get input and determine whether we have to supply a 1D or 2D return value. |
---|
| 406 | if ( !PyArg_ParseTuple(args,"O",&pars) ) { |
---|
| 407 | PyErr_SetString(CLamellarModelError, |
---|
| 408 | "CLamellarModel.run expects a q value."); |
---|
| 409 | return NULL; |
---|
| 410 | } |
---|
| 411 | |
---|
| 412 | // Check params |
---|
| 413 | if( PyList_Check(pars)==1) { |
---|
| 414 | |
---|
| 415 | // Length of list should be 2 for I(qx, qy)) |
---|
| 416 | npars = PyList_GET_SIZE(pars); |
---|
| 417 | if(npars!=2) { |
---|
| 418 | PyErr_SetString(CLamellarModelError, |
---|
| 419 | "CLamellarModel.run expects a double or a list of dimension 2."); |
---|
| 420 | return NULL; |
---|
| 421 | } |
---|
| 422 | // We have a vector q, get the qx and qy values at which |
---|
| 423 | // to evaluate I(qx,qy) |
---|
| 424 | qx_value = CLamellarModel_readDouble(PyList_GET_ITEM(pars,0)); |
---|
| 425 | qy_value = CLamellarModel_readDouble(PyList_GET_ITEM(pars,1)); |
---|
| 426 | return Py_BuildValue("d",(*(self->model))(qx_value,qy_value)); |
---|
| 427 | |
---|
| 428 | } else { |
---|
| 429 | |
---|
| 430 | // We have a scalar q, we will evaluate I(q) |
---|
| 431 | qx_value = CLamellarModel_readDouble(pars); |
---|
| 432 | |
---|
| 433 | return Py_BuildValue("d",(*(self->model))(qx_value)); |
---|
| 434 | } |
---|
| 435 | } |
---|
| 436 | |
---|
| 437 | static PyObject * reset(CLamellarModel *self, PyObject *args) { |
---|
| 438 | |
---|
| 439 | |
---|
| 440 | return Py_BuildValue("d",0.0); |
---|
| 441 | } |
---|
| 442 | |
---|
| 443 | static PyObject * set_dispersion(CLamellarModel *self, PyObject *args) { |
---|
| 444 | PyObject * disp; |
---|
| 445 | const char * par_name; |
---|
| 446 | |
---|
| 447 | if ( !PyArg_ParseTuple(args,"sO", &par_name, &disp) ) { |
---|
| 448 | PyErr_SetString(CLamellarModelError, |
---|
| 449 | "CLamellarModel.set_dispersion expects a DispersionModel object."); |
---|
| 450 | return NULL; |
---|
| 451 | } |
---|
| 452 | void *temp = PyCObject_AsVoidPtr(disp); |
---|
| 453 | DispersionModel * dispersion = static_cast<DispersionModel *>(temp); |
---|
| 454 | |
---|
| 455 | |
---|
| 456 | // Ugliness necessary to go from python to C |
---|
| 457 | // TODO: refactor this |
---|
[975ec8e] | 458 | if (!strcmp(par_name, "bi_thick")) { |
---|
| 459 | self->model->bi_thick.dispersion = dispersion; |
---|
| 460 | } else { |
---|
[34c3020] | 461 | PyErr_SetString(CLamellarModelError, |
---|
| 462 | "CLamellarModel.set_dispersion expects a valid parameter name."); |
---|
| 463 | return NULL; |
---|
| 464 | } |
---|
| 465 | |
---|
| 466 | DispersionVisitor* visitor = new DispersionVisitor(); |
---|
| 467 | PyObject * disp_dict = PyDict_New(); |
---|
| 468 | dispersion->accept_as_source(visitor, dispersion, disp_dict); |
---|
| 469 | PyDict_SetItemString(self->dispersion, par_name, disp_dict); |
---|
| 470 | return Py_BuildValue("i",1); |
---|
| 471 | } |
---|
| 472 | |
---|
| 473 | |
---|
| 474 | static PyMethodDef CLamellarModel_methods[] = { |
---|
| 475 | {"run", (PyCFunction)run , METH_VARARGS, |
---|
| 476 | "Evaluate the model at a given Q or Q, phi"}, |
---|
| 477 | {"runXY", (PyCFunction)runXY , METH_VARARGS, |
---|
| 478 | "Evaluate the model at a given Q or Qx, Qy"}, |
---|
[5eb9154] | 479 | {"calculate_ER", (PyCFunction)calculate_ER , METH_VARARGS, |
---|
| 480 | "Evaluate the model at a given Q or Q, phi"}, |
---|
[9bd69098] | 481 | |
---|
| 482 | {"evalDistribution", (PyCFunction)evalDistribution , METH_VARARGS, |
---|
| 483 | "Evaluate the model at a given Q or Qx, Qy vector "}, |
---|
[34c3020] | 484 | {"reset", (PyCFunction)reset , METH_VARARGS, |
---|
| 485 | "Reset pair correlation"}, |
---|
| 486 | {"set_dispersion", (PyCFunction)set_dispersion , METH_VARARGS, |
---|
| 487 | "Set the dispersion model for a given parameter"}, |
---|
| 488 | {NULL} |
---|
| 489 | }; |
---|
| 490 | |
---|
| 491 | static PyTypeObject CLamellarModelType = { |
---|
| 492 | PyObject_HEAD_INIT(NULL) |
---|
| 493 | 0, /*ob_size*/ |
---|
| 494 | "CLamellarModel", /*tp_name*/ |
---|
| 495 | sizeof(CLamellarModel), /*tp_basicsize*/ |
---|
| 496 | 0, /*tp_itemsize*/ |
---|
| 497 | (destructor)CLamellarModel_dealloc, /*tp_dealloc*/ |
---|
| 498 | 0, /*tp_print*/ |
---|
| 499 | 0, /*tp_getattr*/ |
---|
| 500 | 0, /*tp_setattr*/ |
---|
| 501 | 0, /*tp_compare*/ |
---|
| 502 | 0, /*tp_repr*/ |
---|
| 503 | 0, /*tp_as_number*/ |
---|
| 504 | 0, /*tp_as_sequence*/ |
---|
| 505 | 0, /*tp_as_mapping*/ |
---|
| 506 | 0, /*tp_hash */ |
---|
| 507 | 0, /*tp_call*/ |
---|
| 508 | 0, /*tp_str*/ |
---|
| 509 | 0, /*tp_getattro*/ |
---|
| 510 | 0, /*tp_setattro*/ |
---|
| 511 | 0, /*tp_as_buffer*/ |
---|
| 512 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/ |
---|
| 513 | "CLamellarModel objects", /* tp_doc */ |
---|
| 514 | 0, /* tp_traverse */ |
---|
| 515 | 0, /* tp_clear */ |
---|
| 516 | 0, /* tp_richcompare */ |
---|
| 517 | 0, /* tp_weaklistoffset */ |
---|
| 518 | 0, /* tp_iter */ |
---|
| 519 | 0, /* tp_iternext */ |
---|
| 520 | CLamellarModel_methods, /* tp_methods */ |
---|
| 521 | CLamellarModel_members, /* tp_members */ |
---|
| 522 | 0, /* tp_getset */ |
---|
| 523 | 0, /* tp_base */ |
---|
| 524 | 0, /* tp_dict */ |
---|
| 525 | 0, /* tp_descr_get */ |
---|
| 526 | 0, /* tp_descr_set */ |
---|
| 527 | 0, /* tp_dictoffset */ |
---|
| 528 | (initproc)CLamellarModel_init, /* tp_init */ |
---|
| 529 | 0, /* tp_alloc */ |
---|
| 530 | CLamellarModel_new, /* tp_new */ |
---|
| 531 | }; |
---|
| 532 | |
---|
| 533 | |
---|
[9bd69098] | 534 | //static PyMethodDef module_methods[] = { |
---|
| 535 | // {NULL} |
---|
| 536 | //}; |
---|
[34c3020] | 537 | |
---|
| 538 | /** |
---|
| 539 | * Function used to add the model class to a module |
---|
| 540 | * @param module: module to add the class to |
---|
| 541 | */ |
---|
| 542 | void addCLamellarModel(PyObject *module) { |
---|
| 543 | PyObject *d; |
---|
| 544 | |
---|
| 545 | if (PyType_Ready(&CLamellarModelType) < 0) |
---|
| 546 | return; |
---|
| 547 | |
---|
| 548 | Py_INCREF(&CLamellarModelType); |
---|
| 549 | PyModule_AddObject(module, "CLamellarModel", (PyObject *)&CLamellarModelType); |
---|
| 550 | |
---|
| 551 | d = PyModule_GetDict(module); |
---|
| 552 | CLamellarModelError = PyErr_NewException("CLamellarModel.error", NULL, NULL); |
---|
| 553 | PyDict_SetItemString(d, "CLamellarModelError", CLamellarModelError); |
---|
| 554 | } |
---|
| 555 | |
---|