29#ifndef PECLET_FLOW_SCALAR_KRYLOV_HPP
30#define PECLET_FLOW_SCALAR_KRYLOV_HPP
34#include <Kokkos_Core.hpp>
63 "peclet::flow::skr_axpy",
C3{g, g, g},
C3{e.x - g, e.y - g, e.z - g},
64 KOKKOS_LAMBDA(
int lx,
int ly,
int lz) {
65 const long i = (long)lx + (
long)ly * e.x + (long)lz * (
long)e.x * e.y;
72 "peclet::flow::skr_aypx",
C3{g, g, g},
C3{e.x - g, e.y - g, e.z - g},
73 KOKKOS_LAMBDA(
int lx,
int ly,
int lz) {
74 const long i = (long)lx + (
long)ly * e.x + (long)lz * (
long)e.x * e.y;
75 y(i) = x(i) + a * y(i);
81 "peclet::flow::skr_residual",
C3{g, g, g},
C3{e.x - g, e.y - g, e.z - g},
82 KOKKOS_LAMBDA(
int lx,
int ly,
int lz) {
83 const long i = (long)lx + (
long)ly * e.x + (long)lz * (
long)e.x * e.y;
105 Kokkos::deep_copy(
CCExec(), y.f, x.f);
107 Kokkos::deep_copy(
CCExec(), y.s, x.s);
110 Kokkos::deep_copy(
CCExec(), y.f, 0.0);
112 Kokkos::deep_copy(
CCExec(), y.s, 0.0);
134using Slot = Kokkos::View<double, CCMem>;
135inline Slot slot(
const Kokkos::View<double*, CCMem>& ks,
int k) {
136 return Slot(ks.data() + k);
155 Kokkos::View<double*, CCMem>
ks;
156 Kokkos::View<double*, Kokkos::HostSpace>
pk;
164template <
class State>
167 st.ks = Kokkos::View<double*, CCMem>(
"peclet::flow::skr_slots",
skr::kNSlots);
168 if (st.pk.extent(0) < 3)
169 st.pk = Kokkos::View<double*, Kokkos::HostSpace>(
"peclet::flow::skr_packet", 3);
180 Kokkos::parallel_for(name, Kokkos::RangePolicy<CCExec>(
CCExec(), 0, 1), f);
185 "peclet::flow::skr_init", KOKKOS_LAMBDA(
int) {
195 "peclet::flow::skr_beta", KOKKOS_LAMBDA(
int) {
196 const double rhoNew = ks(
kRhoNew);
197 if (!Kokkos::isfinite(rhoNew) || Kokkos::fabs(rhoNew) < 1e-300) {
208 "peclet::flow::skr_alpha", KOKKOS_LAMBDA(
int) {
209 if (ks(
kStop) != 0.0)
211 const double rhv = ks(
kRhv);
212 if (!Kokkos::isfinite(rhv) || Kokkos::fabs(rhv) < 1e-300) {
222 "peclet::flow::skr_omega", KOKKOS_LAMBDA(
int) {
223 const double tt = ks(
kTt);
224 if (!Kokkos::isfinite(tt) || tt < 1e-300) {
228 const double om = ks(
kTr) / tt;
229 if (!Kokkos::isfinite(om) || Kokkos::fabs(om) < 1e-300) {
239 const Kokkos::View<double*, CCMem>& ks,
C3 e,
int g) {
240 CCField pf = p.f, ps = p.s, vf = v.f, vs = v.s, rf = r.f, rs = r.s;
241 const bool two = p.solid;
243 "peclet::flow::skr_p",
C3{g, g, g},
C3{e.x - g, e.y - g, e.z - g},
244 KOKKOS_LAMBDA(
int lx,
int ly,
int lz) {
245 if (ks(
kStop) != 0.0)
247 const long i = (long)lx + (
long)ly * e.x + (long)lz * (
long)e.x * e.y;
250 pf(i) = rf(i) + beta *
pf(i);
253 ps(i) = rs(i) + beta * ps(i);
259 const Kokkos::View<double*, CCMem>& ks,
C3 e,
int g) {
260 CCField rf = r.f, rs = r.s, vf = v.f, vs = v.s;
261 const bool two = r.solid;
263 "peclet::flow::skr_s",
C3{g, g, g},
C3{e.x - g, e.y - g, e.z - g},
264 KOKKOS_LAMBDA(
int lx,
int ly,
int lz) {
265 if (ks(
kStop) != 0.0)
267 const long i = (long)lx + (
long)ly * e.x + (long)lz * (
long)e.x * e.y;
268 const double na = -ks(
kAlpha);
276 const Kokkos::View<double*, CCMem>& ks,
C3 e,
int g) {
277 CCField xf = x.f, xs = x.s, zf = z.f, zs = z.s;
278 const bool two = x.solid;
280 "peclet::flow::skr_x_half",
C3{g, g, g},
C3{e.x - g, e.y - g, e.z - g},
281 KOKKOS_LAMBDA(
int lx,
int ly,
int lz) {
282 const long i = (long)lx + (
long)ly * e.x + (long)lz * (
long)e.x * e.y;
283 const double a = ks(
kAlpha);
293 const Kokkos::View<double*, CCMem>& ks,
C3 e,
int g) {
294 CCField xf = x.f, xs = x.s, rf = r.f, rs = r.s, zf = z.f, zs = z.s, z2f = z2.
f, z2s = z2.
s,
296 const bool two = x.solid;
298 "peclet::flow::skr_x_r",
C3{g, g, g},
C3{e.x - g, e.y - g, e.z - g},
299 KOKKOS_LAMBDA(
int lx,
int ly,
int lz) {
300 const long i = (long)lx + (
long)ly * e.x + (long)lz * (
long)e.x * e.y;
301 const double a = ks(
kAlpha);
302 if (ks(
kStop) != 0.0) {
308 const double om = ks(
kOmega), nom = -om;
310 xf(i) += om * z2f(i);
311 rf(i) += nom * tf(i);
314 xs(i) += om * z2s(i);
315 rs(i) += nom * ts(i);
343 if (!std::isfinite(rn))
344 throw std::runtime_error(
"scalar cut-cell solve: non-finite residual");
350 ops.matvec(w.t, w.x);
359inline bool stagnated(
double rn,
double& best,
int& lastImprove,
int it) {
360 if (rn < 0.999 * best) {
365 return it - lastImprove > 30;
373 const ScalarVec &x = w.x, &r = w.r, &rh = w.rh, &p = w.p, &v = w.v, &t = w.t, &z = w.z,
375 const bool singular = w.singular;
378 const double r0n = ops.maxabs(r);
380 if (!(r0n > w.thresh))
382 double rho = 1.0, alpha = 1.0, omega = 1.0;
384 int lastImprove = it;
387 for (; it < w.maxit; ++it) {
388 const double rhoNew = ops.dot(rh, r);
389 if (!std::isfinite(rhoNew) || std::fabs(rhoNew) < 1e-300)
391 const double beta = (rhoNew / rho) * (alpha / omega);
393 axpy(p, -omega, v, e, g);
394 aypx(p, beta, r, e, g);
399 const double rhv = ops.dot(rh, v);
400 if (!std::isfinite(rhv) || std::fabs(rhv) < 1e-300)
403 axpy(r, -alpha, v, e, g);
406 double rn = ops.maxabs(r);
408 if (rn <= w.thresh) {
409 axpy(x, alpha, z, e, g);
417 double tr = 0.0, tt = 0.0;
418 ops.dot2(t, r, t, tr, tt);
419 if (!std::isfinite(tt) || tt < 1e-300) {
420 axpy(x, alpha, z, e, g);
425 if (!std::isfinite(omega) || std::fabs(omega) < 1e-300) {
426 axpy(x, alpha, z, e, g);
430 axpy(x, alpha, z, e, g);
431 axpy(x, omega, z2, e, g);
432 axpy(r, -omega, t, e, g);
437 if (rn <= w.thresh ||
stagnated(rn, best, lastImprove, it)) {
448 const auto& ks = ops.ks;
450 Kokkos::deep_copy(ops.pk, Kokkos::subview(ks, std::make_pair((
int)
kStop, (
int)
kStop + 3)));
452 return w.x.solid ? std::fmax(ops.pk(1), ops.pk(2)) : ops.pk(1);
461 const ScalarVec &x = w.x, &r = w.r, &rh = w.rh, &p = w.p, &v = w.v, &t = w.t, &z = w.z,
465 const double r0n = ops.maxabs(r);
467 if (!(r0n > w.thresh))
470 int lastImprove = it;
473 const auto& ks = ops.ks;
475 for (; it < w.maxit; ++it) {
489 if (rn <= w.thresh) {
505 if (rn <= w.thresh ||
stagnated(rn, best, lastImprove, it)) {
516 const double rn = ops.maxabs(w.r);
537 const ScalarVec& z2,
int maxit,
double rtol,
bool singular) {
540 const double ax0 = ops.maxabs(t);
541 const double bmax = ops.maxabs(b);
542 res.ref = bmax > ax0 ? bmax : ax0;
543 if (!std::isfinite(res.ref))
544 throw std::runtime_error(
"scalar cut-cell solve: non-finite right-hand side or initial guess");
545 const double thresh = rtol * res.ref;
546 const skr::BiCGStabArgs w{b, x, r, rh, p, v, t, z, z2, maxit, thresh, singular};
548 const bool resident = ops.resident && !singular;
552 if (res.trueRes > 10.0 * thresh && it < maxit) {
553 res.restarted =
true;
559 res.converged = res.trueRes <= 10.0 * thresh;
flow — portable (Kokkos) cut-cell pressure-operator face openness from an SDF.
The device slots of the resident pass, and the stop codes (exact in double).
void residentAlpha(const Kokkos::View< double *, CCMem > &ks)
(rh, v) breakdown, else alpha = rho / (rh, v).
Kokkos::View< double, CCMem > Slot
Slot slot(const Kokkos::View< double *, CCMem > &ks, int k)
void bicgPass(Ops &ops, const BiCGStabArgs &w, int &it)
One BiCGStab pass from the current x (CutcellMG::solveBiCGStab's loop, with the §5....
double bicgTrueResidual(Ops &ops, const BiCGStabArgs &w)
The true residual max|b - A x| after a pass (mean removed in the singular case).
void residentS(const ScalarVec &r, const ScalarVec &v, const Kokkos::View< double *, CCMem > &ks, C3 e, int g)
r <- s = r - alpha v (axpy(r, -alpha, v)); skipped on a stop.
constexpr double kBrkOmega
bool stagnated(double rn, double &best, int &lastImprove, int it)
The stagnation stop: true once 30 iterations brought no 0.1 % improvement of the best residual (the b...
void aypx(CCField y, double a, CCConst x, C3 e, int g)
y = x + a y over inner cells.
void zero(const ScalarVec &y)
void residualOf(Ops &ops, const BiCGStabArgs &w)
r = b - A x (t as scratch), mean removed in the singular case.
void axpy(CCField y, double a, CCConst x, C3 e, int g)
y += a x over inner cells.
void residentP(const ScalarVec &p, const ScalarVec &v, const ScalarVec &r, const Kokkos::View< double *, CCMem > &ks, C3 e, int g)
p = r + beta (p - omega v): axpy(p, -omega, v) then aypx(p, beta, r), per phase; skipped on a stop.
double readPacket(Ops &ops, const BiCGStabArgs &w, double &stop)
The resident pass's packet {stop, max|r|} (both phases combined with std::fmax, as ops....
void residualFrom(CCField y, CCConst b, C3 e, int g)
y = b - y over inner cells (the residual from a matvec result held in y).
void residentXHalf(const ScalarVec &x, const ScalarVec &z, const Kokkos::View< double *, CCMem > &ks, C3 e, int g)
x += alpha z (the half-step exit).
void bicgPassResident(Ops &ops, const BiCGStabArgs &w, int &it)
The same pass with the scalars on the device (WO-9b; see the file comment).
void residentInit(const Kokkos::View< double *, CCMem > &ks)
void residentXR(const ScalarVec &x, const ScalarVec &r, const ScalarVec &z, const ScalarVec &z2, const ScalarVec &t, const Kokkos::View< double *, CCMem > &ks, C3 e, int g)
x += alpha z; x += omega z2; r -= omega t (the three axpys) — or, after a tt / omega breakdown,...
void residentOmega(const Kokkos::View< double *, CCMem > &ks)
The tt breakdown, else omega = (t, r) / (t, t) and its breakdown.
void scalarKernel(const char *name, F f)
One-thread kernel on the slots (the coefficient arithmetic of the host pass, on the device).
@ kStop
the packet the host reads: {stop, max|r_f|, max|r_s|}
void requireFinite(double rn)
A residual max-norm that is not finite ends the solve.
void copy(const ScalarVec &y, const ScalarVec &x)
void residentBeta(const Kokkos::View< double *, CCMem > &ks)
(rh, r) breakdown, else beta = (rhoNew / rho) (alpha / omega) and rho = rhoNew.
Kokkos::DefaultExecutionSpace CCExec
ScalarKrylovResult scalarBiCGStab(Ops &ops, const ScalarVec &b, const ScalarVec &x, const ScalarVec &r, const ScalarVec &rh, const ScalarVec &p, const ScalarVec &v, const ScalarVec &t, const ScalarVec &z, const ScalarVec &z2, int maxit, double rtol, bool singular)
BiCGStab on A x = b.
Kokkos::View< const double *, CCMem > CCConst
void residentKrylov(State &st, ScalarKrylovOps &ops)
Arm ops for the resident pass with the persistent slots of st (ks, pk, allocated once).
Kokkos::View< double *, CCMem > CCField
void ccFor3(const char *name, C3 lo, C3 hi, F f)
The injected pieces as callables (host-side calls that launch kernels; the overhead of the indirectio...
std::function< void(const ScalarVec &, const ScalarVec &, skr::Slot)> dotTo
std::function< void(const ScalarVec &, const ScalarVec &)> matvec
std::function< double(const ScalarVec &)> maxabs
std::function< void(const ScalarVec &, const ScalarVec &, const ScalarVec &, double &, double &)> dot2
std::function< void(const ScalarVec &, const ScalarVec &)> precond
std::function< void(const ScalarVec &, skr::Slot, skr::Slot)> maxabsTo
std::function< double(const ScalarVec &, const ScalarVec &)> dot
std::function< void(const ScalarVec &, const ScalarVec &, const ScalarVec &, skr::Slot, skr::Slot)> dot2To
Kokkos::View< double *, CCMem > ks
Kokkos::View< double *, Kokkos::HostSpace > pk
std::function< void(const ScalarVec &)> removeMean
bool resident
The resident pass (single rank): ks (>= skr::kNSlots device doubles) and pk (3 host doubles) persiste...
Outcome of one scalarBiCGStab call.
int iterations
BiCGStab iterations (both passes when it restarted).
double trueRes
max|b - A x| at exit (after the restart, if any)
double ref
max(max|b|, max|A x0|)
bool converged
trueRes <= 10 rtol ref
A Krylov vector (§5.1): the fluid field and, for a conjugate scalar, the solid field (WO-7).
The vectors and settings of one scalarBiCGStab call: the right-hand side, the iterate (the initial gu...
static constexpr double F