Tatooine
staggered_flowmap_discretization.h
Go to the documentation of this file.
1#ifndef TATOOINE_STAGGERED_FLOWMAP_DISCRETIZATION_H
2#define TATOOINE_STAGGERED_FLOWMAP_DISCRETIZATION_H
3//==============================================================================
4#include <tatooine/field.h>
5//==============================================================================
6namespace tatooine {
7//==============================================================================
8template <typename InternalFlowmapDiscretization>
10 using internal_flowmap_discretization_type = InternalFlowmapDiscretization;
11 using real_type = typename internal_flowmap_discretization_type::real_type;
12 static auto constexpr num_dimensions() -> std::size_t {
13 return internal_flowmap_discretization_type::num_dimensions();
14 }
17 //============================================================================
18 mutable std::vector<std::unique_ptr<internal_flowmap_discretization_type>>
19 m_steps = {};
20 std::vector<filesystem::path> m_filepaths_to_steps = {};
21 bool m_write_to_disk = false;
22 mutable std::mutex m_deletion_mutex;
23 //============================================================================
28 for (auto const &step : m_steps) {
30 }
31 }
32 //----------------------------------------------------------------------------
34 staggered_flowmap_discretization &&other) noexcept
35 : m_steps{std::move(other.m_steps)},
36 m_filepaths_to_steps{std::move(other.m_filepaths_to_steps)},
37 m_write_to_disk{other.m_write_to_disk} {}
38 //----------------------------------------------------------------------------
41 for (auto const &step : m_steps) {
43 }
44 m_filepaths_to_steps = other.m_filepaths_to_steps;
45 m_write_to_disk = other.m_write_to_disk;
46 return *this;
47 }
48 //----------------------------------------------------------------------------
51 m_steps = std::move(other.m_steps);
52 m_filepaths_to_steps = std::move(other.m_filepaths_to_steps);
53 m_write_to_disk = std::move(other.m_write_to_disk);
54 return *this;
55 }
56 //============================================================================
57 template <typename Flowmap, typename... InternalFlowmapArgs>
59 arithmetic auto const tau,
60 arithmetic auto const delta_t,
61 InternalFlowmapArgs &&...args) {
62 auto cur_t0 = real_type(t0);
63 auto const t_end = static_cast<real_type>(t0) + static_cast<real_type>(tau);
64 m_steps.reserve(static_cast<std::size_t>((t_end - t0) / delta_t) + 2);
65 static auto const eps = real_type(1e-10);
66 auto cnt = std::size_t{};
67 auto const prefix = random::alpha_numeric_string(5) + "_";
68 while (cur_t0 + eps < t0 + tau) {
69 std::cout << "begin of while\n";
70 auto cur_tau = static_cast<real_type>(delta_t);
71 if (cur_t0 + cur_tau > t_end) {
72 cur_tau = static_cast<real_type>(t0) + static_cast<real_type>(tau) -
73 static_cast<real_type>(cur_t0);
74 }
75 std::cout << "cur_tau: " << cur_tau << '\n';
76 std::cout << "generating path\n";
77 auto const &path =
78 m_filepaths_to_steps.emplace_back(prefix + std::to_string(cnt++));
79 std::cout << "advecting " << path << '\n';
81 std::forward<Flowmap>(flowmap), cur_t0, cur_tau,
82 std::forward<InternalFlowmapArgs>(args)...});
83 cur_t0 += cur_tau;
84
85 if (m_write_to_disk) {
86 std::cout << "writing " << path << '\n';
87 m_steps.back()->write(path);
88 std::cout << "resetting " << path << '\n';
89 m_steps.back().reset();
90 }
91 std::cout << "done with " << path << '\n';
92 }
93 }
94 //============================================================================
95 auto write_to_disk(bool const w = true) { m_write_to_disk = w; }
96 //----------------------------------------------------------------------------
97 auto num_steps() const { return m_steps.size(); }
98 //============================================================================
99 auto step(std::size_t const i) const -> auto const & {
100 if (m_write_to_disk && m_steps[i] == nullptr) {
101 auto lock = std::lock_guard{m_deletion_mutex};
102 if (m_steps[i] == nullptr) {
103 for (auto &step : m_steps) {
104 step.reset();
105 }
106 }
107 m_steps[i]->read(m_filepaths_to_steps[i]);
108 }
109 return *m_steps[i];
110 }
111 //----------------------------------------------------------------------------
112 auto step(std::size_t const i) -> auto & {
113 if (m_write_to_disk && m_steps[i] == nullptr) {
114 auto lock = std::lock_guard{m_deletion_mutex};
115 if (m_steps[i] == nullptr) {
116 for (auto &step : m_steps) {
117 if (step != nullptr) {
118 step.reset();
119 }
120 }
121 }
122 m_steps[i] = std::make_unique<internal_flowmap_discretization_type>(
124 }
125 return *m_steps[i];
126 }
127 //============================================================================
132 auto sample(pos_type x, forward_tag const tag) const {
133 for (std::size_t i = 0; i < num_steps(); ++i) {
134 x = step(i).sample(x, tag);
135 }
136 return x;
137 }
138 //----------------------------------------------------------------------------
143 auto sample(pos_type x, backward_tag const tag) const {
144 for (std::int64_t i = static_cast<std::int64_t>(num_steps() - 1); i >= 0;
145 --i) {
146 x = step(i).sample(x, tag);
147 }
148 return x;
149 }
150};
151//==============================================================================
152} // namespace tatooine
153//==============================================================================
154#endif
Definition: concepts.h:33
auto alpha_numeric_string(std::size_t const size)
Definition: random.h:83
Definition: algorithm.h:6
auto flowmap(vectorfield< V, Real, NumDimensions > const &v, tag::numerical_t)
Definition: numerical_flowmap.h:412
Definition: tags.h:10
Definition: tags.h:8
Definition: staggered_flowmap_discretization.h:9
staggered_flowmap_discretization(staggered_flowmap_discretization &&other) noexcept
Definition: staggered_flowmap_discretization.h:33
std::mutex m_deletion_mutex
Definition: staggered_flowmap_discretization.h:22
std::vector< filesystem::path > m_filepaths_to_steps
Definition: staggered_flowmap_discretization.h:20
auto num_steps() const
Definition: staggered_flowmap_discretization.h:97
std::vector< std::unique_ptr< internal_flowmap_discretization_type > > m_steps
Definition: staggered_flowmap_discretization.h:19
InternalFlowmapDiscretization internal_flowmap_discretization_type
Definition: staggered_flowmap_discretization.h:10
auto operator=(staggered_flowmap_discretization &&other) -> staggered_flowmap_discretization &
Definition: staggered_flowmap_discretization.h:49
auto step(std::size_t const i) -> auto &
Definition: staggered_flowmap_discretization.h:112
vec< real_type, num_dimensions()> vec_type
Definition: staggered_flowmap_discretization.h:15
staggered_flowmap_discretization(Flowmap &&flowmap, arithmetic auto const t0, arithmetic auto const tau, arithmetic auto const delta_t, InternalFlowmapArgs &&...args)
Definition: staggered_flowmap_discretization.h:58
auto sample(pos_type x, backward_tag const tag) const
Definition: staggered_flowmap_discretization.h:143
auto operator=(staggered_flowmap_discretization const &other) -> staggered_flowmap_discretization &
Definition: staggered_flowmap_discretization.h:39
bool m_write_to_disk
Definition: staggered_flowmap_discretization.h:21
auto write_to_disk(bool const w=true)
Definition: staggered_flowmap_discretization.h:95
static auto constexpr num_dimensions() -> std::size_t
Definition: staggered_flowmap_discretization.h:12
auto sample(pos_type x, forward_tag const tag) const
Definition: staggered_flowmap_discretization.h:132
typename internal_flowmap_discretization_type::real_type real_type
Definition: staggered_flowmap_discretization.h:11
staggered_flowmap_discretization(staggered_flowmap_discretization const &other)
Definition: staggered_flowmap_discretization.h:24
auto step(std::size_t const i) const -> auto const &
Definition: staggered_flowmap_discretization.h:99