numerical-collection-cpp 0.10.0
A collection of algorithms in numerical analysis implemented in C++
Loading...
Searching...
No Matches
basic_step_size_controller.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
22#include <cmath>
23
30#include "num_collect/ode/error_tolerances.h" // IWYU pragma: keep
33#include "num_collect/ode/step_size_limits.h" // IWYU pragma: keep
34
35namespace num_collect::ode {
36
39 logging::log_tag_view("num_collect::ode::basic_step_size_controller");
40
46template <concepts::formula Formula>
48 : public step_size_controller_base<basic_step_size_controller<Formula>,
49 Formula> {
50public:
52 using base_type =
54
55 using typename base_type::formula_type;
56 using typename base_type::problem_type;
57 using typename base_type::scalar_type;
58 using typename base_type::variable_type;
59
65
69 void init() {
70 // no operation.
71 }
72
82 [[nodiscard]] auto check_and_calc_next(scalar_type& step_size,
83 const variable_type& variable, const variable_type& error) -> bool {
84 if (this->reduce_if_needed(step_size, variable, error)) {
85 return false;
86 }
87 calc_next(step_size, variable, error);
88 return true;
89 }
90
99 NUM_COLLECT_PRECONDITION(val > static_cast<scalar_type>(0),
100 "Safety coefficient for factors of step sizes must be a positive "
101 "value.");
103 return *this;
104 }
105
114 NUM_COLLECT_PRECONDITION(val > static_cast<scalar_type>(0),
115 "Maximum factor of step sizes must be a positive value.");
117 return *this;
118 }
119
120private:
128 void calc_next(scalar_type& step_size, const variable_type& variable,
129 const variable_type& error) {
130 // First, calculate factor of step size using a formula in the
131 // reference.
132 const scalar_type error_norm =
133 this->tolerances().calc_norm(variable, error);
134 constexpr index_type order_for_exponent =
136 constexpr scalar_type exponent = -static_cast<scalar_type>(1) /
137 static_cast<scalar_type>(order_for_exponent + 1);
138 using std::pow;
139 scalar_type factor = pow(error_norm, exponent);
140
141 // Secondly, change the factor for safety.
142 using std::isfinite;
144 if (factor > max_step_size_factor_ || !isfinite(factor)) {
145 factor = max_step_size_factor_;
146 }
147
148 // Finally, multiply the factor to the step size.
149 step_size *= factor;
150 step_size = this->limits().apply(step_size);
151 }
152
155 static_cast<scalar_type>(0.8);
156
160
162 static constexpr auto default_max_step_size_factor =
163 static_cast<scalar_type>(2);
164
167};
168
169} // namespace num_collect::ode
Class of tags of logs without memory management.
Class to control step sizes using well-known method hairer1993.
auto step_size_factor_safety_coeff(const scalar_type &val) -> basic_step_size_controller &
Set the safety coefficient for factors of step sizes.
auto max_step_size_factor(const scalar_type &val) -> basic_step_size_controller &
Set the maximum factor of step sizes.
scalar_type step_size_factor_safety_coeff_
Safety coefficient for factors of step sizes.
auto check_and_calc_next(scalar_type &step_size, const variable_type &variable, const variable_type &error) -> bool
Check the error estimate and calculate the next step size.
static constexpr auto default_step_size_factor_safety_coeff
Default safety coefficient for factors of step sizes.
void calc_next(scalar_type &step_size, const variable_type &variable, const variable_type &error)
Calculate the next step size.
static constexpr auto default_max_step_size_factor
Default maximum factor of step sizes.
scalar_type max_step_size_factor_
Maximum factor of step sizes.
Base class of classes to control step sizes.
auto reduce_if_needed(scalar_type &step_size, const variable_type &variable, const variable_type &error) -> bool
Definition of error_tolerances class.
Definition of exceptions.
Definition of formula concept.
Definition of get_least_known_order function.
Definition of index_type type.
Definition of log_tag_view class.
Definition of macros for logging.
std::ptrdiff_t index_type
Type of indices in this library.
Definition index_type.h:33
auto isfinite(const T &val) -> bool
Check whether a number is finite.
Definition isfinite.h:39
constexpr auto get_least_known_order() -> index_type
Get the least known order of a formula.
Namespace of solvers of ordinary differential equations (ODE).
constexpr auto basic_step_size_controller_log_tag
Log tag.
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_controller_base class.
Definition of step_size_limits class.