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