Basilisk CFD
Adaptive Cartesian mesh PDE framework
Loading...
Searching...
No Matches
simple.h
Go to the documentation of this file.
1/** @file simple.h
2 */
3/**
4# Simple multi-dimensional arrays
5
6This is the simplest implementation of a full multidimensional array,
7without any optimisation for sparsity.
8
9The interface is the same as that in [range.h]() which implements
10"range sparse arrays".
11
12The `Memindex` structure defines multi-dimensional arrays. The
13interface is that used by [/src/grid/tree.h](). */
14
15struct _Memindex {
16 int len;
17#if dimension == 1
18 char ** b;
19#elif dimension == 2
20 char *** b;
21#else // dimension == 3
22 char **** b;
23#endif
24};
25
26#define Memindex struct _Memindex *
27
28/**
29The `mem_data()` macros return the data stored at a specific
30(multidimensional) index. It assumes that the index is allocated. This
31can be checked with `mem_allocated()`. */
32
33#if dimension == 1
34inline static
35const bool mem_allocated (const Memindex m, const int i) {
36 return (i >= 0 && i < m->len && m->b[i]);
37}
38#define mem_data(m,i) ((m)->b[i])
39#elif dimension == 2
40inline static
41const bool mem_allocated (const Memindex m, const int i, const int j) {
42 return (i >= 0 && i < m->len && m->b[i] &&
43 j >= 0 && j < m->len && m->b[i][j]);
44}
45#define mem_data(m,i,j) ((m)->b[i][j])
46#else // dimension == 3
47inline static
48const bool mem_allocated (const Memindex m, const int i, const int j, const int k) {
49 return (i >= 0 && i < m->len && m->b[i] &&
50 j >= 0 && j < m->len && m->b[i][j] &&
51 k >= 0 && k < m->len && m->b[i][j][k]);
52}
53#define mem_data(m,i,j,k) ((m)->b[i][j][k])
54#endif // dimension == 3
55
56/**
57The `mem_new()` function returns a new (empty) `Memindex`. */
58
60{
61 Memindex m = calloc (1, sizeof (struct _Memindex));
62 return m;
63}
64
65/**
66The `mem_destroy()` function frees all the memory allocated by a given
67`Memindex`. */
68
69void mem_destroy (Memindex m, int len)
70{
71#if dimension > 1
72 for (int i = 0; i < len; i++)
73 if (m->b[i]) {
74 #if dimension > 2
75 for (int j = 0; j < len; j++)
76 if (m->b[i][j])
77 free (m->b[i][j]);
78 #endif // dimension > 2
79 free (m->b[i]);
80 }
81#endif // dimension > 1
82 if (m->b)
83 free (m->b);
84 free (m);
85}
86
87/**
88The `mem_assign()` function assigns a (pointer) value to a given index. */
89
90void mem_assign (Memindex m, int i, int len, void * b)
91{
92 if (!m->b) {
93 m->b = calloc (len, sizeof(char *));
94 m->len = len;
95 }
96 mem_data(m,i) = b;
97}
98#elif dimension == 2
99void mem_assign (Memindex m, int i, int j, int len, void * b)
100{
101 if (!m->b) {
102 m->b = malloc (len*sizeof(char *));
103 m->len = len;
104 }
105 if (!m->b[i])
106 m->b[i] = calloc (len, sizeof(char *));
107 mem_data(m,i,j) = b;
108}
109#else // dimension == 3
110void mem_assign (Memindex m, int i, int j, int k, int len, void * b)
111{
112 if (!m->b) {
113 m->b = malloc (len*sizeof(char *));
114 m->len = len;
115 }
116 if (!m->b[i])
117 m->b[i] = calloc (len, sizeof(char *));
118 if (!m->b[i][j])
119 m->b[i][j] = calloc (len, sizeof(char *));
120 mem_data(m,i,j,k) = b;
121}
122#endif // dimension == 3
123
124/**
125The `mem_free()` function frees a given index. */
126
127#if dimension == 1
128void mem_free (Memindex m, int i, int len, void * b)
129{
130 mem_data(m,i) = NULL;
131}
132#elif dimension == 2
133void mem_free (Memindex m, int i, int j, int len, void * b)
134{
135 mem_data(m,i,j) = NULL;
136}
137#else // dimension == 3
138void mem_free (Memindex m, int i, int j, int k, int len, void * b)
139{
140 mem_data(m,i,j,k) = NULL;
141}
142#endif // dimension == 3
143
144/**
145The `foreach_mem()` macro traverses every `_i` allocated elements of
146array `_m` taking into account a periodicity of `_len` (and ghost
147cells). */
148
150 Memindex _m = index;
151 int _len = len;
152 Point point = {0};
153 for (point.i = max(Period.x*GHOSTS, 0);
154 point.i < min(_len - Period.x*GHOSTS, _len);
155 point.i += _i)
156 if (_m->b[point.i])
157#if dimension > 1
158 for (point.j = max(Period.y*GHOSTS, 0);
159 point.j < min(_len - Period.y*GHOSTS, _len);
160 point.j += _i)
161 if (_m->b[point.i][point.j])
162#if dimension > 2
163 for (point.k = max(Period.z*GHOSTS, 0);
164 point.k < min(_len - Period.z*GHOSTS, _len);
165 point.k += _i)
166 if (_m->b[point.i][point.j][point.k])
167#endif // dimension > 2
168#endif // dimension > 1
169 {...}
170}
int min
Definition balance.h:192
define k
define m((k)==0 &&(l)==0 &&(m)==0) macro2 foreach_point(double _x=0.
#define GHOSTS
Definition cartesian.h:13
free(list1)
int x
Definition common.h:76
struct @0 Period
define sysmalloc malloc define syscalloc calloc define sysrealloc realloc define sysfree free define systrdup strdup define line calloc(n, s) @ define prealloc(p
Point point
Definition conserving.h:86
*cs[i, 0, 0] a *[i -1, 0, 0] j
Definition embed.h:88
scalar int i
Definition embed.h:74
macro
We also redefine the "per field" (inner) traversal.
Definition layers.h:18
size_t max
Definition mtrace.h:8
size *double * b
#define mem_allocated(m, i, j)
The mem_data() macros return the data stored at a specific (multidimensional) index.
Definition range.h:153
struct _Memindex * mem_new(int len)
The mem_new() function returns a new (empty) Memindex.
Definition simple.h:59
macro foreach_mem(struct _Memindex *index, int len, int _i)
The foreach_mem() macro traverses every _i allocated elements of array _m taking into account a perio...
Definition simple.h:149
void mem_free(struct _Memindex *m, int i, int j, int len, void *b)
The mem_free() function frees a given index.
Definition simple.h:133
#define Memindex
Definition simple.h:26
#define mem_data(m, i, j)
Definition simple.h:45
void mem_destroy(struct _Memindex *m, int len)
The mem_destroy() function frees all the memory allocated by a given Memindex.
Definition simple.h:69
void mem_assign(struct _Memindex *m, int i, int len, void *b)
The mem_assign() function assigns a (pointer) value to a given index.
Definition simple.h:90
def _i
Definition stencils.h:405
Definition linear.h:21
int i
Definition linear.h:23
int len
Definition simple.h:16
char *** b
Definition range.h:134
Array * index