-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathreconstruction_1d.cpp
143 lines (119 loc) · 5.59 KB
/
reconstruction_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
// Copyright 2018-2025 the samurai's authors
// SPDX-License-Identifier: BSD-3-Clause
#include <chrono>
#include <filesystem>
#include <samurai/algorithm.hpp>
#include <samurai/field.hpp>
#include <samurai/io/hdf5.hpp>
#include <samurai/mr/adapt.hpp>
#include <samurai/mr/mesh.hpp>
#include <samurai/reconstruction.hpp>
#include <samurai/samurai.hpp>
#include <samurai/uniform_mesh.hpp>
namespace fs = std::filesystem;
enum class Case : int
{
abs,
exp,
tanh
};
using namespace samurai::math;
template <class Mesh>
auto init(Mesh& mesh, Case& c)
{
using mesh_id_t = typename Mesh::mesh_id_t;
auto u = samurai::make_scalar_field<double>("u", mesh);
samurai::for_each_interval(mesh[mesh_id_t::cells],
[&](std::size_t level, const auto& i, const auto&)
{
const double dx = mesh.cell_length(level);
auto x = mesh.origin_point()[0] + dx * arange<double>(i.start, i.end) + 0.5 * dx;
switch (c)
{
case Case::abs:
u(level, i) = abs(x);
break;
case Case::exp:
u(level, i) = exp(-100 * x * x);
break;
case Case::tanh:
u(level, i) = tanh(50 * abs(x)) - 1;
break;
}
});
return u;
}
int main(int argc, char* argv[])
{
auto& app = samurai::initialize("1d reconstruction of an adapted solution using multiresolution", argc, argv);
constexpr size_t dim = 1;
constexpr std::size_t max_stencil_width_ = 2;
constexpr std::size_t graduation_width_ = 2;
constexpr std::size_t max_refinement_level_ = samurai::default_config::max_level;
constexpr std::size_t prediction_order_ = 1;
using MRConfig = samurai::MRConfig<dim, max_stencil_width_, graduation_width_, prediction_order_, max_refinement_level_>;
Case test_case{Case::abs};
const std::map<std::string, Case> map{
{"abs", Case::abs },
{"exp", Case::exp },
{"tanh", Case::tanh}
};
// Adaptation parameters
std::size_t min_level = 3;
std::size_t max_level = 8;
double mr_epsilon = 1.e-4; // Threshold used by multiresolution
double mr_regularity = 2.; // Regularity guess for multiresolution
// Output parameters
fs::path path = fs::current_path();
std::string filename = "reconstruction_1d";
app.add_option("--case", test_case, "Test case")->capture_default_str()->transform(CLI::CheckedTransformer(map, CLI::ignore_case));
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("Output");
app.add_option("--filename", filename, "File name prefix")->capture_default_str()->group("Output");
SAMURAI_PARSE(argc, argv);
if (!fs::exists(path))
{
fs::create_directory(path);
}
using MRMesh = samurai::MRMesh<MRConfig>;
using mrmesh_id_t = typename MRMesh::mesh_id_t;
using UConfig = samurai::UniformConfig<dim>;
using UMesh = samurai::UniformMesh<UConfig>;
const samurai::Box<double, dim> box({-1}, {1});
MRMesh mrmesh{box, min_level, max_level, 0, 1};
UMesh umesh{box, max_level, 0, 1};
auto u = init(mrmesh, test_case);
auto u_exact = init(umesh, test_case);
auto MRadaptation = samurai::make_MRAdapt(u);
MRadaptation(mr_epsilon, mr_regularity);
auto level_ = samurai::make_scalar_field<std::size_t>("level", mrmesh);
samurai::for_each_cell(mrmesh[mrmesh_id_t::cells],
[&](const auto& cell)
{
level_[cell] = cell.level;
});
samurai::save(path, filename, mrmesh, u, level_);
auto t1 = std::chrono::high_resolution_clock::now();
auto u_reconstruct = reconstruction(u);
auto t2 = std::chrono::high_resolution_clock::now();
std::cout << "execution time " << std::chrono::duration_cast<std::chrono::microseconds>(t2 - t1).count() << std::endl;
auto error = samurai::make_scalar_field<double>("error", u_reconstruct.mesh());
samurai::for_each_interval(u_reconstruct.mesh(),
[&](std::size_t level, const auto& i, const auto&)
{
error(level, i) = abs(u_reconstruct(level, i) - u_exact(level, i));
});
samurai::save(path, fmt::format("uniform_{}", filename), u_reconstruct.mesh(), u_reconstruct, error);
samurai::finalize();
return 0;
}