Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow timescale tuner to not decrease timescale #5583

Merged
merged 1 commit into from
Oct 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 18 additions & 8 deletions src/ControlSystem/CalculateMeasurementTimescales.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,31 @@
#include "ControlSystem/Controller.hpp"
#include "ControlSystem/TimescaleTuner.hpp"
#include "DataStructures/DataVector.hpp"
#include "Utilities/GenerateInstantiations.hpp"

namespace control_system {
template <size_t DerivOrder>
template <size_t DerivOrder, bool AllowDecrease>
DataVector calculate_measurement_timescales(
const ::Controller<DerivOrder>& controller, const ::TimescaleTuner& tuner,
const ::Controller<DerivOrder>& controller,
const ::TimescaleTuner<AllowDecrease>& tuner,
const int measurements_per_update) {
return tuner.current_timescale() * controller.get_update_fraction() /
static_cast<double>(measurements_per_update);
}

template DataVector calculate_measurement_timescales<2>(
const ::Controller<2>& controller, const ::TimescaleTuner& tuner,
const int measurements_per_update);
template DataVector calculate_measurement_timescales<3>(
const ::Controller<3>& controller, const ::TimescaleTuner& tuner,
const int measurements_per_update);
#define DIM(data) BOOST_PP_TUPLE_ELEM(0, data)
#define ALLOWDECREASE(data) BOOST_PP_TUPLE_ELEM(1, data)

#define INSTANTIATE(_, data) \
template DataVector calculate_measurement_timescales( \
const ::Controller<DIM(data)>& controller, \
const ::TimescaleTuner<ALLOWDECREASE(data)>& tuner, \
const int measurements_per_update);

GENERATE_INSTANTIATIONS(INSTANTIATE, (1, 2, 3), (true, false))

#undef INSTANTIATE
#undef DIM
#undef ALLOWDECREASE

} // namespace control_system
6 changes: 4 additions & 2 deletions src/ControlSystem/CalculateMeasurementTimescales.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
template <size_t DerivOrder>
class Controller;
class DataVector;
template <bool AllowDecrease>
class TimescaleTuner;
/// \endcond

Expand All @@ -29,8 +30,9 @@ namespace control_system {
* we calculate the measurement timescales as \f$\tau_\mathrm{m} =
* \tau_\mathrm{update} / N\f$ where \f$N\f$ is `measurements_per_update`.
*/
template <size_t DerivOrder>
template <size_t DerivOrder, bool AllowDecrease>
DataVector calculate_measurement_timescales(
const ::Controller<DerivOrder>& controller, const ::TimescaleTuner& tuner,
const ::Controller<DerivOrder>& controller,
const ::TimescaleTuner<AllowDecrease>& tuner,
const int measurements_per_update);
} // namespace control_system
2 changes: 1 addition & 1 deletion src/ControlSystem/ControlErrors/Expansion.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ struct Expansion : tt::ConformsTo<protocols::ControlError> {
void pup(PUP::er& /*p*/) {}

template <typename Metavariables, typename... TupleTags>
DataVector operator()(const ::TimescaleTuner& /*unused*/,
DataVector operator()(const ::TimescaleTuner<true>& /*unused*/,
const Parallel::GlobalCache<Metavariables>& cache,
const double time,
const std::string& function_of_time_name,
Expand Down
2 changes: 1 addition & 1 deletion src/ControlSystem/ControlErrors/Rotation.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ struct Rotation : tt::ConformsTo<protocols::ControlError> {
void pup(PUP::er& /*p*/) {}

template <typename Metavariables, typename... TupleTags>
DataVector operator()(const ::TimescaleTuner& /*unused*/,
DataVector operator()(const ::TimescaleTuner<true>& /*unused*/,
const Parallel::GlobalCache<Metavariables>& cache,
const double /*time*/,
const std::string& /*function_of_time_name*/,
Expand Down
2 changes: 1 addition & 1 deletion src/ControlSystem/ControlErrors/Shape.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ struct Shape : tt::ConformsTo<protocols::ControlError> {
void pup(PUP::er& /*p*/) {}

template <typename Metavariables, typename... TupleTags>
DataVector operator()(const ::TimescaleTuner& /*unused*/,
DataVector operator()(const ::TimescaleTuner<true>& /*unused*/,
const Parallel::GlobalCache<Metavariables>& cache,
const double time,
const std::string& function_of_time_name,
Expand Down
2 changes: 1 addition & 1 deletion src/ControlSystem/ControlErrors/Size.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ namespace ControlErrors {
template <size_t DerivOrder, ::domain::ObjectLabel Horizon>
Size<DerivOrder, Horizon>::Size(
const int max_times, const double smooth_avg_timescale_frac,
TimescaleTuner smoother_tuner,
TimescaleTuner<true> smoother_tuner,
std::optional<DeltaRDriftOutwardOptions> delta_r_drift_outward_options)
: smoother_tuner_(std::move(smoother_tuner)),
delta_r_drift_outward_options_(delta_r_drift_outward_options) {
Expand Down
8 changes: 4 additions & 4 deletions src/ControlSystem/ControlErrors/Size.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ struct Size : tt::ConformsTo<protocols::ControlError> {
};

struct SmootherTuner {
using type = TimescaleTuner;
using type = TimescaleTuner<true>;
static constexpr Options::String help{
"TimescaleTuner for smoothing horizon measurements."};
};
Expand Down Expand Up @@ -250,7 +250,7 @@ struct Size : tt::ConformsTo<protocols::ControlError> {
* is moved inside this class.
*/
Size(const int max_times, const double smooth_avg_timescale_frac,
TimescaleTuner smoother_tuner,
TimescaleTuner<true> smoother_tuner,
std::optional<DeltaRDriftOutwardOptions> delta_r_drift_outward_options);

/// Returns the internal `control_system::size::Info::suggested_time_scale`. A
Expand Down Expand Up @@ -299,7 +299,7 @@ struct Size : tt::ConformsTo<protocols::ControlError> {
* \return DataVector should be of size 1
*/
template <typename Metavariables, typename... TupleTags>
DataVector operator()(const ::TimescaleTuner& tuner,
DataVector operator()(const ::TimescaleTuner<false>& tuner,
const Parallel::GlobalCache<Metavariables>& cache,
const double time,
const std::string& function_of_time_name,
Expand Down Expand Up @@ -471,7 +471,7 @@ struct Size : tt::ConformsTo<protocols::ControlError> {
}

private:
TimescaleTuner smoother_tuner_{};
TimescaleTuner<true> smoother_tuner_{};
Averager<DerivOrder> horizon_coef_averager_{};
size::Info info_{};
intrp::ZeroCrossingPredictor char_speed_predictor_{};
Expand Down
2 changes: 1 addition & 1 deletion src/ControlSystem/ControlErrors/Size/Update.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ void update_averager(
*/
template <size_t DerivOrder, ::domain::ObjectLabel Horizon,
typename Metavariables>
void update_tuner(const gsl::not_null<TimescaleTuner*> tuner,
void update_tuner(const gsl::not_null<TimescaleTuner<false>*> tuner,
const gsl::not_null<ControlErrors::Size<DerivOrder, Horizon>*>
control_error,
const Parallel::GlobalCache<Metavariables>& cache,
Expand Down
2 changes: 1 addition & 1 deletion src/ControlSystem/ControlErrors/Translation.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ struct Translation : tt::ConformsTo<protocols::ControlError> {
void pup(PUP::er& /*p*/) {}

template <typename Metavariables, typename... TupleTags>
DataVector operator()(const ::TimescaleTuner& tuner,
DataVector operator()(const ::TimescaleTuner<true>& tuner,
const Parallel::GlobalCache<Metavariables>& cache,
const double time,
const std::string& /*function_of_time_name*/,
Expand Down
10 changes: 9 additions & 1 deletion src/ControlSystem/IsSize.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,17 @@

#pragma once

#include "ControlSystem/Systems/Size.hpp"
#include <cstddef>

#include "Domain/Structure/ObjectLabel.hpp"

/// \cond
namespace control_system::Systems {
template <::domain::ObjectLabel Horizon, size_t DerivOrder>
struct Size;
} // namespace control_system::Systems
/// \endcond

namespace control_system::size {
// tt::is_a doesn't work because of domain::ObjectLabel and size_t
template <typename T>
Expand Down
34 changes: 21 additions & 13 deletions src/ControlSystem/Protocols/ControlError.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,26 @@
#include "Utilities/TMPL.hpp"
#include "Utilities/TaggedTuple.hpp"

template <bool AllowDecrease>
struct TimescaleTuner;

namespace control_system::protocols {
namespace detail {

struct DummyMetavariables {
using component_list = tmpl::list<>;
};
struct DummyTupleTags {
using type = int;
};

template <typename T, bool AllowDecrease>
struct has_signature
: std::is_invocable_r<DataVector, T, const ::TimescaleTuner<AllowDecrease>&,
const Parallel::GlobalCache<DummyMetavariables>&,
const double, const std::string&,
const tuples::TaggedTuple<DummyTupleTags>&> {};
} // namespace detail
/// \brief Definition of a control error
///
/// A control error is used within a control system to compute how far off the
Expand All @@ -31,29 +48,20 @@ namespace control_system::protocols {
/// `domain::Tags::ObjectCenter`s tags to be in the GlobalCache for this
/// control system to work.
///
/// \note The TimescaleTuner can have it's template parameter be either `true`
/// or `false`.
///
/// \snippet Helpers/ControlSystem/Examples.hpp ControlError
struct ControlError {
template <typename ConformingType>
struct test {
struct DummyMetavariables;
struct DummyTupleTags;

static constexpr size_t expected_number_of_excisions =
ConformingType::expected_number_of_excisions;

using object_centers = typename ConformingType::object_centers;

static_assert(
std::is_same_v<
DataVector,
decltype(ConformingType{}(
std::declval<const ::TimescaleTuner&>(),
std::declval<
const Parallel::GlobalCache<DummyMetavariables>&>(),
std::declval<const double>(),
std::declval<const std::string&>(),
std::declval<const tuples::TaggedTuple<DummyTupleTags>&>()))>);
static_assert(detail::has_signature<ConformingType, true>::value or
detail::has_signature<ConformingType, false>::value);
};
};
} // namespace control_system::protocols
5 changes: 3 additions & 2 deletions src/ControlSystem/Tags/MeasurementTimescales.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,9 @@ struct MeasurementTimescales : db::SimpleTag {
const std::string& combined_name = map_of_names[control_system_name];

auto tuner = option_holder.tuner;
Tags::detail::initialize_tuner(make_not_null(&tuner), domain_creator,
initial_time, control_system_name);
::control_system::Tags::detail::initialize_tuner(
make_not_null(&tuner), domain_creator, initial_time,
control_system_name);

const auto& controller = option_holder.controller;
DataVector measurement_timescales = calculate_measurement_timescales(
Expand Down
12 changes: 9 additions & 3 deletions src/ControlSystem/Tags/OptionTags.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

#include "ControlSystem/Averager.hpp"
#include "ControlSystem/Controller.hpp"
#include "ControlSystem/IsSize.hpp"
#include "ControlSystem/Protocols/ControlSystem.hpp"
#include "ControlSystem/TimescaleTuner.hpp"
#include "IO/Logging/Verbosity.hpp"
Expand All @@ -24,6 +25,11 @@ namespace control_system {
/// their public member names and assigned to their corresponding DataBox tags.
template <typename ControlSystem>
struct OptionHolder {
private:
static constexpr bool is_size =
::control_system::size::is_size_v<ControlSystem>;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[future work] It would be nice to not have everything in the control system infrastructure special-cased on size control. At some point maybe we should have control systems report what features they want with something like ControlSystem::allow_timescale_decrease.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree. I'll hopefully put this into the control system protocol eventually, but was a bit too much work at the moment.


public:
static_assert(tt::assert_conforms_to_v<
ControlSystem, control_system::protocols::ControlSystem>);
using control_system = ControlSystem;
Expand Down Expand Up @@ -51,7 +57,7 @@ struct OptionHolder {
};

struct TimescaleTuner {
using type = ::TimescaleTuner;
using type = ::TimescaleTuner<not is_size>;
static constexpr Options::String help = {
"Keeps track of the damping timescales for the control system upon "
"which other timescales are based of off."};
Expand All @@ -71,7 +77,7 @@ struct OptionHolder {
OptionHolder(const bool input_is_active,
::Averager<deriv_order - 1> input_averager,
::Controller<deriv_order> input_controller,
::TimescaleTuner input_tuner,
::TimescaleTuner<not is_size> input_tuner,
typename ControlSystem::control_error input_control_error)
: is_active(input_is_active),
averager(std::move(input_averager)),
Expand Down Expand Up @@ -100,7 +106,7 @@ struct OptionHolder {
bool is_active{true};
::Averager<deriv_order - 1> averager{};
::Controller<deriv_order> controller{};
::TimescaleTuner tuner{};
::TimescaleTuner<not is_size> tuner{};
typename ControlSystem::control_error control_error{};
};

Expand Down
12 changes: 7 additions & 5 deletions src/ControlSystem/Tags/SystemTags.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
#include "Utilities/GenerateInstantiations.hpp"

namespace control_system::Tags::detail {
template <size_t Dim>
template <bool AllowDecrease, size_t Dim>
void initialize_tuner(
const gsl::not_null<::TimescaleTuner*> tuner,
const gsl::not_null<::TimescaleTuner<AllowDecrease>*> tuner,
const std::unique_ptr<::DomainCreator<Dim>>& domain_creator,
const double initial_time, const std::string& name) {
// We get the functions of time in order to get the number of components
Expand Down Expand Up @@ -51,17 +51,19 @@ void initialize_tuner(
}
}

#define DIM(data) BOOST_PP_TUPLE_ELEM(0, data)
#define ALLOWDECREASE(data) BOOST_PP_TUPLE_ELEM(0, data)
#define DIM(data) BOOST_PP_TUPLE_ELEM(1, data)

#define INSTANTIATE(_, data) \
template void initialize_tuner( \
const gsl::not_null<::TimescaleTuner*> tuner, \
const gsl::not_null<::TimescaleTuner<ALLOWDECREASE(data)>*> tuner, \
const std::unique_ptr<::DomainCreator<DIM(data)>>& domain_creator, \
const double initial_time, const std::string& name);

GENERATE_INSTANTIATIONS(INSTANTIATE, (1, 2, 3))
GENERATE_INSTANTIATIONS(INSTANTIATE, (true, false), (1, 2, 3))

#undef INSTANTIATE
#undef ALLOWDECREASE
#undef DIM

} // namespace control_system::Tags::detail
14 changes: 10 additions & 4 deletions src/ControlSystem/Tags/SystemTags.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include "ControlSystem/Averager.hpp"
#include "ControlSystem/CombinedName.hpp"
#include "ControlSystem/Controller.hpp"
#include "ControlSystem/IsSize.hpp"
#include "ControlSystem/Metafunctions.hpp"
#include "ControlSystem/Protocols/ControlSystem.hpp"
#include "ControlSystem/Tags/OptionTags.hpp"
Expand Down Expand Up @@ -95,9 +96,9 @@ struct Averager : db::SimpleTag {
};

namespace detail {
template <size_t Dim>
template <bool AllowDecrease, size_t Dim>
void initialize_tuner(
const gsl::not_null<::TimescaleTuner*> tuner,
gsl::not_null<::TimescaleTuner<AllowDecrease>*> tuner,
const std::unique_ptr<::DomainCreator<Dim>>& domain_creator,
const double initial_time, const std::string& name);
} // namespace detail
Expand All @@ -107,7 +108,12 @@ void initialize_tuner(
/// DataBox tag for the timescale tuner
template <typename ControlSystem>
struct TimescaleTuner : db::SimpleTag {
using type = ::TimescaleTuner;
private:
static constexpr bool is_size =
control_system::size::is_size_v<ControlSystem>;

public:
using type = ::TimescaleTuner<not is_size>;

template <typename Metavariables>
using option_tags =
Expand Down Expand Up @@ -150,7 +156,7 @@ struct Controller : db::SimpleTag {
domain_creator,
const double initial_time) {
type controller = option_holder.controller;
::TimescaleTuner tuner = option_holder.tuner;
auto tuner = option_holder.tuner;
detail::initialize_tuner(make_not_null(&tuner), domain_creator,
initial_time, ControlSystem::name());

Expand Down
Loading
Loading