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