18#include <Kokkos_Core.hpp>
29double fld(
int c,
int gx,
int gy,
int gz, std::array<int, 3> gd) {
30 const double x = 2.0 * M_PI * gx / gd[0],
y = 2.0 * M_PI * gy / gd[1],
31 z = 2.0 * M_PI * gz / gd[2];
34 return std::sin(x) * std::cos(y) + 0.3 * std::cos(2 * z);
36 return std::cos(x) * std::sin(z) - 0.2 * std::sin(y);
38 return std::sin(y) * std::sin(z) + 0.1 * std::cos(x);
40 return std::cos(x + y) + 0.5 * std::sin(z - x);
42 const double s = 0.5 + 0.5 * std::sin(x + 2 * y - z);
43 return s < 0.02 ? 0.0 :
s;
48bool close(
double a,
double b,
double tol) {
49 return std::fabs(a - b) <= tol;
55bool closeArray(
const char* cfg,
const char* key,
const std::vector<double>& d,
56 const std::vector<double>& o,
double rtol) {
57 if (
d.size() != o.size()) {
58 std::printf(
"[%s] FAIL %s: size %zu vs %zu\n", cfg, key,
d.size(), o.size());
61 double scale = 1e-300, dev = 0.0;
63 scale = std::max(scale, std::fabs(x));
65 for (std::size_t i = 0; i < o.size(); ++i) {
66 dev = std::max(dev, std::fabs(d[i] - o[i]));
67 ok =
ok && (rtol == 0.0 ?
d[i] == o[i] : close(d[i], o[i], rtol * scale));
69 std::printf(
"[%s] %s %s: n=%zu max|dev|/max|ref| = %.3e%s\n", cfg, ok ?
"ok " :
"FAIL", key,
70 o.size(), dev / scale, rtol == 0.0 ?
" (bitwise)" :
"");
74int runConfig(
const char* name,
bool withOpen, MPI_Comm comm) {
75 const std::array<int, 3> gd{36, 30, 24};
76 const std::array<float, 3> org{0.5f, -1.0f, 2.0f};
77 const std::array<float, 3> spc{0.5f, 1.0f, 1.5f};
78 const std::array<double, 3> gp{1.0e-3, -2.0e-3, 0.5e-3};
80 MPI_Comm_rank(comm, &rank);
82 std::array<int, 3> bo{}, bs{};
84 const std::size_t nl = std::size_t(bs[0]) * bs[1] * bs[2];
85 std::vector<float> sdfL(nl);
86 std::vector<double> fldL[7];
87 for (
int c = 0; c < 7; ++c)
89 for (
int z = 0;
z < bs[2]; ++
z)
90 for (
int y = 0;
y < bs[1]; ++
y)
91 for (
int x = 0;
x < bs[0]; ++
x) {
92 const std::size_t i = (std::size_t(z) * bs[1] +
y) * bs[0] + x;
93 const int gx = bo[0] +
x, gy = bo[1] +
y, gz = bo[2] +
z;
95 for (
int c = 0; c < 4; ++c)
96 fldL[c][i] = fld(c, gx, gy, gz, gd);
97 for (
int c = 4; c < 7; ++c)
98 fldL[c][i] = fld(4, gx + c, gy, gz, gd);
100 std::vector<double> none;
102 withOpen ? fldL[4] : none, withOpen ? fldL[5] : none,
103 withOpen ? fldL[6] : none, gp, comm);
107 const std::size_t n = std::size_t(gd[0]) * gd[1] * gd[2];
108 std::vector<float> sdfG(n);
109 std::vector<double> fldG[7];
110 for (
int c = 0; c < 7; ++c)
112 for (
int z = 0;
z < gd[2]; ++
z)
113 for (
int y = 0;
y < gd[1]; ++
y)
114 for (
int x = 0;
x < gd[0]; ++
x) {
115 const std::size_t i = (std::size_t(z) * gd[1] +
y) * gd[0] + x;
117 for (
int c = 0; c < 4; ++c)
118 fldG[c][i] = fld(c, x, y, z, gd);
119 for (
int c = 4; c < 7; ++c)
120 fldG[c][i] = fld(4, x + c, y, z, gd);
123 withOpen ? fldG[4] : none, withOpen ? fldG[5] : none,
124 withOpen ? fldG[6] : none, gp);
126 for (
double q : orc.throat_flow)
127 qs = std::max(qs, std::fabs(q));
128 if (dist.throats != orc.throats) {
129 std::printf(
"[%s] FAIL throat list: %zu vs %zu\n", name, dist.throats.size(),
133 for (std::size_t t = 0; t < orc.throats.size() && !fail; ++t) {
134 if (!close(dist.throat_flow[t], orc.throat_flow[t], 1e-10 * qs) ||
135 !close(dist.throat_area[t], orc.throat_area[t], 1e-10 * qs) ||
136 !close(dist.throat_dp[t], orc.throat_dp[t], 1e-8)) {
137 std::printf(
"[%s] FAIL throat %zu: Q %.15e vs %.15e A %.15e vs %.15e dp %g vs %g\n",
138 name, t, dist.throat_flow[t], orc.throat_flow[t], dist.throat_area[t],
139 orc.throat_area[t], dist.throat_dp[t], orc.throat_dp[t]);
144 if (dist.pores.size() != orc.pores.size()) {
145 std::printf(
"[%s] FAIL pore count %zu vs %zu\n", name, dist.pores.size(), orc.pores.size());
148 for (std::size_t k = 0; k < orc.pores.size() && !fail; ++k) {
149 const Pore &a = dist.pores[k], &b = orc.pores[k];
150 if (a.
radius != b.radius || !close(a.
x, b.x, 1e-5 * spc[0]) ||
151 !close(a.
y, b.y, 1e-5 * spc[1]) || !close(a.
z, b.z, 1e-5 * spc[2]) ||
152 !close(dist.pore_pressure[k], orc.pore_pressure[k], 1e-9) ||
153 !close(dist.pore_residual[k], orc.pore_residual[k], 1e-10 * qs)) {
154 std::printf(
"[%s] FAIL pore %zu (r %.9g vs %.9g, p %.15e vs %.15e)\n", name, k, a.
radius,
155 b.radius, dist.pore_pressure[k], orc.pore_pressure[k]);
162 const double rt = 1e-10;
163 bool ok = closeArray(name,
"throat_momentum_flux_zyx", dist.throat_momentum_flux_zyx,
164 orc.throat_momentum_flux_zyx, rt);
165 ok &= closeArray(name,
"throat_kinetic_energy_flux", dist.throat_kinetic_energy_flux,
166 orc.throat_kinetic_energy_flux, rt);
167 ok &= closeArray(name,
"throat_pressure_work_flux", dist.throat_pressure_work_flux,
168 orc.throat_pressure_work_flux, rt);
170 closeArray(name,
"throat_normal_zyx", dist.throat_normal_zyx, orc.throat_normal_zyx, rt);
171 ok &= closeArray(name,
"throat_centroid_zyx", dist.throat_centroid_zyx,
172 orc.throat_centroid_zyx, rt);
173 ok &= closeArray(name,
"throat_dp_mean", dist.throat_dp_mean, orc.throat_dp_mean, rt);
174 ok &= closeArray(name,
"pore_volume", dist.pore_volume, orc.pore_volume, 0.0);
175 ok &= closeArray(name,
"pore_mean_pressure", dist.pore_mean_pressure, orc.pore_mean_pressure,
177 ok &= closeArray(name,
"pore_mean_kinetic_energy", dist.pore_mean_kinetic_energy,
178 orc.pore_mean_kinetic_energy, rt);
179 ok &= closeArray(name,
"pore_mean_velocity_zyx", dist.pore_mean_velocity_zyx,
180 orc.pore_mean_velocity_zyx, rt);
181 ok &= closeArray(name,
"pore_volume_centroid_zyx", dist.pore_volume_centroid_zyx,
182 orc.pore_volume_centroid_zyx, 0.0);
186 std::printf(
"[%s] PASS: %zu pores, %zu throats match the single-rank oracle\n", name,
187 orc.pores.size(), orc.throats.size());
189 MPI_Bcast(&fail, 1, MPI_INT, 0, comm);
195int main(
int argc,
char** argv) {
196 MPI_Init(&argc, &argv);
199 Kokkos::ScopeGuard kokkos(argc, argv);
200 fail += runConfig(
"openness",
true, MPI_COMM_WORLD);
201 fail += runConfig(
"fully_open",
false, MPI_COMM_WORLD);
float sphereLatticeSdf(int gx, int gy, int gz, std::array< int, 3 > gd)
2x2x2 lattice of SOLID spheres in a periodic box (min-image distance); pores at the interstices.
NetworkFlow extract_network_flow_k(const std::vector< float > &sdf_h, std::array< int, 3 > resolution, std::array< float, 3 > origin, std::array< float, 3 > spacing, const std::vector< double > &u_h, const std::vector< double > &v_h, const std::vector< double > &w_h, const std::vector< double > &p_h, const std::vector< double > &ox_h, const std::vector< double > &oy_h, const std::vector< double > &oz_h, std::array< double, 3 > grad_p)
NetworkFlow extract_network_flow_mpi(const std::vector< float > &sdf_local, std::array< int, 3 > gdims, std::array< float, 3 > origin, std::array< float, 3 > spacing, const std::vector< double > &u_h, const std::vector< double > &v_h, const std::vector< double > &w_h, const std::vector< double > &p_h, const std::vector< double > &ox_h, const std::vector< double > &oy_h, const std::vector< double > &oz_h, std::array< double, 3 > grad_p, MPI_Comm comm)
void mpi_block_of(std::array< int, 3 > gdims, MPI_Comm comm, std::array< int, 3 > &offset, std::array< int, 3 > &size)
This rank's ORB block of the global grid (deterministic; same partition as flow/dem): offset = the bl...
int main(int argc, char **argv)