-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathgraduation_case_2.cpp
191 lines (161 loc) · 6.19 KB
/
graduation_case_2.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
// Copyright 2018-2025 the samurai's authors
// SPDX-License-Identifier: BSD-3-Clause
#include <filesystem>
#include <xtensor/xfixed.hpp>
#include <xtensor/xmasked_view.hpp>
#include <xtensor/xrandom.hpp>
#include <xtensor/xview.hpp>
#include <samurai/box.hpp>
#include <samurai/cell_array.hpp>
#include <samurai/field.hpp>
#include <samurai/io/hdf5.hpp>
#include <samurai/samurai.hpp>
#include <samurai/subset/node.hpp>
namespace fs = std::filesystem;
auto generate_mesh(std::size_t min_level, std::size_t max_level, std::size_t nsamples = 100)
{
constexpr std::size_t dim = 2;
xt::random::seed(42);
samurai::CellList<dim> cl;
cl[0][{0}].add_point(0);
for (std::size_t s = 0; s < nsamples; ++s)
{
auto level = xt::random::randint<std::size_t>({1}, min_level, max_level)[0];
auto x = xt::random::randint<int>({1}, 0, (1 << level) - 1)[0];
auto y = xt::random::randint<int>({1}, 0, (1 << level) - 1)[0];
cl[level][{y}].add_point(x);
}
return samurai::CellArray<dim>(cl, true);
}
int main(int argc, char* argv[])
{
auto& app = samurai::initialize("Graduation example: test case 2", argc, argv);
constexpr std::size_t dim = 2;
std::size_t min_level = 1;
std::size_t max_level = 7;
bool with_corner = false;
// Output parameters
fs::path path = fs::current_path();
std::string filename = "graduation_case_2";
app.add_option("--min-level", min_level, "Minimum level of the mesh generator")->capture_default_str();
app.add_option("--max-level", max_level, "Maximum level of the mesh generator")->capture_default_str();
app.add_flag("--with-corner", with_corner, "Make the graduation including the diagonal")->capture_default_str();
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);
}
auto ca = generate_mesh(min_level, max_level);
samurai::save(path, fmt::format("{}_initial", filename), ca);
std::size_t ite = 0;
while (true)
{
std::cout << "Iteration for remove intersection: " << ite++ << "\n";
auto tag = samurai::make_field<bool, 1>("tag", ca);
tag.fill(false);
for (std::size_t level = ca.min_level() + 1; level <= ca.max_level(); ++level)
{
for (std::size_t level_below = ca.min_level(); level_below < level; ++level_below)
{
auto set = samurai::intersection(ca[level], ca[level_below]).on(level_below);
set(
[&](const auto& i, const auto& index)
{
tag(level_below, i, index[0]) = true;
});
}
}
samurai::CellList<dim> cl;
samurai::for_each_cell(ca,
[&](auto cell)
{
auto i = cell.indices[0];
auto j = cell.indices[1];
if (tag[cell])
{
cl[cell.level + 1][{2 * j}].add_interval({2 * i, 2 * i + 2});
cl[cell.level + 1][{2 * j + 1}].add_interval({2 * i, 2 * i + 2});
}
else
{
cl[cell.level][{j}].add_point(i);
}
});
samurai::CellArray<dim> new_ca = {cl, true};
if (new_ca == ca)
{
break;
}
std::swap(ca, new_ca);
}
samurai::save(path, fmt::format("{}_without_intersection", filename), ca);
xt::xtensor_fixed<int, xt::xshape<4, dim>> stencil;
if (with_corner)
{
stencil = {
{1, 0 },
{-1, 0 },
{0, 1 },
{0, -1}
};
}
else
{
stencil = {
{1, 1 },
{-1, -1},
{-1, 1 },
{1, -1}
};
}
ite = 0;
while (true)
{
std::cout << "Iteration for graduation: " << ite++ << "\n";
auto tag = samurai::make_field<bool, 1>("tag", ca);
tag.fill(false);
for (std::size_t level = ca.min_level() + 2; level <= ca.max_level(); ++level)
{
for (std::size_t level_below = ca.min_level(); level_below < level - 1; ++level_below)
{
for (std::size_t is = 0; is < stencil.shape()[0]; ++is)
{
auto s = xt::view(stencil, is);
auto set = samurai::intersection(samurai::translate(ca[level], s), ca[level_below]).on(level_below);
set(
[&](const auto& i, const auto& index)
{
tag(level_below, i, index[0]) = true;
});
}
}
}
samurai::CellList<dim> cl;
samurai::for_each_cell(ca,
[&](auto cell)
{
auto i = cell.indices[0];
auto j = cell.indices[1];
if (tag[cell])
{
cl[cell.level + 1][{2 * j}].add_interval({2 * i, 2 * i + 2});
cl[cell.level + 1][{2 * j + 1}].add_interval({2 * i, 2 * i + 2});
}
else
{
cl[cell.level][{j}].add_point(i);
}
});
samurai::CellArray<dim> new_ca = {cl, true};
if (new_ca == ca)
{
break;
}
std::swap(ca, new_ca);
}
samurai::save(path, fmt::format("{}_graduated", filename), ca);
samurai::finalize();
return 0;
}