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