numerical-collection-cpp 0.10.0
A collection of algorithms in numerical analysis implemented in C++
Loading...
Searching...
No Matches
rk4_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
29
31
37template <concepts::problem Problem>
38class rk4_formula : public formula_base<rk4_formula<Problem>, Problem> {
39public:
42
43 using typename base_type::problem_type;
44 using typename base_type::scalar_type;
45 using typename base_type::variable_type;
46
47 using base_type::base_type;
49
50 static_assert(!problem_type::allowed_evaluations.mass,
51 "Mass matrix is not supported.");
52
53protected:
54 using base_type::coeff;
55
56public:
58 static constexpr index_type stages = 4;
59
61 static constexpr index_type order = 4;
62
64 static constexpr auto log_tag =
65 logging::log_tag_view("num_collect::ode::runge_kutta::rk4_formula");
66
78 static constexpr scalar_type a21 = coeff(1, 2);
79 static constexpr scalar_type a32 = coeff(1, 2);
80 static constexpr scalar_type a43 = coeff(1);
81
82 static constexpr scalar_type b1 = coeff(0);
83 static constexpr scalar_type b2 = coeff(1, 2);
84 static constexpr scalar_type b3 = coeff(1, 2);
85 static constexpr scalar_type b4 = coeff(1);
86
87 static constexpr scalar_type c1 = coeff(1, 6);
88 static constexpr scalar_type c2 = coeff(1, 3);
89 static constexpr scalar_type c3 = coeff(1, 3);
90 static constexpr scalar_type c4 = coeff(1, 6);
92
94 void step(scalar_type time, scalar_type step_size,
95 const variable_type& current, variable_type& estimate) {
96 constexpr auto evaluations = evaluation_type{.diff_coeff = true};
97
98 problem().evaluate_on(time, current, evaluations);
100
101 problem().evaluate_on(time + b2 * step_size,
102 current + step_size * a21 * k1_, evaluations);
103 k2_ = problem().diff_coeff();
104
105 problem().evaluate_on(time + b3 * step_size,
106 current + step_size * a32 * k2_, evaluations);
107 k3_ = problem().diff_coeff();
108
109 problem().evaluate_on(
110 time + step_size, current + step_size * a43 * k3_, evaluations);
111 k4_ = problem().diff_coeff();
112
113 estimate =
114 current + step_size * (c1 * k1_ + c2 * k2_ + c3 * k3_ + c4 * k4_);
115 }
116
117private:
128};
129
136template <concepts::problem Problem>
138
145template <concepts::problem Problem>
147
148} // 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.
static constexpr auto coeff(T val) -> scalar_type
Class of Runge-Kutta 4 formula (classic Runge-Kutta method).
Definition rk4_formula.h:38
static constexpr scalar_type c1
Definition rk4_formula.h:87
static constexpr scalar_type a43
Definition rk4_formula.h:80
static constexpr scalar_type b4
Definition rk4_formula.h:85
static constexpr scalar_type b1
Definition rk4_formula.h:82
static constexpr index_type order
Order of this formula.
Definition rk4_formula.h:61
static constexpr index_type stages
Number of stages of this formula.
Definition rk4_formula.h:58
void step(scalar_type time, scalar_type step_size, const variable_type &current, variable_type &estimate)
Compute the next variable.
Definition rk4_formula.h:94
static constexpr scalar_type a21
Definition rk4_formula.h:78
static constexpr scalar_type c4
Definition rk4_formula.h:90
static constexpr scalar_type b2
Definition rk4_formula.h:83
static constexpr scalar_type c2
Definition rk4_formula.h:88
static constexpr scalar_type a32
Definition rk4_formula.h:79
static constexpr scalar_type b3
Definition rk4_formula.h:84
static constexpr scalar_type c3
Definition rk4_formula.h:89
static constexpr auto log_tag
Log tag.
Definition rk4_formula.h:64
Class of simple solver of ODEs.
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 non_embedded_formula_wrapper class.
Definition of problem concept.
Definition of simple_solver class.
Struct to specify types of evaluations.
bool diff_coeff
Differential coefficient.