-
Notifications
You must be signed in to change notification settings - Fork 15
/
linear_convection.cpp
223 lines (189 loc) · 7.67 KB
/
linear_convection.cpp
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
// Copyright 2018-2024 the samurai's authors
// SPDX-License-Identifier: BSD-3-Clause
#include <CLI/CLI.hpp>
#include <samurai/hdf5.hpp>
#include <samurai/mr/adapt.hpp>
#include <samurai/mr/mesh.hpp>
#include <samurai/petsc.hpp>
#include <samurai/samurai.hpp>
#include <filesystem>
namespace fs = std::filesystem;
template <class Field>
void save(const fs::path& path, const std::string& filename, const Field& u, const std::string& suffix = "")
{
auto mesh = u.mesh();
auto level_ = samurai::make_field<std::size_t, 1>("level", mesh);
if (!fs::exists(path))
{
fs::create_directory(path);
}
samurai::for_each_cell(mesh,
[&](const auto& cell)
{
level_[cell] = cell.level;
});
samurai::save(path, fmt::format("{}{}", filename, suffix), mesh, u, level_);
}
int main(int argc, char* argv[])
{
samurai::initialize(argc, argv);
static constexpr std::size_t dim = 2;
using Config = samurai::MRConfig<dim, 3>;
using Box = samurai::Box<double, dim>;
using point_t = typename Box::point_t;
std::cout << "------------------------- Linear convection -------------------------" << std::endl;
//--------------------//
// Program parameters //
//--------------------//
// Simulation parameters
double left_box = -1;
double right_box = 1;
// Time integration
double Tf = 3;
double dt = 0;
double cfl = 0.95;
// Multiresolution parameters
std::size_t min_level = 1;
std::size_t max_level = dim == 1 ? 6 : 4;
double mr_epsilon = 1e-4; // Threshold used by multiresolution
double mr_regularity = 1.; // Regularity guess for multiresolution
// Output parameters
fs::path path = fs::current_path();
std::string filename = "linear_convection_" + std::to_string(dim) + "D";
std::size_t nfiles = 0;
CLI::App app{"Finite volume example for the linear convection equation"};
app.add_option("--left", left_box, "The left border of the box")->capture_default_str()->group("Simulation parameters");
app.add_option("--right", right_box, "The right border of the box")->capture_default_str()->group("Simulation parameters");
app.add_option("--Tf", Tf, "Final time")->capture_default_str()->group("Simulation parameters");
app.add_option("--dt", dt, "Time step")->capture_default_str()->group("Simulation parameters");
app.add_option("--cfl", cfl, "The CFL")->capture_default_str()->group("Simulation parameters");
app.add_option("--min-level", min_level, "Minimum level of the multiresolution")->capture_default_str()->group("Multiresolution");
app.add_option("--max-level", max_level, "Maximum level of the multiresolution")->capture_default_str()->group("Multiresolution");
app.add_option("--mr-eps", mr_epsilon, "The epsilon used by the multiresolution to adapt the mesh")
->capture_default_str()
->group("Multiresolution");
app.add_option("--mr-reg",
mr_regularity,
"The regularity criteria used by the multiresolution to "
"adapt the mesh")
->capture_default_str()
->group("Multiresolution");
app.add_option("--path", path, "Output path")->capture_default_str()->group("Ouput");
app.add_option("--filename", filename, "File name prefix")->capture_default_str()->group("Ouput");
app.add_option("--nfiles", nfiles, "Number of output files")->capture_default_str()->group("Ouput");
app.allow_extras();
CLI11_PARSE(app, argc, argv);
//--------------------//
// Problem definition //
//--------------------//
point_t box_corner1, box_corner2;
box_corner1.fill(left_box);
box_corner2.fill(right_box);
Box box(box_corner1, box_corner2);
std::array<bool, dim> periodic;
periodic.fill(true);
samurai::MRMesh<Config> mesh{box, min_level, max_level, periodic};
// Initial solution
auto u = samurai::make_field<1>("u",
mesh,
[](const auto& coords)
{
if constexpr (dim == 1)
{
auto& x = coords(0);
return (x >= -0.8 && x <= -0.3) ? 1. : 0.;
}
else
{
auto& x = coords(0);
auto& y = coords(1);
return (x >= -0.8 && x <= -0.3 && y >= 0.3 && y <= 0.8) ? 1. : 0.;
}
});
auto unp1 = samurai::make_field<1>("unp1", mesh);
// Intermediary fields for the RK3 scheme
auto u1 = samurai::make_field<1>("u1", mesh);
auto u2 = samurai::make_field<1>("u2", mesh);
unp1.fill(0);
u1.fill(0);
u2.fill(0);
// Convection operator
samurai::VelocityVector<dim> velocity;
velocity.fill(1);
if constexpr (dim == 2)
{
velocity(1) = -1;
}
auto conv = samurai::make_convection_weno5<decltype(u)>(velocity);
//--------------------//
// Time iteration //
//--------------------//
if (dt == 0)
{
double dx = mesh.cell_length(max_level);
auto a = xt::abs(velocity);
double sum_velocities = xt::sum(xt::abs(velocity))();
dt = cfl * dx / sum_velocities;
}
auto MRadaptation = samurai::make_MRAdapt(u);
MRadaptation(mr_epsilon, mr_regularity);
double dt_save = nfiles == 0 ? dt : Tf / static_cast<double>(nfiles);
std::size_t nsave = 0, nt = 0;
if (nfiles != 1)
{
std::string suffix = (nfiles != 1) ? fmt::format("_ite_{}", nsave++) : "";
save(path, filename, u, suffix);
}
double t = 0;
while (t != Tf)
{
// Move to next timestep
t += dt;
if (t > Tf)
{
dt += Tf - t;
t = Tf;
}
std::cout << fmt::format("iteration {}: t = {:.2f}, dt = {}", nt++, t, dt) << std::flush;
// Mesh adaptation
MRadaptation(mr_epsilon, mr_regularity);
samurai::update_ghost_mr(u);
unp1.resize();
u1.resize();
u2.resize();
u1.fill(0);
u2.fill(0);
// unp1 = u - dt * conv(u);
// TVD-RK3 (SSPRK3)
u1 = u - dt * conv(u);
samurai::update_ghost_mr(u1);
u2 = 3. / 4 * u + 1. / 4 * (u1 - dt * conv(u1));
samurai::update_ghost_mr(u2);
unp1 = 1. / 3 * u + 2. / 3 * (u2 - dt * conv(u2));
// u <-- unp1
std::swap(u.array(), unp1.array());
// Save the result
if (nfiles == 0 || t >= static_cast<double>(nsave + 1) * dt_save || t == Tf)
{
if (nfiles != 1)
{
std::string suffix = (nfiles != 1) ? fmt::format("_ite_{}", nsave++) : "";
save(path, filename, u, suffix);
}
else
{
save(path, filename, u);
}
}
std::cout << std::endl;
}
if constexpr (dim == 1)
{
std::cout << std::endl;
std::cout << "Run the following command to view the results:" << std::endl;
std::cout << "python <<path to samurai>>/python/read_mesh.py " << filename << "_ite_ --field u level --start 0 --end " << nsave
<< std::endl;
}
samurai::finalize();
return 0;
}