-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain_1d.cpp
More file actions
173 lines (137 loc) · 5.81 KB
/
main_1d.cpp
File metadata and controls
173 lines (137 loc) · 5.81 KB
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
// Copyright 2025 the samurai team
// SPDX-License-Identifier: BSD-3-Clause
#include <samurai/algorithm/update.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 "euler/schemes.hpp"
#include "euler/utils.hpp"
#include "euler/variables.hpp"
double rhoL = 1.;
double pL = 0.4;
double vL = -2.;
double rhoR = 1.;
double pR = 0.4;
double vR = 2.;
void init(auto& u)
{
static constexpr std::size_t dim = std::decay_t<decltype(u)>::dim;
using EulerConsVar = EulerLayout<dim>;
auto& mesh = u.mesh();
u.resize();
auto set_conserved = [](auto&& u, double rho, double p, double v)
{
u[EulerConsVar::rho] = rho;
double norm2 = 0.;
for (std::size_t d = 0; d < dim; ++d)
{
u[EulerConsVar::mom(d)] = rho * v;
norm2 += v * v;
}
u[EulerConsVar::rhoE] = rho * (EOS::stiffened_gas::e(rho, p) + 0.5 * norm2);
};
samurai::for_each_cell(mesh,
[&](auto& cell)
{
auto x = cell.center();
if (x[0] < 0.5)
{
set_conserved(u[cell], rhoL, pL, vL);
}
else
{
set_conserved(u[cell], rhoR, pR, vR);
}
});
}
int main(int argc, char* argv[])
{
constexpr std::size_t dim = 1;
auto& app = samurai::initialize("Euler equations solver", argc, argv);
// Simulation parameters
xt::xtensor_fixed<double, xt::xshape<dim>> min_corner = {0.};
xt::xtensor_fixed<double, xt::xshape<dim>> max_corner = {1.};
double Tf = .15;
double cfl = 0.4;
double t = 0.;
std::string restart_file;
std::string scheme = "hll";
// Output parameters
fs::path path = fs::current_path();
std::string filename = fmt::format("euler_{}d", dim);
std::size_t nfiles = 1;
app.add_option("--min-corner", min_corner, "The min corner of the box")->capture_default_str()->group("Simulation parameters");
app.add_option("--max-corner", max_corner, "The max corner of the box")->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("--scheme", scheme, "Finite volume scheme")
->capture_default_str()
->check(CLI::IsMember({"rusanov", "hll", "hllc"}))
->group("Simulation parameters");
app.add_option("--restart-file", restart_file, "Restart file")->capture_default_str()->group("Simulation parameters");
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);
// Initialize the mesh
const samurai::Box<double, dim> box(min_corner, max_corner);
auto config = samurai::mesh_config<dim>().min_level(8).max_level(8).max_stencil_size(2).disable_minimal_ghost_width();
config.parse_args();
auto mesh = samurai::mra::make_empty_mesh(config);
auto u = samurai::make_vector_field<double, 2 + dim>("euler", mesh);
if (restart_file.empty())
{
mesh = samurai::mra::make_mesh(box, config);
init(u);
}
else
{
samurai::load(restart_file, mesh, u);
}
std::cout << config.min_level() << " " << config.max_level() << std::endl;
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, rhoL, rhoL * (EOS::stiffened_gas::e(rhoL, pL) + 0.5 * vL * vL), rhoL * vL)->on(left);
samurai::make_bc<samurai::Dirichlet<1>>(u, rhoR, rhoR * (EOS::stiffened_gas::e(rhoR, pR) + 0.5 * vR * vR), rhoR * vR)->on(right);
auto unp1 = samurai::make_vector_field<double, 2 + dim>("euler", mesh);
double dx = mesh.cell_length(config.max_level());
const double dt_save = Tf / static_cast<double>(nfiles);
std::size_t nsave = 1;
std::size_t nt = 0;
samurai::save("results", fmt::format("{}_{}_init", filename, scheme), mesh, u);
std::cout << "Using scheme: " << scheme << std::endl;
auto fv_scheme = get_fv_scheme<decltype(u)>(scheme);
auto MRadaptation = samurai::make_MRAdapt(u);
auto mra_config = samurai::mra_config().relative_detail(true);
while (t != Tf)
{
MRadaptation(mra_config);
double dt = cfl * dx / get_max_lambda(u);
t += dt;
if (std::isnan(t))
{
std::cerr << "Error: Time became NaN, stopping simulation" << std::endl;
break;
}
if (t > Tf)
{
dt += Tf - t;
t = Tf;
}
std::cout << fmt::format("iteration {}: t = {}, dt = {}", nt++, t, dt) << std::endl;
unp1.resize();
unp1 = u - dt * fv_scheme(u);
samurai::swap(u, unp1);
if (t >= static_cast<double>(nsave + 1) * dt_save || t == Tf)
{
const std::string suffix = (nfiles != 1) ? fmt::format("_ite_{}", nsave++) : "";
samurai::save("results", fmt::format("{}_{}{}", filename, scheme, suffix), mesh, u);
}
}
samurai::finalize();
return 0;
}