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