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