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