1 | /** CTestSphere2 |
---|
2 | * |
---|
3 | * C extension |
---|
4 | * |
---|
5 | * WARNING: THIS FILE WAS GENERATED BY WRAPPERGENERATOR.PY |
---|
6 | * DO NOT MODIFY THIS FILE, MODIFY testsphere.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 "testsphere.h" |
---|
20 | |
---|
21 | static double UNDEFINED = 1000010010.1234567; |
---|
22 | |
---|
23 | /// Error object for raised exceptions |
---|
24 | static PyObject * CTestSphere2Error = 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 | TestSphereParameters model_pars; |
---|
36 | } CTestSphere2; |
---|
37 | |
---|
38 | |
---|
39 | static void |
---|
40 | CTestSphere2_dealloc(CTestSphere2* self) |
---|
41 | { |
---|
42 | self->ob_type->tp_free((PyObject*)self); |
---|
43 | modelcalculations_dealloc(&(self->model_pars.calcPars)); |
---|
44 | |
---|
45 | } |
---|
46 | |
---|
47 | static PyObject * |
---|
48 | CTestSphere2_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
---|
49 | { |
---|
50 | CTestSphere2 *self; |
---|
51 | |
---|
52 | self = (CTestSphere2 *)type->tp_alloc(type, 0); |
---|
53 | |
---|
54 | return (PyObject *)self; |
---|
55 | } |
---|
56 | |
---|
57 | static int |
---|
58 | CTestSphere2_init(CTestSphere2 *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,"scale",Py_BuildValue("d",1.000000)); |
---|
67 | PyDict_SetItemString(self->params,"radius",Py_BuildValue("d",20.000000)); |
---|
68 | PyDict_SetItemString(self->params,"qmax",Py_BuildValue("d",0.100000)); |
---|
69 | |
---|
70 | |
---|
71 | // Create empty log |
---|
72 | self->log = PyDict_New(); |
---|
73 | |
---|
74 | modelcalculations_init(&(self->model_pars.calcPars)); |
---|
75 | |
---|
76 | } |
---|
77 | return 0; |
---|
78 | } |
---|
79 | |
---|
80 | static PyMemberDef CTestSphere2_members[] = { |
---|
81 | {"params", T_OBJECT, offsetof(CTestSphere2, params), 0, |
---|
82 | "Parameters"}, |
---|
83 | {"log", T_OBJECT, offsetof(CTestSphere2, log), 0, |
---|
84 | "Log"}, |
---|
85 | {NULL} /* Sentinel */ |
---|
86 | }; |
---|
87 | |
---|
88 | /** Read double from PyObject |
---|
89 | @param p PyObject |
---|
90 | @return double |
---|
91 | */ |
---|
92 | double CTestSphere2_readDouble(PyObject *p) { |
---|
93 | if (PyFloat_Check(p)==1) { |
---|
94 | return (double)(((PyFloatObject *)(p))->ob_fval); |
---|
95 | } else if (PyInt_Check(p)==1) { |
---|
96 | return (double)(((PyIntObject *)(p))->ob_ival); |
---|
97 | } else if (PyLong_Check(p)==1) { |
---|
98 | return (double)PyLong_AsLong(p); |
---|
99 | } else { |
---|
100 | return 0.0; |
---|
101 | } |
---|
102 | } |
---|
103 | |
---|
104 | |
---|
105 | /** |
---|
106 | * Function to call to evaluate model |
---|
107 | * @param args: input q or [q,phi] |
---|
108 | * @return: function value |
---|
109 | */ |
---|
110 | static PyObject * run(CTestSphere2 *self, PyObject *args) { |
---|
111 | double q_value, phi_value; |
---|
112 | PyObject* pars; |
---|
113 | int npars; |
---|
114 | |
---|
115 | // Get parameters |
---|
116 | |
---|
117 | // Reader parameter dictionary |
---|
118 | self->model_pars.scale = PyFloat_AsDouble( PyDict_GetItemString(self->params, "scale") ); |
---|
119 | self->model_pars.radius = PyFloat_AsDouble( PyDict_GetItemString(self->params, "radius") ); |
---|
120 | self->model_pars.qmax = PyFloat_AsDouble( PyDict_GetItemString(self->params, "qmax") ); |
---|
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(CTestSphere2Error, |
---|
126 | "CTestSphere2.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(CTestSphere2Error, |
---|
137 | "CTestSphere2.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 = CTestSphere2_readDouble(PyList_GET_ITEM(pars,0)); |
---|
143 | phi_value = CTestSphere2_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",testsphere_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 = CTestSphere2_readDouble(pars); |
---|
154 | |
---|
155 | return Py_BuildValue("d",testsphere_analytical_1D(&(self->model_pars),q_value)); |
---|
156 | } |
---|
157 | } |
---|
158 | |
---|
159 | static PyObject * reset(CTestSphere2 *self, PyObject *args) { |
---|
160 | modelcalculations_reset(&(self->model_pars.calcPars)); |
---|
161 | |
---|
162 | return Py_BuildValue("d",0.0); |
---|
163 | } |
---|
164 | |
---|
165 | |
---|
166 | static PyMethodDef CTestSphere2_methods[] = { |
---|
167 | {"run", (PyCFunction)run , METH_VARARGS, |
---|
168 | "Evaluate the model at a given Q"}, |
---|
169 | {"reset", (PyCFunction)reset , METH_VARARGS, |
---|
170 | "Reset pair correlation"}, |
---|
171 | //{"numerical_1D", (PyCFunction)numerical_1D , METH_VARARGS, |
---|
172 | // "Evaluate the 1D model at a given Q"}, |
---|
173 | {NULL} |
---|
174 | }; |
---|
175 | |
---|
176 | static PyTypeObject CTestSphere2Type = { |
---|
177 | PyObject_HEAD_INIT(NULL) |
---|
178 | 0, /*ob_size*/ |
---|
179 | "CTestSphere2", /*tp_name*/ |
---|
180 | sizeof(CTestSphere2), /*tp_basicsize*/ |
---|
181 | 0, /*tp_itemsize*/ |
---|
182 | (destructor)CTestSphere2_dealloc, /*tp_dealloc*/ |
---|
183 | 0, /*tp_print*/ |
---|
184 | 0, /*tp_getattr*/ |
---|
185 | 0, /*tp_setattr*/ |
---|
186 | 0, /*tp_compare*/ |
---|
187 | 0, /*tp_repr*/ |
---|
188 | 0, /*tp_as_number*/ |
---|
189 | 0, /*tp_as_sequence*/ |
---|
190 | 0, /*tp_as_mapping*/ |
---|
191 | 0, /*tp_hash */ |
---|
192 | 0, /*tp_call*/ |
---|
193 | 0, /*tp_str*/ |
---|
194 | 0, /*tp_getattro*/ |
---|
195 | 0, /*tp_setattro*/ |
---|
196 | 0, /*tp_as_buffer*/ |
---|
197 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/ |
---|
198 | "CTestSphere2 objects", /* tp_doc */ |
---|
199 | 0, /* tp_traverse */ |
---|
200 | 0, /* tp_clear */ |
---|
201 | 0, /* tp_richcompare */ |
---|
202 | 0, /* tp_weaklistoffset */ |
---|
203 | 0, /* tp_iter */ |
---|
204 | 0, /* tp_iternext */ |
---|
205 | CTestSphere2_methods, /* tp_methods */ |
---|
206 | CTestSphere2_members, /* tp_members */ |
---|
207 | 0, /* tp_getset */ |
---|
208 | 0, /* tp_base */ |
---|
209 | 0, /* tp_dict */ |
---|
210 | 0, /* tp_descr_get */ |
---|
211 | 0, /* tp_descr_set */ |
---|
212 | 0, /* tp_dictoffset */ |
---|
213 | (initproc)CTestSphere2_init, /* tp_init */ |
---|
214 | 0, /* tp_alloc */ |
---|
215 | CTestSphere2_new, /* tp_new */ |
---|
216 | }; |
---|
217 | |
---|
218 | |
---|
219 | static PyMethodDef module_methods[] = { |
---|
220 | {NULL} |
---|
221 | }; |
---|
222 | |
---|
223 | /** |
---|
224 | * Function used to add the model class to a module |
---|
225 | * @param module: module to add the class to |
---|
226 | */ |
---|
227 | void addCTestSphere2(PyObject *module) { |
---|
228 | PyObject *d; |
---|
229 | |
---|
230 | if (PyType_Ready(&CTestSphere2Type) < 0) |
---|
231 | return; |
---|
232 | |
---|
233 | Py_INCREF(&CTestSphere2Type); |
---|
234 | PyModule_AddObject(module, "CTestSphere2", (PyObject *)&CTestSphere2Type); |
---|
235 | |
---|
236 | d = PyModule_GetDict(module); |
---|
237 | CTestSphere2Error = PyErr_NewException("CTestSphere2.error", NULL, NULL); |
---|
238 | PyDict_SetItemString(d, "CTestSphere2Error", CTestSphere2Error); |
---|
239 | } |
---|
240 | |
---|