WriteSimpleData.hpp
1 // Distributed under the MIT License.
2 // See LICENSE.txt for details.
3 
4 #pragma once
5 
6 #include <cstddef>
7 
10 #include "IO/H5/AccessType.hpp"
11 #include "IO/H5/File.hpp"
12 #include "IO/Observer/ArrayComponentId.hpp"
13 #include "IO/Observer/Tags.hpp"
14 #include "Parallel/GlobalCache.hpp"
15 #include "Parallel/Info.hpp"
16 #include "Parallel/Invoke.hpp"
17 #include "Parallel/NodeLock.hpp"
18 #include "Utilities/Requires.hpp"
19 #include "Utilities/TMPL.hpp"
20 
21 namespace observers {
22 namespace ThreadedActions {
23 
24 /*!
25  * \brief Append data to an h5::Dat subfile of `Tags::VolumeFileName`.
26  *
27  * \details This is a streamlined interface for getting data to the volume file
28  * associated with a node; it will simply write the `.dat` object
29  * `subfile_name`, giving it the `file_legend` if it does not yet exist,
30  * appending `data_row` to the end of the dat.
31  */
33  template <
34  typename ParallelComponent, typename DbTagsList, typename Metavariables,
35  typename ArrayIndex,
37  static void apply(db::DataBox<DbTagsList>& box,
39  const ArrayIndex& /*array_index*/,
40  const gsl::not_null<Parallel::NodeLock*> node_lock,
41  const std::vector<std::string>& file_legend,
42  const std::vector<double>& data_row,
43  const std::string& subfile_name) noexcept {
44  node_lock->lock();
45  Parallel::NodeLock* file_lock = nullptr;
46  db::mutate<Tags::H5FileLock>(
47  make_not_null(&box),
48  [&file_lock](
49  const gsl::not_null<Parallel::NodeLock*> in_file_lock) noexcept {
50  file_lock = in_file_lock;
51  });
52  node_lock->unlock();
53 
54  file_lock->lock();
55  // scoped to close file
56  {
57  const auto& file_prefix = Parallel::get<Tags::VolumeFileName>(cache);
59  file_prefix + std::to_string(Parallel::my_node()) + ".h5", true);
60  const size_t version_number = 0;
61  auto& output_dataset =
62  h5file.try_insert<h5::Dat>(subfile_name, file_legend, version_number);
63  output_dataset.append(data_row);
64  h5file.close_current_object();
65  }
66  file_lock->unlock();
67  }
68 };
69 } // namespace ThreadedActions
70 } // namespace observers
AccessType.hpp
std::string
DataBoxTag.hpp
Parallel::GlobalCache
Definition: ElementReceiveInterpPoints.hpp:16
GlobalCache.hpp
h5::H5File::try_insert
ObjectType & try_insert(const std::string &path, Args &&... args) noexcept
Inserts an object like insert if it does not exist, returns the object if it does.
Definition: File.hpp:269
h5::Dat
Represents a multicolumn dat file inside an HDF5 file.
Definition: Dat.hpp:42
std::vector< std::string >
Info.hpp
File.hpp
Parallel::NodeLock
A typesafe wrapper for a lock for synchronization of shared resources on a given node,...
Definition: NodeLock.hpp:23
h5::H5File< h5::AccessType::ReadWrite >
Parallel::my_node
int my_node()
Index of my node.
Definition: Info.hpp:34
DataBox.hpp
h5::Dat::append
HIDDEN_SYMBOLS void append(const std::vector< double > &data)
Definition: Dat.cpp:154
cstddef
h5::H5File::close_current_object
void close_current_object() const noexcept
Definition: File.hpp:151
Requires.hpp
observers::ThreadedActions::WriteSimpleData
Append data to an h5::Dat subfile of Tags::VolumeFileName.
Definition: WriteSimpleData.hpp:32
make_not_null
gsl::not_null< T * > make_not_null(T *ptr) noexcept
Construct a not_null from a pointer. Often this will be done as an implicit conversion,...
Definition: Gsl.hpp:880
Requires
typename Requires_detail::requires_impl< B >::template_error_type_failed_to_meet_requirements_on_template_parameters Requires
Express requirements on the template parameters of a function or class, replaces std::enable_if_t
Definition: Requires.hpp:67
db::DataBox
Definition: InterpolationTargetWedgeSectionTorus.hpp:24
TMPL.hpp
gsl::not_null
Require a pointer to not be a nullptr
Definition: Gsl.hpp:183