summaryrefslogtreecommitdiff
path: root/modules/core/tests/descriptor.cpp
blob: 96d65315a7c05decaf24f86ba1c5385116084fb2 (plain)
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
114
115
116
117
118
119
120
121
122
123
#include <forstio/test/suite.hpp>

#include "../c++/descriptor.hpp"

#include <cmath>


namespace {
/**
 * The lattice moment conditions:
 *
 *   sum_i w_i                 = 1
 *   sum_i w_i e_ia            = 0
 *   sum_i w_i e_ia e_ib       = cs^2 * delta_ab
 *
 * A weight table that violates these still compiles and still passes an
 * opposite-index check, but it makes the equilibrium return the wrong mass and
 * momentum, and the only symptom is a simulation that diverges within a few
 * steps. D3Q27 carried exactly that bug: its six face weights read 1/36 rather
 * than 2/27, so the weights summed to 13/18.
 *
 * The second moment is the useful one -- it pins the weights per shell, where
 * the plain sum only pins them in aggregate.
 */
template<typename Descriptor>
void check_weight_moments(){
	using namespace kel;

	using dfi = lbm::df_info<lbm::sch::Float64, Descriptor>;

	constexpr double tol = 1e-12;

	double sum_w = 0.0;
	for(uint64_t k = 0u; k < Descriptor::Q; ++k){
		sum_w += dfi::weights[k];
	}
	SAW_EXPECT(std::abs(sum_w - 1.0) < tol, "Weights have to sum to 1");

	for(uint64_t a = 0u; a < Descriptor::D; ++a){
		double first = 0.0;
		for(uint64_t k = 0u; k < Descriptor::Q; ++k){
			first += dfi::weights[k] * static_cast<double>(dfi::directions[k][a]);
		}
		SAW_EXPECT(std::abs(first) < tol, "First moment has to vanish");
	}

	for(uint64_t a = 0u; a < Descriptor::D; ++a){
		for(uint64_t b = 0u; b < Descriptor::D; ++b){
			double second = 0.0;
			for(uint64_t k = 0u; k < Descriptor::Q; ++k){
				second +=
					dfi::weights[k]
					* static_cast<double>(dfi::directions[k][a])
					* static_cast<double>(dfi::directions[k][b]);
			}
			const double expected = (a == b) ? static_cast<double>(dfi::cs2) : 0.0;
			SAW_EXPECT(std::abs(second - expected) < tol, "Second moment has to be cs^2 delta_ab");
		}
	}
}

template<typename Descriptor>
void check_opposite_dirs(){
	using namespace kel;

	using dfi = lbm::df_info<lbm::sch::Float32, Descriptor>;

	for(uint64_t k = 0u; k < Descriptor::Q; ++k){
		auto k_inv = dfi::opposite_index[k];

		for(uint64_t i = 0u; i < Descriptor::D; ++i){
			SAW_EXPECT(dfi::directions[k][i] == (-1*dfi::directions[k_inv][i]), "Opposites are inconsistent");
		}
	}
}

SAW_TEST("Opposites and Dirs D1Q3"){
	using namespace kel;
	check_opposite_dirs<lbm::sch::Descriptor<1,3>>();
}

SAW_TEST("Opposites and Dirs D2Q5"){
	using namespace kel;
	check_opposite_dirs<lbm::sch::Descriptor<2,5>>();
}
SAW_TEST("Opposites and Dirs D2Q9"){
	using namespace kel;
	check_opposite_dirs<lbm::sch::Descriptor<2,9>>();
}
SAW_TEST("Opposites and Dirs D3Q19"){
	using namespace kel;
	check_opposite_dirs<lbm::sch::Descriptor<3,19>>();
}
SAW_TEST("Opposites and Dirs D3Q27"){
	using namespace kel;
	check_opposite_dirs<lbm::sch::Descriptor<3,27>>();
}

/**
 * D2Q4 is deliberately absent: it is experimental and its weights sum to 5/6,
 * so it would fail these outright.
 */
SAW_TEST("Weight moments D1Q3"){
	using namespace kel;
	check_weight_moments<lbm::sch::Descriptor<1,3>>();
}
SAW_TEST("Weight moments D2Q5"){
	using namespace kel;
	check_weight_moments<lbm::sch::Descriptor<2,5>>();
}
SAW_TEST("Weight moments D2Q9"){
	using namespace kel;
	check_weight_moments<lbm::sch::Descriptor<2,9>>();
}
SAW_TEST("Weight moments D3Q19"){
	using namespace kel;
	check_weight_moments<lbm::sch::Descriptor<3,19>>();
}
SAW_TEST("Weight moments D3Q27"){
	using namespace kel;
	check_weight_moments<lbm::sch::Descriptor<3,27>>();
}
}