Basilisk CFD
Adaptive Cartesian mesh PDE framework
Loading...
Searching...
No Matches
python.h
Go to the documentation of this file.
1/** @file python.h
2 */
6@else
9@endif
10
12 if (!PyCallable_Check(f)) {
13 fprintf (stderr, "parameter must be callable\n");
14 return -1;
15 }
16@if PY_MAJOR_VERSION >= 3
17 PyObject * code = PyObject_GetAttrString (f, "__code__");
18@else
19 PyObject * code = PyObject_GetAttrString (f, "func_code");
20@endif
21 PyObject * nargs = PyObject_GetAttrString (code, "co_argcount");
23 int n = PYINT (nargs);
25 char * format = (n == 0 ? "()" :
26 n == 1 ? "(d)" :
27 n == 2 ? "(dd)" :
28 n == 3 ? "(ddd)" :
29 "(error)");
30 double status = 0;
31 foreach(reduction(min:status))
32 if (!status) {
36 if (result == NULL)
37 status = -1;
38 else {
41 }
42 }
43 return status;
44}
45
46typedef struct {
47 PyObject * i, * t, * action;
48} PyEvent;
49
50static double get_double (PyObject * item) {
51 if (PyFloat_Check (item))
52 return PyFloat_AsDouble (item);
53 else if (PYINTCHECK (item))
54 return PYINT (item);
55 else {
56 fprintf (stderr, "expecting a float\n");
57 exit (1);
58 }
59 return 0;
60}
61
62static int py_start (int * i, double * t, Event * ev) {
63 PyEvent * p = ev->data;
64 if (p->i != Py_None) {
65 if (PYINTCHECK (p->i))
66 *i = PYINT (p->i);
67 else {
68 PyObject * item = PySequence_GetItem (p->i, (ev->a = 0));
69 if (!item || !PYINTCHECK(item)) {
70 fprintf (stderr, "expecting an integer\n");
71 exit (1);
72 }
73 *i = PYINT (item);
75 }
76 }
77 else {
78 if (PyFloat_Check (p->t))
79 *t = PyFloat_AsDouble (p->t);
80 else if (PYINTCHECK (p->t))
81 *t = PYINT (p->t);
82 else {
83 PyObject * item = PySequence_GetItem (p->t, (ev->a = 0));
84 if (!item) {
85 fprintf (stderr, "expecting a float\n");
86 exit (1);
87 }
88 *t = get_double (item);
90 }
91 }
92 return 0;
93}
94
95static int py_end (int * i, double * t, Event * ev) {
96 PyEvent * p = ev->data;
97 if (p->i != Py_None)
98 return ev->a < PySequence_Size (p->i);
99 else
100 return ev->a < PySequence_Size (p->t);
101}
102
103static int py_inc (int * i, double * t, Event * ev) {
104 PyEvent * p = ev->data;
105 ev->a++;
106 if (p->i != Py_None) {
107 if (ev->a < PySequence_Size (p->i)) {
108 PyObject * item = PySequence_GetItem (p->i, ev->a);
109 *i = PYINT (item);
110 Py_DECREF (item);
111 }
112 else
113 (*i)++;
114 }
115 else {
116 if (ev->a < PySequence_Size (p->t)) {
117 PyObject * item = PySequence_GetItem (p->t, ev->a);
118 *t = get_double (item);
119 Py_DECREF (item);
120 }
121 else
122 (*t)++;
123 }
124 return 0;
125}
126
127static int py_action (const int i, const double t, Event * ev) {
128 PyEvent * p = ev->data;
129 PyObject * arglist = Py_BuildValue ("(id)", i, t);
132 if (result == NULL)
133 return 0;
135 return 0;
136}
137
139 if (!PyCallable_Check(action)) {
140 fprintf (stderr, "parameter must be callable\n");
141 return -1;
142 }
143
144 Event ev;
145 ev.last = false;
146 ev.action = py_action;
147 ev.arrayi = NULL;
148 ev.arrayt = NULL;
149 PyObject * code = PyObject_GetAttrString (action, "__code__");
150 PyObject * file = PyObject_GetAttrString (code, "co_filename");
151@if PY_MAJOR_VERSION >= 3
152 PyObject * temp_bytes = PyUnicode_AsEncodedString(file, "UTF-8", "strict");
155@else
156 ev.file = strdup (PyString_AsString (file));
157@endif
158 Py_DECREF (file);
159 PyObject * lineno = PyObject_GetAttrString (code, "co_firstlineno");
160 ev.line = PYINT (lineno);
162@if PY_MAJOR_VERSION >= 3
163 PyObject * name = PyObject_GetAttrString (action, "__name__");
164 PyObject * temp_bytes2 = PyUnicode_AsEncodedString(name, "UTF-8", "strict");
167@else
168 PyObject * name = PyObject_GetAttrString (action, "func_name");
169 ev.name = strdup (PyString_AsString (name));
170@endif
171 Py_DECREF (name);
172 ev.a = 0;
173
174 if (i != Py_None && !PySequence_Check(i) && !PYINTCHECK(i)) {
175 fprintf (stderr, "parameter i must be a sequence or an int\n");
176 exit (1);
177 }
178 if (t != Py_None && !PySequence_Check(t) && !PyFloat_Check(t)) {
179 fprintf (stderr, "parameter t must be a sequence or a float\n");
180 exit (1);
181 }
182
183 if (PYINTCHECK(i) || PyFloat_Check(t)) {
184 ev.expr[0] = py_start;
185 ev.nexpr = 1;
186 }
187 else { // sequence
188 ev.expr[0] = py_end; ev.expr[1] = py_start; ev.expr[2] = py_inc;
189 ev.nexpr = 3;
190 }
191
192 // fixme: memory should be freed before exit
193 PyEvent * p = ev.data = malloc (sizeof (PyEvent));
194 p->i = i; Py_INCREF (i);
195 p->t = t; Py_INCREF (t);
196 p->action = action; Py_INCREF (action);
197
199 return 0;
200}
int min
Definition balance.h:192
int y
Definition common.h:76
#define define
int x
Definition common.h:76
scalar f[]
The primary fields are:
Definition two-phase.h:56
#define p
Definition tree.h:320
define sysmalloc malloc define syscalloc calloc define sysrealloc realloc define sysfree free define systrdup strdup define file
Definition config.h:120
define sysmalloc malloc define syscalloc calloc define sysrealloc realloc define sysfree free define systrdup strdup define line line line line strdup(s) @ define tracing(...) @ define end_tracing(...) @define tid() 0 @define pid() 0 @define npe() 1 @define mpi_all_reduce(v
else return n
Definition curvature.h:101
if(2.*fabs(alpha)< fabs(n.y))
We need to reconstruct the face fractions fs for the fine cells.
Definition embed-tree.h:102
scalar s
Definition embed-tree.h:56
scalar int i
Definition embed.h:74
double t
Definition events.h:24
void event_register(Event event)
Definition events.h:115
int code(int p, int l)
Definition linear.h:44
static int py_inc(int *i, double *t, Event *ev)
Definition python.h:103
static double get_double(PyObject *item)
Definition python.h:50
static int py_start(int *i, double *t, Event *ev)
Definition python.h:62
static int py_end(int *i, double *t, Event *ev)
Definition python.h:95
static int py_action(const int i, const double t, Event *ev)
Definition python.h:127
int py_register_event(PyObject *action, PyObject *i, PyObject *t)
Definition python.h:138
if PY_MAJOR_VERSION
Definition python.h:3
Definition events.h:6
int last
Definition events.h:7