[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 |
---|
| 92 | PyDict_SetItemString(self->params,"core_sld",Py_BuildValue("d",0.000006)); |
---|
| 93 | PyDict_SetItemString(self->params,"core_radius",Py_BuildValue("d",60.000000)); |
---|
| 94 | PyDict_SetItemString(self->params,"n_pairs",Py_BuildValue("d",2.000000)); |
---|
| 95 | PyDict_SetItemString(self->params,"w_thickness",Py_BuildValue("d",10.000000)); |
---|
| 96 | PyDict_SetItemString(self->params,"s_thickness",Py_BuildValue("d",10.000000)); |
---|
| 97 | PyDict_SetItemString(self->params,"scale",Py_BuildValue("d",1.000000)); |
---|
| 98 | PyDict_SetItemString(self->params,"background",Py_BuildValue("d",0.000000)); |
---|
| 99 | PyDict_SetItemString(self->params,"shell_sld",Py_BuildValue("d",0.000000)); |
---|
| 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; |
---|
| 189 | int i,j, x_len, y_len, dims[2]; |
---|
| 190 | //check validity of input vectors |
---|
| 191 | if (x->nd != 2 || x->descr->type_num != PyArray_DOUBLE |
---|
| 192 | || y->nd != 2 || y->descr->type_num != PyArray_DOUBLE |
---|
| 193 | || y->dimensions[1] != x->dimensions[0]){ |
---|
| 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 | |
---|
[9ce41c6] | 201 | x_len = dims[1]= x->dimensions[1]; |
---|
| 202 | y_len = dims[0]= y->dimensions[0]; |
---|
[9bd69098] | 203 | |
---|
| 204 | // Make a new double matrix of same dims |
---|
| 205 | result=(PyArrayObject *) PyArray_FromDims(2,dims,NPY_DOUBLE); |
---|
| 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. */ |
---|
[e0a8a3c] | 213 | for ( j=0; j< y_len; j++) { |
---|
| 214 | for ( i=0; i< x_len; i++) { |
---|
[9ce41c6] | 215 | double x_value = *(double *)(x->data + i*x->strides[1]); |
---|
| 216 | double y_value = *(double *)(y->data + j*y->strides[0]); |
---|
[9bd69098] | 217 | double *result_value = (double *)(result->data + |
---|
[870f131] | 218 | j*result->strides[0] + i*result->strides[1]); |
---|
[9bd69098] | 219 | *result_value = (*model)(x_value, y_value); |
---|
| 220 | } |
---|
| 221 | } |
---|
| 222 | return PyArray_Return(result); |
---|
| 223 | |
---|
| 224 | }else{ |
---|
| 225 | PyErr_SetString(CMultiShellModelError, |
---|
| 226 | "CMultiShellModel.evaluateTwoDimXY couldn't run."); |
---|
| 227 | return NULL; |
---|
| 228 | } |
---|
| 229 | } |
---|
| 230 | /** |
---|
| 231 | * evalDistribution function evaluate a model function with input vector |
---|
| 232 | * @param args: input q as vector or [qx, qy] where qx, qy are vectors |
---|
| 233 | * |
---|
| 234 | */ |
---|
| 235 | static PyObject * evalDistribution(CMultiShellModel *self, PyObject *args){ |
---|
| 236 | PyObject *qx, *qy; |
---|
| 237 | PyArrayObject * pars; |
---|
| 238 | int npars ,mpars; |
---|
| 239 | |
---|
| 240 | // Get parameters |
---|
| 241 | |
---|
| 242 | // Reader parameter dictionary |
---|
| 243 | self->model->core_sld = PyFloat_AsDouble( PyDict_GetItemString(self->params, "core_sld") ); |
---|
| 244 | self->model->core_radius = PyFloat_AsDouble( PyDict_GetItemString(self->params, "core_radius") ); |
---|
| 245 | self->model->n_pairs = PyFloat_AsDouble( PyDict_GetItemString(self->params, "n_pairs") ); |
---|
| 246 | self->model->w_thickness = PyFloat_AsDouble( PyDict_GetItemString(self->params, "w_thickness") ); |
---|
| 247 | self->model->s_thickness = PyFloat_AsDouble( PyDict_GetItemString(self->params, "s_thickness") ); |
---|
| 248 | self->model->scale = PyFloat_AsDouble( PyDict_GetItemString(self->params, "scale") ); |
---|
| 249 | self->model->background = PyFloat_AsDouble( PyDict_GetItemString(self->params, "background") ); |
---|
| 250 | self->model->shell_sld = PyFloat_AsDouble( PyDict_GetItemString(self->params, "shell_sld") ); |
---|
| 251 | // Read in dispersion parameters |
---|
| 252 | PyObject* disp_dict; |
---|
| 253 | DispersionVisitor* visitor = new DispersionVisitor(); |
---|
| 254 | disp_dict = PyDict_GetItemString(self->dispersion, "core_radius"); |
---|
| 255 | self->model->core_radius.dispersion->accept_as_destination(visitor, self->model->core_radius.dispersion, disp_dict); |
---|
| 256 | disp_dict = PyDict_GetItemString(self->dispersion, "s_thickness"); |
---|
| 257 | self->model->s_thickness.dispersion->accept_as_destination(visitor, self->model->s_thickness.dispersion, disp_dict); |
---|
| 258 | disp_dict = PyDict_GetItemString(self->dispersion, "w_thickness"); |
---|
| 259 | self->model->w_thickness.dispersion->accept_as_destination(visitor, self->model->w_thickness.dispersion, disp_dict); |
---|
| 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(CMultiShellModelError, |
---|
| 265 | "CMultiShellModel.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(CMultiShellModelError, |
---|
| 281 | "CMultiShellModel.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(CMultiShellModelError, |
---|
| 289 | "CMultiShellModel.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(CMultiShellModelError, |
---|
| 299 | "CMultiShellModel.evalDistribution expect 2 numpy arrays in list."); |
---|
| 300 | return NULL; |
---|
| 301 | } |
---|
| 302 | } |
---|
[e0a8a3c] | 303 | PyErr_SetString(CMultiShellModelError, |
---|
| 304 | "CMultiShellModel.evalDistribution couln't be run."); |
---|
| 305 | return NULL; |
---|
| 306 | |
---|
[9bd69098] | 307 | } |
---|
[3d25331f] | 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(CMultiShellModel *self, PyObject *args) { |
---|
| 315 | double q_value, phi_value; |
---|
| 316 | PyObject* pars; |
---|
| 317 | int npars; |
---|
| 318 | |
---|
| 319 | // Get parameters |
---|
| 320 | |
---|
| 321 | // Reader parameter dictionary |
---|
| 322 | self->model->core_sld = PyFloat_AsDouble( PyDict_GetItemString(self->params, "core_sld") ); |
---|
| 323 | self->model->core_radius = PyFloat_AsDouble( PyDict_GetItemString(self->params, "core_radius") ); |
---|
| 324 | self->model->n_pairs = PyFloat_AsDouble( PyDict_GetItemString(self->params, "n_pairs") ); |
---|
| 325 | self->model->w_thickness = PyFloat_AsDouble( PyDict_GetItemString(self->params, "w_thickness") ); |
---|
| 326 | self->model->s_thickness = PyFloat_AsDouble( PyDict_GetItemString(self->params, "s_thickness") ); |
---|
| 327 | self->model->scale = PyFloat_AsDouble( PyDict_GetItemString(self->params, "scale") ); |
---|
| 328 | self->model->background = PyFloat_AsDouble( PyDict_GetItemString(self->params, "background") ); |
---|
| 329 | self->model->shell_sld = PyFloat_AsDouble( PyDict_GetItemString(self->params, "shell_sld") ); |
---|
| 330 | // Read in dispersion parameters |
---|
| 331 | PyObject* disp_dict; |
---|
| 332 | DispersionVisitor* visitor = new DispersionVisitor(); |
---|
| 333 | disp_dict = PyDict_GetItemString(self->dispersion, "core_radius"); |
---|
| 334 | self->model->core_radius.dispersion->accept_as_destination(visitor, self->model->core_radius.dispersion, disp_dict); |
---|
[2cc633b] | 335 | disp_dict = PyDict_GetItemString(self->dispersion, "s_thickness"); |
---|
| 336 | self->model->s_thickness.dispersion->accept_as_destination(visitor, self->model->s_thickness.dispersion, disp_dict); |
---|
| 337 | disp_dict = PyDict_GetItemString(self->dispersion, "w_thickness"); |
---|
| 338 | self->model->w_thickness.dispersion->accept_as_destination(visitor, self->model->w_thickness.dispersion, disp_dict); |
---|
[3d25331f] | 339 | |
---|
| 340 | |
---|
| 341 | // Get input and determine whether we have to supply a 1D or 2D return value. |
---|
| 342 | if ( !PyArg_ParseTuple(args,"O",&pars) ) { |
---|
| 343 | PyErr_SetString(CMultiShellModelError, |
---|
| 344 | "CMultiShellModel.run expects a q value."); |
---|
| 345 | return NULL; |
---|
| 346 | } |
---|
| 347 | |
---|
| 348 | // Check params |
---|
| 349 | if( PyList_Check(pars)==1) { |
---|
| 350 | |
---|
| 351 | // Length of list should be 2 for I(q,phi) |
---|
| 352 | npars = PyList_GET_SIZE(pars); |
---|
| 353 | if(npars!=2) { |
---|
| 354 | PyErr_SetString(CMultiShellModelError, |
---|
| 355 | "CMultiShellModel.run expects a double or a list of dimension 2."); |
---|
| 356 | return NULL; |
---|
| 357 | } |
---|
| 358 | // We have a vector q, get the q and phi values at which |
---|
| 359 | // to evaluate I(q,phi) |
---|
| 360 | q_value = CMultiShellModel_readDouble(PyList_GET_ITEM(pars,0)); |
---|
| 361 | phi_value = CMultiShellModel_readDouble(PyList_GET_ITEM(pars,1)); |
---|
| 362 | // Skip zero |
---|
| 363 | if (q_value==0) { |
---|
| 364 | return Py_BuildValue("d",0.0); |
---|
| 365 | } |
---|
| 366 | return Py_BuildValue("d",(*(self->model)).evaluate_rphi(q_value,phi_value)); |
---|
| 367 | |
---|
| 368 | } else { |
---|
| 369 | |
---|
| 370 | // We have a scalar q, we will evaluate I(q) |
---|
| 371 | q_value = CMultiShellModel_readDouble(pars); |
---|
| 372 | |
---|
| 373 | return Py_BuildValue("d",(*(self->model))(q_value)); |
---|
| 374 | } |
---|
| 375 | } |
---|
[5eb9154] | 376 | /** |
---|
| 377 | * Function to call to calculate_ER |
---|
| 378 | * @return: effective radius value |
---|
| 379 | */ |
---|
| 380 | static PyObject * calculate_ER(CMultiShellModel *self) { |
---|
| 381 | |
---|
| 382 | PyObject* pars; |
---|
| 383 | int npars; |
---|
| 384 | |
---|
| 385 | // Get parameters |
---|
| 386 | |
---|
| 387 | // Reader parameter dictionary |
---|
| 388 | self->model->core_sld = PyFloat_AsDouble( PyDict_GetItemString(self->params, "core_sld") ); |
---|
| 389 | self->model->core_radius = PyFloat_AsDouble( PyDict_GetItemString(self->params, "core_radius") ); |
---|
| 390 | self->model->n_pairs = PyFloat_AsDouble( PyDict_GetItemString(self->params, "n_pairs") ); |
---|
| 391 | self->model->w_thickness = PyFloat_AsDouble( PyDict_GetItemString(self->params, "w_thickness") ); |
---|
| 392 | self->model->s_thickness = PyFloat_AsDouble( PyDict_GetItemString(self->params, "s_thickness") ); |
---|
| 393 | self->model->scale = PyFloat_AsDouble( PyDict_GetItemString(self->params, "scale") ); |
---|
| 394 | self->model->background = PyFloat_AsDouble( PyDict_GetItemString(self->params, "background") ); |
---|
| 395 | self->model->shell_sld = PyFloat_AsDouble( PyDict_GetItemString(self->params, "shell_sld") ); |
---|
| 396 | // Read in dispersion parameters |
---|
| 397 | PyObject* disp_dict; |
---|
| 398 | DispersionVisitor* visitor = new DispersionVisitor(); |
---|
| 399 | disp_dict = PyDict_GetItemString(self->dispersion, "core_radius"); |
---|
| 400 | self->model->core_radius.dispersion->accept_as_destination(visitor, self->model->core_radius.dispersion, disp_dict); |
---|
| 401 | disp_dict = PyDict_GetItemString(self->dispersion, "s_thickness"); |
---|
| 402 | self->model->s_thickness.dispersion->accept_as_destination(visitor, self->model->s_thickness.dispersion, disp_dict); |
---|
| 403 | disp_dict = PyDict_GetItemString(self->dispersion, "w_thickness"); |
---|
| 404 | self->model->w_thickness.dispersion->accept_as_destination(visitor, self->model->w_thickness.dispersion, disp_dict); |
---|
[3d25331f] | 405 | |
---|
[5eb9154] | 406 | |
---|
| 407 | return Py_BuildValue("d",(*(self->model)).calculate_ER()); |
---|
| 408 | |
---|
| 409 | } |
---|
[3d25331f] | 410 | /** |
---|
| 411 | * Function to call to evaluate model in cartesian coordinates |
---|
| 412 | * @param args: input q or [qx, qy]] |
---|
| 413 | * @return: function value |
---|
| 414 | */ |
---|
| 415 | static PyObject * runXY(CMultiShellModel *self, PyObject *args) { |
---|
| 416 | double qx_value, qy_value; |
---|
| 417 | PyObject* pars; |
---|
| 418 | int npars; |
---|
| 419 | |
---|
| 420 | // Get parameters |
---|
| 421 | |
---|
| 422 | // Reader parameter dictionary |
---|
| 423 | self->model->core_sld = PyFloat_AsDouble( PyDict_GetItemString(self->params, "core_sld") ); |
---|
| 424 | self->model->core_radius = PyFloat_AsDouble( PyDict_GetItemString(self->params, "core_radius") ); |
---|
| 425 | self->model->n_pairs = PyFloat_AsDouble( PyDict_GetItemString(self->params, "n_pairs") ); |
---|
| 426 | self->model->w_thickness = PyFloat_AsDouble( PyDict_GetItemString(self->params, "w_thickness") ); |
---|
| 427 | self->model->s_thickness = PyFloat_AsDouble( PyDict_GetItemString(self->params, "s_thickness") ); |
---|
| 428 | self->model->scale = PyFloat_AsDouble( PyDict_GetItemString(self->params, "scale") ); |
---|
| 429 | self->model->background = PyFloat_AsDouble( PyDict_GetItemString(self->params, "background") ); |
---|
| 430 | self->model->shell_sld = PyFloat_AsDouble( PyDict_GetItemString(self->params, "shell_sld") ); |
---|
| 431 | // Read in dispersion parameters |
---|
| 432 | PyObject* disp_dict; |
---|
| 433 | DispersionVisitor* visitor = new DispersionVisitor(); |
---|
| 434 | disp_dict = PyDict_GetItemString(self->dispersion, "core_radius"); |
---|
| 435 | self->model->core_radius.dispersion->accept_as_destination(visitor, self->model->core_radius.dispersion, disp_dict); |
---|
[2cc633b] | 436 | disp_dict = PyDict_GetItemString(self->dispersion, "s_thickness"); |
---|
| 437 | self->model->s_thickness.dispersion->accept_as_destination(visitor, self->model->s_thickness.dispersion, disp_dict); |
---|
| 438 | disp_dict = PyDict_GetItemString(self->dispersion, "w_thickness"); |
---|
| 439 | self->model->w_thickness.dispersion->accept_as_destination(visitor, self->model->w_thickness.dispersion, disp_dict); |
---|
[3d25331f] | 440 | |
---|
| 441 | |
---|
| 442 | // Get input and determine whether we have to supply a 1D or 2D return value. |
---|
| 443 | if ( !PyArg_ParseTuple(args,"O",&pars) ) { |
---|
| 444 | PyErr_SetString(CMultiShellModelError, |
---|
| 445 | "CMultiShellModel.run expects a q value."); |
---|
| 446 | return NULL; |
---|
| 447 | } |
---|
| 448 | |
---|
| 449 | // Check params |
---|
| 450 | if( PyList_Check(pars)==1) { |
---|
| 451 | |
---|
| 452 | // Length of list should be 2 for I(qx, qy)) |
---|
| 453 | npars = PyList_GET_SIZE(pars); |
---|
| 454 | if(npars!=2) { |
---|
| 455 | PyErr_SetString(CMultiShellModelError, |
---|
| 456 | "CMultiShellModel.run expects a double or a list of dimension 2."); |
---|
| 457 | return NULL; |
---|
| 458 | } |
---|
| 459 | // We have a vector q, get the qx and qy values at which |
---|
| 460 | // to evaluate I(qx,qy) |
---|
| 461 | qx_value = CMultiShellModel_readDouble(PyList_GET_ITEM(pars,0)); |
---|
| 462 | qy_value = CMultiShellModel_readDouble(PyList_GET_ITEM(pars,1)); |
---|
| 463 | return Py_BuildValue("d",(*(self->model))(qx_value,qy_value)); |
---|
| 464 | |
---|
| 465 | } else { |
---|
| 466 | |
---|
| 467 | // We have a scalar q, we will evaluate I(q) |
---|
| 468 | qx_value = CMultiShellModel_readDouble(pars); |
---|
| 469 | |
---|
| 470 | return Py_BuildValue("d",(*(self->model))(qx_value)); |
---|
| 471 | } |
---|
| 472 | } |
---|
| 473 | |
---|
| 474 | static PyObject * reset(CMultiShellModel *self, PyObject *args) { |
---|
| 475 | |
---|
| 476 | |
---|
| 477 | return Py_BuildValue("d",0.0); |
---|
| 478 | } |
---|
| 479 | |
---|
| 480 | static PyObject * set_dispersion(CMultiShellModel *self, PyObject *args) { |
---|
| 481 | PyObject * disp; |
---|
| 482 | const char * par_name; |
---|
| 483 | |
---|
| 484 | if ( !PyArg_ParseTuple(args,"sO", &par_name, &disp) ) { |
---|
| 485 | PyErr_SetString(CMultiShellModelError, |
---|
| 486 | "CMultiShellModel.set_dispersion expects a DispersionModel object."); |
---|
| 487 | return NULL; |
---|
| 488 | } |
---|
| 489 | void *temp = PyCObject_AsVoidPtr(disp); |
---|
| 490 | DispersionModel * dispersion = static_cast<DispersionModel *>(temp); |
---|
| 491 | |
---|
| 492 | |
---|
| 493 | // Ugliness necessary to go from python to C |
---|
| 494 | // TODO: refactor this |
---|
| 495 | if (!strcmp(par_name, "core_radius")) { |
---|
| 496 | self->model->core_radius.dispersion = dispersion; |
---|
[2cc633b] | 497 | } else if (!strcmp(par_name, "s_thickness")) { |
---|
| 498 | self->model->s_thickness.dispersion = dispersion; |
---|
| 499 | } else if (!strcmp(par_name, "w_thickness")) { |
---|
| 500 | self->model->w_thickness.dispersion = dispersion; |
---|
[3d25331f] | 501 | } else { |
---|
| 502 | PyErr_SetString(CMultiShellModelError, |
---|
| 503 | "CMultiShellModel.set_dispersion expects a valid parameter name."); |
---|
| 504 | return NULL; |
---|
| 505 | } |
---|
| 506 | |
---|
| 507 | DispersionVisitor* visitor = new DispersionVisitor(); |
---|
| 508 | PyObject * disp_dict = PyDict_New(); |
---|
| 509 | dispersion->accept_as_source(visitor, dispersion, disp_dict); |
---|
| 510 | PyDict_SetItemString(self->dispersion, par_name, disp_dict); |
---|
| 511 | return Py_BuildValue("i",1); |
---|
| 512 | } |
---|
| 513 | |
---|
| 514 | |
---|
| 515 | static PyMethodDef CMultiShellModel_methods[] = { |
---|
| 516 | {"run", (PyCFunction)run , METH_VARARGS, |
---|
| 517 | "Evaluate the model at a given Q or Q, phi"}, |
---|
| 518 | {"runXY", (PyCFunction)runXY , METH_VARARGS, |
---|
| 519 | "Evaluate the model at a given Q or Qx, Qy"}, |
---|
[5eb9154] | 520 | {"calculate_ER", (PyCFunction)calculate_ER , METH_VARARGS, |
---|
| 521 | "Evaluate the model at a given Q or Q, phi"}, |
---|
[9bd69098] | 522 | |
---|
| 523 | {"evalDistribution", (PyCFunction)evalDistribution , METH_VARARGS, |
---|
| 524 | "Evaluate the model at a given Q or Qx, Qy vector "}, |
---|
[3d25331f] | 525 | {"reset", (PyCFunction)reset , METH_VARARGS, |
---|
| 526 | "Reset pair correlation"}, |
---|
| 527 | {"set_dispersion", (PyCFunction)set_dispersion , METH_VARARGS, |
---|
| 528 | "Set the dispersion model for a given parameter"}, |
---|
| 529 | {NULL} |
---|
| 530 | }; |
---|
| 531 | |
---|
| 532 | static PyTypeObject CMultiShellModelType = { |
---|
| 533 | PyObject_HEAD_INIT(NULL) |
---|
| 534 | 0, /*ob_size*/ |
---|
| 535 | "CMultiShellModel", /*tp_name*/ |
---|
| 536 | sizeof(CMultiShellModel), /*tp_basicsize*/ |
---|
| 537 | 0, /*tp_itemsize*/ |
---|
| 538 | (destructor)CMultiShellModel_dealloc, /*tp_dealloc*/ |
---|
| 539 | 0, /*tp_print*/ |
---|
| 540 | 0, /*tp_getattr*/ |
---|
| 541 | 0, /*tp_setattr*/ |
---|
| 542 | 0, /*tp_compare*/ |
---|
| 543 | 0, /*tp_repr*/ |
---|
| 544 | 0, /*tp_as_number*/ |
---|
| 545 | 0, /*tp_as_sequence*/ |
---|
| 546 | 0, /*tp_as_mapping*/ |
---|
| 547 | 0, /*tp_hash */ |
---|
| 548 | 0, /*tp_call*/ |
---|
| 549 | 0, /*tp_str*/ |
---|
| 550 | 0, /*tp_getattro*/ |
---|
| 551 | 0, /*tp_setattro*/ |
---|
| 552 | 0, /*tp_as_buffer*/ |
---|
| 553 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/ |
---|
| 554 | "CMultiShellModel objects", /* tp_doc */ |
---|
| 555 | 0, /* tp_traverse */ |
---|
| 556 | 0, /* tp_clear */ |
---|
| 557 | 0, /* tp_richcompare */ |
---|
| 558 | 0, /* tp_weaklistoffset */ |
---|
| 559 | 0, /* tp_iter */ |
---|
| 560 | 0, /* tp_iternext */ |
---|
| 561 | CMultiShellModel_methods, /* tp_methods */ |
---|
| 562 | CMultiShellModel_members, /* tp_members */ |
---|
| 563 | 0, /* tp_getset */ |
---|
| 564 | 0, /* tp_base */ |
---|
| 565 | 0, /* tp_dict */ |
---|
| 566 | 0, /* tp_descr_get */ |
---|
| 567 | 0, /* tp_descr_set */ |
---|
| 568 | 0, /* tp_dictoffset */ |
---|
| 569 | (initproc)CMultiShellModel_init, /* tp_init */ |
---|
| 570 | 0, /* tp_alloc */ |
---|
| 571 | CMultiShellModel_new, /* tp_new */ |
---|
| 572 | }; |
---|
| 573 | |
---|
| 574 | |
---|
[9bd69098] | 575 | //static PyMethodDef module_methods[] = { |
---|
| 576 | // {NULL} |
---|
| 577 | //}; |
---|
[3d25331f] | 578 | |
---|
| 579 | /** |
---|
| 580 | * Function used to add the model class to a module |
---|
| 581 | * @param module: module to add the class to |
---|
| 582 | */ |
---|
| 583 | void addCMultiShellModel(PyObject *module) { |
---|
| 584 | PyObject *d; |
---|
| 585 | |
---|
| 586 | if (PyType_Ready(&CMultiShellModelType) < 0) |
---|
| 587 | return; |
---|
| 588 | |
---|
| 589 | Py_INCREF(&CMultiShellModelType); |
---|
| 590 | PyModule_AddObject(module, "CMultiShellModel", (PyObject *)&CMultiShellModelType); |
---|
| 591 | |
---|
| 592 | d = PyModule_GetDict(module); |
---|
| 593 | CMultiShellModelError = PyErr_NewException("CMultiShellModel.error", NULL, NULL); |
---|
| 594 | PyDict_SetItemString(d, "CMultiShellModelError", CMultiShellModelError); |
---|
| 595 | } |
---|
| 596 | |
---|