1 | /** CSimCylinder |
---|
2 | * |
---|
3 | * C extension |
---|
4 | * |
---|
5 | * WARNING: THIS FILE WAS GENERATED BY WRAPPERGENERATOR.PY |
---|
6 | * DO NOT MODIFY THIS FILE, MODIFY simcylinder.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 "simcylinder.h" |
---|
20 | |
---|
21 | static double UNDEFINED = 1000010010.1234567; |
---|
22 | |
---|
23 | /// Error object for raised exceptions |
---|
24 | static PyObject * CSimCylinderError = NULL; |
---|
25 | |
---|
26 | |
---|
27 | // Class definition |
---|
28 | typedef struct { |
---|
29 | PyObject_HEAD |
---|
30 | /// Parameters |
---|
31 | PyObject * params; |
---|
32 | /// Log for unit testing |
---|
33 | PyObject * log; |
---|
34 | /// Model parameters |
---|
35 | SimCylinderParameters model_pars; |
---|
36 | } CSimCylinder; |
---|
37 | |
---|
38 | |
---|
39 | static void |
---|
40 | CSimCylinder_dealloc(CSimCylinder* self) |
---|
41 | { |
---|
42 | modelcalculations_dealloc(&(self->model_pars.calcPars)); |
---|
43 | self->ob_type->tp_free((PyObject*)self); |
---|
44 | |
---|
45 | } |
---|
46 | |
---|
47 | static PyObject * |
---|
48 | CSimCylinder_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
---|
49 | { |
---|
50 | CSimCylinder *self; |
---|
51 | |
---|
52 | self = (CSimCylinder *)type->tp_alloc(type, 0); |
---|
53 | |
---|
54 | return (PyObject *)self; |
---|
55 | } |
---|
56 | |
---|
57 | static int |
---|
58 | CSimCylinder_init(CSimCylinder *self, PyObject *args, PyObject *kwds) |
---|
59 | { |
---|
60 | if (self != NULL) { |
---|
61 | |
---|
62 | // Create parameters |
---|
63 | self->params = PyDict_New(); |
---|
64 | |
---|
65 | // Initialize parameter dictionary |
---|
66 | PyDict_SetItemString(self->params,"phi",Py_BuildValue("d",0.000000)); |
---|
67 | PyDict_SetItemString(self->params,"scale",Py_BuildValue("d",1.000000)); |
---|
68 | PyDict_SetItemString(self->params,"qmax",Py_BuildValue("d",0.200000)); |
---|
69 | PyDict_SetItemString(self->params,"length",Py_BuildValue("d",200.000000)); |
---|
70 | PyDict_SetItemString(self->params,"radius",Py_BuildValue("d",80.000000)); |
---|
71 | PyDict_SetItemString(self->params,"theta",Py_BuildValue("d",1.570000)); |
---|
72 | |
---|
73 | |
---|
74 | // Create empty log |
---|
75 | self->log = PyDict_New(); |
---|
76 | |
---|
77 | modelcalculations_init(&(self->model_pars.calcPars)); |
---|
78 | |
---|
79 | } |
---|
80 | return 0; |
---|
81 | } |
---|
82 | |
---|
83 | static PyMemberDef CSimCylinder_members[] = { |
---|
84 | {"params", T_OBJECT, offsetof(CSimCylinder, params), 0, |
---|
85 | "Parameters"}, |
---|
86 | {"log", T_OBJECT, offsetof(CSimCylinder, log), 0, |
---|
87 | "Log"}, |
---|
88 | {NULL} /* Sentinel */ |
---|
89 | }; |
---|
90 | |
---|
91 | /** Read double from PyObject |
---|
92 | @param p PyObject |
---|
93 | @return double |
---|
94 | */ |
---|
95 | double CSimCylinder_readDouble(PyObject *p) { |
---|
96 | if (PyFloat_Check(p)==1) { |
---|
97 | return (double)(((PyFloatObject *)(p))->ob_fval); |
---|
98 | } else if (PyInt_Check(p)==1) { |
---|
99 | return (double)(((PyIntObject *)(p))->ob_ival); |
---|
100 | } else if (PyLong_Check(p)==1) { |
---|
101 | return (double)PyLong_AsLong(p); |
---|
102 | } else { |
---|
103 | return 0.0; |
---|
104 | } |
---|
105 | } |
---|
106 | |
---|
107 | |
---|
108 | /** |
---|
109 | * Function to call to evaluate model |
---|
110 | * @param args: input q or [q,phi] |
---|
111 | * @return: function value |
---|
112 | */ |
---|
113 | static PyObject * run(CSimCylinder *self, PyObject *args) { |
---|
114 | double q_value, phi_value; |
---|
115 | PyObject* pars; |
---|
116 | int npars; |
---|
117 | |
---|
118 | // Get parameters |
---|
119 | |
---|
120 | // Reader parameter dictionary |
---|
121 | self->model_pars.phi = PyFloat_AsDouble( PyDict_GetItemString(self->params, "phi") ); |
---|
122 | self->model_pars.scale = PyFloat_AsDouble( PyDict_GetItemString(self->params, "scale") ); |
---|
123 | self->model_pars.qmax = PyFloat_AsDouble( PyDict_GetItemString(self->params, "qmax") ); |
---|
124 | self->model_pars.length = PyFloat_AsDouble( PyDict_GetItemString(self->params, "length") ); |
---|
125 | self->model_pars.radius = PyFloat_AsDouble( PyDict_GetItemString(self->params, "radius") ); |
---|
126 | self->model_pars.theta = PyFloat_AsDouble( PyDict_GetItemString(self->params, "theta") ); |
---|
127 | |
---|
128 | |
---|
129 | // Get input and determine whether we have to supply a 1D or 2D return value. |
---|
130 | if ( !PyArg_ParseTuple(args,"O",&pars) ) { |
---|
131 | PyErr_SetString(CSimCylinderError, |
---|
132 | "CSimCylinder.run expects a q value."); |
---|
133 | return NULL; |
---|
134 | } |
---|
135 | |
---|
136 | // Check params |
---|
137 | if( PyList_Check(pars)==1) { |
---|
138 | |
---|
139 | // Length of list should be 2 for I(q,phi) |
---|
140 | npars = PyList_GET_SIZE(pars); |
---|
141 | if(npars!=2) { |
---|
142 | PyErr_SetString(CSimCylinderError, |
---|
143 | "CSimCylinder.run expects a double or a list of dimension 2."); |
---|
144 | return NULL; |
---|
145 | } |
---|
146 | // We have a vector q, get the q and phi values at which |
---|
147 | // to evaluate I(q,phi) |
---|
148 | q_value = CSimCylinder_readDouble(PyList_GET_ITEM(pars,0)); |
---|
149 | phi_value = CSimCylinder_readDouble(PyList_GET_ITEM(pars,1)); |
---|
150 | // Skip zero |
---|
151 | if (q_value==0) { |
---|
152 | return Py_BuildValue("d",0.0); |
---|
153 | } |
---|
154 | return Py_BuildValue("d",simcylinder_analytical_2D(&(self->model_pars),q_value,phi_value)); |
---|
155 | |
---|
156 | } else { |
---|
157 | |
---|
158 | // We have a scalar q, we will evaluate I(q) |
---|
159 | q_value = CSimCylinder_readDouble(pars); |
---|
160 | |
---|
161 | return Py_BuildValue("d",simcylinder_analytical_1D(&(self->model_pars),q_value)); |
---|
162 | } |
---|
163 | } |
---|
164 | |
---|
165 | static PyObject * reset(CSimCylinder *self, PyObject *args) { |
---|
166 | modelcalculations_reset(&(self->model_pars.calcPars)); |
---|
167 | |
---|
168 | return Py_BuildValue("d",0.0); |
---|
169 | } |
---|
170 | |
---|
171 | |
---|
172 | static PyMethodDef CSimCylinder_methods[] = { |
---|
173 | {"run", (PyCFunction)run , METH_VARARGS, |
---|
174 | "Evaluate the model at a given Q"}, |
---|
175 | {"reset", (PyCFunction)reset , METH_VARARGS, |
---|
176 | "Reset pair correlation"}, |
---|
177 | //{"numerical_1D", (PyCFunction)numerical_1D , METH_VARARGS, |
---|
178 | // "Evaluate the 1D model at a given Q"}, |
---|
179 | {NULL} |
---|
180 | }; |
---|
181 | |
---|
182 | static PyTypeObject CSimCylinderType = { |
---|
183 | PyObject_HEAD_INIT(NULL) |
---|
184 | 0, /*ob_size*/ |
---|
185 | "CSimCylinder", /*tp_name*/ |
---|
186 | sizeof(CSimCylinder), /*tp_basicsize*/ |
---|
187 | 0, /*tp_itemsize*/ |
---|
188 | (destructor)CSimCylinder_dealloc, /*tp_dealloc*/ |
---|
189 | 0, /*tp_print*/ |
---|
190 | 0, /*tp_getattr*/ |
---|
191 | 0, /*tp_setattr*/ |
---|
192 | 0, /*tp_compare*/ |
---|
193 | 0, /*tp_repr*/ |
---|
194 | 0, /*tp_as_number*/ |
---|
195 | 0, /*tp_as_sequence*/ |
---|
196 | 0, /*tp_as_mapping*/ |
---|
197 | 0, /*tp_hash */ |
---|
198 | 0, /*tp_call*/ |
---|
199 | 0, /*tp_str*/ |
---|
200 | 0, /*tp_getattro*/ |
---|
201 | 0, /*tp_setattro*/ |
---|
202 | 0, /*tp_as_buffer*/ |
---|
203 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/ |
---|
204 | "CSimCylinder objects", /* tp_doc */ |
---|
205 | 0, /* tp_traverse */ |
---|
206 | 0, /* tp_clear */ |
---|
207 | 0, /* tp_richcompare */ |
---|
208 | 0, /* tp_weaklistoffset */ |
---|
209 | 0, /* tp_iter */ |
---|
210 | 0, /* tp_iternext */ |
---|
211 | CSimCylinder_methods, /* tp_methods */ |
---|
212 | CSimCylinder_members, /* tp_members */ |
---|
213 | 0, /* tp_getset */ |
---|
214 | 0, /* tp_base */ |
---|
215 | 0, /* tp_dict */ |
---|
216 | 0, /* tp_descr_get */ |
---|
217 | 0, /* tp_descr_set */ |
---|
218 | 0, /* tp_dictoffset */ |
---|
219 | (initproc)CSimCylinder_init, /* tp_init */ |
---|
220 | 0, /* tp_alloc */ |
---|
221 | CSimCylinder_new, /* tp_new */ |
---|
222 | }; |
---|
223 | |
---|
224 | |
---|
225 | static PyMethodDef module_methods[] = { |
---|
226 | {NULL} |
---|
227 | }; |
---|
228 | |
---|
229 | /** |
---|
230 | * Function used to add the model class to a module |
---|
231 | * @param module: module to add the class to |
---|
232 | */ |
---|
233 | void addCSimCylinder(PyObject *module) { |
---|
234 | PyObject *d; |
---|
235 | |
---|
236 | if (PyType_Ready(&CSimCylinderType) < 0) |
---|
237 | return; |
---|
238 | |
---|
239 | Py_INCREF(&CSimCylinderType); |
---|
240 | PyModule_AddObject(module, "CSimCylinder", (PyObject *)&CSimCylinderType); |
---|
241 | |
---|
242 | d = PyModule_GetDict(module); |
---|
243 | CSimCylinderError = PyErr_NewException("CSimCylinder.error", NULL, NULL); |
---|
244 | PyDict_SetItemString(d, "CSimCylinderError", CSimCylinderError); |
---|
245 | } |
---|
246 | |
---|