[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 | { |
---|
| 62 | self->ob_type->tp_free((PyObject*)self); |
---|
| 63 | |
---|
| 64 | |
---|
| 65 | } |
---|
| 66 | |
---|
| 67 | static PyObject * |
---|
| 68 | CMultiShellModel_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
---|
| 69 | { |
---|
| 70 | CMultiShellModel *self; |
---|
| 71 | |
---|
| 72 | self = (CMultiShellModel *)type->tp_alloc(type, 0); |
---|
| 73 | |
---|
| 74 | return (PyObject *)self; |
---|
| 75 | } |
---|
| 76 | |
---|
| 77 | static int |
---|
| 78 | CMultiShellModel_init(CMultiShellModel *self, PyObject *args, PyObject *kwds) |
---|
| 79 | { |
---|
| 80 | if (self != NULL) { |
---|
| 81 | |
---|
| 82 | // Create parameters |
---|
| 83 | self->params = PyDict_New(); |
---|
| 84 | self->dispersion = PyDict_New(); |
---|
| 85 | self->model = new MultiShellModel(); |
---|
| 86 | |
---|
| 87 | // Initialize parameter dictionary |
---|
| 88 | PyDict_SetItemString(self->params,"core_sld",Py_BuildValue("d",0.000006)); |
---|
| 89 | PyDict_SetItemString(self->params,"core_radius",Py_BuildValue("d",60.000000)); |
---|
| 90 | PyDict_SetItemString(self->params,"n_pairs",Py_BuildValue("d",2.000000)); |
---|
| 91 | PyDict_SetItemString(self->params,"w_thickness",Py_BuildValue("d",10.000000)); |
---|
| 92 | PyDict_SetItemString(self->params,"s_thickness",Py_BuildValue("d",10.000000)); |
---|
| 93 | PyDict_SetItemString(self->params,"scale",Py_BuildValue("d",1.000000)); |
---|
| 94 | PyDict_SetItemString(self->params,"background",Py_BuildValue("d",0.000000)); |
---|
| 95 | PyDict_SetItemString(self->params,"shell_sld",Py_BuildValue("d",0.000000)); |
---|
| 96 | // Initialize dispersion / averaging parameter dict |
---|
| 97 | DispersionVisitor* visitor = new DispersionVisitor(); |
---|
| 98 | PyObject * disp_dict; |
---|
| 99 | disp_dict = PyDict_New(); |
---|
| 100 | self->model->core_radius.dispersion->accept_as_source(visitor, self->model->core_radius.dispersion, disp_dict); |
---|
| 101 | PyDict_SetItemString(self->dispersion, "core_radius", disp_dict); |
---|
[2cc633b] | 102 | disp_dict = PyDict_New(); |
---|
| 103 | self->model->s_thickness.dispersion->accept_as_source(visitor, self->model->s_thickness.dispersion, disp_dict); |
---|
| 104 | PyDict_SetItemString(self->dispersion, "s_thickness", disp_dict); |
---|
| 105 | disp_dict = PyDict_New(); |
---|
| 106 | self->model->w_thickness.dispersion->accept_as_source(visitor, self->model->w_thickness.dispersion, disp_dict); |
---|
| 107 | PyDict_SetItemString(self->dispersion, "w_thickness", disp_dict); |
---|
[3d25331f] | 108 | |
---|
| 109 | |
---|
| 110 | |
---|
| 111 | // Create empty log |
---|
| 112 | self->log = PyDict_New(); |
---|
| 113 | |
---|
| 114 | |
---|
| 115 | |
---|
| 116 | } |
---|
| 117 | return 0; |
---|
| 118 | } |
---|
| 119 | |
---|
| 120 | static PyMemberDef CMultiShellModel_members[] = { |
---|
| 121 | {"params", T_OBJECT, offsetof(CMultiShellModel, params), 0, |
---|
| 122 | "Parameters"}, |
---|
| 123 | {"dispersion", T_OBJECT, offsetof(CMultiShellModel, dispersion), 0, |
---|
| 124 | "Dispersion parameters"}, |
---|
| 125 | {"log", T_OBJECT, offsetof(CMultiShellModel, log), 0, |
---|
| 126 | "Log"}, |
---|
| 127 | {NULL} /* Sentinel */ |
---|
| 128 | }; |
---|
| 129 | |
---|
| 130 | /** Read double from PyObject |
---|
| 131 | @param p PyObject |
---|
| 132 | @return double |
---|
| 133 | */ |
---|
| 134 | double CMultiShellModel_readDouble(PyObject *p) { |
---|
| 135 | if (PyFloat_Check(p)==1) { |
---|
| 136 | return (double)(((PyFloatObject *)(p))->ob_fval); |
---|
| 137 | } else if (PyInt_Check(p)==1) { |
---|
| 138 | return (double)(((PyIntObject *)(p))->ob_ival); |
---|
| 139 | } else if (PyLong_Check(p)==1) { |
---|
| 140 | return (double)PyLong_AsLong(p); |
---|
| 141 | } else { |
---|
| 142 | return 0.0; |
---|
| 143 | } |
---|
| 144 | } |
---|
[9bd69098] | 145 | /** |
---|
| 146 | * Function to call to evaluate model |
---|
| 147 | * @param args: input numpy array q[] |
---|
| 148 | * @return: numpy array object |
---|
| 149 | */ |
---|
| 150 | |
---|
| 151 | static PyObject *evaluateOneDim(MultiShellModel* model, PyArrayObject *q){ |
---|
| 152 | PyArrayObject *result; |
---|
| 153 | |
---|
| 154 | // Check validity of array q , q must be of dimension 1, an array of double |
---|
| 155 | if (q->nd != 1 || q->descr->type_num != PyArray_DOUBLE) |
---|
| 156 | { |
---|
| 157 | //const char * message= "Invalid array: q->nd=%d,type_num=%d\n",q->nd,q->descr->type_num; |
---|
| 158 | //PyErr_SetString(PyExc_ValueError , message); |
---|
| 159 | return NULL; |
---|
| 160 | } |
---|
| 161 | result = (PyArrayObject *)PyArray_FromDims(q->nd, (int *)(q->dimensions), |
---|
| 162 | PyArray_DOUBLE); |
---|
| 163 | if (result == NULL) { |
---|
| 164 | const char * message= "Could not create result "; |
---|
| 165 | PyErr_SetString(PyExc_RuntimeError , message); |
---|
| 166 | return NULL; |
---|
| 167 | } |
---|
| 168 | for (int i = 0; i < q->dimensions[0]; i++){ |
---|
| 169 | double q_value = *(double *)(q->data + i*q->strides[0]); |
---|
| 170 | double *result_value = (double *)(result->data + i*result->strides[0]); |
---|
| 171 | *result_value =(*model)(q_value); |
---|
| 172 | } |
---|
| 173 | return PyArray_Return(result); |
---|
| 174 | } |
---|
| 175 | /** |
---|
| 176 | * Function to call to evaluate model |
---|
| 177 | * @param args: input numpy array [q[],phi[]] |
---|
| 178 | * @return: numpy array object |
---|
| 179 | */ |
---|
| 180 | static PyObject * evaluateTwoDim( MultiShellModel* model, |
---|
| 181 | PyArrayObject *q, PyArrayObject *phi) |
---|
| 182 | { |
---|
| 183 | PyArrayObject *result; |
---|
| 184 | //check validity of input vectors |
---|
| 185 | if (q->nd != 1 || q->descr->type_num != PyArray_DOUBLE |
---|
| 186 | || phi->nd != 1 || phi->descr->type_num != PyArray_DOUBLE |
---|
| 187 | || phi->dimensions[0] != q->dimensions[0]){ |
---|
| 188 | |
---|
| 189 | //const char * message= "Invalid array: q->nd=%d,type_num=%d\n",q->nd,q->descr->type_num; |
---|
| 190 | PyErr_SetString(PyExc_ValueError ,"wrong input"); |
---|
| 191 | return NULL; |
---|
| 192 | } |
---|
| 193 | result= (PyArrayObject *)PyArray_FromDims(q->nd,(int*)(q->dimensions), PyArray_DOUBLE); |
---|
[3d25331f] | 194 | |
---|
[9bd69098] | 195 | if (result == NULL){ |
---|
| 196 | const char * message= "Could not create result "; |
---|
| 197 | PyErr_SetString(PyExc_RuntimeError , message); |
---|
| 198 | return NULL; |
---|
| 199 | } |
---|
| 200 | |
---|
| 201 | for (int i = 0; i < q->dimensions[0]; i++) { |
---|
| 202 | double q_value = *(double *)(q->data + i*q->strides[0]); |
---|
| 203 | double phi_value = *(double *)(phi->data + i*phi->strides[0]); |
---|
| 204 | double *result_value = (double *)(result->data + i*result->strides[0]); |
---|
| 205 | if (q_value == 0) |
---|
| 206 | *result_value = 0.0; |
---|
| 207 | else |
---|
| 208 | *result_value = model->evaluate_rphi(q_value, phi_value); |
---|
| 209 | } |
---|
| 210 | return PyArray_Return(result); |
---|
| 211 | } |
---|
| 212 | /** |
---|
| 213 | * Function to call to evaluate model |
---|
| 214 | * @param args: input numpy array [x[],y[]] |
---|
| 215 | * @return: numpy array object |
---|
| 216 | */ |
---|
| 217 | static PyObject * evaluateTwoDimXY( MultiShellModel* model, |
---|
| 218 | PyArrayObject *x, PyArrayObject *y) |
---|
| 219 | { |
---|
| 220 | PyArrayObject *result; |
---|
| 221 | int i,j, x_len, y_len, dims[2]; |
---|
| 222 | //check validity of input vectors |
---|
| 223 | if (x->nd != 2 || x->descr->type_num != PyArray_DOUBLE |
---|
| 224 | || y->nd != 2 || y->descr->type_num != PyArray_DOUBLE |
---|
| 225 | || y->dimensions[1] != x->dimensions[0]){ |
---|
| 226 | const char * message= "evaluateTwoDimXY expect 2 numpy arrays"; |
---|
| 227 | PyErr_SetString(PyExc_ValueError , message); |
---|
| 228 | return NULL; |
---|
| 229 | } |
---|
| 230 | |
---|
| 231 | if (PyArray_Check(x) && PyArray_Check(y)) { |
---|
| 232 | x_len = dims[0]= x->dimensions[0]; |
---|
| 233 | y_len = dims[1]= y->dimensions[1]; |
---|
| 234 | |
---|
| 235 | // Make a new double matrix of same dims |
---|
| 236 | result=(PyArrayObject *) PyArray_FromDims(2,dims,NPY_DOUBLE); |
---|
| 237 | if (result == NULL){ |
---|
| 238 | const char * message= "Could not create result "; |
---|
| 239 | PyErr_SetString(PyExc_RuntimeError , message); |
---|
| 240 | return NULL; |
---|
| 241 | } |
---|
| 242 | |
---|
| 243 | /* Do the calculation. */ |
---|
| 244 | for ( i=0; i< x_len; i++) { |
---|
| 245 | for ( j=0; j< y_len; j++) { |
---|
| 246 | double x_value = *(double *)(x->data + i*x->strides[0]); |
---|
| 247 | double y_value = *(double *)(y->data + j*y->strides[1]); |
---|
| 248 | double *result_value = (double *)(result->data + |
---|
| 249 | i*result->strides[0] + j*result->strides[1]); |
---|
| 250 | *result_value = (*model)(x_value, y_value); |
---|
| 251 | } |
---|
| 252 | } |
---|
| 253 | return PyArray_Return(result); |
---|
| 254 | |
---|
| 255 | }else{ |
---|
| 256 | PyErr_SetString(CMultiShellModelError, |
---|
| 257 | "CMultiShellModel.evaluateTwoDimXY couldn't run."); |
---|
| 258 | return NULL; |
---|
| 259 | } |
---|
| 260 | } |
---|
| 261 | /** |
---|
| 262 | * evalDistribution function evaluate a model function with input vector |
---|
| 263 | * @param args: input q as vector or [qx, qy] where qx, qy are vectors |
---|
| 264 | * |
---|
| 265 | */ |
---|
| 266 | static PyObject * evalDistribution(CMultiShellModel *self, PyObject *args){ |
---|
| 267 | PyObject *qx, *qy; |
---|
| 268 | PyArrayObject * pars; |
---|
| 269 | int npars ,mpars; |
---|
| 270 | |
---|
| 271 | // Get parameters |
---|
| 272 | |
---|
| 273 | // Reader parameter dictionary |
---|
| 274 | self->model->core_sld = PyFloat_AsDouble( PyDict_GetItemString(self->params, "core_sld") ); |
---|
| 275 | self->model->core_radius = PyFloat_AsDouble( PyDict_GetItemString(self->params, "core_radius") ); |
---|
| 276 | self->model->n_pairs = PyFloat_AsDouble( PyDict_GetItemString(self->params, "n_pairs") ); |
---|
| 277 | self->model->w_thickness = PyFloat_AsDouble( PyDict_GetItemString(self->params, "w_thickness") ); |
---|
| 278 | self->model->s_thickness = PyFloat_AsDouble( PyDict_GetItemString(self->params, "s_thickness") ); |
---|
| 279 | self->model->scale = PyFloat_AsDouble( PyDict_GetItemString(self->params, "scale") ); |
---|
| 280 | self->model->background = PyFloat_AsDouble( PyDict_GetItemString(self->params, "background") ); |
---|
| 281 | self->model->shell_sld = PyFloat_AsDouble( PyDict_GetItemString(self->params, "shell_sld") ); |
---|
| 282 | // Read in dispersion parameters |
---|
| 283 | PyObject* disp_dict; |
---|
| 284 | DispersionVisitor* visitor = new DispersionVisitor(); |
---|
| 285 | disp_dict = PyDict_GetItemString(self->dispersion, "core_radius"); |
---|
| 286 | self->model->core_radius.dispersion->accept_as_destination(visitor, self->model->core_radius.dispersion, disp_dict); |
---|
| 287 | disp_dict = PyDict_GetItemString(self->dispersion, "s_thickness"); |
---|
| 288 | self->model->s_thickness.dispersion->accept_as_destination(visitor, self->model->s_thickness.dispersion, disp_dict); |
---|
| 289 | disp_dict = PyDict_GetItemString(self->dispersion, "w_thickness"); |
---|
| 290 | self->model->w_thickness.dispersion->accept_as_destination(visitor, self->model->w_thickness.dispersion, disp_dict); |
---|
| 291 | |
---|
| 292 | |
---|
| 293 | // Get input and determine whether we have to supply a 1D or 2D return value. |
---|
| 294 | if ( !PyArg_ParseTuple(args,"O",&pars) ) { |
---|
| 295 | PyErr_SetString(CMultiShellModelError, |
---|
| 296 | "CMultiShellModel.evalDistribution expects a q value."); |
---|
| 297 | return NULL; |
---|
| 298 | } |
---|
| 299 | // Check params |
---|
| 300 | |
---|
| 301 | if(PyArray_Check(pars)==1) { |
---|
| 302 | |
---|
| 303 | // Length of list should 1 or 2 |
---|
| 304 | npars = pars->nd; |
---|
| 305 | if(npars==1) { |
---|
| 306 | // input is a numpy array |
---|
| 307 | if (PyArray_Check(pars)) { |
---|
| 308 | return evaluateOneDim(self->model, (PyArrayObject*)pars); |
---|
| 309 | } |
---|
| 310 | }else{ |
---|
| 311 | PyErr_SetString(CMultiShellModelError, |
---|
| 312 | "CMultiShellModel.evalDistribution expect numpy array of one dimension."); |
---|
| 313 | return NULL; |
---|
| 314 | } |
---|
| 315 | }else if( PyList_Check(pars)==1) { |
---|
| 316 | // Length of list should be 2 for I(qx,qy) |
---|
| 317 | mpars = PyList_GET_SIZE(pars); |
---|
| 318 | if(mpars!=2) { |
---|
| 319 | PyErr_SetString(CMultiShellModelError, |
---|
| 320 | "CMultiShellModel.evalDistribution expects a list of dimension 2."); |
---|
| 321 | return NULL; |
---|
| 322 | } |
---|
| 323 | qx = PyList_GET_ITEM(pars,0); |
---|
| 324 | qy = PyList_GET_ITEM(pars,1); |
---|
| 325 | if (PyArray_Check(qx) && PyArray_Check(qy)) { |
---|
| 326 | return evaluateTwoDimXY(self->model, (PyArrayObject*)qx, |
---|
| 327 | (PyArrayObject*)qy); |
---|
| 328 | }else{ |
---|
| 329 | PyErr_SetString(CMultiShellModelError, |
---|
| 330 | "CMultiShellModel.evalDistribution expect 2 numpy arrays in list."); |
---|
| 331 | return NULL; |
---|
| 332 | } |
---|
| 333 | }else{ |
---|
| 334 | PyErr_SetString(CMultiShellModelError, |
---|
| 335 | "CMultiShellModel.evalDistribution couln't be run."); |
---|
| 336 | return NULL; |
---|
| 337 | } |
---|
| 338 | } |
---|
[3d25331f] | 339 | |
---|
| 340 | /** |
---|
| 341 | * Function to call to evaluate model |
---|
| 342 | * @param args: input q or [q,phi] |
---|
| 343 | * @return: function value |
---|
| 344 | */ |
---|
| 345 | static PyObject * run(CMultiShellModel *self, PyObject *args) { |
---|
| 346 | double q_value, phi_value; |
---|
| 347 | PyObject* pars; |
---|
| 348 | int npars; |
---|
| 349 | |
---|
| 350 | // Get parameters |
---|
| 351 | |
---|
| 352 | // Reader parameter dictionary |
---|
| 353 | self->model->core_sld = PyFloat_AsDouble( PyDict_GetItemString(self->params, "core_sld") ); |
---|
| 354 | self->model->core_radius = PyFloat_AsDouble( PyDict_GetItemString(self->params, "core_radius") ); |
---|
| 355 | self->model->n_pairs = PyFloat_AsDouble( PyDict_GetItemString(self->params, "n_pairs") ); |
---|
| 356 | self->model->w_thickness = PyFloat_AsDouble( PyDict_GetItemString(self->params, "w_thickness") ); |
---|
| 357 | self->model->s_thickness = PyFloat_AsDouble( PyDict_GetItemString(self->params, "s_thickness") ); |
---|
| 358 | self->model->scale = PyFloat_AsDouble( PyDict_GetItemString(self->params, "scale") ); |
---|
| 359 | self->model->background = PyFloat_AsDouble( PyDict_GetItemString(self->params, "background") ); |
---|
| 360 | self->model->shell_sld = PyFloat_AsDouble( PyDict_GetItemString(self->params, "shell_sld") ); |
---|
| 361 | // Read in dispersion parameters |
---|
| 362 | PyObject* disp_dict; |
---|
| 363 | DispersionVisitor* visitor = new DispersionVisitor(); |
---|
| 364 | disp_dict = PyDict_GetItemString(self->dispersion, "core_radius"); |
---|
| 365 | self->model->core_radius.dispersion->accept_as_destination(visitor, self->model->core_radius.dispersion, disp_dict); |
---|
[2cc633b] | 366 | disp_dict = PyDict_GetItemString(self->dispersion, "s_thickness"); |
---|
| 367 | self->model->s_thickness.dispersion->accept_as_destination(visitor, self->model->s_thickness.dispersion, disp_dict); |
---|
| 368 | disp_dict = PyDict_GetItemString(self->dispersion, "w_thickness"); |
---|
| 369 | self->model->w_thickness.dispersion->accept_as_destination(visitor, self->model->w_thickness.dispersion, disp_dict); |
---|
[3d25331f] | 370 | |
---|
| 371 | |
---|
| 372 | // Get input and determine whether we have to supply a 1D or 2D return value. |
---|
| 373 | if ( !PyArg_ParseTuple(args,"O",&pars) ) { |
---|
| 374 | PyErr_SetString(CMultiShellModelError, |
---|
| 375 | "CMultiShellModel.run expects a q value."); |
---|
| 376 | return NULL; |
---|
| 377 | } |
---|
| 378 | |
---|
| 379 | // Check params |
---|
| 380 | if( PyList_Check(pars)==1) { |
---|
| 381 | |
---|
| 382 | // Length of list should be 2 for I(q,phi) |
---|
| 383 | npars = PyList_GET_SIZE(pars); |
---|
| 384 | if(npars!=2) { |
---|
| 385 | PyErr_SetString(CMultiShellModelError, |
---|
| 386 | "CMultiShellModel.run expects a double or a list of dimension 2."); |
---|
| 387 | return NULL; |
---|
| 388 | } |
---|
| 389 | // We have a vector q, get the q and phi values at which |
---|
| 390 | // to evaluate I(q,phi) |
---|
| 391 | q_value = CMultiShellModel_readDouble(PyList_GET_ITEM(pars,0)); |
---|
| 392 | phi_value = CMultiShellModel_readDouble(PyList_GET_ITEM(pars,1)); |
---|
| 393 | // Skip zero |
---|
| 394 | if (q_value==0) { |
---|
| 395 | return Py_BuildValue("d",0.0); |
---|
| 396 | } |
---|
| 397 | return Py_BuildValue("d",(*(self->model)).evaluate_rphi(q_value,phi_value)); |
---|
| 398 | |
---|
| 399 | } else { |
---|
| 400 | |
---|
| 401 | // We have a scalar q, we will evaluate I(q) |
---|
| 402 | q_value = CMultiShellModel_readDouble(pars); |
---|
| 403 | |
---|
| 404 | return Py_BuildValue("d",(*(self->model))(q_value)); |
---|
| 405 | } |
---|
| 406 | } |
---|
| 407 | |
---|
| 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"}, |
---|
[9bd69098] | 518 | |
---|
| 519 | {"evalDistribution", (PyCFunction)evalDistribution , METH_VARARGS, |
---|
| 520 | "Evaluate the model at a given Q or Qx, Qy vector "}, |
---|
[3d25331f] | 521 | {"reset", (PyCFunction)reset , METH_VARARGS, |
---|
| 522 | "Reset pair correlation"}, |
---|
| 523 | {"set_dispersion", (PyCFunction)set_dispersion , METH_VARARGS, |
---|
| 524 | "Set the dispersion model for a given parameter"}, |
---|
| 525 | {NULL} |
---|
| 526 | }; |
---|
| 527 | |
---|
| 528 | static PyTypeObject CMultiShellModelType = { |
---|
| 529 | PyObject_HEAD_INIT(NULL) |
---|
| 530 | 0, /*ob_size*/ |
---|
| 531 | "CMultiShellModel", /*tp_name*/ |
---|
| 532 | sizeof(CMultiShellModel), /*tp_basicsize*/ |
---|
| 533 | 0, /*tp_itemsize*/ |
---|
| 534 | (destructor)CMultiShellModel_dealloc, /*tp_dealloc*/ |
---|
| 535 | 0, /*tp_print*/ |
---|
| 536 | 0, /*tp_getattr*/ |
---|
| 537 | 0, /*tp_setattr*/ |
---|
| 538 | 0, /*tp_compare*/ |
---|
| 539 | 0, /*tp_repr*/ |
---|
| 540 | 0, /*tp_as_number*/ |
---|
| 541 | 0, /*tp_as_sequence*/ |
---|
| 542 | 0, /*tp_as_mapping*/ |
---|
| 543 | 0, /*tp_hash */ |
---|
| 544 | 0, /*tp_call*/ |
---|
| 545 | 0, /*tp_str*/ |
---|
| 546 | 0, /*tp_getattro*/ |
---|
| 547 | 0, /*tp_setattro*/ |
---|
| 548 | 0, /*tp_as_buffer*/ |
---|
| 549 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/ |
---|
| 550 | "CMultiShellModel objects", /* tp_doc */ |
---|
| 551 | 0, /* tp_traverse */ |
---|
| 552 | 0, /* tp_clear */ |
---|
| 553 | 0, /* tp_richcompare */ |
---|
| 554 | 0, /* tp_weaklistoffset */ |
---|
| 555 | 0, /* tp_iter */ |
---|
| 556 | 0, /* tp_iternext */ |
---|
| 557 | CMultiShellModel_methods, /* tp_methods */ |
---|
| 558 | CMultiShellModel_members, /* tp_members */ |
---|
| 559 | 0, /* tp_getset */ |
---|
| 560 | 0, /* tp_base */ |
---|
| 561 | 0, /* tp_dict */ |
---|
| 562 | 0, /* tp_descr_get */ |
---|
| 563 | 0, /* tp_descr_set */ |
---|
| 564 | 0, /* tp_dictoffset */ |
---|
| 565 | (initproc)CMultiShellModel_init, /* tp_init */ |
---|
| 566 | 0, /* tp_alloc */ |
---|
| 567 | CMultiShellModel_new, /* tp_new */ |
---|
| 568 | }; |
---|
| 569 | |
---|
| 570 | |
---|
[9bd69098] | 571 | //static PyMethodDef module_methods[] = { |
---|
| 572 | // {NULL} |
---|
| 573 | //}; |
---|
[3d25331f] | 574 | |
---|
| 575 | /** |
---|
| 576 | * Function used to add the model class to a module |
---|
| 577 | * @param module: module to add the class to |
---|
| 578 | */ |
---|
| 579 | void addCMultiShellModel(PyObject *module) { |
---|
| 580 | PyObject *d; |
---|
| 581 | |
---|
| 582 | if (PyType_Ready(&CMultiShellModelType) < 0) |
---|
| 583 | return; |
---|
| 584 | |
---|
| 585 | Py_INCREF(&CMultiShellModelType); |
---|
| 586 | PyModule_AddObject(module, "CMultiShellModel", (PyObject *)&CMultiShellModelType); |
---|
| 587 | |
---|
| 588 | d = PyModule_GetDict(module); |
---|
| 589 | CMultiShellModelError = PyErr_NewException("CMultiShellModel.error", NULL, NULL); |
---|
| 590 | PyDict_SetItemString(d, "CMultiShellModelError", CMultiShellModelError); |
---|
| 591 | } |
---|
| 592 | |
---|