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