56 void build(
const Octree& finest,
double h0,
double idiag,
double mu,
const MomentumOp& fineOp,
57 const std::vector<double>& kappa,
const std::vector<char>& fluid,
58 const std::vector<char>& cut, Index minCoarse = 256) {
62 void build(
const Octree& finest,
const Vec<3>& h0,
double idiag,
double mu,
63 const MomentumOp& fineOp,
const std::vector<double>& kappa,
64 const std::vector<char>& fluid,
const std::vector<char>& cut, Index minCoarse = 256) {
65 hmg_ = std::make_unique<AmrMultigrid<3, Bits>>();
70 hmg_->build(finest, h0,
false);
71 std::size_t nl = hmg_->numLevels();
72 while (nl > 1 && hmg_->op(nl - 1).octree().numLeaves() < minCoarse)
78 std::vector<std::vector<double>> kap(nl);
80 for (std::size_t L = 0; L + 1 < nl; ++L) {
81 const Octree& f = hmg_->op(L).octree();
82 const Octree& c = hmg_->op(L + 1).octree();
84 std::vector<double> ks(
static_cast<std::size_t
>(nc), 0.0),
85 kn(
static_cast<std::size_t
>(nc), 0.0);
86 for (Index i = 0; i < nf; ++i) {
87 Code par = M::from_code(f.
code(i)).ancestor(f.
level(i) + 1).code();
88 Index p = c.
find(par);
90 ks[
static_cast<std::size_t
>(p)] += kap[L][
static_cast<std::size_t
>(i)];
91 kn[
static_cast<std::size_t
>(p)] += 1.0;
94 for (Index p = 0; p < nc; ++p)
95 ks[
static_cast<std::size_t
>(p)] /=
96 (kn[
static_cast<std::size_t
>(p)] > 0 ? kn[
static_cast<std::size_t
>(p)] : 1.0);
97 kap[L + 1] = std::move(ks);
106 levels_[0].op = fineOp;
108 std::vector<char> excl(fluid.size());
109 for (std::size_t i = 0; i < fluid.size(); ++i)
110 excl[i] = (!fluid[i] || cut[i]) ? 1 : 0;
111 levels_[0].solid = toDevice(excl,
"vmg_excl0");
112 allocScratch(levels_[0],
static_cast<Index
>(fluid.size()));
115 std::vector<Index> hs(
static_cast<std::size_t
>(fineOp.n) + 1), hn(fineOp.faceNbr.extent(0));
116 auto ms = Kokkos::create_mirror_view(fineOp.faceStart);
117 auto mn = Kokkos::create_mirror_view(fineOp.faceNbr);
118 Kokkos::deep_copy(ms, fineOp.faceStart);
119 Kokkos::deep_copy(mn, fineOp.faceNbr);
120 for (std::size_t i = 0; i < hs.size(); ++i)
121 hs[i] = ms(
static_cast<Index
>(i));
122 for (std::size_t i = 0; i < hn.size(); ++i)
123 hn[i] = mn(
static_cast<Index
>(i));
124 levels_[0].col = greedyColoring(hs, hn, fineOp.n);
127 for (std::size_t L = 1; L < nl; ++L)
128 buildStaircase(L, idiag, mu, kap[L]);
131 for (std::size_t L = 0; L + 1 < nl; ++L)
135 std::size_t
numLevels()
const {
return levels_.size(); }
136 Index
numLeaves(std::size_t L = 0)
const {
return levels_[L].op.n; }
137 View<double>
x(std::size_t L = 0) {
return levels_[L].x; }
138 View<double>
b(std::size_t L = 0) {
return levels_[L].b; }
140 void setFineOp(
const MomentumOp& fineOp) { levels_[0].op = fineOp; }
149 void vcycle(
int pre = 2,
int post = 2,
int bottom = 30,
double omega = 0.7, std::size_t L = 0) {
150 Level& lv = levels_[L];
151 View<const double> bc(lv.b);
152 if (L + 1 == levels_.size()) {
153 smooth(lv, bottom, omega);
156 smooth(lv, pre, omega);
157 residualMom(lv.op, View<const double>(lv.x), bc, lv.res);
161 zeroMasked(lv.res, View<const char>(lv.solid), lv.op.n);
162 Level& cl = levels_[L + 1];
163 restrictField(lv.childStart, lv.childIdx, View<const double>(lv.res), cl.b, cl.op.n);
164 Kokkos::deep_copy(cl.x, 0.0);
165 vcycle(pre, post, bottom, omega, L + 1);
166 prolongAddMasked(lv.c2p, View<const double>(cl.x), View<const char>(lv.solid), lv.x, lv.op.n);
167 smooth(lv, post, omega);
174 View<double> x, b, res, tmp;
175 View<Index> c2p, childStart, childIdx;
178 void smooth(Level& lv,
int sweeps,
double omega) {
179 View<const double> bc(lv.b);
183 for (
int s = 0; s < sweeps; ++s)
184 multicolorGSMom(lv.op, lv.x, bc, lv.col, 1.0);
186 for (
int s = 0; s < sweeps; ++s)
187 jacobiMom(lv.op, lv.x, bc, lv.tmp, omega);
190 void allocScratch(Level& lv, Index n) {
191 lv.x = View<double>(
"vmg_x",
static_cast<std::size_t
>(n));
192 lv.b = View<double>(
"vmg_b",
static_cast<std::size_t
>(n));
193 lv.res = View<double>(
"vmg_res",
static_cast<std::size_t
>(n));
194 lv.tmp = View<double>(
"vmg_tmp",
static_cast<std::size_t
>(n));
198 void buildStaircase(std::size_t L,
double idiag,
double mu,
const std::vector<double>& kap) {
199 const Poisson& ap = hmg_->op(L);
200 const Octree& oct = ap.octree();
201 const Index n = oct.numLeaves();
209 const double Ldom = ap.h0()[0] *
static_cast<double>(oct.brick()[0] * (Index(1) << oct.lmax()));
210 const double id = std::max(idiag, mu / (Ldom * Ldom));
211 std::vector<double> diag(
static_cast<std::size_t
>(n), 1.0);
212 std::vector<char> solid(
static_cast<std::size_t
>(n), 1);
213 std::vector<Index> start(
static_cast<std::size_t
>(n) + 1, 0);
214 std::vector<std::vector<std::pair<Index, double>>> rows(
static_cast<std::size_t
>(n));
215 for (Index i = 0; i < n; ++i) {
216 if (kap[
static_cast<std::size_t
>(i)] < 0.5)
218 solid[
static_cast<std::size_t
>(i)] = 0;
223 for (
int a = 0; a < 3; ++a) {
224 const double Ha = ap.cellWidth(i, a);
225 mcoef[a] = mu / (Ha * Ha);
228 for (
int axis = 0; axis < 3; ++axis)
229 for (
int dir = -1; dir <= 1; dir += 2) {
230 Index j = ap.periodicNeighbor(i, axis, dir);
233 rows[
static_cast<std::size_t
>(i)].emplace_back(j, -mcoef[axis]);
236 diag[
static_cast<std::size_t
>(i)] = dsum;
238 for (Index i = 0; i < n; ++i)
239 start[
static_cast<std::size_t
>(i) + 1] =
240 start[
static_cast<std::size_t
>(i)] +
241 static_cast<Index
>(rows[
static_cast<std::size_t
>(i)].size());
242 const Index nnz = start[
static_cast<std::size_t
>(n)];
243 std::vector<Index> nbr(
static_cast<std::size_t
>(nnz));
244 std::vector<double> coef(
static_cast<std::size_t
>(nnz));
245 for (Index i = 0; i < n; ++i) {
246 Index k = start[
static_cast<std::size_t
>(i)];
247 for (
auto& e : rows[
static_cast<std::size_t
>(i)]) {
248 nbr[
static_cast<std::size_t
>(k)] = e.first;
249 coef[
static_cast<std::size_t
>(k)] = e.second;
253 Level& lv = levels_[L];
255 lv.op.diag = toDevice(diag,
"vmg_diag");
256 lv.op.faceStart = toDevice(start,
"vmg_fstart");
257 lv.op.faceNbr = toDevice(nbr,
"vmg_fnbr");
258 lv.op.faceCoef = toDevice(coef,
"vmg_fcoef");
259 lv.solid = toDevice(solid,
"vmg_solid");
260 lv.col = greedyColoring(start, nbr, n);
264 void buildTransfer(std::size_t L) {
265 const Octree& f = hmg_->op(L).octree();
266 const Octree& c = hmg_->op(L + 1).octree();
267 const Index nf = f.numLeaves(), nc = c.numLeaves();
268 std::vector<Index> c2p(
static_cast<std::size_t
>(nf));
269 std::vector<Index> cnt(
static_cast<std::size_t
>(nc), 0);
270 for (Index i = 0; i < nf; ++i) {
271 Code par = M::from_code(f.code(i)).ancestor(f.level(i) + 1).code();
272 Index p = c.find(par);
273 c2p[
static_cast<std::size_t
>(i)] = p;
275 ++cnt[
static_cast<std::size_t
>(p)];
277 std::vector<Index> cstart(
static_cast<std::size_t
>(nc) + 1, 0);
278 for (Index p = 0; p < nc; ++p)
279 cstart[
static_cast<std::size_t
>(p) + 1] =
280 cstart[
static_cast<std::size_t
>(p)] + cnt[
static_cast<std::size_t
>(p)];
281 std::vector<Index> cidx(
static_cast<std::size_t
>(nf));
282 std::vector<Index> cur(cstart.begin(), cstart.end() - 1);
283 for (Index i = 0; i < nf; ++i) {
284 Index p = c2p[
static_cast<std::size_t
>(i)];
286 cidx[
static_cast<std::size_t
>(cur[
static_cast<std::size_t
>(p)]++)] = i;
288 levels_[L].c2p = toDevice(c2p,
"vmg_c2p");
289 levels_[L].childStart = toDevice(cstart,
"vmg_cstart");
290 levels_[L].childIdx = toDevice(cidx,
"vmg_cidx");
293 std::unique_ptr<AmrMultigrid<3, Bits>> hmg_;
294 std::vector<Level> levels_;
void build(const Octree &finest, double h0, double idiag, double mu, const MomentumOp &fineOp, const std::vector< double > &kappa, const std::vector< char > &fluid, const std::vector< char > &cut, Index minCoarse=256)
Build the rediscretized hierarchy.
void build(const Octree &finest, const Vec< 3 > &h0, double idiag, double mu, const MomentumOp &fineOp, const std::vector< double > &kappa, const std::vector< char > &fluid, const std::vector< char > &cut, Index minCoarse=256)
Phase 3: the finest spacing per axis; every coarse level inherits the aspect ratio (AM1).