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