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