numerical-collection-cpp 0.10.0
A collection of algorithms in numerical analysis implemented in C++
Loading...
Searching...
No Matches
step_size_controller_base.h
Go to the documentation of this file.
1/*
2 * Copyright 2021 MusicScience37 (Kenta Kabashima)
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
20#pragma once
21
30
31namespace num_collect::ode {
32
39template <typename Derived, concepts::formula Formula>
41public:
43 using formula_type = Formula;
44
46 using problem_type = typename formula_type::problem_type;
47
49 using variable_type = typename problem_type::variable_type;
50
52 using scalar_type = typename problem_type::scalar_type;
53
60 auto limits(const step_size_limits<scalar_type>& val) -> Derived& {
61 limits_ = val;
62 return derived();
63 }
64
70 [[nodiscard]] auto limits() const -> const step_size_limits<scalar_type>& {
71 return limits_;
72 }
73
80 auto tolerances(const error_tolerances<variable_type>& val) -> Derived& {
81 tolerances_ = val;
82 return derived();
83 }
84
90 [[nodiscard]] auto tolerances() const
92 return tolerances_;
93 }
94
101 auto reduction_rate(const scalar_type& val) -> Derived& {
102 NUM_COLLECT_PRECONDITION(val > static_cast<scalar_type>(0),
103 "Rate to reduce step sizes when error is large must be a positive "
104 "value.");
105 reduction_rate_ = val;
106 return derived();
107 }
108
109protected:
117
123 [[nodiscard]] auto derived() noexcept -> Derived& {
124 return *static_cast<Derived*>(this);
125 }
126
132 [[nodiscard]] auto derived() const noexcept -> const Derived& {
133 return *static_cast<const Derived*>(this);
134 }
135
145 [[nodiscard]] auto reduce_if_needed(scalar_type& step_size,
146 const variable_type& variable, const variable_type& error) -> bool {
147 const bool tolerance_satisfied = tolerances().check(variable, error);
148 if (!tolerance_satisfied) {
149 if (step_size > limits_.lower_limit()) {
151 "Error tolerance not satisfied with step size {}.",
152 step_size);
153 step_size *= reduction_rate_;
154 step_size = limits_.apply(step_size);
155 return true;
156 }
158 "Error tolerance not satisfied even with the lowest step size "
159 "{} (error: {}).",
160 step_size, tolerances().calc_norm(variable, error));
161 }
162 return false;
163 }
164
165private:
168
171
173 static constexpr auto default_reduction_rate =
174 static_cast<scalar_type>(0.5);
175
178};
179
180} // namespace num_collect::ode
Class of tags of logs without memory management.
Class to incorporate logging in algorithms.
logging_mixin(log_tag_view tag)
Constructor.
auto logger() const noexcept -> const num_collect::logging::logger &
Access to the logger.
Class of error tolerances hairer1993.
Base class of classes to control step sizes.
typename formula_type::problem_type problem_type
Type of problem.
scalar_type reduction_rate_
Rate to reduce step sizes when error is large.
static constexpr auto default_reduction_rate
Default rate to reduce step sizes when error is large.
step_size_limits< scalar_type > limits_
Limits of step sizes.
auto limits(const step_size_limits< scalar_type > &val) -> Derived &
Set the limits of step sizes.
auto reduce_if_needed(scalar_type &step_size, const variable_type &variable, const variable_type &error) -> bool
Reduce step size if needed.
error_tolerances< variable_type > tolerances_
Error tolerances.
auto limits() const -> const step_size_limits< scalar_type > &
Get the limits of step sizes.
auto derived() noexcept -> Derived &
Access derived object.
auto reduction_rate(const scalar_type &val) -> Derived &
Set the rate to reduce step sizes when error is large.
auto tolerances() const -> const error_tolerances< variable_type > &
Get the error tolerances.
auto tolerances(const error_tolerances< variable_type > &val) -> Derived &
Set the error tolerances.
auto derived() const noexcept -> const Derived &
Access derived object.
typename problem_type::scalar_type scalar_type
Type of scalars.
typename problem_type::variable_type variable_type
Type of variables.
step_size_controller_base(logging::log_tag_view tag)
Constructor.
Class of limits of step sizes.
auto apply(scalar_type val) const -> scalar_type
Apply the limit of this object.
auto lower_limit() const -> const scalar_type &
Get the lower limit.
Definition of error_tolerances class.
Definition of exceptions.
Definition of formula concept.
Definition of log_tag_view class.
Definition of macros for logging.
#define NUM_COLLECT_LOG_TRACE(LOGGER,...)
Write a trace log.
#define NUM_COLLECT_LOG_WARNING(LOGGER,...)
Write a warning log.
Definition of logging_mixin class.
Namespace of solvers of ordinary differential equations (ODE).
Definition of NUM_COLLECT_PRECONDITION macro.
#define NUM_COLLECT_PRECONDITION(CONDITION,...)
Check whether a precondition is satisfied and throw an exception if not.
Definition of step_size_limits class.