-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathburgers_mra.cpp
321 lines (267 loc) · 10.7 KB
/
burgers_mra.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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
// Copyright 2021 SAMURAI TEAM. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
#include <CLI/CLI.hpp>
#include <samurai/io/hdf5.hpp>
#include <samurai/mr/adapt.hpp>
#include <samurai/mr/mesh.hpp>
#include <samurai/samurai.hpp>
#include <samurai/schemes/fv.hpp>
#include <algorithm>
#include <filesystem>
namespace fs = std::filesystem;
double hat_exact_solution(double x, double t)
{
const double x0 = -1;
double x_top;
double x1;
double top;
if (t < 1)
{
x_top = t;
x1 = 1;
top = 1;
}
else
{
x_top = std::sqrt(2 * (1 + t)) - 1;
x1 = x_top;
top = std::sqrt(2 / (1 + t));
}
if (x > x0 && x < x_top)
{
double a = top / (x_top - x0);
return a * (x - x0);
}
else if (x >= x_top && x < x1)
{
double a = -top / (x1 - x_top);
return a * (x - x_top) + top;
}
return 0.;
}
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_);
}
template <class Field, class Scheme>
void run_simulation(Field& u,
Field& unp1,
Field& u_max,
Field& unp1_max,
Scheme& scheme,
double cfl,
double mr_epsilon,
double mr_regularity,
const std::string& init_sol,
std::size_t nfiles,
const fs::path& path,
std::string filename,
std::size_t& nsave)
{
auto& mesh = u.mesh();
std::cout << std::endl << "max-level-flux enabled: " << scheme.enable_max_level_flux() << std::endl;
if (scheme.enable_max_level_flux())
{
filename += "_mlf";
}
// Initial solution
if (init_sol == "hat")
{
samurai::for_each_cell(mesh,
[&](auto& cell)
{
u[cell] = hat_exact_solution(cell.center(0), 0);
});
samurai::for_each_cell(u_max.mesh(),
[&](auto& cell)
{
u_max[cell] = hat_exact_solution(cell.center(0), 0);
});
}
else // gaussian
{
samurai::for_each_cell(mesh,
[&](auto& cell)
{
double x = cell.center(0);
u[cell] = 0.1 * exp(-2.0 * x * x);
});
samurai::for_each_cell(u_max.mesh(),
[&](auto& cell)
{
double x = cell.center(0);
u_max[cell] = 0.1 * exp(-2.0 * x * x);
});
}
nsave = 0;
std::size_t nt = 0;
if (nfiles > 1)
{
std::string suffix = fmt::format("_ite_{}", nsave);
save(path, filename, u, suffix);
samurai::update_ghost_mr(u);
auto u_recons = samurai::reconstruction(u);
std::string file = fmt::format("{}_recons_ite_{}", filename, nsave);
samurai::save(path, file, u_recons.mesh(), u_recons);
nsave++;
}
//--------------------//
// Time iteration //
//--------------------//
double Tf = init_sol == "hat" ? 4. : 16.;
if (nfiles == 1)
{
Tf = 0.1; // for automatic test
}
double dx = mesh.cell_length(mesh.max_level());
double dt = cfl * dx;
auto MRadaptation = samurai::make_MRAdapt(u);
MRadaptation(mr_epsilon, mr_regularity);
double dt_save = Tf / static_cast<double>(nfiles);
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();
unp1 = u - dt * scheme(u);
unp1_max = u_max - dt * scheme(u_max);
// u <-- unp1
std::swap(u.array(), unp1.array());
std::swap(u_max.array(), unp1_max.array());
// Reconstruction
samurai::update_ghost_mr(u);
auto u_recons = samurai::reconstruction(u);
// Error
std::cout << ", L2-error: " << std::scientific;
std::cout.precision(2);
if (init_sol == "hat")
{
double error = samurai::L2_error(u,
[&](const auto& coord)
{
return hat_exact_solution(coord[0], t);
});
std::cout << "[w.r.t. exact, no recons] " << error;
error = samurai::L2_error(u_recons,
[&](const auto& coord)
{
return hat_exact_solution(coord[0], t);
});
std::cout << ", [w.r.t. exact, recons] " << error;
}
double error = 0;
samurai::for_each_cell(u_max.mesh(),
[&](auto& cell)
{
auto cell2 = samurai::find_cell(u_recons.mesh(), cell.center());
error += std::pow(u_max[cell] - u_recons[cell2], 2) * std::pow(cell.length, 1);
});
error = std::sqrt(error);
std::cout << ", [w.r.t. max level, recons] " << error;
// Save the result
if (t >= static_cast<double>(nsave + 1) * dt_save || t == Tf)
{
std::string suffix = (nfiles != 1) ? fmt::format("_ite_{}", nsave) : "";
save(path, filename, u, suffix);
std::string file = (nfiles != 1) ? fmt::format("{}_recons_ite_{}", filename, nsave) : fmt::format("{}_recons", filename);
samurai::save(path, file, u_recons.mesh(), u_recons);
nsave++;
}
std::cout << std::endl;
}
}
int main(int argc, char* argv[])
{
constexpr std::size_t dim = 1;
using Config = samurai::MRConfig<dim, 2, 2>;
using Box = samurai::Box<double, dim>;
auto& app = samurai::initialize("Finite volume example for the Burgers equation in 1d", argc, argv);
std::cout << "------------------------- Burgers -------------------------" << std::endl;
//--------------------//
// Program parameters //
//--------------------//
// Simulation parameters
double left_box = -2;
double right_box = 3;
std::string init_sol = "hat";
// Time integration
double cfl = 0.95;
// Multiresolution parameters
std::size_t min_level = 1;
std::size_t max_level = 7;
double mr_epsilon = 1e-5; // Threshold used by multiresolution
double mr_regularity = 0.; // Regularity guess for multiresolution
// Output parameters
fs::path path = fs::current_path();
std::string filename = "burgers_mra";
std::size_t nfiles = 50;
app.add_option("--init-sol", init_sol, "Initial solution: hat/gaussian")->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();
SAMURAI_PARSE(argc, argv);
//--------------------//
// Problem definition //
//--------------------//
Box box({left_box}, {right_box});
samurai::MRMesh<Config> mesh{box, min_level, max_level};
samurai::MRMesh<Config> max_level_mesh{box, max_level, max_level};
auto u = samurai::make_field<1>("u", mesh);
auto unp1 = samurai::make_field<1>("unp1", mesh);
auto u_max = samurai::make_field<1>("u", max_level_mesh);
auto unp1_max = samurai::make_field<1>("unp1", max_level_mesh);
filename += "_" + init_sol;
// Boundary conditions
samurai::make_bc<samurai::Dirichlet<1>>(u, 0.0);
samurai::make_bc<samurai::Dirichlet<1>>(u_max, 0.0);
auto scheme = 0.5 * samurai::make_convection_upwind<decltype(u)>();
//-----------------//
// Run simulations //
//-----------------//
std::size_t nsave;
scheme.enable_max_level_flux(false);
run_simulation(u, unp1, u_max, unp1_max, scheme, cfl, mr_epsilon, mr_regularity, init_sol, nfiles, path, filename, nsave);
scheme.enable_max_level_flux(true);
run_simulation(u, unp1, u_max, unp1_max, scheme, cfl, mr_epsilon, mr_regularity, init_sol, nfiles, path, filename, nsave);
std::cout << std::endl;
std::cout << "Run the following commands to view the results:" << std::endl;
std::cout << "max-level-flux disabled:" << std::endl;
std::cout << " python ../python/read_mesh.py " << filename << "_recons_ite_ --field u --start 0 --end " << nsave << std::endl;
std::cout << "max-level-flux enabled:" << std::endl;
std::cout << " python ../python/read_mesh.py " << filename << "_mlf_recons_ite_ --field u --start 0 --end " << nsave << std::endl;
samurai::finalize();
return 0;
}