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