1 | /** CVesicleModel |
---|
2 | * |
---|
3 | * C extension |
---|
4 | * |
---|
5 | * WARNING: THIS FILE WAS GENERATED BY WRAPPERGENERATOR.PY |
---|
6 | * DO NOT MODIFY THIS FILE, MODIFY vesicle.h |
---|
7 | * AND RE-RUN THE GENERATOR SCRIPT |
---|
8 | * |
---|
9 | * @author M.Doucet / UTK |
---|
10 | */ |
---|
11 | |
---|
12 | #include <Python.h> |
---|
13 | #include "structmember.h" |
---|
14 | #include <stdio.h> |
---|
15 | #include <stdlib.h> |
---|
16 | #include <math.h> |
---|
17 | #include <time.h> |
---|
18 | |
---|
19 | #include "vesicle.h" |
---|
20 | |
---|
21 | /// Error object for raised exceptions |
---|
22 | static PyObject * CVesicleModelError = NULL; |
---|
23 | |
---|
24 | |
---|
25 | // Class definition |
---|
26 | typedef struct { |
---|
27 | PyObject_HEAD |
---|
28 | /// Parameters |
---|
29 | PyObject * params; |
---|
30 | /// Log for unit testing |
---|
31 | PyObject * log; |
---|
32 | /// Model parameters |
---|
33 | VesicleParameters model_pars; |
---|
34 | } CVesicleModel; |
---|
35 | |
---|
36 | |
---|
37 | static void |
---|
38 | CVesicleModel_dealloc(CVesicleModel* self) |
---|
39 | { |
---|
40 | self->ob_type->tp_free((PyObject*)self); |
---|
41 | |
---|
42 | |
---|
43 | } |
---|
44 | |
---|
45 | static PyObject * |
---|
46 | CVesicleModel_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
---|
47 | { |
---|
48 | CVesicleModel *self; |
---|
49 | |
---|
50 | self = (CVesicleModel *)type->tp_alloc(type, 0); |
---|
51 | |
---|
52 | return (PyObject *)self; |
---|
53 | } |
---|
54 | |
---|
55 | static int |
---|
56 | CVesicleModel_init(CVesicleModel *self, PyObject *args, PyObject *kwds) |
---|
57 | { |
---|
58 | if (self != NULL) { |
---|
59 | |
---|
60 | // Create parameters |
---|
61 | self->params = PyDict_New(); |
---|
62 | |
---|
63 | // Initialize parameter dictionary |
---|
64 | PyDict_SetItemString(self->params,"core_sld",Py_BuildValue("d",0.000006)); |
---|
65 | PyDict_SetItemString(self->params,"core_radius",Py_BuildValue("d",100.000000)); |
---|
66 | PyDict_SetItemString(self->params,"thickness",Py_BuildValue("d",30.000000)); |
---|
67 | PyDict_SetItemString(self->params,"scale",Py_BuildValue("d",1.000000)); |
---|
68 | PyDict_SetItemString(self->params,"background",Py_BuildValue("d",0.000000)); |
---|
69 | PyDict_SetItemString(self->params,"shell_sld",Py_BuildValue("d",0.000000)); |
---|
70 | |
---|
71 | |
---|
72 | // Create empty log |
---|
73 | self->log = PyDict_New(); |
---|
74 | |
---|
75 | |
---|
76 | |
---|
77 | } |
---|
78 | return 0; |
---|
79 | } |
---|
80 | |
---|
81 | static PyMemberDef CVesicleModel_members[] = { |
---|
82 | {"params", T_OBJECT, offsetof(CVesicleModel, params), 0, |
---|
83 | "Parameters"}, |
---|
84 | {"log", T_OBJECT, offsetof(CVesicleModel, log), 0, |
---|
85 | "Log"}, |
---|
86 | {NULL} /* Sentinel */ |
---|
87 | }; |
---|
88 | |
---|
89 | /** Read double from PyObject |
---|
90 | @param p PyObject |
---|
91 | @return double |
---|
92 | */ |
---|
93 | double CVesicleModel_readDouble(PyObject *p) { |
---|
94 | if (PyFloat_Check(p)==1) { |
---|
95 | return (double)(((PyFloatObject *)(p))->ob_fval); |
---|
96 | } else if (PyInt_Check(p)==1) { |
---|
97 | return (double)(((PyIntObject *)(p))->ob_ival); |
---|
98 | } else if (PyLong_Check(p)==1) { |
---|
99 | return (double)PyLong_AsLong(p); |
---|
100 | } else { |
---|
101 | return 0.0; |
---|
102 | } |
---|
103 | } |
---|
104 | |
---|
105 | |
---|
106 | /** |
---|
107 | * Function to call to evaluate model |
---|
108 | * @param args: input q or [q,phi] |
---|
109 | * @return: function value |
---|
110 | */ |
---|
111 | static PyObject * run(CVesicleModel *self, PyObject *args) { |
---|
112 | double q_value, phi_value; |
---|
113 | PyObject* pars; |
---|
114 | int npars; |
---|
115 | |
---|
116 | // Get parameters |
---|
117 | |
---|
118 | // Reader parameter dictionary |
---|
119 | self->model_pars.core_sld = PyFloat_AsDouble( PyDict_GetItemString(self->params, "core_sld") ); |
---|
120 | self->model_pars.core_radius = PyFloat_AsDouble( PyDict_GetItemString(self->params, "core_radius") ); |
---|
121 | self->model_pars.thickness = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thickness") ); |
---|
122 | self->model_pars.scale = PyFloat_AsDouble( PyDict_GetItemString(self->params, "scale") ); |
---|
123 | self->model_pars.background = PyFloat_AsDouble( PyDict_GetItemString(self->params, "background") ); |
---|
124 | self->model_pars.shell_sld = PyFloat_AsDouble( PyDict_GetItemString(self->params, "shell_sld") ); |
---|
125 | |
---|
126 | |
---|
127 | // Get input and determine whether we have to supply a 1D or 2D return value. |
---|
128 | if ( !PyArg_ParseTuple(args,"O",&pars) ) { |
---|
129 | PyErr_SetString(CVesicleModelError, |
---|
130 | "CVesicleModel.run expects a q value."); |
---|
131 | return NULL; |
---|
132 | } |
---|
133 | |
---|
134 | // Check params |
---|
135 | if( PyList_Check(pars)==1) { |
---|
136 | |
---|
137 | // Length of list should be 2 for I(q,phi) |
---|
138 | npars = PyList_GET_SIZE(pars); |
---|
139 | if(npars!=2) { |
---|
140 | PyErr_SetString(CVesicleModelError, |
---|
141 | "CVesicleModel.run expects a double or a list of dimension 2."); |
---|
142 | return NULL; |
---|
143 | } |
---|
144 | // We have a vector q, get the q and phi values at which |
---|
145 | // to evaluate I(q,phi) |
---|
146 | q_value = CVesicleModel_readDouble(PyList_GET_ITEM(pars,0)); |
---|
147 | phi_value = CVesicleModel_readDouble(PyList_GET_ITEM(pars,1)); |
---|
148 | // Skip zero |
---|
149 | if (q_value==0) { |
---|
150 | return Py_BuildValue("d",0.0); |
---|
151 | } |
---|
152 | return Py_BuildValue("d",vesicle_analytical_2D(&(self->model_pars),q_value,phi_value)); |
---|
153 | |
---|
154 | } else { |
---|
155 | |
---|
156 | // We have a scalar q, we will evaluate I(q) |
---|
157 | q_value = CVesicleModel_readDouble(pars); |
---|
158 | |
---|
159 | return Py_BuildValue("d",vesicle_analytical_1D(&(self->model_pars),q_value)); |
---|
160 | } |
---|
161 | } |
---|
162 | |
---|
163 | /** |
---|
164 | * Function to call to evaluate model in cartesian coordinates |
---|
165 | * @param args: input q or [qx, qy]] |
---|
166 | * @return: function value |
---|
167 | */ |
---|
168 | static PyObject * runXY(CVesicleModel *self, PyObject *args) { |
---|
169 | double qx_value, qy_value; |
---|
170 | PyObject* pars; |
---|
171 | int npars; |
---|
172 | |
---|
173 | // Get parameters |
---|
174 | |
---|
175 | // Reader parameter dictionary |
---|
176 | self->model_pars.core_sld = PyFloat_AsDouble( PyDict_GetItemString(self->params, "core_sld") ); |
---|
177 | self->model_pars.core_radius = PyFloat_AsDouble( PyDict_GetItemString(self->params, "core_radius") ); |
---|
178 | self->model_pars.thickness = PyFloat_AsDouble( PyDict_GetItemString(self->params, "thickness") ); |
---|
179 | self->model_pars.scale = PyFloat_AsDouble( PyDict_GetItemString(self->params, "scale") ); |
---|
180 | self->model_pars.background = PyFloat_AsDouble( PyDict_GetItemString(self->params, "background") ); |
---|
181 | self->model_pars.shell_sld = PyFloat_AsDouble( PyDict_GetItemString(self->params, "shell_sld") ); |
---|
182 | |
---|
183 | |
---|
184 | // Get input and determine whether we have to supply a 1D or 2D return value. |
---|
185 | if ( !PyArg_ParseTuple(args,"O",&pars) ) { |
---|
186 | PyErr_SetString(CVesicleModelError, |
---|
187 | "CVesicleModel.run expects a q value."); |
---|
188 | return NULL; |
---|
189 | } |
---|
190 | |
---|
191 | // Check params |
---|
192 | if( PyList_Check(pars)==1) { |
---|
193 | |
---|
194 | // Length of list should be 2 for I(qx, qy)) |
---|
195 | npars = PyList_GET_SIZE(pars); |
---|
196 | if(npars!=2) { |
---|
197 | PyErr_SetString(CVesicleModelError, |
---|
198 | "CVesicleModel.run expects a double or a list of dimension 2."); |
---|
199 | return NULL; |
---|
200 | } |
---|
201 | // We have a vector q, get the qx and qy values at which |
---|
202 | // to evaluate I(qx,qy) |
---|
203 | qx_value = CVesicleModel_readDouble(PyList_GET_ITEM(pars,0)); |
---|
204 | qy_value = CVesicleModel_readDouble(PyList_GET_ITEM(pars,1)); |
---|
205 | return Py_BuildValue("d",vesicle_analytical_2DXY(&(self->model_pars),qx_value,qy_value)); |
---|
206 | |
---|
207 | } else { |
---|
208 | |
---|
209 | // We have a scalar q, we will evaluate I(q) |
---|
210 | qx_value = CVesicleModel_readDouble(pars); |
---|
211 | |
---|
212 | return Py_BuildValue("d",vesicle_analytical_1D(&(self->model_pars),qx_value)); |
---|
213 | } |
---|
214 | } |
---|
215 | |
---|
216 | static PyObject * reset(CVesicleModel *self, PyObject *args) { |
---|
217 | |
---|
218 | |
---|
219 | return Py_BuildValue("d",0.0); |
---|
220 | } |
---|
221 | |
---|
222 | |
---|
223 | static PyMethodDef CVesicleModel_methods[] = { |
---|
224 | {"run", (PyCFunction)run , METH_VARARGS, |
---|
225 | "Evaluate the model at a given Q or Q, phi"}, |
---|
226 | {"runXY", (PyCFunction)runXY , METH_VARARGS, |
---|
227 | "Evaluate the model at a given Q or Qx, Qy"}, |
---|
228 | {"reset", (PyCFunction)reset , METH_VARARGS, |
---|
229 | "Reset pair correlation"}, |
---|
230 | //{"numerical_1D", (PyCFunction)numerical_1D , METH_VARARGS, |
---|
231 | // "Evaluate the 1D model at a given Q"}, |
---|
232 | {NULL} |
---|
233 | }; |
---|
234 | |
---|
235 | static PyTypeObject CVesicleModelType = { |
---|
236 | PyObject_HEAD_INIT(NULL) |
---|
237 | 0, /*ob_size*/ |
---|
238 | "CVesicleModel", /*tp_name*/ |
---|
239 | sizeof(CVesicleModel), /*tp_basicsize*/ |
---|
240 | 0, /*tp_itemsize*/ |
---|
241 | (destructor)CVesicleModel_dealloc, /*tp_dealloc*/ |
---|
242 | 0, /*tp_print*/ |
---|
243 | 0, /*tp_getattr*/ |
---|
244 | 0, /*tp_setattr*/ |
---|
245 | 0, /*tp_compare*/ |
---|
246 | 0, /*tp_repr*/ |
---|
247 | 0, /*tp_as_number*/ |
---|
248 | 0, /*tp_as_sequence*/ |
---|
249 | 0, /*tp_as_mapping*/ |
---|
250 | 0, /*tp_hash */ |
---|
251 | 0, /*tp_call*/ |
---|
252 | 0, /*tp_str*/ |
---|
253 | 0, /*tp_getattro*/ |
---|
254 | 0, /*tp_setattro*/ |
---|
255 | 0, /*tp_as_buffer*/ |
---|
256 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/ |
---|
257 | "CVesicleModel objects", /* tp_doc */ |
---|
258 | 0, /* tp_traverse */ |
---|
259 | 0, /* tp_clear */ |
---|
260 | 0, /* tp_richcompare */ |
---|
261 | 0, /* tp_weaklistoffset */ |
---|
262 | 0, /* tp_iter */ |
---|
263 | 0, /* tp_iternext */ |
---|
264 | CVesicleModel_methods, /* tp_methods */ |
---|
265 | CVesicleModel_members, /* tp_members */ |
---|
266 | 0, /* tp_getset */ |
---|
267 | 0, /* tp_base */ |
---|
268 | 0, /* tp_dict */ |
---|
269 | 0, /* tp_descr_get */ |
---|
270 | 0, /* tp_descr_set */ |
---|
271 | 0, /* tp_dictoffset */ |
---|
272 | (initproc)CVesicleModel_init, /* tp_init */ |
---|
273 | 0, /* tp_alloc */ |
---|
274 | CVesicleModel_new, /* tp_new */ |
---|
275 | }; |
---|
276 | |
---|
277 | |
---|
278 | static PyMethodDef module_methods[] = { |
---|
279 | {NULL} |
---|
280 | }; |
---|
281 | |
---|
282 | /** |
---|
283 | * Function used to add the model class to a module |
---|
284 | * @param module: module to add the class to |
---|
285 | */ |
---|
286 | void addCVesicleModel(PyObject *module) { |
---|
287 | PyObject *d; |
---|
288 | |
---|
289 | if (PyType_Ready(&CVesicleModelType) < 0) |
---|
290 | return; |
---|
291 | |
---|
292 | Py_INCREF(&CVesicleModelType); |
---|
293 | PyModule_AddObject(module, "CVesicleModel", (PyObject *)&CVesicleModelType); |
---|
294 | |
---|
295 | d = PyModule_GetDict(module); |
---|
296 | CVesicleModelError = PyErr_NewException("CVesicleModel.error", NULL, NULL); |
---|
297 | PyDict_SetItemString(d, "CVesicleModelError", CVesicleModelError); |
---|
298 | } |
---|
299 | |
---|