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