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"
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<CmiNodeLock*> 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  Parallel::lock(node_lock);
45  CmiNodeLock file_lock = nullptr;
46  db::mutate<Tags::H5FileLock>(
47  make_not_null(&box), [&file_lock](const gsl::not_null<CmiNodeLock*>
48  in_file_lock) noexcept {
49  file_lock = *in_file_lock;
50  });
51  Parallel::unlock(node_lock);
52 
53  Parallel::lock(&file_lock);
54  // scoped to close file
55  {
56  const auto& file_prefix = Parallel::get<Tags::VolumeFileName>(cache);
58  file_prefix + std::to_string(Parallel::my_node()) + ".h5", true);
59  const size_t version_number = 0;
60  auto& output_dataset =
61  h5file.try_insert<h5::Dat>(subfile_name, file_legend, version_number);
62  output_dataset.append(data_row);
63  h5file.close_current_object();
64  }
65  Parallel::unlock(&file_lock);
66  }
67 };
68 } // namespace ThreadedActions
69 } // namespace observers
Parallel::ConstGlobalCache
Definition: ElementReceiveInterpPoints.hpp:16
AccessType.hpp
std::string
DataBoxTag.hpp
Parallel::lock
void lock(const gsl::not_null< CmiNodeLock * > node_lock) noexcept
Lock a converse CmiNodeLock.
Definition: NodeLock.hpp:34
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
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
Parallel::unlock
void unlock(const gsl::not_null< CmiNodeLock * > node_lock) noexcept
Unlock a converse CmiNodeLock.
Definition: NodeLock.hpp:62
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
ConstGlobalCache.hpp
gsl::not_null
Require a pointer to not be a nullptr
Definition: Gsl.hpp:183