numerical-collection-cpp 0.10.0
A collection of algorithms in numerical analysis implemented in C++
Loading...
Searching...
No Matches
solver_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
26
27namespace num_collect::ode {
28
35template <typename Derived, concepts::formula Formula>
39public:
41 using formula_type = Formula;
42
44 using problem_type = typename formula_type::problem_type;
45
47 using variable_type = typename problem_type::variable_type;
48
50 using scalar_type = typename problem_type::scalar_type;
51
53 static constexpr index_type stages = formula_type::stages;
54
56 static constexpr index_type order = formula_type::order;
57
64 : logging::logging_mixin(formula_type::log_tag), formula_(problem) {
65 this->logger().set_iterative();
67 }
68
76 derived().init(time, variable);
77 }
78
85 void step() { derived().step(); }
86
95 void solve_till(scalar_type end_time) {
96 auto& iter_logger = this->initialize_iteration_logger();
97 iter_logger.write_iteration(&derived());
98 while (time() < end_time) {
99 const scalar_type max_step_size = end_time - time();
100 const scalar_type current_step_size = step_size();
101 const bool rewrite_step_size = current_step_size > max_step_size;
102 if (rewrite_step_size) {
103 step_size(max_step_size);
104 }
105 step();
106 if (rewrite_step_size && (step_size() >= max_step_size)) {
107 step_size(current_step_size);
108 }
109 iter_logger.write_iteration(&derived());
110 }
111 iter_logger.write_summary(&derived());
112 }
113
119 [[nodiscard]] auto formula() -> formula_type& { return formula_; }
120
126 [[nodiscard]] auto formula() const -> const formula_type& {
127 return formula_;
128 }
129
135 [[nodiscard]] auto problem() -> problem_type& { return formula_.problem(); }
136
142 [[nodiscard]] auto problem() const -> const problem_type& {
143 return formula_.problem();
144 }
145
151 [[nodiscard]] auto time() const -> scalar_type { return derived().time(); }
152
158 [[nodiscard]] auto variable() const -> const variable_type& {
159 return derived().variable();
160 }
161
167 [[nodiscard]] auto step_size() const -> scalar_type {
168 return derived().step_size();
169 }
170
176 [[nodiscard]] auto steps() const -> index_type { return derived().steps(); }
177
184 auto step_size(scalar_type val) -> Derived& {
185 return derived().step_size(val);
186 }
187
188protected:
194 [[nodiscard]] auto derived() noexcept -> Derived& {
195 return *static_cast<Derived*>(this);
196 }
197
203 [[nodiscard]] auto derived() const noexcept -> const Derived& {
204 return *static_cast<const Derived*>(this);
205 }
206
207private:
210};
211
212} // namespace num_collect::ode
Class to incorporate num_collect::logging::iterations::iteration_logger in algorithms.
auto initialize_iteration_logger() -> num_collect::logging::iterations::iteration_logger< Derived > &
Get the iteration logger.
Class to incorporate logging in algorithms.
void configure_child_algorithm_logger_if_exists(Child &child)
Configure a logger of a child algorithm if exists.
logging_mixin(log_tag_view tag)
Constructor.
auto logger() const noexcept -> const num_collect::logging::logger &
Access to the logger.
Base class of solvers of ODEs.
Definition solver_base.h:38
auto steps() const -> index_type
Get the number of steps.
auto problem() const -> const problem_type &
Get the problem.
Formula formula_type
Type of formula.
Definition solver_base.h:41
typename formula_type::problem_type problem_type
Type of problem.
Definition solver_base.h:44
auto formula() const -> const formula_type &
Get the formula.
typename problem_type::variable_type variable_type
Type of variables.
Definition solver_base.h:47
void solve_till(scalar_type end_time)
Compute the variable at the given time.
Definition solver_base.h:95
static constexpr index_type stages
Number of stages of the formula.
Definition solver_base.h:53
auto problem() -> problem_type &
Get the problem.
static constexpr index_type order
Order of the formula.
Definition solver_base.h:56
void init(scalar_type time, const variable_type &variable)
Initialize.
Definition solver_base.h:75
auto variable() const -> const variable_type &
Get the current variable.
auto step_size() const -> scalar_type
Get the step size.
auto step_size(scalar_type val) -> Derived &
Set the step size.
solver_base(const problem_type &problem)
Constructor.
Definition solver_base.h:63
auto derived() noexcept -> Derived &
Access derived object.
auto derived() const noexcept -> const Derived &
Access derived object.
void step()
Compute the variable of the next step.
Definition solver_base.h:85
typename problem_type::scalar_type scalar_type
Type of scalars.
Definition solver_base.h:50
auto formula() -> formula_type &
Get the formula.
formula_type formula_
Formula.
auto time() const -> scalar_type
Get the current time.
Definition of formula concept.
Definition of index_type type.
Definition of iteration_logger_mixin class.
Definition of logging_mixin class.
std::ptrdiff_t index_type
Type of indices in this library.
Definition index_type.h:33
Namespace of solvers of ordinary differential equations (ODE).