-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathadvection_1d.cpp
232 lines (194 loc) · 8.55 KB
/
advection_1d.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
224
225
226
227
228
229
230
231
232
// Copyright 2018-2025 the samurai's authors
// SPDX-License-Identifier: BSD-3-Clause
#include <xtensor/xfixed.hpp>
#include <samurai/algorithm.hpp>
#include <samurai/bc.hpp>
#include <samurai/field.hpp>
#include <samurai/io/hdf5.hpp>
#include <samurai/io/restart.hpp>
#include <samurai/mr/adapt.hpp>
#include <samurai/mr/mesh.hpp>
#include <samurai/samurai.hpp>
#include <samurai/stencil_field.hpp>
#include <samurai/subset/node.hpp>
#include <filesystem>
namespace fs = std::filesystem;
template <class Field>
void init(Field& u)
{
auto& mesh = u.mesh();
u.resize();
samurai::for_each_cell(mesh,
[&](auto& cell)
{
auto center = cell.center();
const double radius = .2;
const double x_center = 0;
if (std::abs(center[0] - x_center) <= radius)
{
u[cell] = 1;
}
});
}
template <class Field>
void flux_correction(double dt, double a, const Field& u, Field& unp1)
{
using mesh_t = typename Field::mesh_t;
using mesh_id_t = typename mesh_t::mesh_id_t;
using interval_t = typename mesh_t::interval_t;
constexpr std::size_t dim = Field::dim;
auto mesh = u.mesh();
for (std::size_t level = mesh.min_level(); level < mesh.max_level(); ++level)
{
xt::xtensor_fixed<int, xt::xshape<dim>> stencil;
stencil = {{-1}};
auto subset_right = samurai::intersection(samurai::translate(mesh[mesh_id_t::cells][level + 1], stencil),
mesh[mesh_id_t::cells][level])
.on(level);
subset_right(
[&](const auto& i, auto)
{
const double dx = mesh.cell_length(level);
unp1(level, i) = unp1(level, i)
- dt / dx
* (-samurai::upwind_op<dim, interval_t>(level, i).right_flux(a, u)
+ samurai::upwind_op<dim, interval_t>(level + 1, 2 * i + 1).right_flux(a, u));
});
stencil = {{1}};
auto subset_left = samurai::intersection(samurai::translate(mesh[mesh_id_t::cells][level + 1], stencil),
mesh[mesh_id_t::cells][level])
.on(level);
subset_left(
[&](const auto& i, auto)
{
const double dx = mesh.cell_length(level);
unp1(level, i) = unp1(level, i)
- dt / dx
* (samurai::upwind_op<dim, interval_t>(level, i).left_flux(a, u)
- samurai::upwind_op<dim, interval_t>(level + 1, 2 * i).left_flux(a, u));
});
}
}
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_);
samurai::dump(path, fmt::format("{}_restart{}", filename, suffix), mesh, u);
}
int main(int argc, char* argv[])
{
auto& app = samurai::initialize("Finite volume example for the advection equation in 1d using multiresolution", argc, argv);
constexpr std::size_t dim = 1; // cppcheck-suppress unreadVariable
using Config = samurai::MRConfig<dim>;
// Simulation parameters
double left_box = -2;
double right_box = 2;
bool is_periodic = false;
double a = 1.;
double Tf = 1.;
double cfl = 0.95;
double t = 0.;
std::string restart_file;
// Multiresolution parameters
std::size_t min_level = 6;
std::size_t max_level = 12;
double mr_epsilon = 2.e-4; // Threshold used by multiresolution
double mr_regularity = 1.; // Regularity guess for multiresolution
bool correction = false;
// Output parameters
fs::path path = fs::current_path();
std::string filename = "FV_advection_1d";
std::size_t nfiles = 1;
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_flag("--periodic", is_periodic, "Set the domain periodic")->capture_default_str()->group("Simulation parameters");
app.add_option("--velocity", a, "The velocity of the advection equation")->capture_default_str()->group("Simulation parameters");
app.add_option("--cfl", cfl, "The CFL")->capture_default_str()->group("Simulation parameters");
app.add_option("--Ti", t, "Initial time")->capture_default_str()->group("Simulation parameters");
app.add_option("--Tf", Tf, "Final time")->capture_default_str()->group("Simulation parameters");
app.add_option("--restart-file", restart_file, "Restart file")->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("--with-correction", correction, "Apply flux correction at the interface of two refinement levels")
->capture_default_str()
->group("Multiresolution");
app.add_option("--path", path, "Output path")->capture_default_str()->group("Output");
app.add_option("--filename", filename, "File name prefix")->capture_default_str()->group("Output");
app.add_option("--nfiles", nfiles, "Number of output files")->capture_default_str()->group("Output");
SAMURAI_PARSE(argc, argv);
const samurai::Box<double, dim> box({left_box}, {right_box});
samurai::MRMesh<Config> mesh;
auto u = samurai::make_field<double, 1>("u", mesh);
if (restart_file.empty())
{
mesh = {box, min_level, max_level, std::array<bool, dim>{is_periodic}};
init(u);
}
else
{
samurai::load(restart_file, mesh, u);
}
double dt = cfl * mesh.cell_length(max_level);
const double dt_save = Tf / static_cast<double>(nfiles);
if (!is_periodic)
{
const xt::xtensor_fixed<int, xt::xshape<1>> left{-1};
const xt::xtensor_fixed<int, xt::xshape<1>> right{1};
samurai::make_bc<samurai::Dirichlet<1>>(u, 0.)->on(left, right);
// same as (just to test OnDirection instead of Everywhere)
// samurai::make_bc<samurai::Dirichlet<1>>(u, 0.);
}
auto unp1 = samurai::make_field<double, 1>("unp1", mesh);
auto MRadaptation = samurai::make_MRAdapt(u);
MRadaptation(mr_epsilon, mr_regularity);
save(path, filename, u, "_init");
std::size_t nsave = 1;
std::size_t nt = 0;
while (t != Tf)
{
MRadaptation(mr_epsilon, mr_regularity);
t += dt;
if (t > Tf)
{
dt += Tf - t;
t = Tf;
}
std::cout << fmt::format("iteration {}: t = {}, dt = {}", nt++, t, dt) << std::endl;
samurai::update_ghost_mr(u);
unp1.resize();
unp1.fill(0);
unp1 = u - dt * samurai::upwind(a, u);
if (correction)
{
flux_correction(dt, a, u, unp1);
}
std::swap(u.array(), unp1.array());
if (t >= static_cast<double>(nsave + 1) * dt_save || t == Tf)
{
const std::string suffix = (nfiles != 1) ? fmt::format("_ite_{}", nsave++) : "";
save(path, filename, u, suffix);
}
}
samurai::finalize();
return 0;
}