Finite Volume Solver  prototype
A framework to build finite volume solvers for the AG Klein at the Freie Universität Berlin.
output/DebugOutput.hpp
Go to the documentation of this file.
1 // Copyright (c) 2020 Maikel Nadolski
2 // Copyright (c) 2020 Stefan Vater
3 //
4 // Permission is hereby granted, free of charge, to any person obtaining a copy
5 // of this software and associated documentation files (the "Software"), to deal
6 // in the Software without restriction, including without limitation the rights
7 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 // copies of the Software, and to permit persons to whom the Software is
9 // furnished to do so, subject to the following conditions:
10 //
11 // The above copyright notice and this permission notice shall be included in
12 // all copies or substantial portions of the Software.
13 //
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 // SOFTWARE.
21 
22 #ifndef FUB_AMREX_DEBUG_OUTPUT_HPP
23 #define FUB_AMREX_DEBUG_OUTPUT_HPP
24 
27 
28 namespace fub::amrex {
29 
30 /// \brief This class stores debug data for a debug output for a single
31 /// hierarchy state.
32 ///
33 /// This class will be created by \a DebugStorage and modified through \a
34 /// DebugSnapshotProxy.
35 ///
36 /// \see DebugStorage
37 /// \see DebugSnapshotProxy
39 public:
40  using Hierarchy = ::amrex::Vector<::amrex::MultiFab>;
41  using GeomHierarchy = ::amrex::Vector<::amrex::Geometry>;
42  using ComponentNames = ::amrex::Vector<std::string>;
43 
44  /// \brief Initializes an empty snapshot
45  DebugSnapshot() = default;
46 
47  /// @{
48  /// \brief Saves a current hierarchy state with given component names
49  ///
50  /// The actual output will be handled by the DebugOutput class and will
51  /// usually happen at a later time point.
52  void SaveData(const ::amrex::MultiFab& mf, const std::string& name,
53  const ::amrex::Geometry& geom,
54  ::amrex::SrcComp component = ::amrex::SrcComp(0));
55 
56  void SaveData(const ::amrex::MultiFab& mf, const ComponentNames& names,
57  const ::amrex::Geometry& geom,
58  ::amrex::SrcComp first_component = ::amrex::SrcComp(0));
59 
60  void SaveData(const ::amrex::Vector<const ::amrex::MultiFab*>& hierarchy,
61  const std::string& name,
62  const ::amrex::Vector<const ::amrex::Geometry*>& geomhier,
63  ::amrex::SrcComp component = ::amrex::SrcComp(0));
64 
65  void SaveData(const ::amrex::Vector<::amrex::MultiFab>& hierarchy,
66  const std::string& name,
67  const ::amrex::Vector<::amrex::Geometry>& geomhier,
68  ::amrex::SrcComp component = ::amrex::SrcComp(0));
69 
70  void SaveData(const ::amrex::Vector<const ::amrex::MultiFab*>& hierarchy,
71  const ComponentNames& names,
72  const ::amrex::Vector<const ::amrex::Geometry*>& geomhier,
73  ::amrex::SrcComp first_component = ::amrex::SrcComp(0));
74 
75  void SaveData(const ::amrex::Vector<::amrex::MultiFab>& hierarchy,
76  const ComponentNames& names,
77  const ::amrex::Vector<::amrex::Geometry>& geomhier,
78  ::amrex::SrcComp first_component = ::amrex::SrcComp(0));
79  /// @}
80 
81  /// \brief Deletes all currently stored data
82  void ClearAll();
83 
84  /// \brief Returns all the hierarchies which are stored via SaveData
85  const std::vector<Hierarchy>& GetHierarchies() const noexcept;
86 
87  /// \brief Returns all the component names which are stored via SaveData
88  const std::vector<ComponentNames>& GetNames() const noexcept;
89 
90  /// \brief Returns all the associated geometries which are stored via SaveData
91  const std::vector<GeomHierarchy>& GetGeometries() const noexcept;
92 
93  /// \brief Changes component names that appear more than once by appending
94  /// assending numbers to each. This also makes names unique, which appear
95  /// more than once in one hierarchy.
97 
98  /// \brief Collects all hierachies and associated names which are stored on
99  /// the specified location.
100  ///
101  /// This function will join all compatible hierachies to a common big one if
102  /// possible.
103  std::vector<std::tuple<Hierarchy, ComponentNames, GeomHierarchy>>
104  GatherFields(::amrex::IndexType location) const;
105 
106  /// \brief Sets the directory in which the output will be saved to.
107  void SetSnapshotDirectory(const std::string& snapshot_directory) {
108  snapshot_directory_ = snapshot_directory;
109  }
110 
111  /// \brief Returns the directory in which the output will be saved to.
112  const std::string GetSnapshotDirectory() const { return snapshot_directory_; }
113 
114 private:
115  /// \brief Each SaveData will append a hierarchy
116  std::vector<Hierarchy> saved_hierarchies_{};
117 
118  /// \brief Each SaveData will append a list of names on the hierachy
119  std::vector<ComponentNames> names_per_hierarchy_{};
120 
121  /// \brief Each SaveData will append a hierarchy of corresponding geometries
122  std::vector<GeomHierarchy> saved_geometries_{};
123 
124  /// \brief output directory of storage;
125  std::string snapshot_directory_{};
126 };
127 
128 /// \brief This class is a possibly empty handle to a existing
129 /// DebugSnapshotProxy.
130 ///
131 /// DebugSnapshotProxy maybe in a valid or invalid
133 public:
134  /// \brief Initializes an empty proxy snapshot
135  DebugSnapshotProxy() = default;
136 
137  /// \brief Initializes a proxy snapshot for a real snapshot
138  DebugSnapshotProxy(DebugSnapshot& snapshot) : snapshot_(&snapshot){};
139 
140  /// @{
141  /// \brief Saves a current hierarchy state with given component names
142  ///
143  /// The actual output will be handled by the DebugOutput class and will
144  /// usually happen at a later time point.
145  void SaveData(const ::amrex::MultiFab& mf, const std::string& name,
146  const ::amrex::Geometry& geom,
147  ::amrex::SrcComp component = ::amrex::SrcComp(0));
148 
149  void SaveData(const ::amrex::MultiFab& mf,
150  const DebugSnapshot::ComponentNames& names,
151  const ::amrex::Geometry& geom,
152  ::amrex::SrcComp first_component = ::amrex::SrcComp(0));
153 
154  void SaveData(const ::amrex::Vector<const ::amrex::MultiFab*>& hierarchy,
155  const std::string& name,
156  const ::amrex::Vector<const ::amrex::Geometry*>& geomhier,
157  ::amrex::SrcComp component = ::amrex::SrcComp(0));
158 
159  void SaveData(const ::amrex::Vector<::amrex::MultiFab>& hierarchy,
160  const std::string& name,
161  const ::amrex::Vector<::amrex::Geometry>& geomhier,
162  ::amrex::SrcComp component = ::amrex::SrcComp(0));
163 
164  void SaveData(const ::amrex::Vector<const ::amrex::MultiFab*>& hierarchy,
165  const DebugSnapshot::ComponentNames& names,
166  const ::amrex::Vector<const ::amrex::Geometry*>& geomhier,
167  ::amrex::SrcComp first_component = ::amrex::SrcComp(0));
168 
169  void SaveData(const ::amrex::Vector<::amrex::MultiFab>& hierarchy,
170  const DebugSnapshot::ComponentNames& names,
171  const ::amrex::Vector<::amrex::Geometry>& geomhier,
172  ::amrex::SrcComp first_component = ::amrex::SrcComp(0));
173  /// @}
174 
175  /// \brief Returns true if this object is valid.
176  explicit operator bool() const noexcept { return snapshot_ == nullptr; }
177 
178  /// \brief Returns true if both proxy objects point to the same snapshot.
179  friend inline bool operator==(const DebugSnapshotProxy& p1,
180  const DebugSnapshotProxy& p2) noexcept {
181  return p1.snapshot_ == p2.snapshot_;
182  }
183 
184 private:
185  /// \brief Pointer to the real snapshot
187 };
188 
189 /// \brief Returns true if both proxy objects point to different snapshots.
190 inline bool operator!=(const DebugSnapshotProxy& p1,
191  const DebugSnapshotProxy& p2) noexcept {
192  return !(p1 == p2);
193 }
194 
195 /// \brief This class stores a list of snapshots and returns proxy objects to
196 /// those.
197 ///
198 /// As long as this class is in a disabled mode the returned proxy objects are
199 /// invalid and subsequent calls to SaveData will have no effect. Only if this
200 /// the storage is enabled debug data will be stored.
202 public:
203  /// \brief Initializes the storage
204  DebugStorage() = default;
205 
206  /// \brief Checks if debug storage is enabled
207  bool IsEnabled() const noexcept { return is_enabled_; }
208 
209  void Enable() { is_enabled_ = true; }
210  void Disable() { is_enabled_ = false; }
211 
212  /// \brief Adds snapshot with given directory name to storage.
213  DebugSnapshotProxy AddSnapshot(const std::string& snapshot_directory);
214 
215  /// \brief Writes data to disk and clears all data present in the storage.
216  void FlushData(const std::string& directory, int cycle = -1,
217  Duration time_point = Duration(0.0));
218 
219  /// \brief Deletes all currently stored snapshots in the debug storage
220  void ClearAll() { saved_snapshots_.clear(); }
221 
222 private:
223  /// \brief List of snapshots
224  std::list<DebugSnapshot> saved_snapshots_;
225 
226  /// \brief If this is false no data will be saved.
227  bool is_enabled_{false};
228 
229  int cycle_{-1};
230 };
231 
232 /// \brief This output method enables a debug storage and manages its output in
233 /// every time step.
234 class DebugOutput : public OutputAtFrequencyOrInterval<GriddingAlgorithm> {
235 public:
236  /// \brief Read program options from opts and enable the storage.
237  explicit DebugOutput(const ProgramOptions& opts,
238  const std::shared_ptr<DebugStorage>& storage);
239 
240  /// \brief Write out the debug storage on the grid.
241  void operator()(const GriddingAlgorithm& grid) override;
242 
243 private:
244  /// \brief This is the base directory where the snapshots will be output to.
245  std::string directory_;
246 };
247 
248 } // namespace fub::amrex
249 
250 #endif
Definition: OutputAtFrequencyOrInterval.hpp:32
This output method enables a debug storage and manages its output in every time step.
Definition: output/DebugOutput.hpp:234
std::string directory_
This is the base directory where the snapshots will be output to.
Definition: output/DebugOutput.hpp:245
DebugOutput(const ProgramOptions &opts, const std::shared_ptr< DebugStorage > &storage)
Read program options from opts and enable the storage.
void operator()(const GriddingAlgorithm &grid) override
Write out the debug storage on the grid.
This class is a possibly empty handle to a existing DebugSnapshotProxy.
Definition: output/DebugOutput.hpp:132
void SaveData(const ::amrex::Vector< const ::amrex::MultiFab * > &hierarchy, const DebugSnapshot::ComponentNames &names, const ::amrex::Vector< const ::amrex::Geometry * > &geomhier, ::amrex::SrcComp first_component=::amrex::SrcComp(0))
Saves a current hierarchy state with given component names.
void SaveData(const ::amrex::MultiFab &mf, const DebugSnapshot::ComponentNames &names, const ::amrex::Geometry &geom, ::amrex::SrcComp first_component=::amrex::SrcComp(0))
Saves a current hierarchy state with given component names.
void SaveData(const ::amrex::MultiFab &mf, const std::string &name, const ::amrex::Geometry &geom, ::amrex::SrcComp component=::amrex::SrcComp(0))
Saves a current hierarchy state with given component names.
void SaveData(const ::amrex::Vector<::amrex::MultiFab > &hierarchy, const DebugSnapshot::ComponentNames &names, const ::amrex::Vector<::amrex::Geometry > &geomhier, ::amrex::SrcComp first_component=::amrex::SrcComp(0))
Saves a current hierarchy state with given component names.
friend bool operator==(const DebugSnapshotProxy &p1, const DebugSnapshotProxy &p2) noexcept
Returns true if both proxy objects point to the same snapshot.
Definition: output/DebugOutput.hpp:179
DebugSnapshotProxy()=default
Initializes an empty proxy snapshot.
void SaveData(const ::amrex::Vector<::amrex::MultiFab > &hierarchy, const std::string &name, const ::amrex::Vector<::amrex::Geometry > &geomhier, ::amrex::SrcComp component=::amrex::SrcComp(0))
Saves a current hierarchy state with given component names.
DebugSnapshot * snapshot_
Pointer to the real snapshot.
Definition: output/DebugOutput.hpp:186
DebugSnapshotProxy(DebugSnapshot &snapshot)
Initializes a proxy snapshot for a real snapshot.
Definition: output/DebugOutput.hpp:138
void SaveData(const ::amrex::Vector< const ::amrex::MultiFab * > &hierarchy, const std::string &name, const ::amrex::Vector< const ::amrex::Geometry * > &geomhier, ::amrex::SrcComp component=::amrex::SrcComp(0))
Saves a current hierarchy state with given component names.
This class stores debug data for a debug output for a single hierarchy state.
Definition: output/DebugOutput.hpp:38
std::vector< GeomHierarchy > saved_geometries_
Each SaveData will append a hierarchy of corresponding geometries.
Definition: output/DebugOutput.hpp:122
void SaveData(const ::amrex::Vector<::amrex::MultiFab > &hierarchy, const ComponentNames &names, const ::amrex::Vector<::amrex::Geometry > &geomhier, ::amrex::SrcComp first_component=::amrex::SrcComp(0))
Saves a current hierarchy state with given component names.
const std::vector< Hierarchy > & GetHierarchies() const noexcept
Returns all the hierarchies which are stored via SaveData.
const std::vector< ComponentNames > & GetNames() const noexcept
Returns all the component names which are stored via SaveData.
void SaveData(const ::amrex::MultiFab &mf, const ComponentNames &names, const ::amrex::Geometry &geom, ::amrex::SrcComp first_component=::amrex::SrcComp(0))
Saves a current hierarchy state with given component names.
DebugSnapshot()=default
Initializes an empty snapshot.
const std::vector< GeomHierarchy > & GetGeometries() const noexcept
Returns all the associated geometries which are stored via SaveData.
std::vector< std::tuple< Hierarchy, ComponentNames, GeomHierarchy > > GatherFields(::amrex::IndexType location) const
Collects all hierachies and associated names which are stored on the specified location.
::amrex::Vector< std::string > ComponentNames
Definition: output/DebugOutput.hpp:42
::amrex::Vector<::amrex::Geometry > GeomHierarchy
Definition: output/DebugOutput.hpp:41
void SaveData(const ::amrex::Vector< const ::amrex::MultiFab * > &hierarchy, const ComponentNames &names, const ::amrex::Vector< const ::amrex::Geometry * > &geomhier, ::amrex::SrcComp first_component=::amrex::SrcComp(0))
Saves a current hierarchy state with given component names.
void SaveData(const ::amrex::MultiFab &mf, const std::string &name, const ::amrex::Geometry &geom, ::amrex::SrcComp component=::amrex::SrcComp(0))
Saves a current hierarchy state with given component names.
const std::string GetSnapshotDirectory() const
Returns the directory in which the output will be saved to.
Definition: output/DebugOutput.hpp:112
void SaveData(const ::amrex::Vector<::amrex::MultiFab > &hierarchy, const std::string &name, const ::amrex::Vector<::amrex::Geometry > &geomhier, ::amrex::SrcComp component=::amrex::SrcComp(0))
Saves a current hierarchy state with given component names.
::amrex::Vector<::amrex::MultiFab > Hierarchy
Definition: output/DebugOutput.hpp:40
std::vector< Hierarchy > saved_hierarchies_
Each SaveData will append a hierarchy.
Definition: output/DebugOutput.hpp:116
void MakeUniqueComponentNames()
Changes component names that appear more than once by appending assending numbers to each.
void SaveData(const ::amrex::Vector< const ::amrex::MultiFab * > &hierarchy, const std::string &name, const ::amrex::Vector< const ::amrex::Geometry * > &geomhier, ::amrex::SrcComp component=::amrex::SrcComp(0))
Saves a current hierarchy state with given component names.
std::vector< ComponentNames > names_per_hierarchy_
Each SaveData will append a list of names on the hierachy.
Definition: output/DebugOutput.hpp:119
void ClearAll()
Deletes all currently stored data.
void SetSnapshotDirectory(const std::string &snapshot_directory)
Sets the directory in which the output will be saved to.
Definition: output/DebugOutput.hpp:107
std::string snapshot_directory_
output directory of storage;
Definition: output/DebugOutput.hpp:125
This class stores a list of snapshots and returns proxy objects to those.
Definition: output/DebugOutput.hpp:201
bool IsEnabled() const noexcept
Checks if debug storage is enabled.
Definition: output/DebugOutput.hpp:207
void FlushData(const std::string &directory, int cycle=-1, Duration time_point=Duration(0.0))
Writes data to disk and clears all data present in the storage.
void Disable()
Definition: output/DebugOutput.hpp:210
std::list< DebugSnapshot > saved_snapshots_
List of snapshots.
Definition: output/DebugOutput.hpp:224
DebugStorage()=default
Initializes the storage.
bool is_enabled_
If this is false no data will be saved.
Definition: output/DebugOutput.hpp:227
int cycle_
Definition: output/DebugOutput.hpp:229
void ClearAll()
Deletes all currently stored snapshots in the debug storage.
Definition: output/DebugOutput.hpp:220
void Enable()
Definition: output/DebugOutput.hpp:209
DebugSnapshotProxy AddSnapshot(const std::string &snapshot_directory)
Adds snapshot with given directory name to storage.
This class modifies and initializes a PatchLevel in a PatchHierarchy.
Definition: AMReX/GriddingAlgorithm.hpp:60
The amrex namespace.
Definition: AverageState.hpp:33
bool operator!=(const DebugSnapshotProxy &p1, const DebugSnapshotProxy &p2) noexcept
Returns true if both proxy objects point to different snapshots.
Definition: output/DebugOutput.hpp:190
std::chrono::duration< double > Duration
Definition: Duration.hpp:31
std::map< std::string, pybind11::object > ProgramOptions
Definition: ProgramOptions.hpp:40