Basilisk CFD
Adaptive Cartesian mesh PDE framework
Loading...
Searching...
No Matches
cartesian.h
Go to the documentation of this file.
1/** @file cartesian.h
2 */
3#if SINGLE_PRECISION
4typedef float real;
5#else
6typedef double real;
7#endif
8
9#ifndef GRIDNAME
10# define GRIDNAME "Cartesian"
11#endif
12#define dimension 2
13#define GHOSTS 1
14
15#define _I (point.i - 1)
16#define _J (point.j - 1)
17#define _DELTA (1./(real)N)
18
19typedef struct {
21 char * d;
22 int n;
23} Cartesian;
24
25struct _Point {
26 int i, j, level, n;
27#if LAYERS
28 int l;
30#else
32#endif
33};
35
36#define cartesian ((Cartesian *)grid)
37
39@define val(a,k,l,m) (((real *)cartesian->d)[(point.i + k + _index(a,m)*(size_t)(point.n + 2))*(point.n + 2) + point.j + l])
40@define allocated(...) true
41
43
44macro2 foreach (char flags = 0, Reduce reductions = None)
45{
47 int ig = 0, jg = 0; NOT_UNUSED(ig); NOT_UNUSED(jg);
48 Point point = {0};
49 point.n = cartesian->n;
50 int _k;
51 OMP(omp for schedule(static))
52 for (_k = 1; _k <= point.n; _k++) {
53 point.i = _k;
54 for (point.j = 1; point.j <= point.n; point.j++)
55 {...}
56 }
57 }
58}
59
61 const char * order = "xyz")
62{
64 int ig = 0, jg = 0; NOT_UNUSED(ig); NOT_UNUSED(jg);
65 Point point = {0};
66 point.n = cartesian->n;
67 int _k;
68 OMP(omp for schedule(static))
69 for (_k = 1; _k <= point.n + 1; _k++) {
70 point.i = _k;
71 for (point.j = 1; point.j <= point.n + 1; point.j++)
72 {...}
73 }
74 }
75}
76
77#define foreach_edge() for (int _i = 0; _i < _N; _i++) /* foreach_face */
78
80 if (p.j <= p.n) {
81 int ig = -1; NOT_UNUSED(ig);
82 {...}
83 }
84}
85
87 if (p.i <= p.n) {
88 int jg = -1; NOT_UNUSED(jg);
89 {...}
90 }
91}
92
93@if TRASH
96@endif
97
98#include "neighbors.h"
99
100void reset (void * alist, double val)
101{
102 scalar * list = (scalar *) alist;
103 size_t len = sq(cartesian->n + 2);
104 for (int _s = 0; _s < 1; _s++) /* scalar in list */
105 if (!is_constant(s))
106 for (size_t i = 0; i < len; i++)
107 ((real *)cartesian->d)[i + s.i*len] = val;
108}
109
110// Boundaries
111
113{
114 OMP_PARALLEL() {
115 int ig = 0, jg = 0, kg = 0; NOT_UNUSED(ig); NOT_UNUSED(jg); NOT_UNUSED(kg);
116 Point point = {0};
117 point.n = cartesian->n;
118 int * _i = &point.j;
119 if (d == left) {
120 point.i = GHOSTS;
121 ig = -1;
122 }
123 else if (d == right) {
124 point.i = point.n + GHOSTS - 1;
125 ig = 1;
126 }
127 else if (d == bottom) {
128 point.j = GHOSTS;
129 _i = &point.i;
130 jg = -1;
131 }
132 else if (d == top) {
133 point.j = point.n + GHOSTS - 1;
134 _i = &point.i;
135 jg = 1;
136 }
137 int _l;
138 OMP(omp for schedule(static))
139 for (_l = 0; _l < point.n + 2*GHOSTS; _l++) {
140 *_i = _l;
141 {...}
142 }
143 }
144}
145
148 point.j < GHOSTS || point.j >= point.n + GHOSTS)
149@
150
151// ghost cell coordinates for each direction
152static int _ig[] = {1,-1,0,0}, _jg[] = {0,0,1,-1};
153
154static void box_boundary_level_normal (const Boundary * b, scalar * list, int l)
155{
156 int d = ((BoxBoundary *)b)->d;
157
158 OMP_PARALLEL() {
159 Point point = {0};
160 int ig, jg;
161 point.n = cartesian->n;
162 if (d % 2)
163 ig = jg = 0;
164 else {
165 ig = _ig[d]; jg = _jg[d];
166 }
167 int _start = GHOSTS, _end = point.n + GHOSTS, _k;
168 OMP(omp for schedule(static))
169 for (_k = _start; _k < _end; _k++) {
170 point.i = d > left ? _k : d == right ? point.n + GHOSTS - 1 : GHOSTS;
171 point.j = d < top ? _k : d == top ? point.n + GHOSTS - 1 : GHOSTS;
172 Point neighbor = {point.i + ig, point.j + jg};
173 for (int _s = 0; _s < 1; _s++) /* scalar in list */ {
174 scalar b = s.v.x;
175 val(s,ig,jg) = b.boundary[d] (point, neighbor, s, NULL);
176 }
177 }
178 }
179}
180
182 scalar * list, int l)
183{
184 int d = ((BoxBoundary *)b)->d;
185
186 OMP_PARALLEL() {
187 Point point = {0};
188 point.n = cartesian->n;
189 int ig = _ig[d], jg = _jg[d];
190 int _start = GHOSTS, _end = point.n + 2*GHOSTS, _k;
191
192 OMP(omp for schedule(static))
193 for (_k = _start; _k < _end; _k++) {
194 point.i = d > left ? _k : d == right ? point.n + GHOSTS - 1 : GHOSTS;
195 point.j = d < top ? _k : d == top ? point.n + GHOSTS - 1 : GHOSTS;
196 Point neighbor = {point.i + ig, point.j + jg};
197 for (int _s = 0; _s < 1; _s++) /* scalar in list */ {
198 scalar b = s.v.y;
199 val(s,ig,jg) = b.boundary[d] (point, neighbor, s, NULL);
200 }
201 }
202 }
203}
204
205extern double (* default_scalar_bc[]) (Point, Point, scalar, bool *);
206static double periodic_bc (Point point, Point neighbor, scalar s, bool * data);
207
208macro2 for (int _b = 0; _b < 1; _b++) /* boundary int b */
209{
212 if (!is_boundary(point))
213 {...}
214}
215
216static void box_boundary_level (const Boundary * b, scalar * list, int l)
217{
218 int d = ((BoxBoundary *)b)->d;
219 scalar * centered = NULL, * normal = NULL, * tangent = NULL;
220
221 int component = d/2;
222 for (int _s = 0; _s < 1; _s++) /* scalar in list */
223 if (!is_constant(s)) {
224 if (s.face) {
225 if ((&s.d.x)[component]) {
226 scalar b = s.v.x;
227 if (b.boundary[d] && b.boundary[d] != periodic_bc)
228 normal = list_add (normal, s);
229 }
230 else {
231 scalar b = s.v.y;
232 if (b.boundary[d] && b.boundary[d] != periodic_bc)
234 }
235 }
236 else if (s.boundary[d] && s.boundary[d] != periodic_bc)
238 }
239
240 OMP_PARALLEL() {
241 Point point = {0};
242 point.n = cartesian->n;
243 int ig = _ig[d], jg = _jg[d];
244 int _start = 1, _end = point.n, _k;
245 /* traverse corners only for top and bottom */
246 if (d > left) { _start--; _end++; }
247 OMP(omp for schedule(static))
248 for (_k = _start; _k <= _end; _k++) {
249 point.i = d > left ? _k : d == right ? point.n : 1;
250 point.j = d < top ? _k : d == top ? point.n : 1;
251 Point neighbor = {point.i + ig, point.j + jg};
252 for (int _s = 0; _s < 1; _s++) /* scalar in centered */ {
253 scalar b = (s.v.x.i < 0 ? s :
254 s.i == s.v.x.i && d < top ? s.v.x :
255 s.i == s.v.y.i && d >= top ? s.v.x :
256 s.v.y);
257 val(s,ig,jg) = b.boundary[d] (point, neighbor, s, NULL);
258 }
259 }
260 }
261 free (centered);
262
264 free (normal);
266 free (tangent);
267}
268
269/* Periodic boundaries */
270
271#if !_MPI
272
274
275for (int _d = 0; _d < dimension; _d++)
276static void periodic_boundary_level_x (const Boundary * b, scalar * list, int l)
277{
278 scalar * list1 = NULL;
279 for (int _s = 0; _s < 1; _s++) /* scalar in list */
280 if (!is_constant(s) && s.block > 0) {
281 if (s.face) {
282 scalar vt = VT;
283 if (vt.boundary[right] == periodic_bc)
284 list1 = list_add (list1, s);
285 }
286 else if (s.boundary[right] == periodic_bc)
287 list1 = list_add (list1, s);
288 }
289 if (!list1)
290 return;
291
293 Point point = {0};
294 point.n = cartesian->n;
295 int j;
296 OMP(omp for schedule(static))
297 for (j = 0; j < point.n + 2*GHOSTS; j++) {
298 for (int i = 0; i < GHOSTS; i++)
299 for (int _s = 0; _s < 1; _s++) /* scalar in list1 */
300 memcpy (&s[i,j], &s[i + point.n,j], s.block*sizeof(real));
301 for (int i = point.n + GHOSTS; i < point.n + 2*GHOSTS; i++)
302 for (int _s = 0; _s < 1; _s++) /* scalar in list1 */
303 memcpy (&s[i,j], &s[i - point.n,j], s.block*sizeof(real));
304 }
305 }
307}
308
309@undef VT
310
311#endif // !_MPI
312
313void free_grid (void)
314{
315 if (!grid)
316 return;
318 free (cartesian->d);
319 free (cartesian);
320 grid = NULL;
321}
322
323void init_grid (int n)
324{
325 if (cartesian && n == cartesian->n)
326 return;
327 free_grid();
328 Cartesian * p = qmalloc (1, Cartesian);
329 size_t len = sq((size_t)n + 2)*datasize;
330 p->n = N = n;
331 p->d = qmalloc (len, char);
332 grid = (Grid *) p;
333 reset (all, 0.);
334 for (int d = 0; d < nboundary; d++) {
336 box->d = d;
337 Boundary * b = (Boundary *) box;
339 add_boundary (b);
340 }
341 // periodic boundaries
342 for (int _d = 0; _d < dimension; _d++) {
343 Boundary * b = qcalloc (1, Boundary);
345 add_boundary (b);
346 }
347 // mesh size
348 grid->n = grid->tn = sq((size_t)n);
349}
350
352{
354 datasize += size;
355 qrealloc (p->d, sq((size_t)p->n + 2)*datasize, char);
356}
357
358Point locate (double xp = 0, double yp = 0, double zp = 0)
359{
360 Point point;
361 point.n = cartesian->n;
362 point.i = (xp - X0)/L0*point.n + 1;
363 point.j = (yp - Y0)/L0*point.n + 1;
364 point.level = (point.i >= 1 && point.i <= point.n &&
365 point.j >= 1 && point.j <= point.n) ? 0 : - 1;
366 return point;
367}
368
369#include "variables.h"
370#if !_GPU
371#include "cartesian-common.h"
372#endif
373
374macro2 for (int _i = 0; _i < _N; _i++) /* foreach_vertex */
375{
377 int ig = -1, jg = -1; NOT_UNUSED(ig); NOT_UNUSED(jg);
378 {...}
379 }
380}
vector q[]
The primitive variables are the momentum , pressure , density , (face) specific volume ,...
Definition all-mach.h:44
const vector a
Definition all-mach.h:59
void add_boundary(Boundary *b)
Definition boundaries.h:16
void free_boundaries()
Definition boundaries.h:27
define k
define double double char flags
define double double char Reduce reductions
define m((k)==0 &&(l)==0 &&(m)==0) macro2 foreach_point(double _x=0.
static int _ig[]
Definition cartesian1D.h:64
static void periodic_boundary_level_x(const Boundary *b, scalar *list, int l)
macro1 is_face_x()
Definition cartesian1D.h:61
Point locate(double xp=0, double yp=0, double zp=0)
Definition cartesian.h:358
define neighbor(o, p, q)((Point)
Definition cartesian.h:146
static void box_boundary_level_tangent(const Boundary *b, scalar *list, int l)
Definition cartesian.h:181
double real
Definition cartesian.h:6
macro1 is_face_y(Point p=point)
Definition cartesian.h:86
if TRASH undef trash define trash(list) reset(list
macro2 foreach_face_generic(char flags=0, Reduce reductions=None, const char *order="xyz")
Definition cartesian.h:60
def is_boundary() _jg[]
Definition cartesian.h:152
static Point last_point
Definition cartesian.h:34
undef val define val(a, k, l, m)(((real *)((Cartesian *) grid) -> d)[(point.i+k+_index(a, m) *(size_t)(point.n+2)) *(point.n+2)+point.j+l]) @define allocated(...) true macro POINT_VARIABLES(Point point=point)
Definition cartesian.h:39
undef VT void free_grid(void)
Definition cartesian.h:313
#define GHOSTS
Definition cartesian.h:13
static void box_boundary_level(const Boundary *b, scalar *list, int l)
Definition cartesian.h:216
static double periodic_bc(Point point, Point neighbor, scalar s, bool *data)
free(list1)
define VT _attribute[s.i] v y scalar * list
Definition cartesian.h:276
static void box_boundary_level_normal(const Boundary *b, scalar *list, int l)
Definition cartesian.h:154
OMP_PARALLEL()
Definition cartesian.h:292
macro2 foreach_boundary_dir(int l, int d)
Definition cartesian.h:112
#define cartesian
Definition cartesian.h:36
#define dimension
Definition cartesian.h:12
double(* default_scalar_bc[])(Point, Point, scalar, bool *)
#define define
int x
Definition common.h:76
scalar * all
Definition common.h:342
static _Attributes * _attribute
Definition common.h:165
scalar * list_add(scalar *list, scalar s)
Definition common.h:208
double L0
Definition common.h:36
int N
Definition common.h:39
static number sq(number x)
Definition common.h:11
Grid * grid
Definition common.h:32
double X0
Definition common.h:34
double Y0
Definition common.h:34
@ top
Definition common.h:123
@ bottom
Definition common.h:123
@ left
Definition common.h:123
@ right
Definition common.h:123
int nboundary
Definition common.h:127
size_t datasize
Definition common.h:132
#define p
Definition tree.h:320
else define undefined((double) DBL_MAX) @ define enable_fpe(flags) @ define disable_fpe(flags) static void set_fpe(void)
Definition config.h:615
#define qcalloc(size, type)
define _index(a, m)(a.i) @define val(a
#define qmalloc(size, type)
Point point
Definition conserving.h:86
coord o
Definition curvature.h:672
else return n
Definition curvature.h:101
trace bool box(bool notics=false, float lc[3]={0}, float lw=1.)
Definition draw.h:1397
scalar s
Definition embed-tree.h:56
*cs[i, 0, 0] a *[i -1, 0, 0] j
Definition embed.h:88
double d[2]
Definition embed.h:383
scalar int i
Definition embed.h:74
define l
Definition cartesian.h:23
#define depth()
Definition cartesian.h:14
#define realloc_scalar(size)
Definition gpu.h:480
#define reset(...)
Definition grid.h:1388
#define init_grid(n)
Definition grid.h:1402
macro
We also redefine the "per field" (inner) traversal.
Definition layers.h:18
#define data(k, l)
Definition linear.h:26
size_t size
size *double * b
def _i
Definition stencils.h:405
def _k
Definition stencils.h:405
char * d
Definition cartesian.h:21
Definition common.h:26
long n
Definition common.h:27
long tn
Definition common.h:28
Definition linear.h:21
int level
Definition linear.h:23
int i
Definition linear.h:23
vector v
Definition common.h:158
void(* level)(const Boundary *b, scalar *list, int l)
Definition boundaries.h:9
int j
Definition cartesian.h:26
int n
Definition cartesian.h:26
int level
Definition cartesian.h:26
int x
Definition multigrid.h:52
int i
Definition cartesian.h:26
int i
Definition common.h:44
scalar y
Definition common.h:49
def allocated(k, l, n)(mem_allocated(((Tree *) grid) -> L[point.level]->m, point.i+k, point.j+l)) @ @def NEIGHBOR(k, l, n)(((((Tree *) grid) ->L[point.level]->m) ->b[point.i+k][point.j+l])) @ @def PARENT(k, l, n)(((((Tree *) grid) ->L[point.level-1]->m) ->b[(point.i+2)/2+k][(point.j+2)/2+l])) @ @def allocated_child(k, l, n)(level< depth() &&mem_allocated(((Tree *) grid) ->L[point.level+1]->m, 2 *point.i- 2+k, 2 *point.j- 2+l)) @ @def CHILD(k, l, n)(((((Tree *) grid) ->L[point.level+1]->m) ->b[2 *point.i- 2+k][2 *point.j- 2+l])) @ @define CELL(m)(*((Cell *)(m))) @define depth()(grid->depth) @define aparent(k, l, n) CELL(PARENT(k, l, n)) @define child(k, l, n) CELL(CHILD(k, l, n)) @define cell CELL(NEIGHBOR(0, 0, 0)) @define neighbor(k, l, n) CELL(NEIGHBOR(k, l, n)) @def neighborp(l, m, n)(Point)
Definition tree.h:253
define n n define n n macro POINT_VARIABLES(Point point=point)
Definition tree.h:322
define l
Definition tree.h:318
#define is_boundary(cell)
Definition tree.h:412
macro VARIABLES(Point point=point, int _ig=ig, int _jg=jg, int _kg=kg)
Definition variables.h:3
Array * normal