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