/
open_3d.hpp
237 lines (204 loc) · 7.83 KB
/
open_3d.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
// 3D open boundary conditions for libmpdata++
//
// licensing: GPU GPL v3
// copyright: University of Warsaw
#pragma once
#include <libmpdata++/bcond/detail/bcond_common.hpp>
namespace libmpdataxx
{
namespace bcond
{
template <typename real_t, int halo, bcond_e knd, drctn_e dir, int n_dims, int d>
class bcond< real_t, halo, knd, dir, n_dims, d,
typename std::enable_if<
knd == open &&
dir == left &&
n_dims == 3
>::type
> : public detail::bcond_common<real_t, halo, n_dims>
{
using parent_t = detail::bcond_common<real_t, halo, n_dims>;
using arr_t = blitz::Array<real_t, 3>;
using parent_t::parent_t; // inheriting ctor
// holds saved initial value of edge velocity
arr_t edge_velocity;
public:
void fill_halos_sclr(arr_t &a, const rng_t &j, const rng_t &k, const bool deriv = false)
{
using namespace idxperm;
for (int i = this->left_halo_sclr.first(); i <= this->left_halo_sclr.last(); ++i)
{
if (deriv)
a(pi<d>(i, j, k)) = 0;
else
a(pi<d>(i, j, k)) = a(pi<d>(this->left_edge_sclr, j, k));
}
}
void fill_halos_pres(arr_t &a, const rng_t &j, const rng_t &k)
{
using namespace idxperm;
// equivalent to one-sided derivatives at the boundary
a(pi<d>(this->left_halo_sclr.last(), j, k)) = 2 * a(pi<d>(this->left_edge_sclr, j, k))
- a(pi<d>(this->left_edge_sclr + 1, j, k));
if (halo > 1)
{
a(pi<d>(this->left_halo_sclr.last() - 1, j, k)) = 3 * a(pi<d>(this->left_edge_sclr, j, k))
- 2 * a(pi<d>(this->left_edge_sclr + 1, j, k));
}
}
void save_edge_vel(const arr_t &a, const rng_t &j, const rng_t &k)
{
using namespace idxperm;
auto s = a.shape();
s[d] = 1;
edge_velocity.resize(s);
if(d != 0) edge_velocity.reindexSelf({a.lbound(0), 0, 0});
edge_velocity(pi<d>(0, j, k)) = a(pi<d>(this->left_edge_sclr, j, k));
}
void set_edge_pres(arr_t &a, const rng_t &j, const rng_t &k, int sign)
{
using namespace idxperm;
a(pi<d>(this->left_edge_sclr, j, k)) = sign * edge_velocity(pi<d>(0, j, k));
}
void fill_halos_vctr_alng(arrvec_t<arr_t> &av, const rng_t &j, const rng_t &k, const bool ad = false)
{
using namespace idxperm;
const int i = this->left_edge_sclr;
// TODO: exactly the same code below!
switch (d) // note: order and lack of breaks intentional!
{
case 1:
av[d+2](pi<d>(i, j, (k-h).first())) = 0;
av[d+2](pi<d>(i, j, (k+h).last() )) = 0;
case 2:
av[d+1](pi<d>(i, (j-h).first(), k)) = 0;
av[d+1](pi<d>(i, (j+h).last(), k)) = 0;
case 0:
break;
default: assert(false);
}
assert(std::isfinite(sum(av[d ](pi<d>(i+h, j, k)))));
assert(std::isfinite(sum(av[d+1](pi<d>(i, j-h, k)))));
assert(std::isfinite(sum(av[d+1](pi<d>(i, j+h, k)))));
assert(std::isfinite(sum(av[d+2](pi<d>(i, j, k-h)))));
assert(std::isfinite(sum(av[d+2](pi<d>(i, j, k+h)))));
// zero-divergence condition
for (int ii = this->left_halo_vctr.first(); ii <= this->left_halo_vctr.last() - (ad ? 1 : 0); ++ii)
{
av[d](pi<d>(ii, j, k)) =
av[d](pi<d>(i+h, j, k))
-(
av[d+1](pi<d>(i, j-h, k)) -
av[d+1](pi<d>(i, j+h, k))
)
-(
av[d+2](pi<d>(i, j, k-h)) -
av[d+2](pi<d>(i, j, k+h))
);
}
}
void fill_halos_vctr_nrml(arr_t &a, const rng_t &j, const rng_t &k)
{
using namespace idxperm;
// note intentional sclr
for (int i = this->left_halo_sclr.first(); i <= this->left_halo_sclr.last(); ++i)
a(pi<d>(i, j, k)) = 0;
}
};
template <typename real_t, int halo, bcond_e knd, drctn_e dir, int n_dims, int d>
class bcond< real_t, halo, knd, dir, n_dims, d,
typename std::enable_if<
knd == open &&
dir == rght &&
n_dims == 3
>::type
> : public detail::bcond_common<real_t, halo, n_dims>
{
using parent_t = detail::bcond_common<real_t, halo, n_dims>;
using arr_t = blitz::Array<real_t, 3>;
using parent_t::parent_t; // inheriting ctor
// holds saved initial value of edge velocity
arr_t edge_velocity;
public:
void fill_halos_sclr(arr_t &a, const rng_t &j, const rng_t &k, const bool deriv = false)
{
using namespace idxperm;
for (int i = this->rght_halo_sclr.first(); i <= this->rght_halo_sclr.last(); ++i)
{
if (deriv)
a(pi<d>(i, j, k)) = 0;
else
a(pi<d>(i, j, k)) = a(pi<d>(this->rght_edge_sclr, j, k));
}
}
void fill_halos_pres(arr_t &a, const rng_t &j, const rng_t &k)
{
using namespace idxperm;
// equivalent to one-sided derivatives at the boundary
a(pi<d>(this->rght_halo_sclr.first(), j, k)) = 2 * a(pi<d>(this->rght_edge_sclr, j, k))
- a(pi<d>(this->rght_edge_sclr - 1, j, k));
if (halo > 1)
{
a(pi<d>(this->rght_halo_sclr.first() + 1, j, k)) = 3 * a(pi<d>(this->rght_edge_sclr, j, k))
- 2 * a(pi<d>(this->rght_edge_sclr - 1, j, k));
}
}
void save_edge_vel(const arr_t &a, const rng_t &j, const rng_t &k)
{
using namespace idxperm;
auto s = a.shape();
s[d] = 1;
edge_velocity.resize(s);
if(d != 0) edge_velocity.reindexSelf({a.lbound(0), 0, 0});
edge_velocity(pi<d>(0, j, k)) = a(pi<d>(this->rght_edge_sclr, j, k));
}
void set_edge_pres(arr_t &a, const rng_t &j, const rng_t &k, int sign)
{
using namespace idxperm;
a(pi<d>(this->rght_edge_sclr, j, k)) = sign * edge_velocity(pi<d>(0, j, k));
}
void fill_halos_vctr_alng(arrvec_t<arr_t> &av, const rng_t &j, const rng_t &k, const bool ad = false)
{
using namespace idxperm;
const int i = this->rght_edge_sclr;
switch (d) // note: order and lack of breaks intentional!
{
case 1:
av[d+2](pi<d>(i, j, (k-h).first())) = 0;
av[d+2](pi<d>(i, j, (k+h).last() )) = 0;
case 2:
av[d+1](pi<d>(i, (j-h).first(), k)) = 0;
av[d+1](pi<d>(i, (j+h).last(), k)) = 0;
case 0:
break;
default: assert(false);
}
assert(std::isfinite(sum(av[d ](pi<d>(i-h, j, k)))));
assert(std::isfinite(sum(av[d+1](pi<d>(i, j-h, k)))));
assert(std::isfinite(sum(av[d+1](pi<d>(i, j+h, k)))));
assert(std::isfinite(sum(av[d+2](pi<d>(i, j, k-h)))));
assert(std::isfinite(sum(av[d+2](pi<d>(i, j, k+h)))));
for (int ii = this->rght_halo_vctr.first() + (ad ? 1 : 0); ii <= this->rght_halo_vctr.last(); ++ii)
{
av[d](pi<d>(ii, j, k)) =
av[d](pi<d>(i-h, j, k))
+(
av[d+1](pi<d>(i, j-h, k)) -
av[d+1](pi<d>(i, j+h, k))
)
+(
av[d+2](pi<d>(i, j, k-h)) -
av[d+2](pi<d>(i, j, k+h))
);
}
}
void fill_halos_vctr_nrml(arr_t &a, const rng_t &j, const rng_t &k)
{
using namespace idxperm;
// note intentional sclr
for (int i = this->rght_halo_sclr.first(); i <= this->rght_halo_sclr.last(); ++i)
a(pi<d>(i, j, k)) = 0;
}
};
} // namespace bcond
} // namespace libmpdataxx