1 | |
---|
2 | #include <Python.h> |
---|
3 | #include "structmember.h" |
---|
4 | #include <stdio.h> |
---|
5 | #include <stdlib.h> |
---|
6 | #include <math.h> |
---|
7 | #include <time.h> |
---|
8 | |
---|
9 | #include "canvas.h" |
---|
10 | |
---|
11 | /// Error object for raised exceptions |
---|
12 | static PyObject * CCanvasError = NULL; |
---|
13 | |
---|
14 | |
---|
15 | // Class definition |
---|
16 | typedef struct { |
---|
17 | PyObject_HEAD |
---|
18 | /// Parameters |
---|
19 | PyObject * params; |
---|
20 | /// Model parameters |
---|
21 | CanvasParams canvas_pars; |
---|
22 | } CCanvas; |
---|
23 | |
---|
24 | |
---|
25 | static void |
---|
26 | CCanvas_dealloc(CCanvas* self) |
---|
27 | { |
---|
28 | canvas_dealloc(&(self->canvas_pars)); |
---|
29 | |
---|
30 | self->ob_type->tp_free((PyObject*)self); |
---|
31 | |
---|
32 | } |
---|
33 | |
---|
34 | static PyObject * |
---|
35 | CCanvas_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
---|
36 | { |
---|
37 | CCanvas *self; |
---|
38 | |
---|
39 | self = (CCanvas *)type->tp_alloc(type, 0); |
---|
40 | |
---|
41 | return (PyObject *)self; |
---|
42 | } |
---|
43 | |
---|
44 | static int |
---|
45 | CCanvas_init(CCanvas *self, PyObject *args, PyObject *kwds) |
---|
46 | { |
---|
47 | if (self != NULL) { |
---|
48 | srand(1001); |
---|
49 | |
---|
50 | // Create parameters |
---|
51 | self->params = PyDict_New(); |
---|
52 | canvas_init(&(self->canvas_pars)); |
---|
53 | } |
---|
54 | return 0; |
---|
55 | } |
---|
56 | |
---|
57 | static PyMemberDef CCanvas_members[] = { |
---|
58 | {"params", T_OBJECT, offsetof(CCanvas, params), 0, |
---|
59 | "Parameters"}, |
---|
60 | {NULL} /* Sentinel */ |
---|
61 | }; |
---|
62 | |
---|
63 | /** Read double from PyObject |
---|
64 | @param p PyObject |
---|
65 | @return double |
---|
66 | */ |
---|
67 | double CCanvas_readDouble(PyObject *p) { |
---|
68 | if (PyFloat_Check(p)==1) { |
---|
69 | return (double)(((PyFloatObject *)(p))->ob_fval); |
---|
70 | } else if (PyInt_Check(p)==1) { |
---|
71 | return (double)(((PyIntObject *)(p))->ob_ival); |
---|
72 | } else if (PyLong_Check(p)==1) { |
---|
73 | return (double)PyLong_AsLong(p); |
---|
74 | } else { |
---|
75 | return 0.0; |
---|
76 | } |
---|
77 | } |
---|
78 | |
---|
79 | |
---|
80 | /** |
---|
81 | * Function to call to evaluate model |
---|
82 | * @param args: input q or [q,phi] |
---|
83 | * @return: function value |
---|
84 | */ |
---|
85 | static PyObject * run(CCanvas *self, PyObject *args) { |
---|
86 | double q_value, phi_value; |
---|
87 | PyObject* pars; |
---|
88 | int npars; |
---|
89 | |
---|
90 | // Get input and determine whether we have to supply a 1D or 2D return value. |
---|
91 | if ( !PyArg_ParseTuple(args,"O",&pars) ) { |
---|
92 | PyErr_SetString(CCanvasError, |
---|
93 | "CCanvas.run expects a q value."); |
---|
94 | return NULL; |
---|
95 | } |
---|
96 | |
---|
97 | // Check params |
---|
98 | if( PyList_Check(pars)==1) { |
---|
99 | |
---|
100 | // Length of list should be 2 for I(q,phi) |
---|
101 | npars = PyList_GET_SIZE(pars); |
---|
102 | if(npars!=2) { |
---|
103 | PyErr_SetString(CCanvasError, |
---|
104 | "CCanvas.run expects a double or a list of dimension 2."); |
---|
105 | return NULL; |
---|
106 | } |
---|
107 | // We have a vector q, get the q and phi values at which |
---|
108 | // to evaluate I(q,phi) |
---|
109 | q_value = CCanvas_readDouble(PyList_GET_ITEM(pars,0)); |
---|
110 | phi_value = CCanvas_readDouble(PyList_GET_ITEM(pars,1)); |
---|
111 | // Skip zero |
---|
112 | if (q_value==0) { |
---|
113 | return Py_BuildValue("d",0.0); |
---|
114 | } |
---|
115 | return Py_BuildValue("d",canvas_intensity(&(self->canvas_pars),q_value,phi_value)); |
---|
116 | |
---|
117 | } else { |
---|
118 | |
---|
119 | // We have a scalar q, we will evaluate I(q) |
---|
120 | q_value = CCanvas_readDouble(pars); |
---|
121 | |
---|
122 | return Py_BuildValue("d",0.0); |
---|
123 | } |
---|
124 | } |
---|
125 | |
---|
126 | static PyObject * add(CCanvas *self, PyObject *args) { |
---|
127 | int id; |
---|
128 | |
---|
129 | id = canvas_add(&(self->canvas_pars),REALSPACE_SPHERE); |
---|
130 | return Py_BuildValue("i",id); |
---|
131 | } |
---|
132 | |
---|
133 | static PyObject * setParam(CCanvas *self, PyObject *args) { |
---|
134 | int id, par_id; |
---|
135 | double value; |
---|
136 | double radius; |
---|
137 | |
---|
138 | // Get input and determine whether we have to supply a 1D or 2D return value. |
---|
139 | if ( !PyArg_ParseTuple(args,"iid",&id, &par_id, &value) ) { |
---|
140 | PyErr_SetString(CCanvasError, |
---|
141 | "CCanvas.run expects a q value."); |
---|
142 | return NULL; |
---|
143 | } |
---|
144 | |
---|
145 | canvas_setParam(&(self->canvas_pars),id,par_id,value); |
---|
146 | |
---|
147 | return Py_BuildValue("i",id); |
---|
148 | } |
---|
149 | |
---|
150 | |
---|
151 | static PyMethodDef CCanvas_methods[] = { |
---|
152 | {"evaluate", (PyCFunction)run , METH_VARARGS, |
---|
153 | "Evaluate the model at a given Q"}, |
---|
154 | {"add", (PyCFunction)add , METH_VARARGS, "Add a shape object"}, |
---|
155 | {"setParam", (PyCFunction)setParam , METH_VARARGS, "Set a parameter"}, |
---|
156 | {NULL} |
---|
157 | }; |
---|
158 | |
---|
159 | static PyTypeObject CCanvasType = { |
---|
160 | PyObject_HEAD_INIT(NULL) |
---|
161 | 0, /*ob_size*/ |
---|
162 | "CCanvas", /*tp_name*/ |
---|
163 | sizeof(CCanvas), /*tp_basicsize*/ |
---|
164 | 0, /*tp_itemsize*/ |
---|
165 | (destructor)CCanvas_dealloc, /*tp_dealloc*/ |
---|
166 | 0, /*tp_print*/ |
---|
167 | 0, /*tp_getattr*/ |
---|
168 | 0, /*tp_setattr*/ |
---|
169 | 0, /*tp_compare*/ |
---|
170 | 0, /*tp_repr*/ |
---|
171 | 0, /*tp_as_number*/ |
---|
172 | 0, /*tp_as_sequence*/ |
---|
173 | 0, /*tp_as_mapping*/ |
---|
174 | 0, /*tp_hash */ |
---|
175 | 0, /*tp_call*/ |
---|
176 | 0, /*tp_str*/ |
---|
177 | 0, /*tp_getattro*/ |
---|
178 | 0, /*tp_setattro*/ |
---|
179 | 0, /*tp_as_buffer*/ |
---|
180 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/ |
---|
181 | "CCanvas objects", /* tp_doc */ |
---|
182 | 0, /* tp_traverse */ |
---|
183 | 0, /* tp_clear */ |
---|
184 | 0, /* tp_richcompare */ |
---|
185 | 0, /* tp_weaklistoffset */ |
---|
186 | 0, /* tp_iter */ |
---|
187 | 0, /* tp_iternext */ |
---|
188 | CCanvas_methods, /* tp_methods */ |
---|
189 | CCanvas_members, /* tp_members */ |
---|
190 | 0, /* tp_getset */ |
---|
191 | 0, /* tp_base */ |
---|
192 | 0, /* tp_dict */ |
---|
193 | 0, /* tp_descr_get */ |
---|
194 | 0, /* tp_descr_set */ |
---|
195 | 0, /* tp_dictoffset */ |
---|
196 | (initproc)CCanvas_init, /* tp_init */ |
---|
197 | 0, /* tp_alloc */ |
---|
198 | CCanvas_new, /* tp_new */ |
---|
199 | }; |
---|
200 | |
---|
201 | |
---|
202 | static PyMethodDef module_methods[] = { |
---|
203 | {NULL} |
---|
204 | }; |
---|
205 | |
---|
206 | /** |
---|
207 | * Function used to add the model class to a module |
---|
208 | * @param module: module to add the class to |
---|
209 | */ |
---|
210 | void addCCanvas(PyObject *module) { |
---|
211 | PyObject *d; |
---|
212 | |
---|
213 | if (PyType_Ready(&CCanvasType) < 0) |
---|
214 | return; |
---|
215 | |
---|
216 | Py_INCREF(&CCanvasType); |
---|
217 | PyModule_AddObject(module, "CCanvas", (PyObject *)&CCanvasType); |
---|
218 | |
---|
219 | d = PyModule_GetDict(module); |
---|
220 | CCanvasError = PyErr_NewException("CCanvas.error", NULL, NULL); |
---|
221 | PyDict_SetItemString(d, "CCanvasError", CCanvasError); |
---|
222 | } |
---|
223 | |
---|