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
|
#include <forstio/test/suite.hpp>
#include <cmath>
#include "../c++/macroscopic.hpp"
#include "../c++/equilibrium.hpp"
namespace {
namespace sch {
using namespace kel::lbm::sch;
using T = Float64;
}
/**
* The macro field used to be called "momentum" while holding velocity:
* compute_rho_u divides the first moment by the density (macroscopic.hpp).
* These pin that the name now matches the content, so the rename cannot be
* quietly undone by removing the division.
*/
template<typename Desc>
void roundtrip(double rho_in, double ux_in){
using namespace kel;
lbm::data<sch::Scalar<sch::T>> rho;
rho.at({}).set(rho_in);
lbm::data<sch::Vector<sch::T,Desc::D>> vel;
for(uint64_t i = 0u; i < Desc::D; ++i){
vel.at({{i}}).set(0.0);
}
vel.at({{0u}}).set(ux_in);
auto dfs = lbm::equilibrium<sch::T,Desc>(rho,vel);
lbm::data<sch::Scalar<sch::T>> rho_out;
lbm::data<sch::Vector<sch::T,Desc::D>> vel_out;
lbm::compute_rho_u<sch::T,Desc>(dfs,rho_out,vel_out);
SAW_EXPECT(std::abs(rho_out.at({}).get() - rho_in) < 1e-12,
"density must round trip through equilibrium");
SAW_EXPECT(std::abs(vel_out.at({{0u}}).get() - ux_in) < 1e-12,
"velocity must round trip through equilibrium");
}
SAW_TEST("Macroscopic / the field holds velocity, not momentum"){
using namespace kel;
using Desc = sch::D2Q9;
// Density deliberately not 1, which is the only case where velocity and
// momentum differ numerically.
const double rho_in = 2.0;
const double ux_in = 0.1;
lbm::data<sch::Scalar<sch::T>> rho;
rho.at({}).set(rho_in);
lbm::data<sch::Vector<sch::T,Desc::D>> vel;
vel.at({{0u}}).set(ux_in);
vel.at({{1u}}).set(0.0);
auto dfs = lbm::equilibrium<sch::T,Desc>(rho,vel);
lbm::data<sch::Scalar<sch::T>> rho_out;
lbm::data<sch::Vector<sch::T,Desc::D>> vel_out;
lbm::compute_rho_u<sch::T,Desc>(dfs,rho_out,vel_out);
const double got = vel_out.at({{0u}}).get();
SAW_EXPECT(std::abs(got - ux_in) < 1e-12, "must be the velocity u");
// rho*u would be 0.2 here. If the division by rho were dropped, this is
// the assertion that would catch it.
SAW_EXPECT(std::abs(got - rho_in * ux_in) > 1e-3,
"must NOT be the momentum rho*u");
}
SAW_TEST("Macroscopic / round trip D2Q9"){
roundtrip<sch::D2Q9>(1.0, 0.05);
roundtrip<sch::D2Q9>(2.0, 0.05);
}
SAW_TEST("Macroscopic / round trip D2Q5"){
roundtrip<sch::Descriptor<2,5>>(1.0, 0.0);
}
SAW_TEST("Macroscopic / round trip D3Q27"){
// The 3D path divides by rho too -- it is the same loop.
roundtrip<sch::D3Q27>(1.0, 0.05);
roundtrip<sch::D3Q27>(1.7, 0.03);
}
SAW_TEST("Macroscopic / a quiescent field has zero velocity at any density"){
using namespace kel;
using Desc = sch::D2Q9;
for(double r : {0.5, 1.0, 3.0}){
lbm::data<sch::Scalar<sch::T>> rho;
rho.at({}).set(r);
lbm::data<sch::Vector<sch::T,Desc::D>> vel;
vel.at({{0u}}).set(0.0);
vel.at({{1u}}).set(0.0);
auto dfs = lbm::equilibrium<sch::T,Desc>(rho,vel);
lbm::data<sch::Scalar<sch::T>> rho_out;
lbm::data<sch::Vector<sch::T,Desc::D>> vel_out;
lbm::compute_rho_u<sch::T,Desc>(dfs,rho_out,vel_out);
SAW_EXPECT(std::abs(rho_out.at({}).get() - r) < 1e-12, "density preserved");
SAW_EXPECT(std::abs(vel_out.at({{0u}}).get()) < 1e-12, "no x velocity at rest");
SAW_EXPECT(std::abs(vel_out.at({{1u}}).get()) < 1e-12, "no y velocity at rest");
}
}
}
|