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