WriteSimpleData.hpp
1 // Distributed under the MIT License.
2 // See LICENSE.txt for details.
3 
4 #pragma once
5 
6 #include <cstddef>
7 
9 #include "IO/H5/AccessType.hpp"
10 #include "IO/H5/File.hpp"
11 #include "IO/Observer/ArrayComponentId.hpp"
12 #include "IO/Observer/Tags.hpp"
13 #include "Parallel/GlobalCache.hpp"
14 #include "Parallel/Info.hpp"
15 #include "Parallel/Invoke.hpp"
16 #include "Parallel/NodeLock.hpp"
17 #include "Utilities/Requires.hpp"
18 #include "Utilities/TMPL.hpp"
19 
20 namespace observers {
21 namespace ThreadedActions {
22 
23 /*!
24  * \brief Append data to an h5::Dat subfile of `Tags::VolumeFileName`.
25  *
26  * \details This is a streamlined interface for getting data to the volume file
27  * associated with a node; it will simply write the `.dat` object
28  * `subfile_name`, giving it the `file_legend` if it does not yet exist,
29  * appending `data_row` to the end of the dat.
30  */
32  template <
33  typename ParallelComponent, typename DbTagsList, typename Metavariables,
34  typename ArrayIndex,
36  static void apply(db::DataBox<DbTagsList>& box,
38  const ArrayIndex& /*array_index*/,
39  const gsl::not_null<Parallel::NodeLock*> node_lock,
40  const std::vector<std::string>& file_legend,
41  const std::vector<double>& data_row,
42  const std::string& subfile_name) noexcept {
43  node_lock->lock();
44  Parallel::NodeLock* file_lock = nullptr;
45  db::mutate<Tags::H5FileLock>(
46  make_not_null(&box),
47  [&file_lock](
48  const gsl::not_null<Parallel::NodeLock*> in_file_lock) noexcept {
49  file_lock = in_file_lock;
50  });
51  node_lock->unlock();
52 
53  file_lock->lock();
54  // scoped to close file
55  {
56  const auto& file_prefix = Parallel::get<Tags::VolumeFileName>(cache);
57  auto& my_proxy =
58  Parallel::get_parallel_component<ParallelComponent>(cache);
60  file_prefix +
61  std::to_string(Parallel::my_node(*my_proxy.ckLocalBranch())) +
62  ".h5",
63  true);
64  const size_t version_number = 0;
65  auto& output_dataset =
66  h5file.try_insert<h5::Dat>(subfile_name, file_legend, version_number);
67  output_dataset.append(data_row);
68  h5file.close_current_object();
69  }
70  file_lock->unlock();
71  }
72 };
73 } // namespace ThreadedActions
74 } // namespace observers
h5::Dat::append
HIDDEN_SYMBOLS void append(const std::vector< double > &data)
AccessType.hpp
std::string
Parallel::GlobalCache
Definition: ElementReceiveInterpPoints.hpp:15
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:25
h5::H5File< h5::AccessType::ReadWrite >
Parallel::my_node
int my_node(const DistribObject &distributed_object) noexcept
Index of my node.
Definition: Info.hpp:51
DataBox.hpp
cstddef
ActionTesting::cache
Parallel::GlobalCache< Metavariables > & cache(MockRuntimeSystem< Metavariables > &runner, const ArrayIndex &array_index) noexcept
Returns the GlobalCache of Component with index array_index.
Definition: MockRuntimeSystemFreeFunctions.hpp:380
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:31
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
TMPL.hpp
gsl::not_null
Require a pointer to not be a nullptr
Definition: ReadSpecThirdOrderPiecewisePolynomial.hpp:13