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