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