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