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