1 | /** CHayterMSAStructure |
---|
2 | * |
---|
3 | * C extension |
---|
4 | * |
---|
5 | * WARNING: THIS FILE WAS GENERATED BY WRAPPERGENERATOR.PY |
---|
6 | * DO NOT MODIFY THIS FILE, MODIFY HayterMSA.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 "HayterMSA.h" |
---|
20 | |
---|
21 | /// Error object for raised exceptions |
---|
22 | static PyObject * CHayterMSAStructureError = 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 | HayterMSAParameters model_pars; |
---|
34 | } CHayterMSAStructure; |
---|
35 | |
---|
36 | |
---|
37 | static void |
---|
38 | CHayterMSAStructure_dealloc(CHayterMSAStructure* self) |
---|
39 | { |
---|
40 | self->ob_type->tp_free((PyObject*)self); |
---|
41 | |
---|
42 | |
---|
43 | } |
---|
44 | |
---|
45 | static PyObject * |
---|
46 | CHayterMSAStructure_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
---|
47 | { |
---|
48 | CHayterMSAStructure *self; |
---|
49 | |
---|
50 | self = (CHayterMSAStructure *)type->tp_alloc(type, 0); |
---|
51 | |
---|
52 | return (PyObject *)self; |
---|
53 | } |
---|
54 | |
---|
55 | static int |
---|
56 | CHayterMSAStructure_init(CHayterMSAStructure *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,"temperature",Py_BuildValue("d",318.160000)); |
---|
65 | PyDict_SetItemString(self->params,"saltconc",Py_BuildValue("d",0.000000)); |
---|
66 | PyDict_SetItemString(self->params,"dielectconst",Py_BuildValue("d",71.080000)); |
---|
67 | PyDict_SetItemString(self->params,"volfraction",Py_BuildValue("d",0.019200)); |
---|
68 | PyDict_SetItemString(self->params,"charge",Py_BuildValue("d",19.000000)); |
---|
69 | PyDict_SetItemString(self->params,"radius",Py_BuildValue("d",20.750000)); |
---|
70 | |
---|
71 | |
---|
72 | // Create empty log |
---|
73 | self->log = PyDict_New(); |
---|
74 | |
---|
75 | |
---|
76 | |
---|
77 | } |
---|
78 | return 0; |
---|
79 | } |
---|
80 | |
---|
81 | static PyMemberDef CHayterMSAStructure_members[] = { |
---|
82 | {"params", T_OBJECT, offsetof(CHayterMSAStructure, params), 0, |
---|
83 | "Parameters"}, |
---|
84 | {"log", T_OBJECT, offsetof(CHayterMSAStructure, log), 0, |
---|
85 | "Log"}, |
---|
86 | {NULL} /* Sentinel */ |
---|
87 | }; |
---|
88 | |
---|
89 | /** Read double from PyObject |
---|
90 | @param p PyObject |
---|
91 | @return double |
---|
92 | */ |
---|
93 | double CHayterMSAStructure_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(CHayterMSAStructure *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.temperature = PyFloat_AsDouble( PyDict_GetItemString(self->params, "temperature") ); |
---|
120 | self->model_pars.saltconc = PyFloat_AsDouble( PyDict_GetItemString(self->params, "saltconc") ); |
---|
121 | self->model_pars.dielectconst = PyFloat_AsDouble( PyDict_GetItemString(self->params, "dielectconst") ); |
---|
122 | self->model_pars.volfraction = PyFloat_AsDouble( PyDict_GetItemString(self->params, "volfraction") ); |
---|
123 | self->model_pars.charge = PyFloat_AsDouble( PyDict_GetItemString(self->params, "charge") ); |
---|
124 | self->model_pars.radius = PyFloat_AsDouble( PyDict_GetItemString(self->params, "radius") ); |
---|
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(CHayterMSAStructureError, |
---|
130 | "CHayterMSAStructure.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(CHayterMSAStructureError, |
---|
141 | "CHayterMSAStructure.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 = CHayterMSAStructure_readDouble(PyList_GET_ITEM(pars,0)); |
---|
147 | phi_value = CHayterMSAStructure_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",HayterMSA_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 = CHayterMSAStructure_readDouble(pars); |
---|
158 | |
---|
159 | return Py_BuildValue("d",HayterMSA_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(CHayterMSAStructure *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.temperature = PyFloat_AsDouble( PyDict_GetItemString(self->params, "temperature") ); |
---|
177 | self->model_pars.saltconc = PyFloat_AsDouble( PyDict_GetItemString(self->params, "saltconc") ); |
---|
178 | self->model_pars.dielectconst = PyFloat_AsDouble( PyDict_GetItemString(self->params, "dielectconst") ); |
---|
179 | self->model_pars.volfraction = PyFloat_AsDouble( PyDict_GetItemString(self->params, "volfraction") ); |
---|
180 | self->model_pars.charge = PyFloat_AsDouble( PyDict_GetItemString(self->params, "charge") ); |
---|
181 | self->model_pars.radius = PyFloat_AsDouble( PyDict_GetItemString(self->params, "radius") ); |
---|
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(CHayterMSAStructureError, |
---|
187 | "CHayterMSAStructure.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(CHayterMSAStructureError, |
---|
198 | "CHayterMSAStructure.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 = CHayterMSAStructure_readDouble(PyList_GET_ITEM(pars,0)); |
---|
204 | qy_value = CHayterMSAStructure_readDouble(PyList_GET_ITEM(pars,1)); |
---|
205 | return Py_BuildValue("d",HayterMSA_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 = CHayterMSAStructure_readDouble(pars); |
---|
211 | |
---|
212 | return Py_BuildValue("d",HayterMSA_analytical_1D(&(self->model_pars),qx_value)); |
---|
213 | } |
---|
214 | } |
---|
215 | |
---|
216 | static PyObject * reset(CHayterMSAStructure *self, PyObject *args) { |
---|
217 | |
---|
218 | |
---|
219 | return Py_BuildValue("d",0.0); |
---|
220 | } |
---|
221 | |
---|
222 | |
---|
223 | static PyMethodDef CHayterMSAStructure_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 CHayterMSAStructureType = { |
---|
236 | PyObject_HEAD_INIT(NULL) |
---|
237 | 0, /*ob_size*/ |
---|
238 | "CHayterMSAStructure", /*tp_name*/ |
---|
239 | sizeof(CHayterMSAStructure), /*tp_basicsize*/ |
---|
240 | 0, /*tp_itemsize*/ |
---|
241 | (destructor)CHayterMSAStructure_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 | "CHayterMSAStructure 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 | CHayterMSAStructure_methods, /* tp_methods */ |
---|
265 | CHayterMSAStructure_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)CHayterMSAStructure_init, /* tp_init */ |
---|
273 | 0, /* tp_alloc */ |
---|
274 | CHayterMSAStructure_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 addCHayterMSAStructure(PyObject *module) { |
---|
287 | PyObject *d; |
---|
288 | |
---|
289 | if (PyType_Ready(&CHayterMSAStructureType) < 0) |
---|
290 | return; |
---|
291 | |
---|
292 | Py_INCREF(&CHayterMSAStructureType); |
---|
293 | PyModule_AddObject(module, "CHayterMSAStructure", (PyObject *)&CHayterMSAStructureType); |
---|
294 | |
---|
295 | d = PyModule_GetDict(module); |
---|
296 | CHayterMSAStructureError = PyErr_NewException("CHayterMSAStructure.error", NULL, NULL); |
---|
297 | PyDict_SetItemString(d, "CHayterMSAStructureError", CHayterMSAStructureError); |
---|
298 | } |
---|
299 | |
---|