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