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