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