Line data Source code
1 0 : // Distributed under the MIT License.
2 : // See LICENSE.txt for details.
3 :
4 : #pragma once
5 :
6 : #include <cstddef>
7 : #include <optional>
8 : #include <tuple>
9 : #include <type_traits>
10 : #include <utility>
11 :
12 : #include "DataStructures/DataBox/DataBox.hpp"
13 : #include "DataStructures/TaggedTuple.hpp"
14 : #include "Domain/Structure/DirectionalId.hpp"
15 : #include "Domain/Structure/Element.hpp"
16 : #include "Domain/Tags.hpp"
17 : #include "Evolution/DgSubcell/ActiveGrid.hpp"
18 : #include "Evolution/DgSubcell/GhostData.hpp"
19 : #include "Evolution/DgSubcell/Mesh.hpp"
20 : #include "Evolution/DgSubcell/Projection.hpp"
21 : #include "Evolution/DgSubcell/RdmpTciData.hpp"
22 : #include "Evolution/DgSubcell/Reconstruction.hpp"
23 : #include "Evolution/DgSubcell/ReconstructionMethod.hpp"
24 : #include "Evolution/DgSubcell/Tags/ActiveGrid.hpp"
25 : #include "Evolution/DgSubcell/Tags/CellCenteredFlux.hpp"
26 : #include "Evolution/DgSubcell/Tags/Coordinates.hpp"
27 : #include "Evolution/DgSubcell/Tags/DataForRdmpTci.hpp"
28 : #include "Evolution/DgSubcell/Tags/DidRollback.hpp"
29 : #include "Evolution/DgSubcell/Tags/GhostDataForReconstruction.hpp"
30 : #include "Evolution/DgSubcell/Tags/GhostZoneInverseJacobian.hpp"
31 : #include "Evolution/DgSubcell/Tags/InitialTciData.hpp"
32 : #include "Evolution/DgSubcell/Tags/Interpolators.hpp"
33 : #include "Evolution/DgSubcell/Tags/Jacobians.hpp"
34 : #include "Evolution/DgSubcell/Tags/Mesh.hpp"
35 : #include "Evolution/DgSubcell/Tags/MeshForGhostData.hpp"
36 : #include "Evolution/DgSubcell/Tags/ReconstructionOrder.hpp"
37 : #include "Evolution/DgSubcell/Tags/StepsSinceTciCall.hpp"
38 : #include "Evolution/DgSubcell/Tags/SubcellOptions.hpp"
39 : #include "Evolution/DgSubcell/Tags/TciCallsSinceRollback.hpp"
40 : #include "Evolution/DgSubcell/Tags/TciGridHistory.hpp"
41 : #include "Evolution/DgSubcell/Tags/TciStatus.hpp"
42 : #include "Evolution/Initialization/SetVariables.hpp"
43 : #include "NumericalAlgorithms/Interpolation/IrregularInterpolant.hpp"
44 : #include "NumericalAlgorithms/Spectral/Basis.hpp"
45 : #include "NumericalAlgorithms/Spectral/Mesh.hpp"
46 : #include "Parallel/AlgorithmExecution.hpp"
47 : #include "Parallel/GlobalCache.hpp"
48 : #include "Utilities/Algorithm.hpp"
49 : #include "Utilities/CallWithDynamicType.hpp"
50 : #include "Utilities/ContainerHelpers.hpp"
51 : #include "Utilities/ErrorHandling/Error.hpp"
52 : #include "Utilities/TMPL.hpp"
53 :
54 : /// \cond
55 : namespace Tags {
56 : template <typename Tag>
57 : struct HistoryEvolvedVariables;
58 : } // namespace Tags
59 : /// \endcond
60 :
61 : namespace evolution::dg::subcell::Actions {
62 : /*!
63 : * \brief Initialize the subcell grid, including the size of the evolved
64 : * `Variables` and, if present, primitive `Variables`.
65 : *
66 : * By default sets the element to `subcell::ActiveGrid::Subcell` unless it
67 : * is not allowed to use subcell either because it is at an external boundary
68 : * or because it or one of its neighbors has been marked as DG-only.
69 : *
70 : * GlobalCache:
71 : * - Uses:
72 : * - `subcell::Tags::SubcellOptions`
73 : *
74 : * DataBox:
75 : * - Uses:
76 : * - `domain::Tags::Mesh<Dim>`
77 : * - `domain::Tags::Element<Dim>`
78 : * - `System::variables_tag`
79 : * - Adds:
80 : * - `subcell::Tags::Mesh<Dim>`
81 : * - `subcell::Tags::MeshForGhostData<Dim>`
82 : * - `subcell::Tags::ActiveGrid`
83 : * - `subcell::Tags::DidRollback`
84 : * - `subcell::Tags::TciGridHistory`
85 : * - `subcell::Tags::TciCallsSinceRollback`
86 : * - `subcell::Tags::GhostDataForReconstruction<Dim>`
87 : * - `subcell::Tags::GhostZoneInverseJacobian<Dim>`
88 : * - `subcell::Tags::TciDecision`
89 : * - `subcell::Tags::DataForRdmpTci`
90 : * - `subcell::fd::Tags::InverseJacobianLogicalToGrid<Dim>`
91 : * - `subcell::fd::Tags::DetInverseJacobianLogicalToGrid`
92 : * - `subcell::Tags::LogicalCoordinates<Dim>`
93 : * - `subcell::Tags::ReconstructionOrder<Dim>` (set as `std::nullopt`)
94 : * - `subcell::Tags::Coordinates<Dim, Frame::Grid>` (as compute tag)
95 : * - `subcell::Tags::Coordinates<Dim, Frame::Inertial>` (as compute tag)
96 : * - Removes: nothing
97 : * - Modifies:
98 : * - `System::variables_tag` and `System::primitive_variables_tag` if the cell
99 : * is troubled
100 : * - `Tags::dt<System::variables_tag>` if the cell is troubled
101 : */
102 : template <size_t Dim, typename System, bool UseNumericInitialData>
103 1 : struct SetSubcellGrid {
104 0 : using const_global_cache_tags = tmpl::list<Tags::SubcellOptions<Dim>>;
105 :
106 0 : using simple_tags = tmpl::list<
107 : Tags::ActiveGrid, Tags::DidRollback, Tags::TciGridHistory,
108 : Tags::TciCallsSinceRollback, Tags::StepsSinceTciCall,
109 : evolution::dg::subcell::Tags::MeshForGhostData<Dim>,
110 : Tags::GhostDataForReconstruction<Dim>,
111 : Tags::GhostZoneInverseJacobian<Dim>, Tags::TciDecision,
112 : Tags::NeighborTciDecisions<Dim>, Tags::DataForRdmpTci,
113 : subcell::Tags::CellCenteredFlux<typename System::flux_variables, Dim>,
114 : subcell::Tags::ReconstructionOrder<Dim>,
115 : evolution::dg::subcell::Tags::InterpolatorsFromFdToNeighborFd<Dim>,
116 : evolution::dg::subcell::Tags::InterpolatorsFromDgToNeighborFd<Dim>,
117 : evolution::dg::subcell::Tags::InterpolatorsFromNeighborDgToFd<Dim>,
118 : typename System::variables_tag,
119 : evolution::dg::subcell::Tags::ExtensionDirections<Dim>>;
120 0 : using compute_tags =
121 : tmpl::list<Tags::MeshCompute<Dim>, Tags::LogicalCoordinatesCompute<Dim>,
122 : ::domain::Tags::MappedCoordinates<
123 : ::domain::Tags::ElementMap<Dim, Frame::Grid>,
124 : subcell::Tags::Coordinates<Dim, Frame::ElementLogical>,
125 : subcell::Tags::Coordinates>,
126 : Tags::InertialCoordinatesCompute<
127 : ::domain::CoordinateMaps::Tags::CoordinateMap<
128 : Dim, Frame::Grid, Frame::Inertial>>,
129 : fd::Tags::InverseJacobianLogicalToGridCompute<
130 : ::domain::Tags::ElementMap<Dim, Frame::Grid>, Dim>,
131 : fd::Tags::DetInverseJacobianLogicalToGridCompute<Dim>,
132 : fd::Tags::InverseJacobianLogicalToInertialCompute<
133 : ::domain::CoordinateMaps::Tags::CoordinateMap<
134 : Dim, Frame::Grid, Frame::Inertial>,
135 : Dim>,
136 : fd::Tags::DetInverseJacobianLogicalToInertialCompute<
137 : ::domain::CoordinateMaps::Tags::CoordinateMap<
138 : Dim, Frame::Grid, Frame::Inertial>,
139 : Dim>>;
140 :
141 : template <typename DbTagsList, typename... InboxTags, typename ArrayIndex,
142 : typename ActionList, typename ParallelComponent,
143 : typename Metavariables>
144 0 : static Parallel::iterable_action_return_t apply(
145 : db::DataBox<DbTagsList>& box,
146 : [[maybe_unused]] const tuples::TaggedTuple<InboxTags...>& inboxes,
147 : [[maybe_unused]] const Parallel::GlobalCache<Metavariables>& cache,
148 : [[maybe_unused]] const ArrayIndex& array_index, ActionList /*meta*/,
149 : const ParallelComponent* const /*meta*/) {
150 : const SubcellOptions& subcell_options =
151 : db::get<Tags::SubcellOptions<Dim>>(box);
152 : const Mesh<Dim>& dg_mesh = db::get<::domain::Tags::Mesh<Dim>>(box);
153 : const Mesh<Dim>& subcell_mesh = db::get<subcell::Tags::Mesh<Dim>>(box);
154 : const Element<Dim>& element = db::get<::domain::Tags::Element<Dim>>(box);
155 :
156 : for (size_t d = 0; d < Dim; ++d) {
157 : if (subcell_options.persson_num_highest_modes() >= dg_mesh.extents(d) and
158 : dg_mesh.basis(d) != Spectral::Basis::Cartoon) {
159 : ERROR("Number of the highest modes to be monitored by the Persson TCI ("
160 : << subcell_options.persson_num_highest_modes()
161 : << ") must be smaller than the extent of the DG mesh ("
162 : << dg_mesh.extents(d) << ").");
163 : }
164 : }
165 :
166 : // Loop over block neighbors and if neighbor id is inside of
167 : // subcell_options.only_dg_block_ids(), then bordering DG-only block
168 : const bool bordering_dg_block = alg::any_of(
169 : element.neighbors(),
170 : [&subcell_options](const auto& direction_and_neighbor) {
171 : const size_t first_block_id =
172 : direction_and_neighbor.second.ids().begin()->block_id();
173 : return alg::found(subcell_options.only_dg_block_ids(),
174 : first_block_id);
175 : });
176 :
177 : const bool subcell_allowed_in_element =
178 : not alg::found(subcell_options.only_dg_block_ids(),
179 : element.id().block_id()) and
180 : not bordering_dg_block;
181 : const bool cell_is_not_on_external_boundary =
182 : db::get<::domain::Tags::Element<Dim>>(box)
183 : .external_boundaries()
184 : .empty();
185 :
186 : constexpr bool subcell_enabled_at_external_boundary =
187 : Metavariables::SubcellOptions::subcell_enabled_at_external_boundary;
188 :
189 : db::mutate<Tags::NeighborTciDecisions<Dim>>(
190 : [&element](const auto neighbor_decisions_ptr) {
191 : neighbor_decisions_ptr->clear();
192 : for (const auto& [direction, neighbors_in_direction] :
193 : element.neighbors()) {
194 : for (const auto& neighbor : neighbors_in_direction.ids()) {
195 : neighbor_decisions_ptr->insert(
196 : std::pair{DirectionalId<Dim>{direction, neighbor}, 0});
197 : }
198 : }
199 : },
200 : make_not_null(&box));
201 :
202 : db::mutate_apply<
203 : tmpl::list<Tags::ActiveGrid, Tags::DidRollback,
204 : typename System::variables_tag, subcell::Tags::TciDecision,
205 : subcell::Tags::TciCallsSinceRollback,
206 : subcell::Tags::StepsSinceTciCall>,
207 : tmpl::list<>>(
208 : [&cell_is_not_on_external_boundary, &dg_mesh,
209 : subcell_allowed_in_element, &subcell_mesh](
210 : const gsl::not_null<ActiveGrid*> active_grid_ptr,
211 : const gsl::not_null<bool*> did_rollback_ptr,
212 : const auto active_vars_ptr,
213 : const gsl::not_null<int*> tci_decision_ptr,
214 : const gsl::not_null<size_t*> tci_calls_since_rollback_ptr,
215 : const gsl::not_null<size_t*> steps_since_tci_call_ptr) {
216 : // We don't consider setting the initial grid to subcell as rolling
217 : // back. Since no time step is undone, we just continue on the
218 : // subcells as a normal solve.
219 : *did_rollback_ptr = false;
220 :
221 : if ((cell_is_not_on_external_boundary or
222 : subcell_enabled_at_external_boundary) and
223 : subcell_allowed_in_element) {
224 : *active_grid_ptr = ActiveGrid::Subcell;
225 : active_vars_ptr->initialize(subcell_mesh.number_of_grid_points());
226 : } else {
227 : *active_grid_ptr = ActiveGrid::Dg;
228 : active_vars_ptr->initialize(dg_mesh.number_of_grid_points());
229 : }
230 :
231 : *tci_decision_ptr = 0;
232 : *tci_calls_since_rollback_ptr = 0;
233 : *steps_since_tci_call_ptr = 0;
234 : },
235 : make_not_null(&box));
236 : if constexpr (System::has_primitive_and_conservative_vars) {
237 : db::mutate<typename System::primitive_variables_tag>(
238 : [&dg_mesh, &subcell_mesh](const auto prim_vars_ptr,
239 : const auto active_grid) {
240 : if (active_grid == ActiveGrid::Dg) {
241 : prim_vars_ptr->initialize(dg_mesh.number_of_grid_points());
242 : } else {
243 : prim_vars_ptr->initialize(subcell_mesh.number_of_grid_points());
244 : }
245 : },
246 : make_not_null(&box), db::get<Tags::ActiveGrid>(box));
247 : }
248 : if constexpr (not UseNumericInitialData) {
249 : if (db::get<Tags::ActiveGrid>(box) ==
250 : evolution::dg::subcell::ActiveGrid::Dg) {
251 : evolution::Initialization::Actions::SetVariables<
252 : ::domain::Tags::Coordinates<Dim, Frame::ElementLogical>>::
253 : apply(box, inboxes, cache, array_index, ActionList{},
254 : std::add_pointer_t<ParallelComponent>{nullptr});
255 : } else {
256 : evolution::Initialization::Actions::
257 : SetVariables<Tags::Coordinates<Dim, Frame::ElementLogical>>::apply(
258 : box, inboxes, cache, array_index, ActionList{},
259 : std::add_pointer_t<ParallelComponent>{nullptr});
260 : }
261 : }
262 : return {Parallel::AlgorithmExecution::Continue, std::nullopt};
263 : }
264 : };
265 :
266 : /*!
267 : * \brief Sets the RDMP data from the initial data and sends it to neighboring
268 : * elements.
269 : *
270 : * GlobalCache:
271 : * - Uses:
272 : * - `ParallelComponent` proxy
273 : *
274 : * DataBox:
275 : * - Uses:
276 : * - `domain::Tags::Element<Dim>`
277 : * - `subcell::Tags::DataForRdmpTci`
278 : * - `subcell::Tags::InitialTciData`
279 : * - whatever `SetInitialRdmpData` uses
280 : * - Adds: nothing
281 : * - Removes: nothing
282 : * - Modifies:
283 : * - whatever `SetInitialRdmpData` mutates
284 : */
285 : template <size_t Dim, typename SetInitialRdmpData>
286 1 : struct SetAndCommunicateInitialRdmpData {
287 0 : using inbox_tags =
288 : tmpl::list<evolution::dg::subcell::Tags::InitialTciData<Dim>>;
289 :
290 : template <typename DbTagsList, typename... InboxTags, typename ArrayIndex,
291 : typename ActionList, typename ParallelComponent,
292 : typename Metavariables>
293 0 : static Parallel::iterable_action_return_t apply(
294 : db::DataBox<DbTagsList>& box,
295 : const tuples::TaggedTuple<InboxTags...>& /*inboxes*/,
296 : Parallel::GlobalCache<Metavariables>& cache,
297 : const ArrayIndex& /*array_index*/, ActionList /*meta*/,
298 : const ParallelComponent* const /*meta*/) {
299 : // Get the RDMP data on this element and then initialize it.
300 : db::mutate_apply<SetInitialRdmpData>(make_not_null(&box));
301 :
302 : // Send RDMP data to neighbors
303 : const auto& element = db::get<domain::Tags::Element<Dim>>(box);
304 : const auto& rdmp_data =
305 : db::get<evolution::dg::subcell::Tags::DataForRdmpTci>(box);
306 : auto& receiver_proxy =
307 : Parallel::get_parallel_component<ParallelComponent>(cache);
308 : for (const auto& [direction, neighbors] : element.neighbors()) {
309 : for (const auto& neighbor : neighbors) {
310 : const auto& orientation = neighbors.orientation(neighbor);
311 : const auto direction_from_neighbor = orientation(direction.opposite());
312 : evolution::dg::subcell::InitialTciData data{{}, rdmp_data};
313 : // We use temporal ID 0 for sending RDMP data
314 : const int temporal_id = 0;
315 : Parallel::receive_data<
316 : evolution::dg::subcell::Tags::InitialTciData<Dim>>(
317 : receiver_proxy[neighbor], temporal_id,
318 : std::make_pair(
319 : DirectionalId<Dim>{direction_from_neighbor, element.id()},
320 : std::move(data)));
321 : }
322 : }
323 :
324 : return {Parallel::AlgorithmExecution::Continue, std::nullopt};
325 : }
326 : };
327 :
328 : /*!
329 : * \brief Apply the TCI on the FD grid to the initial data and send the TCI
330 : * decision to neighboring elements.
331 : *
332 : * GlobalCache:
333 : * - Uses:
334 : * - `ParallelComponent` proxy
335 : *
336 : * DataBox:
337 : * - Uses:
338 : * - `domain::Tags::Element<Dim>`
339 : * - `subcell::Tags::DataForRdmpTci`
340 : * - `subcell::Tags::InitialTciData`
341 : * - `subcell::Tags::SubcellOptions`
342 : * - `subcell::Tags::ActiveGrid`
343 : * - whatever `TciOnFdGridMutator` uses
344 : * - Adds: nothing
345 : * - Removes: nothing
346 : * - Modifies:
347 : * - `subcell::Tags::DataForRdmpTci`
348 : * - `subcell::Tags::TciDecision`
349 : */
350 : template <size_t Dim, typename System, typename TciOnFdGridMutator>
351 1 : struct ComputeAndSendTciOnInitialGrid {
352 0 : using inbox_tags =
353 : tmpl::list<evolution::dg::subcell::Tags::InitialTciData<Dim>>;
354 :
355 : template <typename DbTagsList, typename... InboxTags, typename ArrayIndex,
356 : typename ActionList, typename ParallelComponent,
357 : typename Metavariables>
358 0 : static Parallel::iterable_action_return_t apply(
359 : db::DataBox<DbTagsList>& box, tuples::TaggedTuple<InboxTags...>& inboxes,
360 : Parallel::GlobalCache<Metavariables>& cache,
361 : const ArrayIndex& /*array_index*/, ActionList /*meta*/,
362 : const ParallelComponent* const /*meta*/) {
363 : const Element<Dim>& element = db::get<::domain::Tags::Element<Dim>>(box);
364 :
365 : // Check if we have received all RDMP data.
366 : if (LIKELY(element.number_of_neighbors() != 0)) {
367 : auto& inbox =
368 : tuples::get<evolution::dg::subcell::Tags::InitialTciData<Dim>>(
369 : inboxes);
370 : const auto& received = inbox.find(0);
371 : if (received == inbox.end() or
372 : received->second.size() != element.number_of_neighbors()) {
373 : return {Parallel::AlgorithmExecution::Retry, std::nullopt};
374 : }
375 :
376 : db::mutate<evolution::dg::subcell::Tags::DataForRdmpTci>(
377 : [&element, &received](const auto rdmp_tci_data_ptr) {
378 : (void)element;
379 : const size_t number_of_rdmp_vars =
380 : rdmp_tci_data_ptr->max_variables_values.size();
381 : ASSERT(rdmp_tci_data_ptr->max_variables_values.size() ==
382 : number_of_rdmp_vars,
383 : "The number of local max vars is "
384 : << number_of_rdmp_vars
385 : << " while the number of local min vars is "
386 : << rdmp_tci_data_ptr->max_variables_values.size()
387 : << " the local element ID is " << element.id());
388 : for (const auto& [direction_and_neighbor_element_id,
389 : neighbor_initial_tci_data] : received->second) {
390 : ASSERT(neighbor_initial_tci_data.initial_rdmp_data.has_value(),
391 : "Neighbor in direction "
392 : << direction_and_neighbor_element_id.direction()
393 : << " with element ID "
394 : << direction_and_neighbor_element_id.id() << " of "
395 : << element.id()
396 : << " didn't send initial TCI data correctly");
397 : ASSERT(
398 : neighbor_initial_tci_data.initial_rdmp_data.value()
399 : .max_variables_values.size() == number_of_rdmp_vars,
400 : "The number of local RDMP vars is "
401 : << number_of_rdmp_vars
402 : << " while the number of remote max vars is "
403 : << neighbor_initial_tci_data.initial_rdmp_data.value()
404 : .max_variables_values.size()
405 : << " the local element ID is " << element.id()
406 : << " and the remote id is "
407 : << direction_and_neighbor_element_id.id());
408 : ASSERT(
409 : neighbor_initial_tci_data.initial_rdmp_data.value()
410 : .min_variables_values.size() == number_of_rdmp_vars,
411 : "The number of local RDMP vars is "
412 : << number_of_rdmp_vars
413 : << " while the number of remote min vars is "
414 : << neighbor_initial_tci_data.initial_rdmp_data.value()
415 : .min_variables_values.size()
416 : << " the local element ID is " << element.id()
417 : << " and the remote id is "
418 : << direction_and_neighbor_element_id.id());
419 : for (size_t var_index = 0; var_index < number_of_rdmp_vars;
420 : ++var_index) {
421 : rdmp_tci_data_ptr->max_variables_values[var_index] =
422 : std::max(rdmp_tci_data_ptr->max_variables_values[var_index],
423 : neighbor_initial_tci_data.initial_rdmp_data.value()
424 : .max_variables_values[var_index]);
425 : rdmp_tci_data_ptr->min_variables_values[var_index] =
426 : std::min(rdmp_tci_data_ptr->min_variables_values[var_index],
427 : neighbor_initial_tci_data.initial_rdmp_data.value()
428 : .min_variables_values[var_index]);
429 : }
430 : }
431 : },
432 : make_not_null(&box));
433 : inbox.erase(received);
434 : }
435 :
436 : const auto send_tci_decision = [&cache, &element](const int tci_decision) {
437 : if (UNLIKELY(element.number_of_neighbors() == 0)) {
438 : return;
439 : }
440 : auto& receiver_proxy =
441 : Parallel::get_parallel_component<ParallelComponent>(cache);
442 : for (const auto& [direction, neighbors] : element.neighbors()) {
443 : for (const auto& neighbor : neighbors) {
444 : const auto& orientation = neighbors.orientation(neighbor);
445 : const auto direction_from_neighbor =
446 : orientation(direction.opposite());
447 : evolution::dg::subcell::InitialTciData data{tci_decision, {}};
448 : // We use temporal ID 1 for ending the TCI decision.
449 : const int temporal_id = 1;
450 : Parallel::receive_data<
451 : evolution::dg::subcell::Tags::InitialTciData<Dim>>(
452 : receiver_proxy[neighbor], temporal_id,
453 : std::make_pair(
454 : DirectionalId<Dim>{direction_from_neighbor, element.id()},
455 : std::move(data)));
456 : }
457 : }
458 : };
459 :
460 : const SubcellOptions& subcell_options =
461 : db::get<Tags::SubcellOptions<Dim>>(box);
462 :
463 : if (subcell_options.always_use_subcells() or
464 : get<Tags::ActiveGrid>(box) == ActiveGrid::Dg) {
465 : db::mutate<Tags::TciDecision>(
466 : [](const gsl::not_null<int*> tci_decision_ptr) {
467 : *tci_decision_ptr = 0;
468 : },
469 : make_not_null(&box));
470 : send_tci_decision(0);
471 : return {Parallel::AlgorithmExecution::Continue, std::nullopt};
472 : }
473 :
474 : // Now run the TCI to see if we could switch back to DG.
475 : const std::tuple<int, evolution::dg::subcell::RdmpTciData> tci_result =
476 : db::mutate_apply<TciOnFdGridMutator>(
477 : make_not_null(&box), subcell_options.persson_exponent() + 1.0,
478 : false);
479 :
480 : db::mutate<Tags::TciDecision>(
481 : [&tci_result](const gsl::not_null<int*> tci_decision_ptr) {
482 : *tci_decision_ptr = std::get<0>(tci_result);
483 : },
484 : make_not_null(&box));
485 : send_tci_decision(std::get<0>(tci_result));
486 :
487 : return {Parallel::AlgorithmExecution::Continue, std::nullopt};
488 : }
489 : };
490 :
491 : /*!
492 : * \brief Using the local and neighboring TCI decisions, switches the element to
493 : * DG if the DG solution was determined to be admissible.
494 : *
495 : * GlobalCache:
496 : * - Uses:
497 : * - `ParallelComponent` proxy
498 : *
499 : * DataBox:
500 : * - Uses:
501 : * - `domain::Tags::Element<Dim>`
502 : * - `subcell::Tags::DataForRdmpTci`
503 : * - `subcell::Tags::InitialTciData`
504 : * - `subcell::Tags::SubcellOptions`
505 : * - `subcell::Tags::ActiveGrid`
506 : * - whatever `TciOnFdGridMutator` uses
507 : * - Adds: nothing
508 : * - Removes: nothing
509 : * - Modifies:
510 : * - `subcell::Tags::NeighborTciDecisions`
511 : * - `System::variables_tag`
512 : * - `Tags::HistoryEvolvedVariables<System::variables_tag>`
513 : * - `subcell::Tags::GhostDataForReconstruction`
514 : * - `subcell::Tags::TciGridHistory`
515 : * - `subcell::Tags::CellCenteredFlux`
516 : */
517 : template <size_t Dim, typename System>
518 1 : struct SetInitialGridFromTciData {
519 : template <typename DbTagsList, typename... InboxTags, typename ArrayIndex,
520 : typename ActionList, typename ParallelComponent,
521 : typename Metavariables>
522 0 : static Parallel::iterable_action_return_t apply(
523 : db::DataBox<DbTagsList>& box, tuples::TaggedTuple<InboxTags...>& inboxes,
524 : const Parallel::GlobalCache<Metavariables>& /*cache*/,
525 : const ArrayIndex& /*array_index*/, ActionList /*meta*/,
526 : const ParallelComponent* const /*meta*/) {
527 : const Element<Dim>& element = db::get<::domain::Tags::Element<Dim>>(box);
528 : if (LIKELY(element.number_of_neighbors() != 0)) {
529 : auto& inbox =
530 : tuples::get<evolution::dg::subcell::Tags::InitialTciData<Dim>>(
531 : inboxes);
532 : const auto& received = inbox.find(1);
533 : // Check if we have received all TCI decisions.
534 : if (received == inbox.end() or
535 : received->second.size() != element.number_of_neighbors()) {
536 : return {Parallel::AlgorithmExecution::Retry, std::nullopt};
537 : }
538 :
539 : db::mutate<evolution::dg::subcell::Tags::NeighborTciDecisions<Dim>>(
540 : [&element, &received](const auto neighbor_tci_decisions_ptr) {
541 : for (const auto& [directional_element_id,
542 : neighbor_initial_tci_data] : received->second) {
543 : (void)element;
544 : ASSERT(neighbor_initial_tci_data.tci_status.has_value(),
545 : "Neighbor in direction "
546 : << directional_element_id.direction()
547 : << " with element ID " << directional_element_id.id()
548 : << " of " << element.id()
549 : << " didn't send initial TCI decision correctly");
550 : neighbor_tci_decisions_ptr->at(directional_element_id) =
551 : neighbor_initial_tci_data.tci_status.value();
552 : }
553 : },
554 : make_not_null(&box));
555 : inbox.erase(received);
556 : }
557 :
558 : if (get<Tags::ActiveGrid>(box) == ActiveGrid::Dg) {
559 : // In this case we are allowed to only do DG in this element. No need to
560 : // even do any checks.
561 : return {Parallel::AlgorithmExecution::Continue, std::nullopt};
562 : }
563 :
564 : const SubcellOptions& subcell_options =
565 : db::get<Tags::SubcellOptions<Dim>>(box);
566 :
567 : bool cell_is_troubled =
568 : subcell_options.always_use_subcells() or
569 : (subcell_options.use_halo() and [&box]() -> bool {
570 : for (const auto& [_, neighbor_decision] :
571 : db::get<evolution::dg::subcell::Tags::NeighborTciDecisions<Dim>>(
572 : box)) {
573 : if (neighbor_decision != 0) {
574 : return true;
575 : }
576 : }
577 : return false;
578 : }()) or
579 : (db::get<Tags::TciDecision>(box) != 0);
580 :
581 : if (not cell_is_troubled) {
582 : using variables_tag = typename System::variables_tag;
583 : using flux_variables = typename System::flux_variables;
584 :
585 : const Mesh<Dim>& dg_mesh = db::get<::domain::Tags::Mesh<Dim>>(box);
586 : const Mesh<Dim>& subcell_mesh = db::get<subcell::Tags::Mesh<Dim>>(box);
587 : db::mutate<
588 : variables_tag, ::Tags::HistoryEvolvedVariables<variables_tag>,
589 : Tags::ActiveGrid, subcell::Tags::GhostDataForReconstruction<Dim>,
590 : evolution::dg::subcell::Tags::TciGridHistory,
591 : evolution::dg::subcell::Tags::CellCenteredFlux<flux_variables, Dim>>(
592 : [&dg_mesh, &subcell_mesh, &subcell_options](
593 : const auto active_vars_ptr, const auto active_history_ptr,
594 : const gsl::not_null<ActiveGrid*> active_grid_ptr,
595 : const auto subcell_ghost_data_ptr,
596 : const gsl::not_null<
597 : std::deque<evolution::dg::subcell::ActiveGrid>*>
598 : tci_grid_history_ptr,
599 : const auto subcell_cell_centered_fluxes) {
600 : // Note: strictly speaking, to be conservative this should
601 : // reconstruct uJ instead of u.
602 : *active_vars_ptr = fd::reconstruct(
603 : *active_vars_ptr, dg_mesh, subcell_mesh.extents(),
604 : subcell_options.reconstruction_method());
605 :
606 : // Reconstruct the DG solution for each time in the time stepper
607 : // history
608 : active_history_ptr->map_entries(
609 : [&dg_mesh, &subcell_mesh, &subcell_options](const auto entry) {
610 : *entry =
611 : fd::reconstruct(*entry, dg_mesh, subcell_mesh.extents(),
612 : subcell_options.reconstruction_method());
613 : });
614 : *active_grid_ptr = ActiveGrid::Dg;
615 :
616 : // Clear the neighbor data needed for subcell reconstruction since
617 : // we have now completed the time step.
618 : subcell_ghost_data_ptr->clear();
619 :
620 : // Clear the TCI grid history since we don't need to use it when on
621 : // the DG grid.
622 : tci_grid_history_ptr->clear();
623 :
624 : // Clear the allocation for the cell-centered fluxes.
625 : *subcell_cell_centered_fluxes = std::nullopt;
626 : },
627 : make_not_null(&box));
628 : }
629 : return {Parallel::AlgorithmExecution::Continue, std::nullopt};
630 : }
631 : };
632 : } // namespace evolution::dg::subcell::Actions
|