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