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