numerical-collection-cpp 0.10.0
A collection of algorithms in numerical analysis implemented in C++
Loading...
Searching...
No Matches
rkf45_formula.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
28
30
36template <concepts::problem Problem>
37class rkf45_formula : public formula_base<rkf45_formula<Problem>, Problem> {
38public:
41
42 using typename base_type::problem_type;
43 using typename base_type::scalar_type;
44 using typename base_type::variable_type;
45
46 static_assert(!problem_type::allowed_evaluations.mass,
47 "Mass matrix is not supported.");
48
49 using base_type::base_type;
51
52protected:
53 using base_type::coeff;
54
55public:
57 static constexpr index_type stages = 6;
58
60 static constexpr index_type order = 5;
61
63 static constexpr auto log_tag =
64 logging::log_tag_view("num_collect::ode::runge_kutta::rkf45_formula");
65
67 static constexpr index_type lesser_order = 4;
68
80 static constexpr scalar_type a21 = coeff(1, 4);
81 static constexpr scalar_type a31 = coeff(3, 32);
82 static constexpr scalar_type a32 = coeff(9, 32);
83 static constexpr scalar_type a41 = coeff(1932, 2197);
84 static constexpr scalar_type a42 = coeff(-7200, 2197);
85 static constexpr scalar_type a43 = coeff(7296, 2197);
86 static constexpr scalar_type a51 = coeff(439, 216);
87 static constexpr scalar_type a52 = coeff(-8);
88 static constexpr scalar_type a53 = coeff(3680, 513);
89 static constexpr scalar_type a54 = coeff(-845, 4104);
90 static constexpr scalar_type a61 = coeff(-8, 27);
91 static constexpr scalar_type a62 = coeff(2);
92 static constexpr scalar_type a63 = coeff(-3544, 2565);
93 static constexpr scalar_type a64 = coeff(1859, 4104);
94 static constexpr scalar_type a65 = coeff(-11, 40);
95
96 static constexpr scalar_type b2 = coeff(1, 4);
97 static constexpr scalar_type b3 = coeff(3, 8);
98 static constexpr scalar_type b4 = coeff(12, 13);
99 static constexpr scalar_type b5 = coeff(1);
100 static constexpr scalar_type b6 = coeff(1, 2);
101
102 static constexpr scalar_type c1 = coeff(16, 135);
103 static constexpr scalar_type c3 = coeff(6656, 12825);
104 static constexpr scalar_type c4 = coeff(28561, 56430);
105 static constexpr scalar_type c5 = coeff(-9, 50);
106 static constexpr scalar_type c6 = coeff(2, 55);
107
108 static constexpr scalar_type cw1 = coeff(25, 216);
109 static constexpr scalar_type cw3 = coeff(1408, 2565);
110 static constexpr scalar_type cw4 = coeff(2197, 4104);
111 static constexpr scalar_type cw5 = coeff(-1, 5);
112
113 static constexpr scalar_type ce1 = c1 - cw1;
114 static constexpr scalar_type ce3 = c3 - cw3;
115 static constexpr scalar_type ce4 = c4 - cw4;
116 static constexpr scalar_type ce5 = c5 - cw5;
117 static constexpr scalar_type ce6 = c6;
119
121 void step(scalar_type time, scalar_type step_size,
122 const variable_type& current, variable_type& estimate) {
123 variable_type unused;
124 step_embedded(time, step_size, current, estimate, unused);
125 }
126
138 const variable_type& current, variable_type& estimate,
139 variable_type& error) {
140 constexpr auto evaluations = evaluation_type{.diff_coeff = true};
141
142 problem().evaluate_on(time, current, evaluations);
143 k1_ = problem().diff_coeff();
144
145 problem().evaluate_on(time + b2 * step_size,
146 current + step_size * a21 * k1_, evaluations);
147 k2_ = problem().diff_coeff();
148
149 problem().evaluate_on(time + b3 * step_size,
150 current + step_size * (a31 * k1_ + a32 * k2_), evaluations);
151 k3_ = problem().diff_coeff();
152
153 problem().evaluate_on(time + b4 * step_size,
154 current + step_size * (a41 * k1_ + a42 * k2_ + a43 * k3_),
155 evaluations);
156 k4_ = problem().diff_coeff();
157
158 problem().evaluate_on(time + b5 * step_size,
159 current +
160 step_size * (a51 * k1_ + a52 * k2_ + a53 * k3_ + a54 * k4_),
161 evaluations);
162 k5_ = problem().diff_coeff();
163
164 problem().evaluate_on(time + b6 * step_size,
165 current +
166 step_size *
167 (a61 * k1_ + a62 * k2_ + a63 * k3_ + a64 * k4_ + a65 * k5_),
168 evaluations);
169 k6_ = problem().diff_coeff();
170
171 estimate = current +
172 step_size * (c1 * k1_ + c3 * k3_ + c4 * k4_ + c5 * k5_ + c6 * k6_);
173 error = step_size *
174 (ce1 * k1_ + ce3 * k3_ + ce4 * k4_ + ce5 * k5_ + ce6 * k6_);
175 }
176
177private:
190};
191
197template <concepts::problem Problem>
199
200} // namespace num_collect::ode::runge_kutta
Class of tags of logs without memory management.
Class of solvers of ODEs using embedded formulas.
Base class of formulas in ODE solvers.
Class of Runge-Kutta-Fehlberg 45 formula.
static constexpr index_type lesser_order
Order of lesser coefficients of this formula.
static constexpr index_type stages
Number of stages of this formula.
void step(scalar_type time, scalar_type step_size, const variable_type &current, variable_type &estimate)
Compute the next variable.
static constexpr index_type order
Order of this formula.
void step_embedded(scalar_type time, scalar_type step_size, const variable_type &current, variable_type &estimate, variable_type &error)
Compute the next variable and weak estimate of it with embedded formula.
static constexpr auto log_tag
Log tag.
Definition of embedded_solver class.
Definition of evaluation_type enumeration.
Definition of formula_base class.
Definition of index_type type.
Definition of log_tag_view class.
std::ptrdiff_t index_type
Type of indices in this library.
Definition index_type.h:33
Namespace of Runge-Kutta method.
Definition of problem concept.
Struct to specify types of evaluations.
bool diff_coeff
Differential coefficient.