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