numerical-collection-cpp 0.10.0
A collection of algorithms in numerical analysis implemented in C++
Loading...
Searching...
No Matches
leap_frog_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
25#include "num_collect/constants/half.h" // IWYU pragma: keep
33
35
59template <concepts::multi_variate_problem Problem>
61 : public formula_base<leap_frog_formula<Problem>, Problem> {
62public:
65
66 using typename base_type::problem_type;
67 using typename base_type::scalar_type;
68 using typename base_type::variable_type;
69
70 using base_type::base_type;
72
73 static_assert(!problem_type::allowed_evaluations.mass,
74 "Mass matrix is not supported.");
75
76protected:
77 using base_type::coeff;
78
79public:
81 static constexpr index_type stages = 3;
82
84 static constexpr index_type order = 2;
85
87 static constexpr auto log_tag = logging::log_tag_view(
88 "num_collect::ode::symplectic::leap_frog_formula");
89
91 void step(scalar_type time, scalar_type step_size,
92 const variable_type& current, variable_type& estimate) {
93 const index_type dim = current.size();
94 const index_type half_dim = dim / 2;
95 NUM_COLLECT_PRECONDITION(dim % 2 == 0,
96 "This formula requires vectors with even dimensions.");
97
98 estimate = current;
99
100 constexpr auto evaluations = evaluation_type{.diff_coeff = true};
101
102 problem().evaluate_on(time, estimate, evaluations);
103 estimate.head(half_dim) += step_size * constants::half<scalar_type> *
104 problem().diff_coeff().head(half_dim);
105
106 problem().evaluate_on(time, estimate, evaluations);
107 estimate.tail(half_dim) +=
108 step_size * problem().diff_coeff().tail(half_dim);
109
110 problem().evaluate_on(time, estimate, evaluations);
111 estimate.head(half_dim) += step_size * constants::half<scalar_type> *
112 problem().diff_coeff().head(half_dim);
113 }
114};
115
139template <concepts::problem Problem>
141
142} // namespace num_collect::ode::symplectic
Class of tags of logs without memory management.
Base class of formulas in ODE solvers.
Class of simple solver of ODEs.
static constexpr index_type order
Order 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 stages
Number of stages of this formula.
Definition of evaluation_type enumeration.
Definition of exceptions.
Definition of formula_base class.
Definition of half.
Definition of index_type type.
Definition of log_tag_view class.
Definition of macros for logging.
Definition of multi_variate_problem concept.
std::ptrdiff_t index_type
Type of indices in this library.
Definition index_type.h:33
constexpr T half
Value 0.5.
Definition half.h:30
Namespace of symplectic integration.
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 problem concept.
Definition of simple_solver class.
Struct to specify types of evaluations.
bool diff_coeff
Differential coefficient.