flow 1.4.0
Kokkos cut-cell IBM incompressible Navier-Stokes solver + pnm pore extraction
Loading...
Searching...
No Matches
mg_fp32_problems.hpp
Go to the documentation of this file.
1
5#pragma once
6
7#include <cmath>
8#include <Kokkos_Core.hpp>
9#include <random>
10#include <string>
11#include <vector>
12
13#include "mac_cutcell_mg.hpp"
14
16
17using namespace peclet::flow;
18
19template <class V>
20inline std::vector<typename V::non_const_value_type> toHost(const V& f) {
21 auto h = Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace(), f);
22 return std::vector<typename V::non_const_value_type>(h.data(), h.data() + h.extent(0));
23}
24inline CCField toDevice(const std::vector<double>& v, const char* name) {
25 CCField f(name, v.size());
26 auto h = Kokkos::create_mirror_view(f);
27 for (std::size_t i = 0; i < v.size(); ++i)
28 h(i) = v[i];
29 Kokkos::deep_copy(f, h);
30 return f;
31}
32
33struct Problem {
34 std::string name;
35 int nx = 0, ny = 0, nz = 0, levels = 0;
36 int bc[6] = {0, 0, 0, 0, 0, 0};
37 std::vector<double> ox, oy, oz; // ext-indexed, inner faces set (setOpenness fills the ghosts)
38 C3 ext() const { return C3{nx + 2, ny + 2, nz + 2}; }
39 long idx(int x, int y, int z) const { // 0-based inner coordinates
40 return (long)(x + 1) + (long)(y + 1) * (nx + 2) + (long)(z + 1) * (long)(nx + 2) * (ny + 2);
41 }
42};
43
44// open(x, y, z): the face openness at a physical face centre; rho(x, y, z) at a cell centre.
45template <class Open, class Rho>
46inline Problem makeProblem(const std::string& name, int nx, int ny, int nz, int levels,
47 const int bc[6], Open open, Rho rho) {
48 Problem P;
49 P.name = name;
50 P.nx = nx;
51 P.ny = ny;
52 P.nz = nz;
53 P.levels = levels;
54 for (int f = 0; f < 6; ++f)
55 P.bc[f] = bc[f];
56 const C3 e = P.ext();
57 const std::size_t n = (std::size_t)e.x * e.y * e.z;
58 P.ox.assign(n, 0.0);
59 P.oy.assign(n, 0.0);
60 P.oz.assign(n, 0.0);
61 auto wrap = [](int v, int m) { return (v + m) % m; };
62 for (int z = 0; z < nz; ++z)
63 for (int y = 0; y < ny; ++y)
64 for (int x = 0; x < nx; ++x) {
65 const double cx = x + 0.5, cy = y + 0.5, cz = z + 0.5;
66 const double r0 = rho(cx, cy, cz);
67 const long i = P.idx(x, y, z);
68 // the minus face of cell (x, y, z) against its periodic minus neighbour
69 P.ox[i] = open(cx - 0.5, cy, cz) / (0.5 * (r0 + rho(wrap(x - 1, nx) + 0.5, cy, cz)));
70 P.oy[i] = open(cx, cy - 0.5, cz) / (0.5 * (r0 + rho(cx, wrap(y - 1, ny) + 0.5, cz)));
71 P.oz[i] = open(cx, cy, cz - 0.5) / (0.5 * (r0 + rho(cx, cy, wrap(z - 1, nz) + 0.5)));
72 }
73 return P;
74}
75
76inline double clamp01(double v) {
77 return v < 0.0 ? 0.0 : (v > 1.0 ? 1.0 : v);
78}
79
81 const int bc[6] = {0, 0, 1, 1, 0, 0};
82 const double B[5][4] = {
83 {32, 20, 16, 6}, {80, 30, 40, 5}, {100, 60, 20, 7}, {50, 70, 48, 6}, {16, 48, 56, 4}};
84 auto rho = [B](double x, double y, double z) {
85 for (const auto& b : B) {
86 const double dx = x - b[0], dy = y - b[1], dz = z - b[2];
87 if (dx * dx + dy * dy + dz * dz < b[3] * b[3])
88 return 0.02;
89 }
90 return 1.0;
91 };
92 return makeProblem("column", 128, 96, 64, 5, bc, [](double, double, double) { return 1.0; }, rho);
93}
94
96 const int bc[6] = {0, 0, 0, 0, 0, 0};
97 const double N = 32, c = N / 2, R = 0.25 * N;
98 auto open = [=](double x, double, double z) {
99 return clamp01(0.5 + std::sqrt((x - c) * (x - c) + (z - c) * (z - c)) - R);
100 };
101 auto rho = [=](double x, double y, double z) {
102 const double dx = x - 6, dy = y - c, dz = z - 6;
103 return dx * dx + dy * dy + dz * dz < 25.0 ? 1e4 : 1.0;
104 };
105 return makeProblem("cyl", 32, 32, 32, 4, bc, open, rho);
106}
107
108// Review finding 4: a WALLED stratified problem, so that the G-D3 checks (a)-(e) run against walls
109// (the column is periodic in z; cyl and pack cap in FP64): a sharp heavy-below slab, density
110// ratio 1e3, walls at +-z. FP64 FCG converges here (52 iterations) and is required to; FP64 PCG
111// does not (true residual 172 after 200 iterations at 1e3, 0.085 at 1e2: the V-cycle is not an
112// SPD preconditioner against a stratified wall), so for PCG (a)-(e) are n/a and (b) runs.
113// `ratio` other than 1e3: the distributed design's walled slab at 1e2 (G-DD1), named "slab<ratio>".
114inline Problem slabProblem(double ratio = 1e3) {
115 const int bc[6] = {0, 0, 0, 0, 1, 1};
116 auto rho = [ratio](double, double, double z) { return z < 16.0 ? ratio : 1.0; };
117 const std::string name =
118 ratio == 1e3 ? "slab" : "slab1e" + std::to_string((int)std::lround(std::log10(ratio)));
119 return makeProblem(name, 32, 32, 32, 4, bc, [](double, double, double) { return 1.0; }, rho);
120}
121
123 const int bc[6] = {0, 0, 0, 0, 1, 1};
124 std::vector<double> C;
125 std::mt19937 gen(7);
126 std::uniform_real_distribution<double> jit(-2.0, 2.0);
127 for (int k = 0; k < 4; ++k)
128 for (int j = 0; j < 4; ++j)
129 for (int i = 0; i < 4; ++i) {
130 C.push_back(8 + 16 * i + jit(gen));
131 C.push_back(8 + 16 * j + jit(gen));
132 C.push_back(8 + 16 * k + jit(gen));
133 }
134 auto open = [C](double x, double y, double z) {
135 double d = 1e30;
136 for (std::size_t s = 0; s < C.size(); s += 3) {
137 auto mi = [](double v) { return v - 64.0 * std::round(v / 64.0); };
138 const double dx = mi(x - C[s]), dy = mi(y - C[s + 1]), dz = mi(z - C[s + 2]);
139 d = std::min(d, std::sqrt(dx * dx + dy * dy + dz * dz) - 6.5);
140 }
141 return clamp01(0.5 + d);
142 };
143 auto rho = [](double, double, double z) { return z < 32.0 ? 1e4 : 1.0; };
144 return makeProblem("pack", 64, 64, 64, 5, bc, open, rho);
145}
146
147// A 64^3 periodic box with a sharp heavy sphere at ratio 1e4 (the distributed design's G-K1 /
148// G-K2 box, doc/vof_projection_distributed_design.md §8).
150 const int bc[6] = {0, 0, 0, 0, 0, 0};
151 auto rho = [](double x, double y, double z) {
152 const double dx = x - 20, dy = y - 36, dz = z - 28;
153 return dx * dx + dy * dy + dz * dz < 100.0 ? 1e4 : 1.0;
154 };
155 return makeProblem("box64", 64, 64, 64, 5, bc, [](double, double, double) { return 1.0; }, rho);
156}
157
158inline void setUp(CutcellMG& mg, const Problem& P, int mode) {
159 mg.init(P.nx, P.ny, P.nz, P.levels);
160 mg.setBoundaryConditions(P.bc);
161 mg.setVcyclePrecision(mode);
162 mg.setOpenness(CCConst(toDevice(P.ox, "ox")), CCConst(toDevice(P.oy, "oy")),
163 CCConst(toDevice(P.oz, "oz")), 1.0, 1.0, 1.0);
164}
165
166inline std::vector<double> rhsOf(const Problem& P) {
167 const C3 e = P.ext();
168 const std::size_t n = (std::size_t)e.x * e.y * e.z;
169 std::vector<double> hb(n, 0.0);
170 double sum = 0.0;
171 long cnt = 0;
172 for (int z = 0; z < P.nz; ++z)
173 for (int y = 0; y < P.ny; ++y)
174 for (int x = 0; x < P.nx; ++x) {
175 const double v = std::sin(0.3 * x + 0.1) * std::cos(0.2 * y) + 0.1 * std::sin(0.5 * z);
176 hb[P.idx(x, y, z)] = v;
177 sum += v;
178 ++cnt;
179 }
180 for (int z = 0; z < P.nz; ++z)
181 for (int y = 0; y < P.ny; ++y)
182 for (int x = 0; x < P.nx; ++x)
183 hb[P.idx(x, y, z)] -= sum / (double)cnt;
184 return hb;
185}
186
187} // namespace peclet_flow_fp32_problems
void setOpenness(CCConst ox, CCConst oy, CCConst oz, double idx2, double idy2, double idz2)
void setBoundaryConditions(const int bc[6])
void init(int nx, int ny, int nz, int nLevels)
void setVcyclePrecision(int mode)
flow — portable (Kokkos) geometric multigrid for the cut-cell (variable-openness) pressure Poisson.
Kokkos::View< const double *, CCMem > CCConst
Kokkos::View< double *, CCMem > CCField
std::vector< double > rhsOf(const Problem &P)
Problem slabProblem(double ratio=1e3)
CCField toDevice(const std::vector< double > &v, const char *name)
void setUp(CutcellMG &mg, const Problem &P, int mode)
Problem makeProblem(const std::string &name, int nx, int ny, int nz, int levels, const int bc[6], Open open, Rho rho)
std::vector< typename V::non_const_value_type > toHost(const V &f)
long idx(int x, int y, int z) const
static constexpr int N
static int wrap(int a, int n)
static constexpr double R