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