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