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 <limits> 8 : #include <optional> 9 : #include <string> 10 : 11 : #include "ControlSystem/Averager.hpp" 12 : #include "ControlSystem/ControlErrors/Size.hpp" 13 : #include "ControlSystem/TimescaleTuner.hpp" 14 : #include "DataStructures/DataVector.hpp" 15 : #include "Domain/Structure/ObjectLabel.hpp" 16 : #include "IO/Logging/Verbosity.hpp" 17 : #include "Parallel/GlobalCache.hpp" 18 : #include "Parallel/Printf/Printf.hpp" 19 : #include "Utilities/Gsl.hpp" 20 : 21 : namespace control_system::size { 22 : /*! 23 : * \brief Updates the Averager with information from the \link 24 : * control_system::ControlErrors::Size Size control error \endlink. 25 : * 26 : * \details After we calculate the control error, we check if a discontinuous 27 : * change has occurred (the internal `control_system::size::State` changed). If 28 : * no discontinuous change has occurred, then we do nothing. If one did occur, 29 : * we get a history of control errors from the \link 30 : * control_system::ControlErrors::Size Size control error \endlink and use that 31 : * to repopulate the Averager history. 32 : * 33 : * \note See `control_system::Tags::Averager` for why the Averager is templated 34 : * on `DerivOrder - 1`. 35 : */ 36 : template <size_t DerivOrder, ::domain::ObjectLabel Horizon, 37 : typename Metavariables> 38 1 : void update_averager( 39 : const gsl::not_null<Averager<DerivOrder - 1>*> averager, 40 : const gsl::not_null<ControlErrors::Size<DerivOrder, Horizon>*> 41 : control_error, 42 : const Parallel::GlobalCache<Metavariables>& cache, const double time, 43 : const DataVector& current_timescale, 44 : const std::string& function_of_time_name, const int current_measurement) { 45 : if (control_error->discontinuous_change_has_occurred()) { 46 : control_error->reset(); 47 : averager->clear(); 48 : 49 : const std::deque<std::pair<double, double>> control_error_history = 50 : control_error->control_error_history(); 51 : 52 : if (Parallel::get<Tags::Verbosity>(cache) >= ::Verbosity::Verbose) { 53 : std::vector<double> history_times{}; 54 : for (const auto& history : control_error_history) { 55 : history_times.push_back(history.first); 56 : } 57 : Parallel::printf( 58 : "%s, time = %.16f: current measurement = %d, Discontinuous " 59 : "change has occurred. Repopulating averager with control error " 60 : "history from times: %s\n", 61 : function_of_time_name, time, current_measurement, history_times); 62 : } 63 : 64 : for (const auto& [stored_time, stored_control_error] : 65 : control_error_history) { 66 : // Size 1 because it's size control 67 : averager->update(stored_time, DataVector{1, stored_control_error}, 68 : current_timescale); 69 : } 70 : } 71 : } 72 : } // namespace control_system::size