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