-
Notifications
You must be signed in to change notification settings - Fork 15
/
level_set_from_scratch.cpp
686 lines (574 loc) · 25.8 KB
/
level_set_from_scratch.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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
// Copyright 2018-2024 the samurai's authors
// SPDX-License-Identifier: BSD-3-Clause
#include <CLI/CLI.hpp>
#include <samurai/algorithm/update.hpp>
#include <samurai/algorithm/utils.hpp>
#include <samurai/bc.hpp>
#include <samurai/cell_flag.hpp>
#include <samurai/field.hpp>
#include <samurai/hdf5.hpp>
#include <samurai/mesh.hpp>
#include <samurai/mr/operators.hpp>
#include <samurai/samurai.hpp>
#include "stencil_field.hpp"
#include "../LBM/boundary_conditions.hpp"
#include <filesystem>
namespace fs = std::filesystem;
enum class SimpleID
{
cells = 0,
cells_and_ghosts = 1,
count = 2,
reference = cells_and_ghosts
};
template <>
struct fmt::formatter<SimpleID> : formatter<string_view>
{
// parse is inherited from formatter<string_view>.
template <typename FormatContext>
auto format(SimpleID c, FormatContext& ctx) const
{
string_view name = "unknown";
switch (c)
{
case SimpleID::cells:
name = "cells";
break;
case SimpleID::cells_and_ghosts:
name = "cells and ghosts";
break;
case SimpleID::count:
name = "count";
break;
}
return formatter<string_view>::format(name, ctx);
}
};
template <std::size_t dim_>
struct AMRConfig
{
static constexpr std::size_t dim = dim_;
static constexpr std::size_t max_refinement_level = 20;
static constexpr int max_stencil_width = 2;
static constexpr int ghost_width = 2;
static constexpr std::size_t prediction_order = 1;
using interval_t = samurai::Interval<int>;
using mesh_id_t = SimpleID;
};
template <class Config>
class AMRMesh : public samurai::Mesh_base<AMRMesh<Config>, Config>
{
public:
using base_type = samurai::Mesh_base<AMRMesh<Config>, Config>;
using self_type = AMRMesh<Config>;
using config = typename base_type::config;
static constexpr std::size_t dim = config::dim;
using mesh_id_t = typename base_type::mesh_id_t;
using cl_type = typename base_type::cl_type;
using lcl_type = typename base_type::lcl_type;
using ca_type = typename base_type::ca_type;
AMRMesh() = default;
inline AMRMesh(const cl_type& cl, const self_type& ref_mesh)
: base_type(cl, ref_mesh)
{
}
inline AMRMesh(const cl_type& cl, std::size_t min_level, std::size_t max_level)
: base_type(cl, min_level, max_level)
{
}
inline AMRMesh(const samurai::Box<double, dim>& b, std::size_t start_level, std::size_t min_level, std::size_t max_level)
: base_type(b, start_level, min_level, max_level)
{
}
inline void update_sub_mesh_impl()
{
cl_type cl;
for_each_interval(
this->cells()[mesh_id_t::cells],
[&](std::size_t level, const auto& interval, const auto& index_yz)
{
samurai::static_nested_loop<dim - 1, -config::ghost_width, config::ghost_width + 1>(
[&](auto stencil)
{
auto index = xt::eval(index_yz + stencil);
cl[level][index].add_interval({interval.start - config::ghost_width, interval.end + config::ghost_width});
});
});
this->cells()[mesh_id_t::cells_and_ghosts] = {cl, false};
}
};
template <std::size_t dim, class TInterval>
class projection_op_ : public samurai::field_operator_base<dim, TInterval>
{
public:
INIT_OPERATOR(projection_op_)
template <class T>
inline void operator()(samurai::Dim<2>, T& new_field, const T& field) const
{
new_field(level, i, j) = .25
* (field(level + 1, 2 * i, 2 * j) + field(level + 1, 2 * i, 2 * j + 1) + field(level + 1, 2 * i + 1, 2 * j)
+ field(level + 1, 2 * i + 1, 2 * j + 1));
}
};
template <class T>
inline auto projection(T&& new_field, T&& field)
{
return samurai::make_field_operator_function<projection_op_>(std::forward<T>(new_field), std::forward<T>(field));
}
template <class Mesh>
auto init_level_set(Mesh& mesh)
{
using mesh_id_t = typename Mesh::mesh_id_t;
auto phi = samurai::make_field<double, 1>("phi", mesh);
phi.fill(0);
samurai::for_each_cell(mesh[mesh_id_t::cells],
[&](auto& cell)
{
auto center = cell.center();
const double x = center[0];
const double y = center[1];
constexpr double radius = .15;
constexpr double x_center = 0.5;
constexpr double y_center = 0.75;
phi[cell] = std::sqrt(std::pow(x - x_center, 2.) + std::pow(y - y_center, 2.)) - radius;
});
samurai::make_bc<samurai::Neumann<1>>(phi, 0.);
return phi;
}
template <class Mesh>
auto init_velocity(Mesh& mesh)
{
using mesh_id_t = typename Mesh::mesh_id_t;
const double PI = xt::numeric_constants<double>::PI;
auto u = samurai::make_field<double, 2>("u", mesh);
u.fill(0);
samurai::for_each_cell(mesh[mesh_id_t::cells_and_ghosts],
[&](auto& cell)
{
auto center = cell.center();
const double x = center[0];
const double y = center[1];
u[cell][0] = -std::pow(std::sin(PI * x), 2.) * std::sin(2. * PI * y);
u[cell][1] = std::pow(std::sin(PI * y), 2.) * std::sin(2. * PI * x);
});
samurai::make_bc<samurai::Neumann<1>>(u, 0., 0.);
// samurai::make_bc<samurai::Dirichlet<1>>(u, [PI](auto& coords)
// {
// return xt::xtensor_fixed<double, xt::xshape<2>>{
// -std::pow(std::sin(PI*coords[0]), 2.) *
// std::sin(2.*PI*coords[1]),
// std::pow(std::sin(PI*coords[1]), 2.) * std::sin(2.*PI*coords[0])
// };
// });
return u;
}
template <class Field>
void make_graduation(Field& tag)
{
auto& mesh = tag.mesh();
for (std::size_t level = mesh.max_level(); level >= 1; --level)
{
auto ghost_subset = samurai::intersection(mesh[SimpleID::cells][level], mesh[SimpleID::reference][level - 1]).on(level - 1);
ghost_subset(
[&](const auto& i, const auto& index)
{
auto j = index[0];
tag(level - 1, i, j) |= static_cast<int>(samurai::CellFlag::keep);
});
samurai::for_each_interval(
mesh[SimpleID::cells][level],
[&](std::size_t, const auto& i, const auto& index)
{
auto j = index[0];
xt::xtensor<bool, 1> mask = (tag(level, i, j) & static_cast<int>(samurai::CellFlag::refine)); // NOLINT(misc-const-correctness)
for (int jj = -1; jj < 2; ++jj)
{
for (int ii = -1; ii < 2; ++ii)
{
xt::masked_view(tag(level, i + ii, j + jj), mask) |= static_cast<int>(samurai::CellFlag::keep);
}
}
});
auto keep_subset = samurai::intersection(mesh[SimpleID::cells][level], mesh[SimpleID::cells][level]).on(level - 1);
keep_subset(
[&](const auto& i, const auto& index)
{
auto j = index[0];
// NOLINTBEGIN(misc-const-correctness)
xt::xtensor<bool, 1> mask = (tag(level, 2 * i, 2 * j) & static_cast<int>(samurai::CellFlag::keep))
| (tag(level, 2 * i + 1, 2 * j) & static_cast<int>(samurai::CellFlag::keep))
| (tag(level, 2 * i, 2 * j + 1) & static_cast<int>(samurai::CellFlag::keep))
| (tag(level, 2 * i + 1, 2 * j + 1) & static_cast<int>(samurai::CellFlag::keep));
// NOLINTEND(misc-const-correctness)
xt::masked_view(tag(level, 2 * i, 2 * j), mask) |= static_cast<int>(samurai::CellFlag::keep);
xt::masked_view(tag(level, 2 * i + 1, 2 * j), mask) |= static_cast<int>(samurai::CellFlag::keep);
xt::masked_view(tag(level, 2 * i, 2 * j + 1), mask) |= static_cast<int>(samurai::CellFlag::keep);
xt::masked_view(tag(level, 2 * i + 1, 2 * j + 1), mask) |= static_cast<int>(samurai::CellFlag::keep);
});
xt::xtensor_fixed<int, xt::xshape<4, Field::dim>> stencil{
{1, 1 },
{-1, -1},
{-1, 1 },
{1, -1}
};
// xt::xtensor_fixed<int, xt::xshape<4, dim>> stencil{{1, 0}, {-1, 0},
// {0, 1}, {0, -1}};
for (std::size_t is = 0; is < stencil.shape()[0]; ++is)
{
auto s = xt::view(stencil, is);
auto subset = samurai::intersection(samurai::translate(mesh[SimpleID::cells][level], s), mesh[SimpleID::cells][level - 1]).on(level);
subset(
[&](const auto& i, const auto& index)
{
auto i_f = i.even_elements();
auto j_f = index[0];
if (i_f.is_valid())
{
auto mask = tag(level, i_f - s[0], j_f - s[1]) & static_cast<int>(samurai::CellFlag::refine);
auto i_c = i_f >> 1;
auto j_c = j_f >> 1;
xt::masked_view(tag(level - 1, i_c, j_c), mask) |= static_cast<int>(samurai::CellFlag::refine);
mask = tag(level, i_f - s[0], j_f - s[1]) & static_cast<int>(samurai::CellFlag::keep);
xt::masked_view(tag(level - 1, i_c, j_c), mask) |= static_cast<int>(samurai::CellFlag::keep);
}
i_f = i.odd_elements();
if (i_f.is_valid())
{
auto mask = tag(level, i_f - s[0], j_f - s[1]) & static_cast<int>(samurai::CellFlag::refine);
auto i_c = i_f >> 1;
auto j_c = j_f >> 1;
xt::masked_view(tag(level - 1, i_c, j_c), mask) |= static_cast<int>(samurai::CellFlag::refine);
mask = tag(level, i_f - s[0], j_f - s[1]) & static_cast<int>(samurai::CellFlag::keep);
xt::masked_view(tag(level - 1, i_c, j_c), mask) |= static_cast<int>(samurai::CellFlag::keep);
}
});
}
}
}
template <class Field, class Tag>
void AMR_criteria(const Field& f, Tag& tag)
{
auto& mesh = f.mesh();
std::size_t min_level = mesh.min_level();
std::size_t max_level = mesh.max_level();
samurai::for_each_cell(mesh[SimpleID::cells],
[&](auto cell)
{
const double dx = mesh.cell_length(max_level);
if (std::abs(f[cell]) < 1.2 * 5 * std::sqrt(2.) * dx)
{
if (cell.level == max_level)
{
tag[cell] = static_cast<int>(samurai::CellFlag::keep);
}
else
{
tag[cell] = static_cast<int>(samurai::CellFlag::refine);
}
}
else
{
if (cell.level == min_level)
{
tag[cell] = static_cast<int>(samurai::CellFlag::keep);
}
else
{
tag[cell] = static_cast<int>(samurai::CellFlag::coarsen);
}
}
});
}
template <class Field, class Field_u, class Tag>
bool update_mesh(Field& f, Field_u& u, Tag& tag)
{
constexpr std::size_t dim = Field::dim;
using mesh_t = typename Field::mesh_t;
using cl_type = typename mesh_t::cl_type;
auto& mesh = f.mesh();
cl_type cell_list;
samurai::for_each_interval(mesh[SimpleID::cells],
[&](std::size_t level, const auto& interval, const auto& index_yz)
{
auto itag = interval.start + interval.index;
for (int i = interval.start; i < interval.end; ++i)
{
if (tag[itag] & static_cast<int>(samurai::CellFlag::refine))
{
samurai::static_nested_loop<dim - 1, 0, 2>(
[&](auto stencil)
{
auto index = 2 * index_yz + stencil;
cell_list[level + 1][index].add_interval({2 * i, 2 * i + 2});
});
}
else if (tag[itag] & static_cast<int>(samurai::CellFlag::keep))
{
cell_list[level][index_yz].add_point(i);
}
else
{
cell_list[level - 1][index_yz >> 1].add_point(i >> 1);
}
itag++;
}
});
mesh_t new_mesh(cell_list, mesh);
if (new_mesh == mesh)
{
return true;
}
samurai::update_field(tag, f, u);
tag.mesh().swap(new_mesh);
return false;
}
template <class Field>
inline void amr_projection(Field& field)
{
auto& mesh = field.mesh();
using mesh_id_t = typename Field::mesh_t::mesh_id_t;
const std::size_t max_level = mesh.max_level();
for (std::size_t level = max_level; level >= 1; --level)
{
auto expr = samurai::intersection(mesh[mesh_id_t::cells][level], mesh[mesh_id_t::cells_and_ghosts][level - 1]).on(level - 1);
expr.apply_op(projection(field));
}
}
template <class Field>
inline void amr_prediction(Field& field)
{
auto& mesh = field.mesh();
using mesh_id_t = typename Field::mesh_t::mesh_id_t;
const std::size_t max_level = mesh[mesh_id_t::cells].max_level();
samurai::update_bc(0, field);
for (std::size_t level = 1; level <= max_level; ++level)
{
auto expr = samurai::intersection(mesh.domain(),
samurai::difference(mesh[mesh_id_t::cells_and_ghosts][level], mesh.get_union()[level - 1]))
.on(level);
expr.apply_op(samurai::prediction<1, false>(field));
samurai::update_bc(level, field);
}
}
template <class Field, class Field_u>
void update_ghosts(Field& phi, Field_u& u)
{
amr_projection(phi);
amr_projection(u);
amr_prediction(phi);
amr_prediction(u);
}
template <class Field, class Field_u>
void flux_correction(Field& phi_np1, const Field& phi_n, const Field_u& u, double dt)
{
constexpr std::size_t dim = Field::dim;
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;
auto& mesh = phi_np1.mesh();
const std::size_t min_level = mesh[mesh_id_t::cells].min_level();
const std::size_t max_level = mesh[mesh_id_t::cells].max_level();
for (std::size_t level = min_level; level < max_level; ++level)
{
xt::xtensor_fixed<int, xt::xshape<dim>> stencil;
stencil = {
{-1, 0}
};
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, const auto& index)
{
auto j = index[0];
const double dx = mesh.cell_length(level);
phi_np1(
level,
i,
j) = phi_np1(level, i, j)
+ dt / dx
* (samurai::upwind_variable_op<dim, interval_t>(level, i, j).right_flux(u, phi_n, dt)
- .5 * samurai::upwind_variable_op<dim, interval_t>(level + 1, 2 * i + 1, 2 * j).right_flux(u, phi_n, dt)
- .5 * samurai::upwind_variable_op<dim, interval_t>(level + 1, 2 * i + 1, 2 * j + 1).right_flux(u, phi_n, dt));
});
stencil = {
{1, 0}
};
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, const auto& index)
{
auto j = index[0];
const double dx = mesh.cell_length(level);
phi_np1(level,
i,
j) = phi_np1(level, i, j)
- dt / dx
* (samurai::upwind_variable_op<dim, interval_t>(level, i, j).left_flux(u, phi_n, dt)
- .5 * samurai::upwind_variable_op<dim, interval_t>(level + 1, 2 * i, 2 * j).left_flux(u, phi_n, dt)
- .5 * samurai::upwind_variable_op<dim, interval_t>(level + 1, 2 * i, 2 * j + 1).left_flux(u, phi_n, dt));
});
stencil = {
{0, -1}
};
auto subset_up = samurai::intersection(samurai::translate(mesh[mesh_id_t::cells][level + 1], stencil), mesh[mesh_id_t::cells][level])
.on(level);
subset_up(
[&](const auto& i, const auto& index)
{
auto j = index[0];
const double dx = mesh.cell_length(level);
phi_np1(
level,
i,
j) = phi_np1(level, i, j)
+ dt / dx
* (samurai::upwind_variable_op<dim, interval_t>(level, i, j).up_flux(u, phi_n, dt)
- .5 * samurai::upwind_variable_op<dim, interval_t>(level + 1, 2 * i, 2 * j + 1).up_flux(u, phi_n, dt)
- .5 * samurai::upwind_variable_op<dim, interval_t>(level + 1, 2 * i + 1, 2 * j + 1).up_flux(u, phi_n, dt));
});
stencil = {
{0, 1}
};
auto subset_down = samurai::intersection(samurai::translate(mesh[mesh_id_t::cells][level + 1], stencil),
mesh[mesh_id_t::cells][level])
.on(level);
subset_down(
[&](const auto& i, const auto& index)
{
auto j = index[0];
const double dx = mesh.cell_length(level);
phi_np1(level,
i,
j) = phi_np1(level, i, j)
- dt / dx
* (samurai::upwind_variable_op<dim, interval_t>(level, i, j).down_flux(u, phi_n, dt)
- .5 * samurai::upwind_variable_op<dim, interval_t>(level + 1, 2 * i, 2 * j).down_flux(u, phi_n, dt)
- .5 * samurai::upwind_variable_op<dim, interval_t>(level + 1, 2 * i + 1, 2 * j).down_flux(u, phi_n, dt));
});
}
}
template <class Field, class Phi>
void save(const fs::path& path, const std::string& filename, const Field& u, const Phi& phi, 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, phi, u, level_);
}
int main(int argc, char* argv[])
{
samurai::initialize(argc, argv);
constexpr size_t dim = 2;
using Config = AMRConfig<dim>;
// Simulation parameters
xt::xtensor_fixed<double, xt::xshape<dim>> min_corner = {0., 0.};
xt::xtensor_fixed<double, xt::xshape<dim>> max_corner = {1., 1.};
double Tf = 3.14;
double cfl = 5. / 8;
// AMR parameters
std::size_t start_level = 8;
std::size_t min_level = 4;
std::size_t max_level = 8;
bool correction = false;
// Output parameters
fs::path path = fs::current_path();
std::string filename = "FV_level_set_2d";
std::size_t nfiles = 1;
CLI::App app{"Finite volume example with a level set in 2d using AMR"};
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("--Tf", Tf, "Final time")->capture_default_str()->group("Simulation parameters");
app.add_option("--start-level", start_level, "Start level of AMR")->capture_default_str()->group("AMR parameters");
app.add_option("--min-level", min_level, "Minimum level of AMR")->capture_default_str()->group("AMR parameters");
app.add_option("--max-level", max_level, "Maximum level of AMR")->capture_default_str()->group("AMR parameters");
app.add_flag("--with-correction", correction, "Apply flux correction at the interface of two refinement levels")
->capture_default_str()
->group("AMR parameters");
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");
CLI11_PARSE(app, argc, argv);
const samurai::Box<double, dim> box(min_corner, max_corner);
AMRMesh<Config> mesh{box, max_level, min_level, max_level};
double dt = cfl * mesh.cell_length(max_level);
const double dt_save = Tf / static_cast<double>(nfiles);
double t = 0.;
// We initialize the level set function
// We initialize the velocity field
auto phi = init_level_set(mesh);
auto phinp1 = samurai::make_field<double, 1>("phi", mesh);
auto u = init_velocity(mesh);
std::size_t nsave = 1;
std::size_t nt = 0;
while (t != Tf)
{
// AMR adaptation
std::size_t ite = 0;
while (true)
{
std::cout << "Mesh adaptation iteration " << ite++ << std::endl;
auto tag = samurai::make_field<int, 1>("tag", mesh);
AMR_criteria(phi, tag);
make_graduation(tag);
update_ghosts(phi, u);
if (update_mesh(phi, u, tag))
{
break;
}
}
t += dt;
if (t > Tf)
{
dt += Tf - t;
t = Tf;
}
std::cout << fmt::format("iteration {}: t = {}, dt = {}", nt++, t, dt) << std::endl;
// Numerical scheme
update_ghosts(phi, u);
phinp1.resize();
phinp1 = phi - dt * samurai::upwind_variable(u, phi, dt);
if (correction)
{
flux_correction(phinp1, phi, u, dt);
}
std::swap(phi.array(), phinp1.array());
// Reinitialization of the level set
const std::size_t fict_iteration = 2; // Number of fictitious iterations
const double dt_fict = 0.01 * dt; // Fictitious Time step
auto phi_0 = phi;
for (std::size_t k = 0; k < fict_iteration; ++k)
{
// //Forward Euler - OK
// update_ghosts(phi, u);
// phinp1 = phi - dt_fict * H_wrap(phi, phi_0, max_level);
// std::swap(phi.array(), phinp1.array());
// TVD-RK2
update_ghosts(phi, u);
auto phihat = samurai::make_field<double, 1>("phi", mesh);
samurai::make_bc<samurai::Neumann<1>>(phihat, 0.);
phihat = phi - dt_fict * H_wrap(phi, phi_0, max_level);
update_ghosts(phihat, u);
phinp1 = .5 * phi_0 + .5 * (phihat - dt_fict * H_wrap(phihat, phi_0, max_level));
std::swap(phi.array(), phinp1.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, phi, suffix);
}
}
samurai::finalize();
return 0;
}