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