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