Line data Source code
1 0 : // Distributed under the MIT License. 2 : // See LICENSE.txt for details. 3 : 4 : #pragma once 5 : 6 : #include <optional> 7 : 8 : #include "DataStructures/DataBox/DataBox.hpp" 9 : #include "DataStructures/DataBox/TagName.hpp" 10 : #include "Parallel/AlgorithmExecution.hpp" 11 : #include "Parallel/GlobalCache.hpp" 12 : #include "ParallelAlgorithms/EventsAndTriggers/Tags.hpp" 13 : #include "ParallelAlgorithms/EventsAndTriggers/WhenToCheck.hpp" 14 : #include "Time/SelfStart.hpp" 15 : #include "Time/Tags/Time.hpp" 16 : #include "Time/Triggers/OnSubsteps.hpp" 17 : #include "Utilities/Gsl.hpp" 18 : #include "Utilities/TMPL.hpp" 19 : #include "Utilities/TaggedTuple.hpp" 20 : 21 : /// \cond 22 : namespace Tags { 23 : struct TimeStepId; 24 : } // namespace Tags 25 : /// \endcond 26 : 27 : namespace evolution::Actions { 28 : namespace detail { 29 : template <typename DbTags, typename Metavariables, typename ArrayIndex, 30 : typename ParallelComponent, typename EventsAndTriggers_t> 31 : void run_events_and_triggers(db::DataBox<DbTags>& box, 32 : Parallel::GlobalCache<Metavariables>& cache, 33 : const ArrayIndex& array_index, 34 : const ParallelComponent* const component, 35 : const EventsAndTriggers_t& events_and_triggers, 36 : const TimeStepId& time_step_id) { 37 : if (time_step_id.substep() == 0) { 38 : events_and_triggers.run_events( 39 : make_not_null(&box), cache, array_index, component, 40 : {db::tag_name<::Tags::Time>(), db::get<::Tags::Time>(box)}); 41 : } else { 42 : const double substep_offset = 1.0e6; 43 : const double observation_value = 44 : time_step_id.step_time().value() + 45 : substep_offset * static_cast<double>(time_step_id.substep()); 46 : events_and_triggers.run_events( 47 : make_not_null(&box), cache, array_index, component, 48 : {db::tag_name<::Tags::Time>(), observation_value}, 49 : [&box](const Trigger& trigger) { 50 : const auto* substep_trigger = 51 : dynamic_cast<const ::Triggers::OnSubsteps*>(&trigger); 52 : return substep_trigger != nullptr and 53 : substep_trigger->is_triggered(box); 54 : }); 55 : } 56 : } 57 : } // namespace detail 58 : 59 : /// \ingroup ActionsGroup 60 : /// \ingroup EventsAndTriggersGroup 61 : /// \brief Run the events and triggers 62 : /// 63 : /// Triggers will only be checked on the first step of each slab to 64 : /// ensure that a consistent set of events is run across all elements. 65 : /// 66 : /// Uses: 67 : /// - GlobalCache: the EventsAndTriggers tag, as required by 68 : /// events and triggers 69 : /// - DataBox: as required by events and triggers 70 : /// 71 : /// DataBox changes: 72 : /// - Adds: nothing 73 : /// - Removes: nothing 74 : /// - Modifies: nothing 75 : template <Triggers::WhenToCheck WhenToCheck> 76 1 : struct RunEventsAndTriggers { 77 0 : using const_global_cache_tags = 78 : tmpl::list<::Tags::EventsAndTriggers<WhenToCheck>>; 79 : 80 : template <typename DbTags, typename... InboxTags, typename Metavariables, 81 : typename ArrayIndex, typename ActionList, 82 : typename ParallelComponent> 83 0 : static Parallel::iterable_action_return_t apply( 84 : db::DataBox<DbTags>& box, tuples::TaggedTuple<InboxTags...>& /*inboxes*/, 85 : Parallel::GlobalCache<Metavariables>& cache, 86 : const ArrayIndex& array_index, const ActionList /*meta*/, 87 : const ParallelComponent* const component) { 88 : const auto time_step_id = db::get<::Tags::TimeStepId>(box); 89 : if (SelfStart::is_self_starting(time_step_id)) { 90 : return {Parallel::AlgorithmExecution::Continue, std::nullopt}; 91 : } 92 : 93 : if constexpr (WhenToCheck != Triggers::WhenToCheck::AtSteps) { 94 : if (not time_step_id.step_time().is_at_slab_boundary()) { 95 : return {Parallel::AlgorithmExecution::Continue, std::nullopt}; 96 : } 97 : } 98 : 99 : const auto& events_and_triggers = 100 : Parallel::get<::Tags::EventsAndTriggers<WhenToCheck>>(cache); 101 : detail::run_events_and_triggers(box, cache, array_index, component, 102 : events_and_triggers, time_step_id); 103 : return {Parallel::AlgorithmExecution::Continue, std::nullopt}; 104 : } 105 : }; 106 : } // namespace evolution::Actions