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