InitializeElement.hpp
1 // Distributed under the MIT License.
2 // See LICENSE.txt for details.
3 
4 #pragma once
5 
6 #include <cstddef>
7 #include <limits>
8 
12 #include "Parallel/Info.hpp"
13 #include "Parallel/Invoke.hpp"
14 #include "Parallel/Reduction.hpp"
15 #include "ParallelAlgorithms/Initialization/MergeIntoDataBox.hpp"
19 
20 /// \cond
21 namespace tuples {
22 template <typename...>
23 class TaggedTuple;
24 } // namespace tuples
25 namespace LinearSolver {
26 namespace cg_detail {
27 template <typename Metavariables, typename FieldsTag>
28 struct ResidualMonitor;
29 template <typename FieldsTag, typename BroadcastTarget>
30 struct InitializeResidual;
31 } // namespace cg_detail
32 } // namespace LinearSolver
33 /// \endcond
34 
35 namespace LinearSolver {
36 namespace cg_detail {
37 
38 template <typename FieldsTag, Initialization::MergePolicy MergePolicy =
39  Initialization::MergePolicy::Error>
40 struct InitializeElement {
41  private:
42  using fields_tag = FieldsTag;
44  using operator_applied_to_fields_tag =
46  using operand_tag =
48  using residual_tag =
50 
51  public:
52  template <typename DbTagsList, typename... InboxTags, typename Metavariables,
53  typename ArrayIndex, typename ActionList,
54  typename ParallelComponent>
55  static auto apply(db::DataBox<DbTagsList>& box,
56  const tuples::TaggedTuple<InboxTags...>& /*inboxes*/,
58  const ArrayIndex& array_index, const ActionList /*meta*/,
59  const ParallelComponent* const /*meta*/) noexcept {
60  db::mutate<operand_tag>(
61  make_not_null(&box),
63  const db::const_item_type<source_tag>& source,
65  operator_applied_to_fields) noexcept {
66  *operand = source - operator_applied_to_fields;
67  },
68  get<source_tag>(box), get<operator_applied_to_fields_tag>(box));
69  auto residual = db::item_type<residual_tag>{get<operand_tag>(box)};
70 
71  // Perform global reduction to compute initial residual magnitude square for
72  // residual monitor
74  cg_detail::InitializeResidual<FieldsTag, ParallelComponent>>(
75  Parallel::ReductionData<
77  inner_product(residual, residual)},
78  Parallel::get_parallel_component<ParallelComponent>(cache)[array_index],
80  ResidualMonitor<Metavariables, FieldsTag>>(cache));
81 
82  using compute_tags = db::AddComputeTags<
84  return std::make_tuple(
86  InitializeElement,
89  compute_tags, MergePolicy>(
90  std::move(box),
91  // We have not started iterating yet, so we initialize the current
92  // iteration ID such that the _next_ iteration ID is zero.
93  std::numeric_limits<size_t>::max(), std::move(residual),
95  // Terminate algorithm for now. The `ResidualMonitor` will receive the
96  // reduction that is performed above and then broadcast to the following
97  // action, which is responsible for restarting the algorithm.
98  true);
99  }
100 };
101 
102 template <typename FieldsTag>
103 struct InitializeHasConverged {
104  template <typename ParallelComponent, typename DbTagsList,
105  typename Metavariables, typename ArrayIndex,
106  typename DataBox = db::DataBox<DbTagsList>,
108  DataBox>> = nullptr>
109  static void apply(db::DataBox<DbTagsList>& box,
111  const ArrayIndex& array_index,
113  has_converged) noexcept {
114  db::mutate<LinearSolver::Tags::HasConverged>(
115  make_not_null(&box), [&has_converged](
117  LinearSolver::Tags::HasConverged>*>
118  local_has_converged) noexcept {
119  *local_has_converged = has_converged;
120  });
121 
122  // Proceed with algorithm
123  Parallel::get_parallel_component<ParallelComponent>(cache)[array_index]
124  .perform_algorithm(true);
125  }
126 };
127 
128 } // namespace cg_detail
129 } // namespace LinearSolver
Prefix< DataBox_detail::dispatch_add_tag_prefix_impl< Prefix, Tag, Args... >, Args... > add_tag_prefix
Wrap Tag in Prefix<_, Args...>, also wrapping variables tags if Tag is a Tags::Variables.
Definition: DataBoxTag.hpp:616
Defines functions for interfacing with the parallelization framework.
Functionality for solving linear systems of equations.
Definition: TerminateIfConverged.hpp:22
Definition: TaggedTuple.hpp:27
Define prefixes for DataBox tags.
Holds a Convergence::HasConverged flag that signals the linear solver has converged, along with the reason for convergence.
Definition: Tags.hpp:204
void contribute_to_reduction(ReductionData< Ts... > reduction_data, const SenderProxy &sender_component, const TargetProxy &target_component) noexcept
Perform a reduction from the sender_component (typically your own parallel component) to the target_c...
Definition: Reduction.hpp:243
auto merge_into_databox(db::DataBox< DbTagsList > &&box, Args &&... args) noexcept
Add tags that are not yet in the DataBox.
Definition: MergeIntoDataBox.hpp:132
Holds an IterationId that identifies a step in the linear solver algorithm.
Definition: Tags.hpp:76
An associative container that is indexed by structs.
Definition: TaggedTuple.hpp:273
Defines classes and functions used for manipulating DataBox&#39;s.
tmpl::flatten< tmpl::list< Tags... > > AddSimpleTags
List of Tags to add to the DataBox.
Definition: DataBox.hpp:1222
constexpr bool tag_is_retrievable_v
Equal to true if Tag can be retrieved from a DataBox of type DataBoxType.
Definition: DataBox.hpp:67
Definition: InterpolationTargetWedgeSectionTorus.hpp:24
double inner_product(const Lhs &lhs, const Rhs &rhs) noexcept
The local part of the Euclidean inner product on the vector space w.r.t. which the addition and scala...
Definition: InnerProduct.hpp:71
A Charm++ chare that caches constant data once per Charm++ node.
Definition: ConstGlobalCache.hpp:135
constexpr auto apply(F &&f, const DataBox< BoxTags > &box, Args &&... args) noexcept
Apply the invokable f with argument Tags TagsList from DataBox box
Definition: DataBox.hpp:1623
Defines an inner product for the linear solver.
auto get_parallel_component(ConstGlobalCache< Metavariables > &cache) noexcept -> Parallel::proxy_from_parallel_component< ConstGlobalCache_detail::get_component_if_mocked< typename Metavariables::component_list, ParallelComponentTag >> &
Access the Charm++ proxy associated with a ParallelComponent.
Definition: ConstGlobalCache.hpp:222
typename DataBox_detail::item_type_impl< TagList, Tag >::type item_type
Get the type that can be written to the Tag. If it is a base tag then a TagList must be passed as a s...
Definition: DataBoxTag.hpp:461
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, but it may be necessary to perform the conversion explicitly when type deduction is desired.
Definition: Gsl.hpp:879
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
Defines class template ConstGlobalCache.
Defines DataBox tags for the linear solver.
The data to be reduced, and invokables to be called whenever two reduction messages are combined and ...
Definition: Reduction.hpp:64
Require a pointer to not be a nullptr
Definition: ConservativeFromPrimitive.hpp:12
Defines make_with_value.
tmpl::flatten< tmpl::list< Tags... > > AddComputeTags
List of Compute Item Tags to add to the DataBox.
Definition: DataBox.hpp:1243