Observe.hpp
1 // Distributed under the MIT License.
2 // See LICENSE.txt for details.
3 
4 #pragma once
5 
6 #include <cstddef>
7 #include <string>
8 #include <utility>
9 #include <vector>
10 
11 #include "IO/Observer/ObservationId.hpp"
12 #include "IO/Observer/ObserverComponent.hpp"
13 #include "IO/Observer/ReductionActions.hpp"
14 #include "IO/Observer/TypeOfObservation.hpp"
15 #include "Parallel/GlobalCache.hpp"
16 #include "Parallel/Invoke.hpp"
17 #include "Parallel/Reduction.hpp"
18 #include "Utilities/Functional.hpp"
19 #include "Utilities/PrettyType.hpp"
21 
22 namespace NonlinearSolver::observe_detail {
23 
24 using reduction_data = Parallel::ReductionData<
25  // Iteration
27  // Globalization step
29  // Residual
31  // Step length
33 
34 template <typename OptionsGroup>
35 struct Registration {
36  template <typename ParallelComponent, typename DbTagsList,
37  typename ArrayIndex>
39  register_info(const db::DataBox<DbTagsList>& /*box*/,
40  const ArrayIndex& /*array_index*/) noexcept {
42  observers::ObservationKey{pretty_type::get_name<OptionsGroup>()}};
43  }
44 };
45 
46 /*!
47  * \brief Contributes data from the residual monitor to the reduction observer
48  */
49 template <typename OptionsGroup, typename Metavariables>
50 void contribute_to_reduction_observer(
51  const size_t iteration_id, const size_t globalization_iteration_id,
52  const double residual_magnitude, const double step_length,
53  Parallel::GlobalCache<Metavariables>& cache) noexcept {
54  const auto observation_id = observers::ObservationId(
55  iteration_id, pretty_type::get_name<OptionsGroup>());
56  auto& reduction_writer = Parallel::get_parallel_component<
58  Parallel::threaded_action<observers::ThreadedActions::WriteReductionData>(
59  // Node 0 is always the writer, so directly call the component on that
60  // node
61  reduction_writer[0], observation_id, static_cast<size_t>(sys::my_node()),
62  std::string{"/" + Options::name<OptionsGroup>() + "Residuals"},
63  std::vector<std::string>{"Iteration", "GlobalizationStep", "Residual",
64  "StepLength"},
65  reduction_data{iteration_id, globalization_iteration_id,
66  residual_magnitude, step_length});
67 }
68 
69 } // namespace NonlinearSolver::observe_detail
observers::ObservationId
A unique identifier for an observation representing the type of observation and the instance (e....
Definition: ObservationId.hpp:71
std::string
utility
Parallel::GlobalCache
Definition: ElementReceiveInterpPoints.hpp:15
Parallel::ReductionDatum
The data to be reduced, and invokables to be called whenever two reduction messages are combined and ...
Definition: Reduction.hpp:63
std::pair
GlobalCache.hpp
Parallel::get_parallel_component
auto get_parallel_component(GlobalCache< Metavariables > &cache) noexcept -> Parallel::proxy_from_parallel_component< GlobalCache_detail::get_component_if_mocked< typename Metavariables::component_list, ParallelComponentTag >> &
Access the Charm++ proxy associated with a ParallelComponent.
Definition: GlobalCache.hpp:521
vector
PrettyType.hpp
observers::ObservationKey
Used as a key in maps to keep track of how many elements have registered.
Definition: ObservationId.hpp:28
ParallelInfo.hpp
observers::ObserverWriter
The nodegroup parallel component that is responsible for writing data to disk.
Definition: ObserverComponent.hpp:51
cstddef
observers::TypeOfObservation::Reduction
@ Reduction
The sender will only perform reduction observations.
Registration
Helpers for derived class registration.
Definition: Registration.hpp:32
string