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