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 | /** CReflAdvModel |
---|
16 | * |
---|
17 | * C extension |
---|
18 | * |
---|
19 | * WARNING: THIS FILE WAS GENERATED BY WRAPPERGENERATOR.PY |
---|
20 | * DO NOT MODIFY THIS FILE, MODIFY refl_adv.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 "refl_adv.h" |
---|
36 | } |
---|
37 | |
---|
38 | #include "models.hh" |
---|
39 | #include "dispersion_visitor.hh" |
---|
40 | |
---|
41 | /// Error object for raised exceptions |
---|
42 | static PyObject * CReflAdvModelError = 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 | ReflAdvModel * model; |
---|
54 | /// Log for unit testing |
---|
55 | PyObject * log; |
---|
56 | } CReflAdvModel; |
---|
57 | |
---|
58 | |
---|
59 | static void |
---|
60 | CReflAdvModel_dealloc(CReflAdvModel* 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 | CReflAdvModel_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
---|
73 | { |
---|
74 | CReflAdvModel *self; |
---|
75 | |
---|
76 | self = (CReflAdvModel *)type->tp_alloc(type, 0); |
---|
77 | |
---|
78 | return (PyObject *)self; |
---|
79 | } |
---|
80 | |
---|
81 | static int |
---|
82 | CReflAdvModel_init(CReflAdvModel *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 ReflAdvModel(); |
---|
90 | |
---|
91 | // Initialize parameter dictionary |
---|
92 | PyDict_SetItemString(self->params,"sldIM_flat4",Py_BuildValue("d",0.000000000000)); |
---|
93 | PyDict_SetItemString(self->params,"thick_flat8",Py_BuildValue("d",100.000000000000)); |
---|
94 | PyDict_SetItemString(self->params,"thick_flat9",Py_BuildValue("d",100.000000000000)); |
---|
95 | PyDict_SetItemString(self->params,"sldIM_flat7",Py_BuildValue("d",0.000000000000)); |
---|
96 | PyDict_SetItemString(self->params,"sldIM_flat10",Py_BuildValue("d",0.000000000000)); |
---|
97 | PyDict_SetItemString(self->params,"sldIM_flat5",Py_BuildValue("d",0.000000000000)); |
---|
98 | PyDict_SetItemString(self->params,"thick_flat1",Py_BuildValue("d",100.000000000000)); |
---|
99 | PyDict_SetItemString(self->params,"thick_flat3",Py_BuildValue("d",100.000000000000)); |
---|
100 | PyDict_SetItemString(self->params,"n_layers",Py_BuildValue("d",1.000000000000)); |
---|
101 | PyDict_SetItemString(self->params,"thick_flat6",Py_BuildValue("d",100.000000000000)); |
---|
102 | PyDict_SetItemString(self->params,"thick_flat7",Py_BuildValue("d",100.000000000000)); |
---|
103 | PyDict_SetItemString(self->params,"thick_flat4",Py_BuildValue("d",100.000000000000)); |
---|
104 | PyDict_SetItemString(self->params,"thick_flat5",Py_BuildValue("d",100.000000000000)); |
---|
105 | PyDict_SetItemString(self->params,"thick_inter0",Py_BuildValue("d",50.000000000000)); |
---|
106 | PyDict_SetItemString(self->params,"thick_inter1",Py_BuildValue("d",50.000000000000)); |
---|
107 | PyDict_SetItemString(self->params,"sld_bottom0",Py_BuildValue("d",0.000002070000)); |
---|
108 | PyDict_SetItemString(self->params,"thick_inter3",Py_BuildValue("d",50.000000000000)); |
---|
109 | PyDict_SetItemString(self->params,"thick_inter4",Py_BuildValue("d",50.000000000000)); |
---|
110 | PyDict_SetItemString(self->params,"thick_inter5",Py_BuildValue("d",50.000000000000)); |
---|
111 | PyDict_SetItemString(self->params,"thick_inter6",Py_BuildValue("d",50.000000000000)); |
---|
112 | PyDict_SetItemString(self->params,"thick_inter7",Py_BuildValue("d",50.000000000000)); |
---|
113 | PyDict_SetItemString(self->params,"thick_inter8",Py_BuildValue("d",50.000000000000)); |
---|
114 | PyDict_SetItemString(self->params,"thick_inter9",Py_BuildValue("d",50.000000000000)); |
---|
115 | PyDict_SetItemString(self->params,"npts_inter",Py_BuildValue("d",21.000000000000)); |
---|
116 | PyDict_SetItemString(self->params,"sldIM_flat1",Py_BuildValue("d",0.000000000000)); |
---|
117 | PyDict_SetItemString(self->params,"scale",Py_BuildValue("d",1.000000000000)); |
---|
118 | PyDict_SetItemString(self->params,"sldIM_flat2",Py_BuildValue("d",0.000000000000)); |
---|
119 | PyDict_SetItemString(self->params,"func_inter9",Py_BuildValue("d",0.000000000000)); |
---|
120 | PyDict_SetItemString(self->params,"thick_inter10",Py_BuildValue("d",50.000000000000)); |
---|
121 | PyDict_SetItemString(self->params,"sldIM_flat3",Py_BuildValue("d",0.000000000000)); |
---|
122 | PyDict_SetItemString(self->params,"func_inter8",Py_BuildValue("d",0.000000000000)); |
---|
123 | PyDict_SetItemString(self->params,"thick_flat2",Py_BuildValue("d",100.000000000000)); |
---|
124 | PyDict_SetItemString(self->params,"func_inter0",Py_BuildValue("d",0.000000000000)); |
---|
125 | PyDict_SetItemString(self->params,"sld_medium",Py_BuildValue("d",0.000001000000)); |
---|
126 | PyDict_SetItemString(self->params,"func_inter2",Py_BuildValue("d",0.000000000000)); |
---|
127 | PyDict_SetItemString(self->params,"func_inter3",Py_BuildValue("d",0.000000000000)); |
---|
128 | PyDict_SetItemString(self->params,"thick_flat10",Py_BuildValue("d",100.000000000000)); |
---|
129 | PyDict_SetItemString(self->params,"func_inter5",Py_BuildValue("d",0.000000000000)); |
---|
130 | PyDict_SetItemString(self->params,"func_inter6",Py_BuildValue("d",0.000000000000)); |
---|
131 | PyDict_SetItemString(self->params,"func_inter7",Py_BuildValue("d",0.000000000000)); |
---|
132 | PyDict_SetItemString(self->params,"sld_flat4",Py_BuildValue("d",0.000003500000)); |
---|
133 | PyDict_SetItemString(self->params,"sld_flat5",Py_BuildValue("d",0.000004000000)); |
---|
134 | PyDict_SetItemString(self->params,"sld_flat6",Py_BuildValue("d",0.000003500000)); |
---|
135 | PyDict_SetItemString(self->params,"sld_flat7",Py_BuildValue("d",0.000004000000)); |
---|
136 | PyDict_SetItemString(self->params,"sld_flat1",Py_BuildValue("d",0.000004000000)); |
---|
137 | PyDict_SetItemString(self->params,"sld_flat2",Py_BuildValue("d",0.000003500000)); |
---|
138 | PyDict_SetItemString(self->params,"sld_flat3",Py_BuildValue("d",0.000004000000)); |
---|
139 | PyDict_SetItemString(self->params,"sldIM_flat8",Py_BuildValue("d",0.000000000000)); |
---|
140 | PyDict_SetItemString(self->params,"sld_flat8",Py_BuildValue("d",0.000003500000)); |
---|
141 | PyDict_SetItemString(self->params,"sld_flat9",Py_BuildValue("d",0.000004000000)); |
---|
142 | PyDict_SetItemString(self->params,"sldIM_flat9",Py_BuildValue("d",0.000000000000)); |
---|
143 | PyDict_SetItemString(self->params,"nu_inter10",Py_BuildValue("d",2.500000000000)); |
---|
144 | PyDict_SetItemString(self->params,"background",Py_BuildValue("d",0.000000000000)); |
---|
145 | PyDict_SetItemString(self->params,"func_inter1",Py_BuildValue("d",0.000000000000)); |
---|
146 | PyDict_SetItemString(self->params,"sldIM_sub0",Py_BuildValue("d",0.000000000000)); |
---|
147 | PyDict_SetItemString(self->params,"sldIM_medium",Py_BuildValue("d",0.000000000000)); |
---|
148 | PyDict_SetItemString(self->params,"sld_flat10",Py_BuildValue("d",0.000003500000)); |
---|
149 | PyDict_SetItemString(self->params,"sldIM_flat6",Py_BuildValue("d",0.000000000000)); |
---|
150 | PyDict_SetItemString(self->params,"func_inter4",Py_BuildValue("d",0.000000000000)); |
---|
151 | PyDict_SetItemString(self->params,"thick_inter2",Py_BuildValue("d",50.000000000000)); |
---|
152 | PyDict_SetItemString(self->params,"nu_inter8",Py_BuildValue("d",2.500000000000)); |
---|
153 | PyDict_SetItemString(self->params,"nu_inter5",Py_BuildValue("d",2.500000000000)); |
---|
154 | PyDict_SetItemString(self->params,"nu_inter4",Py_BuildValue("d",2.500000000000)); |
---|
155 | PyDict_SetItemString(self->params,"nu_inter7",Py_BuildValue("d",2.500000000000)); |
---|
156 | PyDict_SetItemString(self->params,"nu_inter6",Py_BuildValue("d",2.500000000000)); |
---|
157 | PyDict_SetItemString(self->params,"nu_inter1",Py_BuildValue("d",2.500000000000)); |
---|
158 | PyDict_SetItemString(self->params,"nu_inter0",Py_BuildValue("d",2.500000000000)); |
---|
159 | PyDict_SetItemString(self->params,"nu_inter3",Py_BuildValue("d",2.500000000000)); |
---|
160 | PyDict_SetItemString(self->params,"nu_inter2",Py_BuildValue("d",2.500000000000)); |
---|
161 | PyDict_SetItemString(self->params,"nu_inter9",Py_BuildValue("d",2.500000000000)); |
---|
162 | PyDict_SetItemString(self->params,"func_inter10",Py_BuildValue("d",0.000000000000)); |
---|
163 | // Initialize dispersion / averaging parameter dict |
---|
164 | DispersionVisitor* visitor = new DispersionVisitor(); |
---|
165 | PyObject * disp_dict; |
---|
166 | disp_dict = PyDict_New(); |
---|
167 | self->model->thick_inter0.dispersion->accept_as_source(visitor, self->model->thick_inter0.dispersion, disp_dict); |
---|
168 | PyDict_SetItemString(self->dispersion, "thick_inter0", disp_dict); |
---|
169 | |
---|
170 | |
---|
171 | |
---|
172 | // Create empty log |
---|
173 | self->log = PyDict_New(); |
---|
174 | |
---|
175 | |
---|
176 | |
---|
177 | } |
---|
178 | return 0; |
---|
179 | } |
---|
180 | |
---|
181 | static char name_params[] = "params"; |
---|
182 | static char def_params[] = "Parameters"; |
---|
183 | static char name_dispersion[] = "dispersion"; |
---|
184 | static char def_dispersion[] = "Dispersion parameters"; |
---|
185 | static char name_log[] = "log"; |
---|
186 | static char def_log[] = "Log"; |
---|
187 | |
---|
188 | static PyMemberDef CReflAdvModel_members[] = { |
---|
189 | {name_params, T_OBJECT, offsetof(CReflAdvModel, params), 0, def_params}, |
---|
190 | {name_dispersion, T_OBJECT, offsetof(CReflAdvModel, dispersion), 0, def_dispersion}, |
---|
191 | {name_log, T_OBJECT, offsetof(CReflAdvModel, log), 0, def_log}, |
---|
192 | {NULL} /* Sentinel */ |
---|
193 | }; |
---|
194 | |
---|
195 | /** Read double from PyObject |
---|
196 | @param p PyObject |
---|
197 | @return double |
---|
198 | */ |
---|
199 | double CReflAdvModel_readDouble(PyObject *p) { |
---|
200 | if (PyFloat_Check(p)==1) { |
---|
201 | return (double)(((PyFloatObject *)(p))->ob_fval); |
---|
202 | } else if (PyInt_Check(p)==1) { |
---|
203 | return (double)(((PyIntObject *)(p))->ob_ival); |
---|
204 | } else if (PyLong_Check(p)==1) { |
---|
205 | return (double)PyLong_AsLong(p); |
---|
206 | } else { |
---|
207 | return 0.0; |
---|
208 | } |
---|
209 | } |
---|
210 | /** |
---|
211 | * Function to call to evaluate model |
---|
212 | * @param args: input numpy array q[] |
---|
213 | * @return: numpy array object |
---|
214 | */ |
---|
215 | |
---|
216 | static PyObject *evaluateOneDim(ReflAdvModel* model, PyArrayObject *q){ |
---|
217 | PyArrayObject *result; |
---|
218 | |
---|
219 | // Check validity of array q , q must be of dimension 1, an array of double |
---|
220 | if (q->nd != 1 || q->descr->type_num != PyArray_DOUBLE) |
---|
221 | { |
---|
222 | //const char * message= "Invalid array: q->nd=%d,type_num=%d\n",q->nd,q->descr->type_num; |
---|
223 | //PyErr_SetString(PyExc_ValueError , message); |
---|
224 | return NULL; |
---|
225 | } |
---|
226 | result = (PyArrayObject *)PyArray_SimpleNew(q->nd, (npy_intp *)(q->dimensions), PyArray_DOUBLE); |
---|
227 | if (result == NULL) { |
---|
228 | const char * message= "Could not create result "; |
---|
229 | PyErr_SetString(PyExc_RuntimeError , message); |
---|
230 | return NULL; |
---|
231 | } |
---|
232 | #pragma omp parallel for |
---|
233 | for (int i = 0; i < q->dimensions[0]; i++){ |
---|
234 | double q_value = *(double *)(q->data + i*q->strides[0]); |
---|
235 | double *result_value = (double *)(result->data + i*result->strides[0]); |
---|
236 | *result_value =(*model)(q_value); |
---|
237 | } |
---|
238 | return PyArray_Return(result); |
---|
239 | } |
---|
240 | |
---|
241 | /** |
---|
242 | * Function to call to evaluate model |
---|
243 | * @param args: input numpy array [x[],y[]] |
---|
244 | * @return: numpy array object |
---|
245 | */ |
---|
246 | static PyObject * evaluateTwoDimXY( ReflAdvModel* model, |
---|
247 | PyArrayObject *x, PyArrayObject *y) |
---|
248 | { |
---|
249 | PyArrayObject *result; |
---|
250 | int x_len, y_len, dims[1]; |
---|
251 | //check validity of input vectors |
---|
252 | if (x->nd != 1 || x->descr->type_num != PyArray_DOUBLE |
---|
253 | || y->nd != 1 || y->descr->type_num != PyArray_DOUBLE |
---|
254 | || y->dimensions[0] != x->dimensions[0]){ |
---|
255 | const char * message= "evaluateTwoDimXY expect 2 numpy arrays"; |
---|
256 | PyErr_SetString(PyExc_ValueError , message); |
---|
257 | return NULL; |
---|
258 | } |
---|
259 | |
---|
260 | if (PyArray_Check(x) && PyArray_Check(y)) { |
---|
261 | |
---|
262 | x_len = dims[0]= x->dimensions[0]; |
---|
263 | y_len = dims[0]= y->dimensions[0]; |
---|
264 | |
---|
265 | // Make a new double matrix of same dims |
---|
266 | result=(PyArrayObject *) PyArray_SimpleNew(1,(npy_intp *)dims,NPY_DOUBLE); |
---|
267 | if (result == NULL){ |
---|
268 | const char * message= "Could not create result "; |
---|
269 | PyErr_SetString(PyExc_RuntimeError , message); |
---|
270 | return NULL; |
---|
271 | } |
---|
272 | |
---|
273 | /* Do the calculation. */ |
---|
274 | #pragma omp parallel for |
---|
275 | for (int i=0; i< x_len; i++) { |
---|
276 | double x_value = *(double *)(x->data + i*x->strides[0]); |
---|
277 | double y_value = *(double *)(y->data + i*y->strides[0]); |
---|
278 | double *result_value = (double *)(result->data + |
---|
279 | i*result->strides[0]); |
---|
280 | *result_value = (*model)(x_value, y_value); |
---|
281 | } |
---|
282 | return PyArray_Return(result); |
---|
283 | |
---|
284 | }else{ |
---|
285 | PyErr_SetString(CReflAdvModelError, |
---|
286 | "CReflAdvModel.evaluateTwoDimXY couldn't run."); |
---|
287 | return NULL; |
---|
288 | } |
---|
289 | } |
---|
290 | /** |
---|
291 | * evalDistribution function evaluate a model function with input vector |
---|
292 | * @param args: input q as vector or [qx, qy] where qx, qy are vectors |
---|
293 | * |
---|
294 | */ |
---|
295 | static PyObject * evalDistribution(CReflAdvModel *self, PyObject *args){ |
---|
296 | PyObject *qx, *qy; |
---|
297 | PyArrayObject * pars; |
---|
298 | int npars ,mpars; |
---|
299 | |
---|
300 | // Get parameters |
---|
301 | |
---|
302 | // Reader parameter dictionary |
---|
303 | self->model->sldIM_flat4 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sldIM_flat4") ); |
---|
304 | self->model->thick_flat8 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thick_flat8") ); |
---|
305 | self->model->thick_flat9 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thick_flat9") ); |
---|
306 | self->model->sldIM_flat7 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sldIM_flat7") ); |
---|
307 | self->model->sldIM_flat10 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sldIM_flat10") ); |
---|
308 | self->model->sldIM_flat5 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sldIM_flat5") ); |
---|
309 | self->model->thick_flat1 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thick_flat1") ); |
---|
310 | self->model->thick_flat3 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thick_flat3") ); |
---|
311 | self->model->n_layers = PyFloat_AsDouble( PyDict_GetItemString(self->params, "n_layers") ); |
---|
312 | self->model->thick_flat6 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thick_flat6") ); |
---|
313 | self->model->thick_flat7 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thick_flat7") ); |
---|
314 | self->model->thick_flat4 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thick_flat4") ); |
---|
315 | self->model->thick_flat5 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thick_flat5") ); |
---|
316 | self->model->thick_inter0 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thick_inter0") ); |
---|
317 | self->model->thick_inter1 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thick_inter1") ); |
---|
318 | self->model->sld_bottom0 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_bottom0") ); |
---|
319 | self->model->thick_inter3 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thick_inter3") ); |
---|
320 | self->model->thick_inter4 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thick_inter4") ); |
---|
321 | self->model->thick_inter5 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thick_inter5") ); |
---|
322 | self->model->thick_inter6 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thick_inter6") ); |
---|
323 | self->model->thick_inter7 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thick_inter7") ); |
---|
324 | self->model->thick_inter8 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thick_inter8") ); |
---|
325 | self->model->thick_inter9 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thick_inter9") ); |
---|
326 | self->model->npts_inter = PyFloat_AsDouble( PyDict_GetItemString(self->params, "npts_inter") ); |
---|
327 | self->model->sldIM_flat1 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sldIM_flat1") ); |
---|
328 | self->model->scale = PyFloat_AsDouble( PyDict_GetItemString(self->params, "scale") ); |
---|
329 | self->model->sldIM_flat2 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sldIM_flat2") ); |
---|
330 | self->model->func_inter9 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "func_inter9") ); |
---|
331 | self->model->thick_inter10 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thick_inter10") ); |
---|
332 | self->model->sldIM_flat3 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sldIM_flat3") ); |
---|
333 | self->model->func_inter8 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "func_inter8") ); |
---|
334 | self->model->thick_flat2 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thick_flat2") ); |
---|
335 | self->model->func_inter0 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "func_inter0") ); |
---|
336 | self->model->sld_medium = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_medium") ); |
---|
337 | self->model->func_inter2 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "func_inter2") ); |
---|
338 | self->model->func_inter3 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "func_inter3") ); |
---|
339 | self->model->thick_flat10 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thick_flat10") ); |
---|
340 | self->model->func_inter5 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "func_inter5") ); |
---|
341 | self->model->func_inter6 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "func_inter6") ); |
---|
342 | self->model->func_inter7 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "func_inter7") ); |
---|
343 | self->model->sld_flat4 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_flat4") ); |
---|
344 | self->model->sld_flat5 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_flat5") ); |
---|
345 | self->model->sld_flat6 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_flat6") ); |
---|
346 | self->model->sld_flat7 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_flat7") ); |
---|
347 | self->model->sld_flat1 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_flat1") ); |
---|
348 | self->model->sld_flat2 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_flat2") ); |
---|
349 | self->model->sld_flat3 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_flat3") ); |
---|
350 | self->model->sldIM_flat8 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sldIM_flat8") ); |
---|
351 | self->model->sld_flat8 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_flat8") ); |
---|
352 | self->model->sld_flat9 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_flat9") ); |
---|
353 | self->model->sldIM_flat9 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sldIM_flat9") ); |
---|
354 | self->model->nu_inter10 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "nu_inter10") ); |
---|
355 | self->model->background = PyFloat_AsDouble( PyDict_GetItemString(self->params, "background") ); |
---|
356 | self->model->func_inter1 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "func_inter1") ); |
---|
357 | self->model->sldIM_sub0 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sldIM_sub0") ); |
---|
358 | self->model->sldIM_medium = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sldIM_medium") ); |
---|
359 | self->model->sld_flat10 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_flat10") ); |
---|
360 | self->model->sldIM_flat6 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sldIM_flat6") ); |
---|
361 | self->model->func_inter4 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "func_inter4") ); |
---|
362 | self->model->thick_inter2 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thick_inter2") ); |
---|
363 | self->model->nu_inter8 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "nu_inter8") ); |
---|
364 | self->model->nu_inter5 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "nu_inter5") ); |
---|
365 | self->model->nu_inter4 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "nu_inter4") ); |
---|
366 | self->model->nu_inter7 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "nu_inter7") ); |
---|
367 | self->model->nu_inter6 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "nu_inter6") ); |
---|
368 | self->model->nu_inter1 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "nu_inter1") ); |
---|
369 | self->model->nu_inter0 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "nu_inter0") ); |
---|
370 | self->model->nu_inter3 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "nu_inter3") ); |
---|
371 | self->model->nu_inter2 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "nu_inter2") ); |
---|
372 | self->model->nu_inter9 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "nu_inter9") ); |
---|
373 | self->model->func_inter10 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "func_inter10") ); |
---|
374 | // Read in dispersion parameters |
---|
375 | PyObject* disp_dict; |
---|
376 | DispersionVisitor* visitor = new DispersionVisitor(); |
---|
377 | disp_dict = PyDict_GetItemString(self->dispersion, "thick_inter0"); |
---|
378 | self->model->thick_inter0.dispersion->accept_as_destination(visitor, self->model->thick_inter0.dispersion, disp_dict); |
---|
379 | |
---|
380 | |
---|
381 | // Get input and determine whether we have to supply a 1D or 2D return value. |
---|
382 | if ( !PyArg_ParseTuple(args,"O",&pars) ) { |
---|
383 | PyErr_SetString(CReflAdvModelError, |
---|
384 | "CReflAdvModel.evalDistribution expects a q value."); |
---|
385 | return NULL; |
---|
386 | } |
---|
387 | // Check params |
---|
388 | |
---|
389 | if(PyArray_Check(pars)==1) { |
---|
390 | |
---|
391 | // Length of list should 1 or 2 |
---|
392 | npars = pars->nd; |
---|
393 | if(npars==1) { |
---|
394 | // input is a numpy array |
---|
395 | if (PyArray_Check(pars)) { |
---|
396 | return evaluateOneDim(self->model, (PyArrayObject*)pars); |
---|
397 | } |
---|
398 | }else{ |
---|
399 | PyErr_SetString(CReflAdvModelError, |
---|
400 | "CReflAdvModel.evalDistribution expect numpy array of one dimension."); |
---|
401 | return NULL; |
---|
402 | } |
---|
403 | }else if( PyList_Check(pars)==1) { |
---|
404 | // Length of list should be 2 for I(qx,qy) |
---|
405 | mpars = PyList_GET_SIZE(pars); |
---|
406 | if(mpars!=2) { |
---|
407 | PyErr_SetString(CReflAdvModelError, |
---|
408 | "CReflAdvModel.evalDistribution expects a list of dimension 2."); |
---|
409 | return NULL; |
---|
410 | } |
---|
411 | qx = PyList_GET_ITEM(pars,0); |
---|
412 | qy = PyList_GET_ITEM(pars,1); |
---|
413 | if (PyArray_Check(qx) && PyArray_Check(qy)) { |
---|
414 | return evaluateTwoDimXY(self->model, (PyArrayObject*)qx, |
---|
415 | (PyArrayObject*)qy); |
---|
416 | }else{ |
---|
417 | PyErr_SetString(CReflAdvModelError, |
---|
418 | "CReflAdvModel.evalDistribution expect 2 numpy arrays in list."); |
---|
419 | return NULL; |
---|
420 | } |
---|
421 | } |
---|
422 | PyErr_SetString(CReflAdvModelError, |
---|
423 | "CReflAdvModel.evalDistribution couln't be run."); |
---|
424 | return NULL; |
---|
425 | |
---|
426 | } |
---|
427 | |
---|
428 | /** |
---|
429 | * Function to call to evaluate model |
---|
430 | * @param args: input q or [q,phi] |
---|
431 | * @return: function value |
---|
432 | */ |
---|
433 | static PyObject * run(CReflAdvModel *self, PyObject *args) { |
---|
434 | double q_value, phi_value; |
---|
435 | PyObject* pars; |
---|
436 | int npars; |
---|
437 | |
---|
438 | // Get parameters |
---|
439 | |
---|
440 | // Reader parameter dictionary |
---|
441 | self->model->sldIM_flat4 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sldIM_flat4") ); |
---|
442 | self->model->thick_flat8 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thick_flat8") ); |
---|
443 | self->model->thick_flat9 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thick_flat9") ); |
---|
444 | self->model->sldIM_flat7 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sldIM_flat7") ); |
---|
445 | self->model->sldIM_flat10 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sldIM_flat10") ); |
---|
446 | self->model->sldIM_flat5 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sldIM_flat5") ); |
---|
447 | self->model->thick_flat1 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thick_flat1") ); |
---|
448 | self->model->thick_flat3 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thick_flat3") ); |
---|
449 | self->model->n_layers = PyFloat_AsDouble( PyDict_GetItemString(self->params, "n_layers") ); |
---|
450 | self->model->thick_flat6 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thick_flat6") ); |
---|
451 | self->model->thick_flat7 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thick_flat7") ); |
---|
452 | self->model->thick_flat4 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thick_flat4") ); |
---|
453 | self->model->thick_flat5 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thick_flat5") ); |
---|
454 | self->model->thick_inter0 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thick_inter0") ); |
---|
455 | self->model->thick_inter1 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thick_inter1") ); |
---|
456 | self->model->sld_bottom0 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_bottom0") ); |
---|
457 | self->model->thick_inter3 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thick_inter3") ); |
---|
458 | self->model->thick_inter4 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thick_inter4") ); |
---|
459 | self->model->thick_inter5 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thick_inter5") ); |
---|
460 | self->model->thick_inter6 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thick_inter6") ); |
---|
461 | self->model->thick_inter7 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thick_inter7") ); |
---|
462 | self->model->thick_inter8 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thick_inter8") ); |
---|
463 | self->model->thick_inter9 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thick_inter9") ); |
---|
464 | self->model->npts_inter = PyFloat_AsDouble( PyDict_GetItemString(self->params, "npts_inter") ); |
---|
465 | self->model->sldIM_flat1 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sldIM_flat1") ); |
---|
466 | self->model->scale = PyFloat_AsDouble( PyDict_GetItemString(self->params, "scale") ); |
---|
467 | self->model->sldIM_flat2 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sldIM_flat2") ); |
---|
468 | self->model->func_inter9 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "func_inter9") ); |
---|
469 | self->model->thick_inter10 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thick_inter10") ); |
---|
470 | self->model->sldIM_flat3 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sldIM_flat3") ); |
---|
471 | self->model->func_inter8 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "func_inter8") ); |
---|
472 | self->model->thick_flat2 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thick_flat2") ); |
---|
473 | self->model->func_inter0 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "func_inter0") ); |
---|
474 | self->model->sld_medium = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_medium") ); |
---|
475 | self->model->func_inter2 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "func_inter2") ); |
---|
476 | self->model->func_inter3 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "func_inter3") ); |
---|
477 | self->model->thick_flat10 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thick_flat10") ); |
---|
478 | self->model->func_inter5 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "func_inter5") ); |
---|
479 | self->model->func_inter6 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "func_inter6") ); |
---|
480 | self->model->func_inter7 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "func_inter7") ); |
---|
481 | self->model->sld_flat4 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_flat4") ); |
---|
482 | self->model->sld_flat5 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_flat5") ); |
---|
483 | self->model->sld_flat6 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_flat6") ); |
---|
484 | self->model->sld_flat7 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_flat7") ); |
---|
485 | self->model->sld_flat1 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_flat1") ); |
---|
486 | self->model->sld_flat2 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_flat2") ); |
---|
487 | self->model->sld_flat3 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_flat3") ); |
---|
488 | self->model->sldIM_flat8 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sldIM_flat8") ); |
---|
489 | self->model->sld_flat8 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_flat8") ); |
---|
490 | self->model->sld_flat9 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_flat9") ); |
---|
491 | self->model->sldIM_flat9 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sldIM_flat9") ); |
---|
492 | self->model->nu_inter10 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "nu_inter10") ); |
---|
493 | self->model->background = PyFloat_AsDouble( PyDict_GetItemString(self->params, "background") ); |
---|
494 | self->model->func_inter1 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "func_inter1") ); |
---|
495 | self->model->sldIM_sub0 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sldIM_sub0") ); |
---|
496 | self->model->sldIM_medium = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sldIM_medium") ); |
---|
497 | self->model->sld_flat10 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_flat10") ); |
---|
498 | self->model->sldIM_flat6 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sldIM_flat6") ); |
---|
499 | self->model->func_inter4 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "func_inter4") ); |
---|
500 | self->model->thick_inter2 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thick_inter2") ); |
---|
501 | self->model->nu_inter8 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "nu_inter8") ); |
---|
502 | self->model->nu_inter5 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "nu_inter5") ); |
---|
503 | self->model->nu_inter4 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "nu_inter4") ); |
---|
504 | self->model->nu_inter7 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "nu_inter7") ); |
---|
505 | self->model->nu_inter6 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "nu_inter6") ); |
---|
506 | self->model->nu_inter1 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "nu_inter1") ); |
---|
507 | self->model->nu_inter0 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "nu_inter0") ); |
---|
508 | self->model->nu_inter3 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "nu_inter3") ); |
---|
509 | self->model->nu_inter2 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "nu_inter2") ); |
---|
510 | self->model->nu_inter9 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "nu_inter9") ); |
---|
511 | self->model->func_inter10 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "func_inter10") ); |
---|
512 | // Read in dispersion parameters |
---|
513 | PyObject* disp_dict; |
---|
514 | DispersionVisitor* visitor = new DispersionVisitor(); |
---|
515 | disp_dict = PyDict_GetItemString(self->dispersion, "thick_inter0"); |
---|
516 | self->model->thick_inter0.dispersion->accept_as_destination(visitor, self->model->thick_inter0.dispersion, disp_dict); |
---|
517 | |
---|
518 | |
---|
519 | // Get input and determine whether we have to supply a 1D or 2D return value. |
---|
520 | if ( !PyArg_ParseTuple(args,"O",&pars) ) { |
---|
521 | PyErr_SetString(CReflAdvModelError, |
---|
522 | "CReflAdvModel.run expects a q value."); |
---|
523 | return NULL; |
---|
524 | } |
---|
525 | |
---|
526 | // Check params |
---|
527 | if( PyList_Check(pars)==1) { |
---|
528 | |
---|
529 | // Length of list should be 2 for I(q,phi) |
---|
530 | npars = PyList_GET_SIZE(pars); |
---|
531 | if(npars!=2) { |
---|
532 | PyErr_SetString(CReflAdvModelError, |
---|
533 | "CReflAdvModel.run expects a double or a list of dimension 2."); |
---|
534 | return NULL; |
---|
535 | } |
---|
536 | // We have a vector q, get the q and phi values at which |
---|
537 | // to evaluate I(q,phi) |
---|
538 | q_value = CReflAdvModel_readDouble(PyList_GET_ITEM(pars,0)); |
---|
539 | phi_value = CReflAdvModel_readDouble(PyList_GET_ITEM(pars,1)); |
---|
540 | // Skip zero |
---|
541 | if (q_value==0) { |
---|
542 | return Py_BuildValue("d",0.0); |
---|
543 | } |
---|
544 | return Py_BuildValue("d",(*(self->model)).evaluate_rphi(q_value,phi_value)); |
---|
545 | |
---|
546 | } else { |
---|
547 | |
---|
548 | // We have a scalar q, we will evaluate I(q) |
---|
549 | q_value = CReflAdvModel_readDouble(pars); |
---|
550 | |
---|
551 | return Py_BuildValue("d",(*(self->model))(q_value)); |
---|
552 | } |
---|
553 | } |
---|
554 | /** |
---|
555 | * Function to call to calculate_ER |
---|
556 | * @return: effective radius value |
---|
557 | */ |
---|
558 | static PyObject * calculate_ER(CReflAdvModel *self) { |
---|
559 | |
---|
560 | // Get parameters |
---|
561 | |
---|
562 | // Reader parameter dictionary |
---|
563 | self->model->sldIM_flat4 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sldIM_flat4") ); |
---|
564 | self->model->thick_flat8 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thick_flat8") ); |
---|
565 | self->model->thick_flat9 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thick_flat9") ); |
---|
566 | self->model->sldIM_flat7 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sldIM_flat7") ); |
---|
567 | self->model->sldIM_flat10 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sldIM_flat10") ); |
---|
568 | self->model->sldIM_flat5 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sldIM_flat5") ); |
---|
569 | self->model->thick_flat1 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thick_flat1") ); |
---|
570 | self->model->thick_flat3 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thick_flat3") ); |
---|
571 | self->model->n_layers = PyFloat_AsDouble( PyDict_GetItemString(self->params, "n_layers") ); |
---|
572 | self->model->thick_flat6 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thick_flat6") ); |
---|
573 | self->model->thick_flat7 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thick_flat7") ); |
---|
574 | self->model->thick_flat4 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thick_flat4") ); |
---|
575 | self->model->thick_flat5 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thick_flat5") ); |
---|
576 | self->model->thick_inter0 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thick_inter0") ); |
---|
577 | self->model->thick_inter1 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thick_inter1") ); |
---|
578 | self->model->sld_bottom0 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_bottom0") ); |
---|
579 | self->model->thick_inter3 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thick_inter3") ); |
---|
580 | self->model->thick_inter4 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thick_inter4") ); |
---|
581 | self->model->thick_inter5 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thick_inter5") ); |
---|
582 | self->model->thick_inter6 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thick_inter6") ); |
---|
583 | self->model->thick_inter7 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thick_inter7") ); |
---|
584 | self->model->thick_inter8 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thick_inter8") ); |
---|
585 | self->model->thick_inter9 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thick_inter9") ); |
---|
586 | self->model->npts_inter = PyFloat_AsDouble( PyDict_GetItemString(self->params, "npts_inter") ); |
---|
587 | self->model->sldIM_flat1 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sldIM_flat1") ); |
---|
588 | self->model->scale = PyFloat_AsDouble( PyDict_GetItemString(self->params, "scale") ); |
---|
589 | self->model->sldIM_flat2 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sldIM_flat2") ); |
---|
590 | self->model->func_inter9 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "func_inter9") ); |
---|
591 | self->model->thick_inter10 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thick_inter10") ); |
---|
592 | self->model->sldIM_flat3 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sldIM_flat3") ); |
---|
593 | self->model->func_inter8 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "func_inter8") ); |
---|
594 | self->model->thick_flat2 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thick_flat2") ); |
---|
595 | self->model->func_inter0 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "func_inter0") ); |
---|
596 | self->model->sld_medium = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_medium") ); |
---|
597 | self->model->func_inter2 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "func_inter2") ); |
---|
598 | self->model->func_inter3 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "func_inter3") ); |
---|
599 | self->model->thick_flat10 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thick_flat10") ); |
---|
600 | self->model->func_inter5 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "func_inter5") ); |
---|
601 | self->model->func_inter6 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "func_inter6") ); |
---|
602 | self->model->func_inter7 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "func_inter7") ); |
---|
603 | self->model->sld_flat4 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_flat4") ); |
---|
604 | self->model->sld_flat5 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_flat5") ); |
---|
605 | self->model->sld_flat6 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_flat6") ); |
---|
606 | self->model->sld_flat7 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_flat7") ); |
---|
607 | self->model->sld_flat1 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_flat1") ); |
---|
608 | self->model->sld_flat2 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_flat2") ); |
---|
609 | self->model->sld_flat3 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_flat3") ); |
---|
610 | self->model->sldIM_flat8 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sldIM_flat8") ); |
---|
611 | self->model->sld_flat8 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_flat8") ); |
---|
612 | self->model->sld_flat9 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_flat9") ); |
---|
613 | self->model->sldIM_flat9 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sldIM_flat9") ); |
---|
614 | self->model->nu_inter10 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "nu_inter10") ); |
---|
615 | self->model->background = PyFloat_AsDouble( PyDict_GetItemString(self->params, "background") ); |
---|
616 | self->model->func_inter1 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "func_inter1") ); |
---|
617 | self->model->sldIM_sub0 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sldIM_sub0") ); |
---|
618 | self->model->sldIM_medium = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sldIM_medium") ); |
---|
619 | self->model->sld_flat10 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_flat10") ); |
---|
620 | self->model->sldIM_flat6 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sldIM_flat6") ); |
---|
621 | self->model->func_inter4 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "func_inter4") ); |
---|
622 | self->model->thick_inter2 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thick_inter2") ); |
---|
623 | self->model->nu_inter8 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "nu_inter8") ); |
---|
624 | self->model->nu_inter5 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "nu_inter5") ); |
---|
625 | self->model->nu_inter4 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "nu_inter4") ); |
---|
626 | self->model->nu_inter7 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "nu_inter7") ); |
---|
627 | self->model->nu_inter6 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "nu_inter6") ); |
---|
628 | self->model->nu_inter1 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "nu_inter1") ); |
---|
629 | self->model->nu_inter0 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "nu_inter0") ); |
---|
630 | self->model->nu_inter3 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "nu_inter3") ); |
---|
631 | self->model->nu_inter2 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "nu_inter2") ); |
---|
632 | self->model->nu_inter9 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "nu_inter9") ); |
---|
633 | self->model->func_inter10 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "func_inter10") ); |
---|
634 | // Read in dispersion parameters |
---|
635 | PyObject* disp_dict; |
---|
636 | DispersionVisitor* visitor = new DispersionVisitor(); |
---|
637 | disp_dict = PyDict_GetItemString(self->dispersion, "thick_inter0"); |
---|
638 | self->model->thick_inter0.dispersion->accept_as_destination(visitor, self->model->thick_inter0.dispersion, disp_dict); |
---|
639 | |
---|
640 | |
---|
641 | return Py_BuildValue("d",(*(self->model)).calculate_ER()); |
---|
642 | |
---|
643 | } |
---|
644 | /** |
---|
645 | * Function to call to evaluate model in cartesian coordinates |
---|
646 | * @param args: input q or [qx, qy]] |
---|
647 | * @return: function value |
---|
648 | */ |
---|
649 | static PyObject * runXY(CReflAdvModel *self, PyObject *args) { |
---|
650 | double qx_value, qy_value; |
---|
651 | PyObject* pars; |
---|
652 | int npars; |
---|
653 | |
---|
654 | // Get parameters |
---|
655 | |
---|
656 | // Reader parameter dictionary |
---|
657 | self->model->sldIM_flat4 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sldIM_flat4") ); |
---|
658 | self->model->thick_flat8 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thick_flat8") ); |
---|
659 | self->model->thick_flat9 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thick_flat9") ); |
---|
660 | self->model->sldIM_flat7 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sldIM_flat7") ); |
---|
661 | self->model->sldIM_flat10 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sldIM_flat10") ); |
---|
662 | self->model->sldIM_flat5 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sldIM_flat5") ); |
---|
663 | self->model->thick_flat1 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thick_flat1") ); |
---|
664 | self->model->thick_flat3 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thick_flat3") ); |
---|
665 | self->model->n_layers = PyFloat_AsDouble( PyDict_GetItemString(self->params, "n_layers") ); |
---|
666 | self->model->thick_flat6 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thick_flat6") ); |
---|
667 | self->model->thick_flat7 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thick_flat7") ); |
---|
668 | self->model->thick_flat4 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thick_flat4") ); |
---|
669 | self->model->thick_flat5 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thick_flat5") ); |
---|
670 | self->model->thick_inter0 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thick_inter0") ); |
---|
671 | self->model->thick_inter1 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thick_inter1") ); |
---|
672 | self->model->sld_bottom0 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_bottom0") ); |
---|
673 | self->model->thick_inter3 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thick_inter3") ); |
---|
674 | self->model->thick_inter4 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thick_inter4") ); |
---|
675 | self->model->thick_inter5 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thick_inter5") ); |
---|
676 | self->model->thick_inter6 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thick_inter6") ); |
---|
677 | self->model->thick_inter7 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thick_inter7") ); |
---|
678 | self->model->thick_inter8 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thick_inter8") ); |
---|
679 | self->model->thick_inter9 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thick_inter9") ); |
---|
680 | self->model->npts_inter = PyFloat_AsDouble( PyDict_GetItemString(self->params, "npts_inter") ); |
---|
681 | self->model->sldIM_flat1 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sldIM_flat1") ); |
---|
682 | self->model->scale = PyFloat_AsDouble( PyDict_GetItemString(self->params, "scale") ); |
---|
683 | self->model->sldIM_flat2 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sldIM_flat2") ); |
---|
684 | self->model->func_inter9 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "func_inter9") ); |
---|
685 | self->model->thick_inter10 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thick_inter10") ); |
---|
686 | self->model->sldIM_flat3 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sldIM_flat3") ); |
---|
687 | self->model->func_inter8 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "func_inter8") ); |
---|
688 | self->model->thick_flat2 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thick_flat2") ); |
---|
689 | self->model->func_inter0 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "func_inter0") ); |
---|
690 | self->model->sld_medium = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_medium") ); |
---|
691 | self->model->func_inter2 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "func_inter2") ); |
---|
692 | self->model->func_inter3 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "func_inter3") ); |
---|
693 | self->model->thick_flat10 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thick_flat10") ); |
---|
694 | self->model->func_inter5 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "func_inter5") ); |
---|
695 | self->model->func_inter6 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "func_inter6") ); |
---|
696 | self->model->func_inter7 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "func_inter7") ); |
---|
697 | self->model->sld_flat4 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_flat4") ); |
---|
698 | self->model->sld_flat5 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_flat5") ); |
---|
699 | self->model->sld_flat6 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_flat6") ); |
---|
700 | self->model->sld_flat7 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_flat7") ); |
---|
701 | self->model->sld_flat1 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_flat1") ); |
---|
702 | self->model->sld_flat2 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_flat2") ); |
---|
703 | self->model->sld_flat3 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_flat3") ); |
---|
704 | self->model->sldIM_flat8 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sldIM_flat8") ); |
---|
705 | self->model->sld_flat8 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_flat8") ); |
---|
706 | self->model->sld_flat9 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_flat9") ); |
---|
707 | self->model->sldIM_flat9 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sldIM_flat9") ); |
---|
708 | self->model->nu_inter10 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "nu_inter10") ); |
---|
709 | self->model->background = PyFloat_AsDouble( PyDict_GetItemString(self->params, "background") ); |
---|
710 | self->model->func_inter1 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "func_inter1") ); |
---|
711 | self->model->sldIM_sub0 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sldIM_sub0") ); |
---|
712 | self->model->sldIM_medium = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sldIM_medium") ); |
---|
713 | self->model->sld_flat10 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sld_flat10") ); |
---|
714 | self->model->sldIM_flat6 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "sldIM_flat6") ); |
---|
715 | self->model->func_inter4 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "func_inter4") ); |
---|
716 | self->model->thick_inter2 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thick_inter2") ); |
---|
717 | self->model->nu_inter8 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "nu_inter8") ); |
---|
718 | self->model->nu_inter5 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "nu_inter5") ); |
---|
719 | self->model->nu_inter4 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "nu_inter4") ); |
---|
720 | self->model->nu_inter7 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "nu_inter7") ); |
---|
721 | self->model->nu_inter6 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "nu_inter6") ); |
---|
722 | self->model->nu_inter1 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "nu_inter1") ); |
---|
723 | self->model->nu_inter0 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "nu_inter0") ); |
---|
724 | self->model->nu_inter3 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "nu_inter3") ); |
---|
725 | self->model->nu_inter2 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "nu_inter2") ); |
---|
726 | self->model->nu_inter9 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "nu_inter9") ); |
---|
727 | self->model->func_inter10 = PyFloat_AsDouble( PyDict_GetItemString(self->params, "func_inter10") ); |
---|
728 | // Read in dispersion parameters |
---|
729 | PyObject* disp_dict; |
---|
730 | DispersionVisitor* visitor = new DispersionVisitor(); |
---|
731 | disp_dict = PyDict_GetItemString(self->dispersion, "thick_inter0"); |
---|
732 | self->model->thick_inter0.dispersion->accept_as_destination(visitor, self->model->thick_inter0.dispersion, disp_dict); |
---|
733 | |
---|
734 | |
---|
735 | // Get input and determine whether we have to supply a 1D or 2D return value. |
---|
736 | if ( !PyArg_ParseTuple(args,"O",&pars) ) { |
---|
737 | PyErr_SetString(CReflAdvModelError, |
---|
738 | "CReflAdvModel.run expects a q value."); |
---|
739 | return NULL; |
---|
740 | } |
---|
741 | |
---|
742 | // Check params |
---|
743 | if( PyList_Check(pars)==1) { |
---|
744 | |
---|
745 | // Length of list should be 2 for I(qx, qy)) |
---|
746 | npars = PyList_GET_SIZE(pars); |
---|
747 | if(npars!=2) { |
---|
748 | PyErr_SetString(CReflAdvModelError, |
---|
749 | "CReflAdvModel.run expects a double or a list of dimension 2."); |
---|
750 | return NULL; |
---|
751 | } |
---|
752 | // We have a vector q, get the qx and qy values at which |
---|
753 | // to evaluate I(qx,qy) |
---|
754 | qx_value = CReflAdvModel_readDouble(PyList_GET_ITEM(pars,0)); |
---|
755 | qy_value = CReflAdvModel_readDouble(PyList_GET_ITEM(pars,1)); |
---|
756 | return Py_BuildValue("d",(*(self->model))(qx_value,qy_value)); |
---|
757 | |
---|
758 | } else { |
---|
759 | |
---|
760 | // We have a scalar q, we will evaluate I(q) |
---|
761 | qx_value = CReflAdvModel_readDouble(pars); |
---|
762 | |
---|
763 | return Py_BuildValue("d",(*(self->model))(qx_value)); |
---|
764 | } |
---|
765 | } |
---|
766 | |
---|
767 | static PyObject * reset(CReflAdvModel *self, PyObject *args) { |
---|
768 | |
---|
769 | |
---|
770 | return Py_BuildValue("d",0.0); |
---|
771 | } |
---|
772 | |
---|
773 | static PyObject * set_dispersion(CReflAdvModel *self, PyObject *args) { |
---|
774 | PyObject * disp; |
---|
775 | const char * par_name; |
---|
776 | |
---|
777 | if ( !PyArg_ParseTuple(args,"sO", &par_name, &disp) ) { |
---|
778 | PyErr_SetString(CReflAdvModelError, |
---|
779 | "CReflAdvModel.set_dispersion expects a DispersionModel object."); |
---|
780 | return NULL; |
---|
781 | } |
---|
782 | void *temp = PyCObject_AsVoidPtr(disp); |
---|
783 | DispersionModel * dispersion = static_cast<DispersionModel *>(temp); |
---|
784 | |
---|
785 | |
---|
786 | // Ugliness necessary to go from python to C |
---|
787 | // TODO: refactor this |
---|
788 | if (!strcmp(par_name, "thick_inter0")) { |
---|
789 | self->model->thick_inter0.dispersion = dispersion; |
---|
790 | } else { |
---|
791 | PyErr_SetString(CReflAdvModelError, |
---|
792 | "CReflAdvModel.set_dispersion expects a valid parameter name."); |
---|
793 | return NULL; |
---|
794 | } |
---|
795 | |
---|
796 | DispersionVisitor* visitor = new DispersionVisitor(); |
---|
797 | PyObject * disp_dict = PyDict_New(); |
---|
798 | dispersion->accept_as_source(visitor, dispersion, disp_dict); |
---|
799 | PyDict_SetItemString(self->dispersion, par_name, disp_dict); |
---|
800 | return Py_BuildValue("i",1); |
---|
801 | } |
---|
802 | |
---|
803 | |
---|
804 | static PyMethodDef CReflAdvModel_methods[] = { |
---|
805 | {"run", (PyCFunction)run , METH_VARARGS, |
---|
806 | "Evaluate the model at a given Q or Q, phi"}, |
---|
807 | {"runXY", (PyCFunction)runXY , METH_VARARGS, |
---|
808 | "Evaluate the model at a given Q or Qx, Qy"}, |
---|
809 | {"calculate_ER", (PyCFunction)calculate_ER , METH_VARARGS, |
---|
810 | "Evaluate the model at a given Q or Q, phi"}, |
---|
811 | |
---|
812 | {"evalDistribution", (PyCFunction)evalDistribution , METH_VARARGS, |
---|
813 | "Evaluate the model at a given Q or Qx, Qy vector "}, |
---|
814 | {"reset", (PyCFunction)reset , METH_VARARGS, |
---|
815 | "Reset pair correlation"}, |
---|
816 | {"set_dispersion", (PyCFunction)set_dispersion , METH_VARARGS, |
---|
817 | "Set the dispersion model for a given parameter"}, |
---|
818 | {NULL} |
---|
819 | }; |
---|
820 | |
---|
821 | static PyTypeObject CReflAdvModelType = { |
---|
822 | PyObject_HEAD_INIT(NULL) |
---|
823 | 0, /*ob_size*/ |
---|
824 | "CReflAdvModel", /*tp_name*/ |
---|
825 | sizeof(CReflAdvModel), /*tp_basicsize*/ |
---|
826 | 0, /*tp_itemsize*/ |
---|
827 | (destructor)CReflAdvModel_dealloc, /*tp_dealloc*/ |
---|
828 | 0, /*tp_print*/ |
---|
829 | 0, /*tp_getattr*/ |
---|
830 | 0, /*tp_setattr*/ |
---|
831 | 0, /*tp_compare*/ |
---|
832 | 0, /*tp_repr*/ |
---|
833 | 0, /*tp_as_number*/ |
---|
834 | 0, /*tp_as_sequence*/ |
---|
835 | 0, /*tp_as_mapping*/ |
---|
836 | 0, /*tp_hash */ |
---|
837 | 0, /*tp_call*/ |
---|
838 | 0, /*tp_str*/ |
---|
839 | 0, /*tp_getattro*/ |
---|
840 | 0, /*tp_setattro*/ |
---|
841 | 0, /*tp_as_buffer*/ |
---|
842 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/ |
---|
843 | "CReflAdvModel objects", /* tp_doc */ |
---|
844 | 0, /* tp_traverse */ |
---|
845 | 0, /* tp_clear */ |
---|
846 | 0, /* tp_richcompare */ |
---|
847 | 0, /* tp_weaklistoffset */ |
---|
848 | 0, /* tp_iter */ |
---|
849 | 0, /* tp_iternext */ |
---|
850 | CReflAdvModel_methods, /* tp_methods */ |
---|
851 | CReflAdvModel_members, /* tp_members */ |
---|
852 | 0, /* tp_getset */ |
---|
853 | 0, /* tp_base */ |
---|
854 | 0, /* tp_dict */ |
---|
855 | 0, /* tp_descr_get */ |
---|
856 | 0, /* tp_descr_set */ |
---|
857 | 0, /* tp_dictoffset */ |
---|
858 | (initproc)CReflAdvModel_init, /* tp_init */ |
---|
859 | 0, /* tp_alloc */ |
---|
860 | CReflAdvModel_new, /* tp_new */ |
---|
861 | }; |
---|
862 | |
---|
863 | |
---|
864 | //static PyMethodDef module_methods[] = { |
---|
865 | // {NULL} |
---|
866 | //}; |
---|
867 | |
---|
868 | /** |
---|
869 | * Function used to add the model class to a module |
---|
870 | * @param module: module to add the class to |
---|
871 | */ |
---|
872 | void addCReflAdvModel(PyObject *module) { |
---|
873 | PyObject *d; |
---|
874 | |
---|
875 | if (PyType_Ready(&CReflAdvModelType) < 0) |
---|
876 | return; |
---|
877 | |
---|
878 | Py_INCREF(&CReflAdvModelType); |
---|
879 | PyModule_AddObject(module, "CReflAdvModel", (PyObject *)&CReflAdvModelType); |
---|
880 | |
---|
881 | d = PyModule_GetDict(module); |
---|
882 | static char error_name[] = "CReflAdvModel.error"; |
---|
883 | CReflAdvModelError = PyErr_NewException(error_name, NULL, NULL); |
---|
884 | PyDict_SetItemString(d, "CReflAdvModelError", CReflAdvModelError); |
---|
885 | } |
---|
886 | |
---|