[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 | } |
---|
[8f5b34a] | 161 | result = (PyArrayObject *)PyArray_FromDims(q->nd, (int *)(q->dimensions), PyArray_DOUBLE); |
---|
[9bd69098] | 162 | if (result == NULL) { |
---|
| 163 | const char * message= "Could not create result "; |
---|
| 164 | PyErr_SetString(PyExc_RuntimeError , message); |
---|
| 165 | return NULL; |
---|
| 166 | } |
---|
[0b082f3] | 167 | #pragma omp parallel for |
---|
[9bd69098] | 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; |
---|
[0b082f3] | 185 | int 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 |
---|
[8f5b34a] | 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. */ |
---|
[0b082f3] | 209 | #pragma omp parallel for |
---|
| 210 | for (int i=0; i< x_len; i++) { |
---|
[3080527] | 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(CHayterMSAStructureError, |
---|
| 221 | "CHayterMSAStructure.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(CHayterMSAStructure *self, PyObject *args){ |
---|
| 231 | PyObject *qx, *qy; |
---|
| 232 | PyArrayObject * pars; |
---|
| 233 | int npars ,mpars; |
---|
| 234 | |
---|
| 235 | // Get parameters |
---|
| 236 | |
---|
[b1c3295] | 237 | // Reader parameter dictionary |
---|
| 238 | self->model->temperature = PyFloat_AsDouble( PyDict_GetItemString(self->params, "temperature") ); |
---|
| 239 | self->model->saltconc = PyFloat_AsDouble( PyDict_GetItemString(self->params, "saltconc") ); |
---|
| 240 | self->model->dielectconst = PyFloat_AsDouble( PyDict_GetItemString(self->params, "dielectconst") ); |
---|
| 241 | self->model->volfraction = PyFloat_AsDouble( PyDict_GetItemString(self->params, "volfraction") ); |
---|
| 242 | self->model->charge = PyFloat_AsDouble( PyDict_GetItemString(self->params, "charge") ); |
---|
| 243 | self->model->effect_radius = PyFloat_AsDouble( PyDict_GetItemString(self->params, "effect_radius") ); |
---|
| 244 | // Read in dispersion parameters |
---|
| 245 | PyObject* disp_dict; |
---|
| 246 | DispersionVisitor* visitor = new DispersionVisitor(); |
---|
| 247 | disp_dict = PyDict_GetItemString(self->dispersion, "effect_radius"); |
---|
| 248 | self->model->effect_radius.dispersion->accept_as_destination(visitor, self->model->effect_radius.dispersion, disp_dict); |
---|
[9bd69098] | 249 | |
---|
| 250 | |
---|
| 251 | // Get input and determine whether we have to supply a 1D or 2D return value. |
---|
| 252 | if ( !PyArg_ParseTuple(args,"O",&pars) ) { |
---|
| 253 | PyErr_SetString(CHayterMSAStructureError, |
---|
| 254 | "CHayterMSAStructure.evalDistribution expects a q value."); |
---|
| 255 | return NULL; |
---|
| 256 | } |
---|
| 257 | // Check params |
---|
| 258 | |
---|
| 259 | if(PyArray_Check(pars)==1) { |
---|
| 260 | |
---|
| 261 | // Length of list should 1 or 2 |
---|
| 262 | npars = pars->nd; |
---|
| 263 | if(npars==1) { |
---|
| 264 | // input is a numpy array |
---|
| 265 | if (PyArray_Check(pars)) { |
---|
| 266 | return evaluateOneDim(self->model, (PyArrayObject*)pars); |
---|
| 267 | } |
---|
| 268 | }else{ |
---|
| 269 | PyErr_SetString(CHayterMSAStructureError, |
---|
| 270 | "CHayterMSAStructure.evalDistribution expect numpy array of one dimension."); |
---|
| 271 | return NULL; |
---|
| 272 | } |
---|
| 273 | }else if( PyList_Check(pars)==1) { |
---|
| 274 | // Length of list should be 2 for I(qx,qy) |
---|
| 275 | mpars = PyList_GET_SIZE(pars); |
---|
| 276 | if(mpars!=2) { |
---|
| 277 | PyErr_SetString(CHayterMSAStructureError, |
---|
| 278 | "CHayterMSAStructure.evalDistribution expects a list of dimension 2."); |
---|
| 279 | return NULL; |
---|
| 280 | } |
---|
| 281 | qx = PyList_GET_ITEM(pars,0); |
---|
| 282 | qy = PyList_GET_ITEM(pars,1); |
---|
| 283 | if (PyArray_Check(qx) && PyArray_Check(qy)) { |
---|
| 284 | return evaluateTwoDimXY(self->model, (PyArrayObject*)qx, |
---|
| 285 | (PyArrayObject*)qy); |
---|
| 286 | }else{ |
---|
| 287 | PyErr_SetString(CHayterMSAStructureError, |
---|
| 288 | "CHayterMSAStructure.evalDistribution expect 2 numpy arrays in list."); |
---|
| 289 | return NULL; |
---|
| 290 | } |
---|
| 291 | } |
---|
[e0a8a3c] | 292 | PyErr_SetString(CHayterMSAStructureError, |
---|
| 293 | "CHayterMSAStructure.evalDistribution couln't be run."); |
---|
| 294 | return NULL; |
---|
| 295 | |
---|
[9bd69098] | 296 | } |
---|
[25579e8] | 297 | |
---|
| 298 | /** |
---|
| 299 | * Function to call to evaluate model |
---|
| 300 | * @param args: input q or [q,phi] |
---|
| 301 | * @return: function value |
---|
| 302 | */ |
---|
| 303 | static PyObject * run(CHayterMSAStructure *self, PyObject *args) { |
---|
| 304 | double q_value, phi_value; |
---|
| 305 | PyObject* pars; |
---|
| 306 | int npars; |
---|
| 307 | |
---|
| 308 | // Get parameters |
---|
| 309 | |
---|
[b1c3295] | 310 | // Reader parameter dictionary |
---|
| 311 | self->model->temperature = PyFloat_AsDouble( PyDict_GetItemString(self->params, "temperature") ); |
---|
| 312 | self->model->saltconc = PyFloat_AsDouble( PyDict_GetItemString(self->params, "saltconc") ); |
---|
| 313 | self->model->dielectconst = PyFloat_AsDouble( PyDict_GetItemString(self->params, "dielectconst") ); |
---|
| 314 | self->model->volfraction = PyFloat_AsDouble( PyDict_GetItemString(self->params, "volfraction") ); |
---|
| 315 | self->model->charge = PyFloat_AsDouble( PyDict_GetItemString(self->params, "charge") ); |
---|
| 316 | self->model->effect_radius = PyFloat_AsDouble( PyDict_GetItemString(self->params, "effect_radius") ); |
---|
| 317 | // Read in dispersion parameters |
---|
| 318 | PyObject* disp_dict; |
---|
| 319 | DispersionVisitor* visitor = new DispersionVisitor(); |
---|
| 320 | disp_dict = PyDict_GetItemString(self->dispersion, "effect_radius"); |
---|
| 321 | self->model->effect_radius.dispersion->accept_as_destination(visitor, self->model->effect_radius.dispersion, disp_dict); |
---|
[25579e8] | 322 | |
---|
| 323 | |
---|
| 324 | // Get input and determine whether we have to supply a 1D or 2D return value. |
---|
| 325 | if ( !PyArg_ParseTuple(args,"O",&pars) ) { |
---|
| 326 | PyErr_SetString(CHayterMSAStructureError, |
---|
| 327 | "CHayterMSAStructure.run expects a q value."); |
---|
| 328 | return NULL; |
---|
| 329 | } |
---|
| 330 | |
---|
| 331 | // Check params |
---|
| 332 | if( PyList_Check(pars)==1) { |
---|
| 333 | |
---|
| 334 | // Length of list should be 2 for I(q,phi) |
---|
| 335 | npars = PyList_GET_SIZE(pars); |
---|
| 336 | if(npars!=2) { |
---|
| 337 | PyErr_SetString(CHayterMSAStructureError, |
---|
| 338 | "CHayterMSAStructure.run expects a double or a list of dimension 2."); |
---|
| 339 | return NULL; |
---|
| 340 | } |
---|
| 341 | // We have a vector q, get the q and phi values at which |
---|
| 342 | // to evaluate I(q,phi) |
---|
| 343 | q_value = CHayterMSAStructure_readDouble(PyList_GET_ITEM(pars,0)); |
---|
| 344 | phi_value = CHayterMSAStructure_readDouble(PyList_GET_ITEM(pars,1)); |
---|
| 345 | // Skip zero |
---|
| 346 | if (q_value==0) { |
---|
| 347 | return Py_BuildValue("d",0.0); |
---|
| 348 | } |
---|
| 349 | return Py_BuildValue("d",(*(self->model)).evaluate_rphi(q_value,phi_value)); |
---|
| 350 | |
---|
| 351 | } else { |
---|
| 352 | |
---|
| 353 | // We have a scalar q, we will evaluate I(q) |
---|
| 354 | q_value = CHayterMSAStructure_readDouble(pars); |
---|
| 355 | |
---|
| 356 | return Py_BuildValue("d",(*(self->model))(q_value)); |
---|
| 357 | } |
---|
| 358 | } |
---|
[5eb9154] | 359 | /** |
---|
| 360 | * Function to call to calculate_ER |
---|
| 361 | * @return: effective radius value |
---|
| 362 | */ |
---|
| 363 | static PyObject * calculate_ER(CHayterMSAStructure *self) { |
---|
[25579e8] | 364 | |
---|
[5eb9154] | 365 | // Get parameters |
---|
| 366 | |
---|
[b1c3295] | 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); |
---|
[5eb9154] | 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 | |
---|
[b1c3295] | 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") ); |
---|
| 402 | self->model->effect_radius = PyFloat_AsDouble( PyDict_GetItemString(self->params, "effect_radius") ); |
---|
| 403 | // Read in dispersion parameters |
---|
| 404 | PyObject* disp_dict; |
---|
| 405 | DispersionVisitor* visitor = new DispersionVisitor(); |
---|
| 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) { |
---|
[b1c3295] | 443 | |
---|
[25579e8] | 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 |
---|
[b1c3295] | 462 | // TODO: refactor this |
---|
| 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); |
---|
[2605da22] | 557 | static char error_name[] = "CHayterMSAStructure.error"; |
---|
| 558 | CHayterMSAStructureError = PyErr_NewException(error_name, NULL, NULL); |
---|
[25579e8] | 559 | PyDict_SetItemString(d, "CHayterMSAStructureError", CHayterMSAStructureError); |
---|
| 560 | } |
---|
[b1c3295] | 561 | |
---|