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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
|
#include "../descriptor.h"
#include <forstio/codec/data.h>
#include <iostream>
namespace kel {
namespace lbm {
namespace schema {
/**
* Basic distribution function
* Base type
* D
* Q
* Scalar factor
* D factor
* Q factor
*/
using T = Float32;
using DfCell2DType = CellType<T, 2, 5, 0, 0, 1>;
using CellInfo2DType = CellType<UInt8, 2, 5, 1, 0, 0>;
/**
* Basic type for simulation
*/
using Cell = CellData<
Member<DfCell2DType, "dfs">,
Member<CellInfo2DType, "info">
>;
}
template<size_t D, size_t Q>
class collision {
public:
typename saw::native_data_type<schema::T>::type relaxation_;
public:
std::array<typename saw::native_data_type<schema::T>::type,Q> equilibrium(
typename saw::native_data_type<schema::T>::type rho,
std::array<typename saw::native_data_type<schema::T>::type, D> vel
){
using dfi = df_info<schema::Descriptor<schema::T, D, Q>>;
typename std::array<saw::native_data_type<schema::T>::type,Q> eq;
for(std::size_t i = 0; i < eq.size(); ++i){
auto vel_c = (vel[0]*dfi::directions[i][0] + vel[1]*dfi::directions[i][1]);
auto vel_c_cs2 = vel_c / dfi::cs2;
eq[i] = dfi::weights[i] * rho * (
1
+ vel_c_cs2
+ vel_c_cs2 * vel_c_cs2
- ( vel[0] * vel[0] + vel[1] * vel[1] ) / ( 2. * dfi::cs2 )
);
}
return eq;
}
void compute_rho_u(
saw::data<schema::DfCell2DType>& dfs,
typename saw::native_data_type<schema::T>::type& rho,
std::array<typename saw::native_data_type<schema::T>::type, 2>& vel
){
using dfi = df_info<schema::Descriptor<schema::T, D, Q>>;
rho = 0;
std::fill(vel.begin(), vel.end(), 0);
for(size_t i = 0; i < Q; ++i){
rho += dfs[i];
vel[0] += dfi::directions[i][0] * dfs.at(i).get();
vel[1] += dfi::directions[i][1] * dfs.at(i).get();
}
vel[0] /= rho;
vel[1] /= rho;
}
};
}
}
constexpr size_t dim_size = 2;
constexpr size_t dim_x = 32;
constexpr size_t dim_y = 32;
struct rectangle {
std::array<size_t,4> data_;
rectangle(size_t x, size_t y, size_t w, size_t h):
data_{x,y,w,h}
{}
bool inside(size_t i, size_t j) const {
return !(i < data_[0] || i > (data_[0]+data_[2]) || j < data_[1] || j > (data_[1] +data_[3]));
}
};
template<typename Func, typename Schema, size_t Dim>
void apply_for_cells(Func&& func, saw::data<saw::schema::Array<Schema, Dim>>& dat){
for(std::size_t i = 0; i < dat.get_dim_size(0); ++i){
for(std::size_t j = 0; j < dat.get_dim_size(1); ++j){
func(dat.at(i,j), i, j);
}
}
}
void set_geometry(saw::data<kel::lbm::schema::Lattice<kel::lbm::schema::Cell,2>>& latt){
using namespace kel::lbm;
apply_for_cells([](auto& cell, std::size_t i, std::size_t j){
uint8_t val = 0;
if(i == 1){
val = 2;
}
if(j == 1 || (i+2) == dim_x || (j+2) == dim_y){
val = 3;
}
if(i == 0 || j == 0 || (i+1) == dim_x || (j+1) == dim_y){
val = 1;
}
cell.template get<"info">().at(0).set(val);
}, latt);
}
void set_initial_conditions(saw::data<kel::lbm::schema::Lattice<kel::lbm::schema::Cell,2>>& latt){
using namespace kel::lbm;
apply_for_cells([](auto& cell, std::size_t i, std::size_t j){
cell.template get<"dfs">().at(0).set(1.0);
}, latt);
}
int main(){
using namespace kel::lbm;
saw::data<schema::Lattice<kel::lbm::schema::Cell,2>, saw::encode::Native> lattice{dim_x, dim_y};
/**
* Set meta information describing what this cell is
*/
set_geometry(lattice);
/**
*
*/
set_initial_conditions(lattice);
/**
* Timeloop
*/
/**
* Print basic setup info
*/
apply_for_cells([](auto& cell, std::size_t i, std::size_t j){
// Not needed
(void) i;
std::cout<<static_cast<uint32_t>(cell.template get<"info">().at(0).get());
if( (j+1) < dim_y){
std::cout<<" ";
}else{
std::cout<<"\n";
}
}, lattice);
std::cout<<"\n";
apply_for_cells([](auto& cell, std::size_t i, std::size_t j){
// Not needed
(void) i;
std::cout<<cell.template get<"dfs">().at(0).get();
if( (j+1) < dim_y){
std::cout<<" ";
}else{
std::cout<<"\n";
}
}, lattice);
std::cout<<std::endl;
return 0;
}
|