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