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.000001000000)); |
---|
93 | PyDict_SetItemString(self->params,"thickness",Py_BuildValue("d",10.000000000000)); |
---|
94 | PyDict_SetItemString(self->params,"solvent_sld",Py_BuildValue("d",0.000003000000)); |
---|
95 | PyDict_SetItemString(self->params,"scale",Py_BuildValue("d",1.000000000000)); |
---|
96 | PyDict_SetItemString(self->params,"radius",Py_BuildValue("d",60.000000000000)); |
---|
97 | PyDict_SetItemString(self->params,"background",Py_BuildValue("d",0.000000000000)); |
---|
98 | PyDict_SetItemString(self->params,"shell_sld",Py_BuildValue("d",0.000002000000)); |
---|
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 char name_params[] = "params"; |
---|
121 | static char def_params[] = "Parameters"; |
---|
122 | static char name_dispersion[] = "dispersion"; |
---|
123 | static char def_dispersion[] = "Dispersion parameters"; |
---|
124 | static char name_log[] = "log"; |
---|
125 | static char def_log[] = "Log"; |
---|
126 | |
---|
127 | static PyMemberDef CCoreShellModel_members[] = { |
---|
128 | {name_params, T_OBJECT, offsetof(CCoreShellModel, params), 0, def_params}, |
---|
129 | {name_dispersion, T_OBJECT, offsetof(CCoreShellModel, dispersion), 0, def_dispersion}, |
---|
130 | {name_log, T_OBJECT, offsetof(CCoreShellModel, log), 0, def_log}, |
---|
131 | {NULL} /* Sentinel */ |
---|
132 | }; |
---|
133 | |
---|
134 | /** Read double from PyObject |
---|
135 | @param p PyObject |
---|
136 | @return double |
---|
137 | */ |
---|
138 | double CCoreShellModel_readDouble(PyObject *p) { |
---|
139 | if (PyFloat_Check(p)==1) { |
---|
140 | return (double)(((PyFloatObject *)(p))->ob_fval); |
---|
141 | } else if (PyInt_Check(p)==1) { |
---|
142 | return (double)(((PyIntObject *)(p))->ob_ival); |
---|
143 | } else if (PyLong_Check(p)==1) { |
---|
144 | return (double)PyLong_AsLong(p); |
---|
145 | } else { |
---|
146 | return 0.0; |
---|
147 | } |
---|
148 | } |
---|
149 | /** |
---|
150 | * Function to call to evaluate model |
---|
151 | * @param args: input numpy array q[] |
---|
152 | * @return: numpy array object |
---|
153 | */ |
---|
154 | |
---|
155 | static PyObject *evaluateOneDim(CoreShellModel* model, PyArrayObject *q){ |
---|
156 | PyArrayObject *result; |
---|
157 | |
---|
158 | // Check validity of array q , q must be of dimension 1, an array of double |
---|
159 | if (q->nd != 1 || q->descr->type_num != PyArray_DOUBLE) |
---|
160 | { |
---|
161 | //const char * message= "Invalid array: q->nd=%d,type_num=%d\n",q->nd,q->descr->type_num; |
---|
162 | //PyErr_SetString(PyExc_ValueError , message); |
---|
163 | return NULL; |
---|
164 | } |
---|
165 | result = (PyArrayObject *)PyArray_SimpleNew(q->nd, (npy_intp *)(q->dimensions), PyArray_DOUBLE); |
---|
166 | if (result == NULL) { |
---|
167 | const char * message= "Could not create result "; |
---|
168 | PyErr_SetString(PyExc_RuntimeError , message); |
---|
169 | return NULL; |
---|
170 | } |
---|
171 | #pragma omp parallel for |
---|
172 | for (int i = 0; i < q->dimensions[0]; i++){ |
---|
173 | double q_value = *(double *)(q->data + i*q->strides[0]); |
---|
174 | double *result_value = (double *)(result->data + i*result->strides[0]); |
---|
175 | *result_value =(*model)(q_value); |
---|
176 | } |
---|
177 | return PyArray_Return(result); |
---|
178 | } |
---|
179 | |
---|
180 | /** |
---|
181 | * Function to call to evaluate model |
---|
182 | * @param args: input numpy array [x[],y[]] |
---|
183 | * @return: numpy array object |
---|
184 | */ |
---|
185 | static PyObject * evaluateTwoDimXY( CoreShellModel* model, |
---|
186 | PyArrayObject *x, PyArrayObject *y) |
---|
187 | { |
---|
188 | PyArrayObject *result; |
---|
189 | int x_len, y_len, dims[1]; |
---|
190 | //check validity of input vectors |
---|
191 | if (x->nd != 1 || x->descr->type_num != PyArray_DOUBLE |
---|
192 | || y->nd != 1 || y->descr->type_num != PyArray_DOUBLE |
---|
193 | || y->dimensions[0] != x->dimensions[0]){ |
---|
194 | const char * message= "evaluateTwoDimXY expect 2 numpy arrays"; |
---|
195 | PyErr_SetString(PyExc_ValueError , message); |
---|
196 | return NULL; |
---|
197 | } |
---|
198 | |
---|
199 | if (PyArray_Check(x) && PyArray_Check(y)) { |
---|
200 | |
---|
201 | x_len = dims[0]= x->dimensions[0]; |
---|
202 | y_len = dims[0]= y->dimensions[0]; |
---|
203 | |
---|
204 | // Make a new double matrix of same dims |
---|
205 | result=(PyArrayObject *) PyArray_SimpleNew(1,(npy_intp *)dims,NPY_DOUBLE); |
---|
206 | if (result == NULL){ |
---|
207 | const char * message= "Could not create result "; |
---|
208 | PyErr_SetString(PyExc_RuntimeError , message); |
---|
209 | return NULL; |
---|
210 | } |
---|
211 | |
---|
212 | /* Do the calculation. */ |
---|
213 | #pragma omp parallel for |
---|
214 | for (int i=0; i< x_len; i++) { |
---|
215 | double x_value = *(double *)(x->data + i*x->strides[0]); |
---|
216 | double y_value = *(double *)(y->data + i*y->strides[0]); |
---|
217 | double *result_value = (double *)(result->data + |
---|
218 | i*result->strides[0]); |
---|
219 | *result_value = (*model)(x_value, y_value); |
---|
220 | } |
---|
221 | return PyArray_Return(result); |
---|
222 | |
---|
223 | }else{ |
---|
224 | PyErr_SetString(CCoreShellModelError, |
---|
225 | "CCoreShellModel.evaluateTwoDimXY couldn't run."); |
---|
226 | return NULL; |
---|
227 | } |
---|
228 | } |
---|
229 | /** |
---|
230 | * evalDistribution function evaluate a model function with input vector |
---|
231 | * @param args: input q as vector or [qx, qy] where qx, qy are vectors |
---|
232 | * |
---|
233 | */ |
---|
234 | static PyObject * evalDistribution(CCoreShellModel *self, PyObject *args){ |
---|
235 | PyObject *qx, *qy; |
---|
236 | PyArrayObject * pars; |
---|
237 | int npars ,mpars; |
---|
238 | |
---|
239 | // Get parameters |
---|
240 | |
---|
241 | // Reader parameter dictionary |
---|
242 | self->model->core_sld = PyFloat_AsDouble( PyDict_GetItemString(self->params, "core_sld") ); |
---|
243 | self->model->thickness = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thickness") ); |
---|
244 | self->model->solvent_sld = PyFloat_AsDouble( PyDict_GetItemString(self->params, "solvent_sld") ); |
---|
245 | self->model->scale = PyFloat_AsDouble( PyDict_GetItemString(self->params, "scale") ); |
---|
246 | self->model->radius = PyFloat_AsDouble( PyDict_GetItemString(self->params, "radius") ); |
---|
247 | self->model->background = PyFloat_AsDouble( PyDict_GetItemString(self->params, "background") ); |
---|
248 | self->model->shell_sld = PyFloat_AsDouble( PyDict_GetItemString(self->params, "shell_sld") ); |
---|
249 | // Read in dispersion parameters |
---|
250 | PyObject* disp_dict; |
---|
251 | DispersionVisitor* visitor = new DispersionVisitor(); |
---|
252 | disp_dict = PyDict_GetItemString(self->dispersion, "radius"); |
---|
253 | self->model->radius.dispersion->accept_as_destination(visitor, self->model->radius.dispersion, disp_dict); |
---|
254 | disp_dict = PyDict_GetItemString(self->dispersion, "thickness"); |
---|
255 | self->model->thickness.dispersion->accept_as_destination(visitor, self->model->thickness.dispersion, disp_dict); |
---|
256 | |
---|
257 | |
---|
258 | // Get input and determine whether we have to supply a 1D or 2D return value. |
---|
259 | if ( !PyArg_ParseTuple(args,"O",&pars) ) { |
---|
260 | PyErr_SetString(CCoreShellModelError, |
---|
261 | "CCoreShellModel.evalDistribution expects a q value."); |
---|
262 | return NULL; |
---|
263 | } |
---|
264 | // Check params |
---|
265 | |
---|
266 | if(PyArray_Check(pars)==1) { |
---|
267 | |
---|
268 | // Length of list should 1 or 2 |
---|
269 | npars = pars->nd; |
---|
270 | if(npars==1) { |
---|
271 | // input is a numpy array |
---|
272 | if (PyArray_Check(pars)) { |
---|
273 | return evaluateOneDim(self->model, (PyArrayObject*)pars); |
---|
274 | } |
---|
275 | }else{ |
---|
276 | PyErr_SetString(CCoreShellModelError, |
---|
277 | "CCoreShellModel.evalDistribution expect numpy array of one dimension."); |
---|
278 | return NULL; |
---|
279 | } |
---|
280 | }else if( PyList_Check(pars)==1) { |
---|
281 | // Length of list should be 2 for I(qx,qy) |
---|
282 | mpars = PyList_GET_SIZE(pars); |
---|
283 | if(mpars!=2) { |
---|
284 | PyErr_SetString(CCoreShellModelError, |
---|
285 | "CCoreShellModel.evalDistribution expects a list of dimension 2."); |
---|
286 | return NULL; |
---|
287 | } |
---|
288 | qx = PyList_GET_ITEM(pars,0); |
---|
289 | qy = PyList_GET_ITEM(pars,1); |
---|
290 | if (PyArray_Check(qx) && PyArray_Check(qy)) { |
---|
291 | return evaluateTwoDimXY(self->model, (PyArrayObject*)qx, |
---|
292 | (PyArrayObject*)qy); |
---|
293 | }else{ |
---|
294 | PyErr_SetString(CCoreShellModelError, |
---|
295 | "CCoreShellModel.evalDistribution expect 2 numpy arrays in list."); |
---|
296 | return NULL; |
---|
297 | } |
---|
298 | } |
---|
299 | PyErr_SetString(CCoreShellModelError, |
---|
300 | "CCoreShellModel.evalDistribution couln't be run."); |
---|
301 | return NULL; |
---|
302 | |
---|
303 | } |
---|
304 | |
---|
305 | /** |
---|
306 | * Function to call to evaluate model |
---|
307 | * @param args: input q or [q,phi] |
---|
308 | * @return: function value |
---|
309 | */ |
---|
310 | static PyObject * run(CCoreShellModel *self, PyObject *args) { |
---|
311 | double q_value, phi_value; |
---|
312 | PyObject* pars; |
---|
313 | int npars; |
---|
314 | |
---|
315 | // Get parameters |
---|
316 | |
---|
317 | // Reader parameter dictionary |
---|
318 | self->model->core_sld = PyFloat_AsDouble( PyDict_GetItemString(self->params, "core_sld") ); |
---|
319 | self->model->thickness = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thickness") ); |
---|
320 | self->model->solvent_sld = PyFloat_AsDouble( PyDict_GetItemString(self->params, "solvent_sld") ); |
---|
321 | self->model->scale = PyFloat_AsDouble( PyDict_GetItemString(self->params, "scale") ); |
---|
322 | self->model->radius = PyFloat_AsDouble( PyDict_GetItemString(self->params, "radius") ); |
---|
323 | self->model->background = PyFloat_AsDouble( PyDict_GetItemString(self->params, "background") ); |
---|
324 | self->model->shell_sld = PyFloat_AsDouble( PyDict_GetItemString(self->params, "shell_sld") ); |
---|
325 | // Read in dispersion parameters |
---|
326 | PyObject* disp_dict; |
---|
327 | DispersionVisitor* visitor = new DispersionVisitor(); |
---|
328 | disp_dict = PyDict_GetItemString(self->dispersion, "radius"); |
---|
329 | self->model->radius.dispersion->accept_as_destination(visitor, self->model->radius.dispersion, disp_dict); |
---|
330 | disp_dict = PyDict_GetItemString(self->dispersion, "thickness"); |
---|
331 | self->model->thickness.dispersion->accept_as_destination(visitor, self->model->thickness.dispersion, disp_dict); |
---|
332 | |
---|
333 | |
---|
334 | // Get input and determine whether we have to supply a 1D or 2D return value. |
---|
335 | if ( !PyArg_ParseTuple(args,"O",&pars) ) { |
---|
336 | PyErr_SetString(CCoreShellModelError, |
---|
337 | "CCoreShellModel.run expects a q value."); |
---|
338 | return NULL; |
---|
339 | } |
---|
340 | |
---|
341 | // Check params |
---|
342 | if( PyList_Check(pars)==1) { |
---|
343 | |
---|
344 | // Length of list should be 2 for I(q,phi) |
---|
345 | npars = PyList_GET_SIZE(pars); |
---|
346 | if(npars!=2) { |
---|
347 | PyErr_SetString(CCoreShellModelError, |
---|
348 | "CCoreShellModel.run expects a double or a list of dimension 2."); |
---|
349 | return NULL; |
---|
350 | } |
---|
351 | // We have a vector q, get the q and phi values at which |
---|
352 | // to evaluate I(q,phi) |
---|
353 | q_value = CCoreShellModel_readDouble(PyList_GET_ITEM(pars,0)); |
---|
354 | phi_value = CCoreShellModel_readDouble(PyList_GET_ITEM(pars,1)); |
---|
355 | // Skip zero |
---|
356 | if (q_value==0) { |
---|
357 | return Py_BuildValue("d",0.0); |
---|
358 | } |
---|
359 | return Py_BuildValue("d",(*(self->model)).evaluate_rphi(q_value,phi_value)); |
---|
360 | |
---|
361 | } else { |
---|
362 | |
---|
363 | // We have a scalar q, we will evaluate I(q) |
---|
364 | q_value = CCoreShellModel_readDouble(pars); |
---|
365 | |
---|
366 | return Py_BuildValue("d",(*(self->model))(q_value)); |
---|
367 | } |
---|
368 | } |
---|
369 | /** |
---|
370 | * Function to call to calculate_ER |
---|
371 | * @return: effective radius value |
---|
372 | */ |
---|
373 | static PyObject * calculate_ER(CCoreShellModel *self) { |
---|
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 | static char error_name[] = "CCoreShellModel.error"; |
---|
576 | CCoreShellModelError = PyErr_NewException(error_name, NULL, NULL); |
---|
577 | PyDict_SetItemString(d, "CCoreShellModelError", CCoreShellModelError); |
---|
578 | } |
---|
579 | |
---|