InterpolationTargetVarsFromElement.hpp
1 // Distributed under the MIT License.
2 // See LICENSE.txt for details.
3 
4 #pragma once
5 
6 #include <unordered_map>
7 #include <utility>
8 #include <vector>
9 
12 #include "NumericalAlgorithms/Interpolation/InterpolationTargetDetail.hpp"
13 #include "NumericalAlgorithms/Interpolation/Tags.hpp"
14 #include "Utilities/Gsl.hpp"
15 #include "Utilities/TMPL.hpp"
16 
17 /// \cond
18 namespace Parallel {
19 template <typename Metavariables>
20 class GlobalCache;
21 } // namespace Parallel
22 /// \endcond
23 
24 namespace intrp {
25 namespace Actions {
26 /// \ingroup ActionsGroup
27 /// \brief Receives interpolated variables from an `Element` on a subset
28 /// of the target points.
29 ///
30 /// If interpolated variables for all target points have been received, then
31 /// - Calls `InterpolationTargetTag::post_interpolation_callback`
32 /// - Removes the finished `temporal_id` from `Tags::TemporalIds<TemporalId>`
33 /// and adds it to `Tags::CompletedTemporalIds<TemporalId>`
34 /// - Removes `Tags::InterpolatedVars<InterpolationTargetTag,TemporalId>`,
35 /// `Tags::IndicesOfFilledInterpPoints`, and
36 /// `Tags::IndicesOfInvalidInterpPoints` for the finished `temporal_id`.
37 ///
38 /// Uses:
39 /// - DataBox:
40 /// - `Tags::TemporalIds<TemporalId>`
41 /// - `Tags::IndicesOfFilledInterpPoints<TemporalId>`
42 /// - `Tags::InterpolatedVars<InterpolationTargetTag,TemporalId>`
43 /// - `Tags::InterpolatedVars<InterpolationTargetTag,TemporalId>`
44 /// - `::Tags::Variables<typename
45 /// InterpolationTargetTag::vars_to_interpolate_to_target>`
46 ///
47 /// DataBox changes:
48 /// - Adds: nothing
49 /// - Removes: nothing
50 /// - Modifies:
51 /// - `Tags::TemporalIds<TemporalId>`
52 /// - `Tags::CompletedTemporalIds<TemporalId>`
53 /// - `Tags::IndicesOfFilledInterpPoints<TemporalId>`
54 /// - `Tags::InterpolatedVars<InterpolationTargetTag,TemporalId>`
55 /// - `::Tags::Variables<typename
56 /// InterpolationTargetTag::vars_to_interpolate_to_target>`
57 ///
58 /// For requirements on InterpolationTargetTag, see InterpolationTarget
59 template <typename InterpolationTargetTag>
61  /// For requirements on Metavariables, see InterpolationTarget
62  template <
63  typename ParallelComponent, typename DbTags, typename Metavariables,
64  typename ArrayIndex, typename TemporalId,
66  nullptr>
67  static void apply(
68  db::DataBox<DbTags>& box,
70  const ArrayIndex& /*array_index*/,
71  const std::vector<Variables<
72  typename InterpolationTargetTag::vars_to_interpolate_to_target>>&
73  vars_src,
74  const std::vector<std::vector<size_t>>& global_offsets,
75  const TemporalId& temporal_id) noexcept {
76  static_assert(
77  not InterpolationTargetTag::compute_target_points::is_sequential::value,
78  "Use InterpolationTargetGetVarsFromElement only with non-sequential"
79  " compute_target_points");
80  // Check if we already have completed interpolation at this
81  // temporal_id.
82  const auto& completed_ids =
83  db::get<Tags::CompletedTemporalIds<TemporalId>>(box);
84  // (Search from the end because temporal_id is more likely to be
85  // at the end of the list then at the beginning.)
86  if (UNLIKELY(std::find(completed_ids.rbegin(), completed_ids.rend(),
87  temporal_id) != completed_ids.rend())) {
88  // The code will get into this 'if' statement in the following
89  // scenario:
90  // - There is at least one interpolation point exactly on the
91  // boundary of two or more Elements, so that
92  // InterpolationTargetVarsFromElement is called more than once
93  // with data for the same interpolation point (this is ok,
94  // and add_received_variables handles this).
95  // - The only Elements that have not yet called
96  // InterpolationTargetVarsFromElement for this temporal_id are
97  // those that have data only for duplicated interpolation
98  // points, and the InterpolationTarget has already received
99  // that data from other Elements.
100  // In this case, the InterpolationTarget proceeds to do its
101  // work because it has all the data it needs. There is now
102  // one more condition needed for the scenario that gets
103  // us inside this 'if':
104  // - The InterpolationTarget has already completed its work at
105  // this temporal_id, and it has cleaned up its data structures
106  // for this temporal_id before all of the remaining calls to
107  // InterpolationTargetVarsFromElement have occurred at this
108  // temporal_id, and now we are in one of those remaining
109  // calls.
110  //
111  // If this scenario occurs, we just return. This is because the
112  // InterpolationTarget is done and there is nothing left to do
113  // at this temporal_id. Note that if there were extra work to
114  // do at this temporal_id, then CompletedTemporalIds would not
115  // have an entry for this temporal_id.
116  return;
117  }
118 
119  // Call set_up_interpolation only if it has not been called for this
120  // temporal_id.
121  // If flag_temporal_ids_for_interpolation returns an empty list, then
122  // flag_temporal_ids_for_interpolation has already been called for the
123  // same temporal_id (by an invocation of InterpolationTargetVarsFromElement
124  // by a different Element) and hence set_up_interpolation has already
125  // been called.
126  if (not InterpolationTarget_detail::flag_temporal_ids_for_interpolation<
127  InterpolationTargetTag>(make_not_null(&box),
128  std::vector<TemporalId>{{temporal_id}})
129  .empty()) {
130  InterpolationTarget_detail::set_up_interpolation<InterpolationTargetTag>(
131  make_not_null(&box), temporal_id,
132  InterpolationTarget_detail::block_logical_coords<
133  InterpolationTargetTag>(box, tmpl::type_<Metavariables>{}));
134  }
135 
136  InterpolationTarget_detail::add_received_variables<InterpolationTargetTag>(
137  make_not_null(&box), vars_src, global_offsets, temporal_id);
138 
139  if (InterpolationTarget_detail::have_data_at_all_points<
140  InterpolationTargetTag>(box, temporal_id)) {
141  // All the valid points have been interpolated.
142  // We throw away the return value of call_callback in this case
143  // (it is known to be always true; it can be false only for
144  // sequential interpolations, which is static-asserted against above).
145  InterpolationTarget_detail::call_callback<InterpolationTargetTag>(
146  make_not_null(&box), make_not_null(&cache), temporal_id);
147  InterpolationTarget_detail::clean_up_interpolation_target<
148  InterpolationTargetTag>(make_not_null(&box), temporal_id);
149  }
150  }
151 };
152 } // namespace Actions
153 } // namespace intrp
utility
UNLIKELY
#define UNLIKELY(x)
Definition: Gsl.hpp:73
Parallel::GlobalCache
Definition: ElementReceiveInterpPoints.hpp:15
intrp
Contains classes and functions for interpolation.
Definition: ElementInitInterpPoints.hpp:23
vector
intrp::Actions::InterpolationTargetVarsFromElement
Receives interpolated variables from an Element on a subset of the target points.
Definition: InterpolationTargetVarsFromElement.hpp:60
DataBox.hpp
Variables.hpp
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:382
Gsl.hpp
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
intrp::Actions::InterpolationTargetVarsFromElement::apply
static void apply(db::DataBox< DbTags > &box, Parallel::GlobalCache< Metavariables > &cache, const ArrayIndex &, const std::vector< Variables< typename InterpolationTargetTag::vars_to_interpolate_to_target >> &vars_src, const std::vector< std::vector< size_t >> &global_offsets, const TemporalId &temporal_id) noexcept
For requirements on Metavariables, see InterpolationTarget.
Definition: InterpolationTargetVarsFromElement.hpp:67
unordered_map
Parallel
Functionality for parallelization.
Definition: ElementReceiveInterpPoints.hpp:13
TMPL.hpp
cpp20::find
constexpr InputIt find(InputIt first, InputIt last, const T &value)
Definition: Algorithm.hpp:136