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