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"
18 
19 /// \cond
20 namespace tuples {
21 template <typename...>
22 class TaggedTuple;
23 } // namespace tuples
24 namespace LinearSolver {
25 namespace gmres_detail {
26 template <typename Metavariables, typename FieldsTag>
27 struct ResidualMonitor;
28 template <typename FieldsTag, typename BroadcastTarget>
29 struct InitializeResidualMagnitude;
30 } // namespace gmres_detail
31 } // namespace LinearSolver
32 /// \endcond
33 
34 namespace LinearSolver {
35 namespace gmres_detail {
36 
37 template <typename FieldsTag, Initialization::MergePolicy MergePolicy =
38  Initialization::MergePolicy::Error>
39 struct InitializeElement {
40  private:
41  using fields_tag = FieldsTag;
42  using initial_fields_tag =
45  using operator_applied_to_fields_tag =
47  using operand_tag =
49  using orthogonalization_iteration_id_tag =
53 
54  public:
55  template <typename DbTagsList, typename... InboxTags, typename Metavariables,
56  typename ArrayIndex, typename ActionList,
57  typename ParallelComponent>
58  static auto apply(db::DataBox<DbTagsList>& box,
59  const tuples::TaggedTuple<InboxTags...>& /*inboxes*/,
61  const ArrayIndex& array_index, const ActionList /*meta*/,
62  const ParallelComponent* const /*meta*/) noexcept {
63  db::mutate<operand_tag>(
64  make_not_null(&box),
66  const db::const_item_type<source_tag>& source,
68  operator_applied_to_fields) noexcept {
69  *operand = source - operator_applied_to_fields;
70  },
71  get<source_tag>(box), get<operator_applied_to_fields_tag>(box));
72  const auto& operand = get<operand_tag>(box);
73 
74  Parallel::contribute_to_reduction<gmres_detail::InitializeResidualMagnitude<
75  FieldsTag, ParallelComponent>>(
76  Parallel::ReductionData<
78  inner_product(operand, operand)},
79  Parallel::get_parallel_component<ParallelComponent>(cache)[array_index],
81  ResidualMonitor<Metavariables, FieldsTag>>(cache));
82 
83  db::item_type<initial_fields_tag> x0(get<fields_tag>(box));
84  db::item_type<basis_history_tag> basis_history{};
85 
86  using compute_tags = db::AddComputeTags<
88  return std::make_tuple(
90  InitializeElement,
92  LinearSolver::Tags::IterationId, initial_fields_tag,
93  orthogonalization_iteration_id_tag, basis_history_tag,
95  compute_tags, MergePolicy>(
96  std::move(box),
97  // We have not started iterating yet, so we initialize the current
98  // iteration ID such that the _next_ iteration ID is zero.
99  std::numeric_limits<size_t>::max(), std::move(x0),
101  std::move(basis_history),
103  // Terminate algorithm for now. The `ResidualMonitor` will receive the
104  // reduction that is performed above and then broadcast to the following
105  // action, which is responsible for restarting the algorithm.
106  true);
107  }
108 };
109 
110 template <typename FieldsTag>
111 struct NormalizeInitialOperand {
112  private:
113  using fields_tag = FieldsTag;
114  using operand_tag =
116  using basis_history_tag = LinearSolver::Tags::KrylovSubspaceBasis<fields_tag>;
117 
118  public:
119  template <typename ParallelComponent, typename DbTagsList,
120  typename Metavariables, typename ArrayIndex,
121  typename DataBox = db::DataBox<DbTagsList>,
123  db::tag_is_retrievable_v<basis_history_tag, DataBox> and
125  DataBox>> = nullptr>
126  static void apply(db::DataBox<DbTagsList>& box,
128  const ArrayIndex& array_index,
129  const double residual_magnitude,
131  has_converged) noexcept {
132  db::mutate<operand_tag, basis_history_tag,
133  LinearSolver::Tags::HasConverged>(
134  make_not_null(&box),
135  [
136  residual_magnitude, &has_converged
140  local_has_converged) noexcept {
141  *operand /= residual_magnitude;
142  basis_history->push_back(*operand);
143  *local_has_converged = has_converged;
144  });
145 
146  // Proceed with algorithm
147  Parallel::get_parallel_component<ParallelComponent>(cache)[array_index]
148  .perform_algorithm(true);
149  }
150 };
151 
152 } // namespace gmres_detail
153 } // 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.
void mutate(const gsl::not_null< DataBox< TagList > *> box, Invokable &&invokable, Args &&... args) noexcept
Allows changing the state of one or more non-computed elements in the DataBox.
Definition: DataBox.hpp:1092
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
A set of vectors that form a basis of the -th Krylov subspace .
Definition: Tags.hpp:189
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
The prefix for tags related to an orthogonalization procedurce.
Definition: Tags.hpp:153
Functional for computing sqrt on an object.
Definition: Functional.hpp:273
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