[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); |
---|
[b1c3295] | 67 | |
---|
[3d25331f] | 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 | |
---|
[b1c3295] | 91 | // Initialize parameter dictionary |
---|
| 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)); |
---|
| 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); |
---|
| 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); |
---|
| 112 | |
---|
[3d25331f] | 113 | |
---|
| 114 | |
---|
| 115 | // Create empty log |
---|
| 116 | self->log = PyDict_New(); |
---|
| 117 | |
---|
[b1c3295] | 118 | |
---|
[3d25331f] | 119 | |
---|
| 120 | } |
---|
| 121 | return 0; |
---|
| 122 | } |
---|
| 123 | |
---|
[b1c3295] | 124 | static char name_params[] = "params"; |
---|
| 125 | static char def_params[] = "Parameters"; |
---|
| 126 | static char name_dispersion[] = "dispersion"; |
---|
| 127 | static char def_dispersion[] = "Dispersion parameters"; |
---|
| 128 | static char name_log[] = "log"; |
---|
| 129 | static char def_log[] = "Log"; |
---|
| 130 | |
---|
[3d25331f] | 131 | static PyMemberDef CMultiShellModel_members[] = { |
---|
[b1c3295] | 132 | {name_params, T_OBJECT, offsetof(CMultiShellModel, params), 0, def_params}, |
---|
| 133 | {name_dispersion, T_OBJECT, offsetof(CMultiShellModel, dispersion), 0, def_dispersion}, |
---|
| 134 | {name_log, T_OBJECT, offsetof(CMultiShellModel, log), 0, def_log}, |
---|
[3d25331f] | 135 | {NULL} /* Sentinel */ |
---|
| 136 | }; |
---|
| 137 | |
---|
| 138 | /** Read double from PyObject |
---|
| 139 | @param p PyObject |
---|
| 140 | @return double |
---|
| 141 | */ |
---|
| 142 | double CMultiShellModel_readDouble(PyObject *p) { |
---|
| 143 | if (PyFloat_Check(p)==1) { |
---|
| 144 | return (double)(((PyFloatObject *)(p))->ob_fval); |
---|
| 145 | } else if (PyInt_Check(p)==1) { |
---|
| 146 | return (double)(((PyIntObject *)(p))->ob_ival); |
---|
| 147 | } else if (PyLong_Check(p)==1) { |
---|
| 148 | return (double)PyLong_AsLong(p); |
---|
| 149 | } else { |
---|
| 150 | return 0.0; |
---|
| 151 | } |
---|
| 152 | } |
---|
[9bd69098] | 153 | /** |
---|
| 154 | * Function to call to evaluate model |
---|
| 155 | * @param args: input numpy array q[] |
---|
| 156 | * @return: numpy array object |
---|
| 157 | */ |
---|
| 158 | |
---|
| 159 | static PyObject *evaluateOneDim(MultiShellModel* model, PyArrayObject *q){ |
---|
| 160 | PyArrayObject *result; |
---|
| 161 | |
---|
| 162 | // Check validity of array q , q must be of dimension 1, an array of double |
---|
| 163 | if (q->nd != 1 || q->descr->type_num != PyArray_DOUBLE) |
---|
| 164 | { |
---|
| 165 | //const char * message= "Invalid array: q->nd=%d,type_num=%d\n",q->nd,q->descr->type_num; |
---|
| 166 | //PyErr_SetString(PyExc_ValueError , message); |
---|
| 167 | return NULL; |
---|
| 168 | } |
---|
[8f5b34a] | 169 | result = (PyArrayObject *)PyArray_FromDims(q->nd, (int *)(q->dimensions), PyArray_DOUBLE); |
---|
[9bd69098] | 170 | if (result == NULL) { |
---|
| 171 | const char * message= "Could not create result "; |
---|
| 172 | PyErr_SetString(PyExc_RuntimeError , message); |
---|
| 173 | return NULL; |
---|
| 174 | } |
---|
[0b082f3] | 175 | #pragma omp parallel for |
---|
[9bd69098] | 176 | for (int i = 0; i < q->dimensions[0]; i++){ |
---|
| 177 | double q_value = *(double *)(q->data + i*q->strides[0]); |
---|
| 178 | double *result_value = (double *)(result->data + i*result->strides[0]); |
---|
| 179 | *result_value =(*model)(q_value); |
---|
| 180 | } |
---|
| 181 | return PyArray_Return(result); |
---|
| 182 | } |
---|
[3d25331f] | 183 | |
---|
[9bd69098] | 184 | /** |
---|
| 185 | * Function to call to evaluate model |
---|
| 186 | * @param args: input numpy array [x[],y[]] |
---|
| 187 | * @return: numpy array object |
---|
| 188 | */ |
---|
| 189 | static PyObject * evaluateTwoDimXY( MultiShellModel* model, |
---|
| 190 | PyArrayObject *x, PyArrayObject *y) |
---|
| 191 | { |
---|
| 192 | PyArrayObject *result; |
---|
[0b082f3] | 193 | int x_len, y_len, dims[1]; |
---|
[9bd69098] | 194 | //check validity of input vectors |
---|
[3080527] | 195 | if (x->nd != 1 || x->descr->type_num != PyArray_DOUBLE |
---|
| 196 | || y->nd != 1 || y->descr->type_num != PyArray_DOUBLE |
---|
| 197 | || y->dimensions[0] != x->dimensions[0]){ |
---|
[9bd69098] | 198 | const char * message= "evaluateTwoDimXY expect 2 numpy arrays"; |
---|
| 199 | PyErr_SetString(PyExc_ValueError , message); |
---|
| 200 | return NULL; |
---|
| 201 | } |
---|
| 202 | |
---|
| 203 | if (PyArray_Check(x) && PyArray_Check(y)) { |
---|
[a8d6888] | 204 | |
---|
[3080527] | 205 | x_len = dims[0]= x->dimensions[0]; |
---|
[9ce41c6] | 206 | y_len = dims[0]= y->dimensions[0]; |
---|
[9bd69098] | 207 | |
---|
| 208 | // Make a new double matrix of same dims |
---|
[8f5b34a] | 209 | result=(PyArrayObject *) PyArray_FromDims(1,dims,NPY_DOUBLE); |
---|
[9bd69098] | 210 | if (result == NULL){ |
---|
| 211 | const char * message= "Could not create result "; |
---|
| 212 | PyErr_SetString(PyExc_RuntimeError , message); |
---|
| 213 | return NULL; |
---|
| 214 | } |
---|
| 215 | |
---|
| 216 | /* Do the calculation. */ |
---|
[0b082f3] | 217 | #pragma omp parallel for |
---|
| 218 | for (int i=0; i< x_len; i++) { |
---|
[3080527] | 219 | double x_value = *(double *)(x->data + i*x->strides[0]); |
---|
| 220 | double y_value = *(double *)(y->data + i*y->strides[0]); |
---|
| 221 | double *result_value = (double *)(result->data + |
---|
| 222 | i*result->strides[0]); |
---|
| 223 | *result_value = (*model)(x_value, y_value); |
---|
| 224 | } |
---|
[9bd69098] | 225 | return PyArray_Return(result); |
---|
| 226 | |
---|
| 227 | }else{ |
---|
| 228 | PyErr_SetString(CMultiShellModelError, |
---|
| 229 | "CMultiShellModel.evaluateTwoDimXY couldn't run."); |
---|
| 230 | return NULL; |
---|
| 231 | } |
---|
| 232 | } |
---|
| 233 | /** |
---|
| 234 | * evalDistribution function evaluate a model function with input vector |
---|
| 235 | * @param args: input q as vector or [qx, qy] where qx, qy are vectors |
---|
| 236 | * |
---|
| 237 | */ |
---|
| 238 | static PyObject * evalDistribution(CMultiShellModel *self, PyObject *args){ |
---|
| 239 | PyObject *qx, *qy; |
---|
| 240 | PyArrayObject * pars; |
---|
| 241 | int npars ,mpars; |
---|
| 242 | |
---|
| 243 | // Get parameters |
---|
| 244 | |
---|
[b1c3295] | 245 | // Reader parameter dictionary |
---|
| 246 | self->model->core_sld = PyFloat_AsDouble( PyDict_GetItemString(self->params, "core_sld") ); |
---|
| 247 | self->model->core_radius = PyFloat_AsDouble( PyDict_GetItemString(self->params, "core_radius") ); |
---|
| 248 | self->model->n_pairs = PyFloat_AsDouble( PyDict_GetItemString(self->params, "n_pairs") ); |
---|
| 249 | self->model->w_thickness = PyFloat_AsDouble( PyDict_GetItemString(self->params, "w_thickness") ); |
---|
| 250 | self->model->s_thickness = PyFloat_AsDouble( PyDict_GetItemString(self->params, "s_thickness") ); |
---|
| 251 | self->model->scale = PyFloat_AsDouble( PyDict_GetItemString(self->params, "scale") ); |
---|
| 252 | self->model->background = PyFloat_AsDouble( PyDict_GetItemString(self->params, "background") ); |
---|
| 253 | self->model->shell_sld = PyFloat_AsDouble( PyDict_GetItemString(self->params, "shell_sld") ); |
---|
| 254 | // Read in dispersion parameters |
---|
| 255 | PyObject* disp_dict; |
---|
| 256 | DispersionVisitor* visitor = new DispersionVisitor(); |
---|
| 257 | disp_dict = PyDict_GetItemString(self->dispersion, "core_radius"); |
---|
| 258 | self->model->core_radius.dispersion->accept_as_destination(visitor, self->model->core_radius.dispersion, disp_dict); |
---|
| 259 | disp_dict = PyDict_GetItemString(self->dispersion, "s_thickness"); |
---|
| 260 | self->model->s_thickness.dispersion->accept_as_destination(visitor, self->model->s_thickness.dispersion, disp_dict); |
---|
| 261 | disp_dict = PyDict_GetItemString(self->dispersion, "w_thickness"); |
---|
| 262 | self->model->w_thickness.dispersion->accept_as_destination(visitor, self->model->w_thickness.dispersion, disp_dict); |
---|
[9bd69098] | 263 | |
---|
| 264 | |
---|
| 265 | // Get input and determine whether we have to supply a 1D or 2D return value. |
---|
| 266 | if ( !PyArg_ParseTuple(args,"O",&pars) ) { |
---|
| 267 | PyErr_SetString(CMultiShellModelError, |
---|
| 268 | "CMultiShellModel.evalDistribution expects a q value."); |
---|
| 269 | return NULL; |
---|
| 270 | } |
---|
| 271 | // Check params |
---|
| 272 | |
---|
| 273 | if(PyArray_Check(pars)==1) { |
---|
| 274 | |
---|
| 275 | // Length of list should 1 or 2 |
---|
| 276 | npars = pars->nd; |
---|
| 277 | if(npars==1) { |
---|
| 278 | // input is a numpy array |
---|
| 279 | if (PyArray_Check(pars)) { |
---|
| 280 | return evaluateOneDim(self->model, (PyArrayObject*)pars); |
---|
| 281 | } |
---|
| 282 | }else{ |
---|
| 283 | PyErr_SetString(CMultiShellModelError, |
---|
| 284 | "CMultiShellModel.evalDistribution expect numpy array of one dimension."); |
---|
| 285 | return NULL; |
---|
| 286 | } |
---|
| 287 | }else if( PyList_Check(pars)==1) { |
---|
| 288 | // Length of list should be 2 for I(qx,qy) |
---|
| 289 | mpars = PyList_GET_SIZE(pars); |
---|
| 290 | if(mpars!=2) { |
---|
| 291 | PyErr_SetString(CMultiShellModelError, |
---|
| 292 | "CMultiShellModel.evalDistribution expects a list of dimension 2."); |
---|
| 293 | return NULL; |
---|
| 294 | } |
---|
| 295 | qx = PyList_GET_ITEM(pars,0); |
---|
| 296 | qy = PyList_GET_ITEM(pars,1); |
---|
| 297 | if (PyArray_Check(qx) && PyArray_Check(qy)) { |
---|
| 298 | return evaluateTwoDimXY(self->model, (PyArrayObject*)qx, |
---|
| 299 | (PyArrayObject*)qy); |
---|
| 300 | }else{ |
---|
| 301 | PyErr_SetString(CMultiShellModelError, |
---|
| 302 | "CMultiShellModel.evalDistribution expect 2 numpy arrays in list."); |
---|
| 303 | return NULL; |
---|
| 304 | } |
---|
| 305 | } |
---|
[e0a8a3c] | 306 | PyErr_SetString(CMultiShellModelError, |
---|
| 307 | "CMultiShellModel.evalDistribution couln't be run."); |
---|
| 308 | return NULL; |
---|
| 309 | |
---|
[9bd69098] | 310 | } |
---|
[3d25331f] | 311 | |
---|
| 312 | /** |
---|
| 313 | * Function to call to evaluate model |
---|
| 314 | * @param args: input q or [q,phi] |
---|
| 315 | * @return: function value |
---|
| 316 | */ |
---|
| 317 | static PyObject * run(CMultiShellModel *self, PyObject *args) { |
---|
| 318 | double q_value, phi_value; |
---|
| 319 | PyObject* pars; |
---|
| 320 | int npars; |
---|
| 321 | |
---|
| 322 | // Get parameters |
---|
| 323 | |
---|
[b1c3295] | 324 | // Reader parameter dictionary |
---|
| 325 | self->model->core_sld = PyFloat_AsDouble( PyDict_GetItemString(self->params, "core_sld") ); |
---|
| 326 | self->model->core_radius = PyFloat_AsDouble( PyDict_GetItemString(self->params, "core_radius") ); |
---|
| 327 | self->model->n_pairs = PyFloat_AsDouble( PyDict_GetItemString(self->params, "n_pairs") ); |
---|
| 328 | self->model->w_thickness = PyFloat_AsDouble( PyDict_GetItemString(self->params, "w_thickness") ); |
---|
| 329 | self->model->s_thickness = PyFloat_AsDouble( PyDict_GetItemString(self->params, "s_thickness") ); |
---|
| 330 | self->model->scale = PyFloat_AsDouble( PyDict_GetItemString(self->params, "scale") ); |
---|
| 331 | self->model->background = PyFloat_AsDouble( PyDict_GetItemString(self->params, "background") ); |
---|
| 332 | self->model->shell_sld = PyFloat_AsDouble( PyDict_GetItemString(self->params, "shell_sld") ); |
---|
| 333 | // Read in dispersion parameters |
---|
| 334 | PyObject* disp_dict; |
---|
| 335 | DispersionVisitor* visitor = new DispersionVisitor(); |
---|
| 336 | disp_dict = PyDict_GetItemString(self->dispersion, "core_radius"); |
---|
| 337 | self->model->core_radius.dispersion->accept_as_destination(visitor, self->model->core_radius.dispersion, disp_dict); |
---|
| 338 | disp_dict = PyDict_GetItemString(self->dispersion, "s_thickness"); |
---|
| 339 | self->model->s_thickness.dispersion->accept_as_destination(visitor, self->model->s_thickness.dispersion, disp_dict); |
---|
| 340 | disp_dict = PyDict_GetItemString(self->dispersion, "w_thickness"); |
---|
| 341 | self->model->w_thickness.dispersion->accept_as_destination(visitor, self->model->w_thickness.dispersion, disp_dict); |
---|
[3d25331f] | 342 | |
---|
| 343 | |
---|
| 344 | // Get input and determine whether we have to supply a 1D or 2D return value. |
---|
| 345 | if ( !PyArg_ParseTuple(args,"O",&pars) ) { |
---|
| 346 | PyErr_SetString(CMultiShellModelError, |
---|
| 347 | "CMultiShellModel.run expects a q value."); |
---|
| 348 | return NULL; |
---|
| 349 | } |
---|
| 350 | |
---|
| 351 | // Check params |
---|
| 352 | if( PyList_Check(pars)==1) { |
---|
| 353 | |
---|
| 354 | // Length of list should be 2 for I(q,phi) |
---|
| 355 | npars = PyList_GET_SIZE(pars); |
---|
| 356 | if(npars!=2) { |
---|
| 357 | PyErr_SetString(CMultiShellModelError, |
---|
| 358 | "CMultiShellModel.run expects a double or a list of dimension 2."); |
---|
| 359 | return NULL; |
---|
| 360 | } |
---|
| 361 | // We have a vector q, get the q and phi values at which |
---|
| 362 | // to evaluate I(q,phi) |
---|
| 363 | q_value = CMultiShellModel_readDouble(PyList_GET_ITEM(pars,0)); |
---|
| 364 | phi_value = CMultiShellModel_readDouble(PyList_GET_ITEM(pars,1)); |
---|
| 365 | // Skip zero |
---|
| 366 | if (q_value==0) { |
---|
| 367 | return Py_BuildValue("d",0.0); |
---|
| 368 | } |
---|
| 369 | return Py_BuildValue("d",(*(self->model)).evaluate_rphi(q_value,phi_value)); |
---|
| 370 | |
---|
| 371 | } else { |
---|
| 372 | |
---|
| 373 | // We have a scalar q, we will evaluate I(q) |
---|
| 374 | q_value = CMultiShellModel_readDouble(pars); |
---|
| 375 | |
---|
| 376 | return Py_BuildValue("d",(*(self->model))(q_value)); |
---|
| 377 | } |
---|
| 378 | } |
---|
[5eb9154] | 379 | /** |
---|
| 380 | * Function to call to calculate_ER |
---|
| 381 | * @return: effective radius value |
---|
| 382 | */ |
---|
| 383 | static PyObject * calculate_ER(CMultiShellModel *self) { |
---|
| 384 | |
---|
| 385 | // Get parameters |
---|
| 386 | |
---|
[b1c3295] | 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 | |
---|
[b1c3295] | 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); |
---|
| 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) { |
---|
[b1c3295] | 475 | |
---|
[3d25331f] | 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 |
---|
[b1c3295] | 494 | // TODO: refactor this |
---|
| 495 | if (!strcmp(par_name, "core_radius")) { |
---|
| 496 | self->model->core_radius.dispersion = dispersion; |
---|
| 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); |
---|
[2605da22] | 593 | static char error_name[] = "CMultiShellModel.error"; |
---|
| 594 | CMultiShellModelError = PyErr_NewException(error_name, NULL, NULL); |
---|
[3d25331f] | 595 | PyDict_SetItemString(d, "CMultiShellModelError", CMultiShellModelError); |
---|
| 596 | } |
---|
[b1c3295] | 597 | |
---|