Gyselalib++
 
Loading...
Searching...
No Matches
rk2.hpp
1// SPDX-License-Identifier: MIT
2#pragma once
3#include <array>
4#include <type_traits>
5
6#include "ddc_alias_inline_functions.hpp"
7#include "ddc_aliases.hpp"
8#include "ddc_helper.hpp"
9#include "itimestepper.hpp"
10#include "vector_field_common.hpp"
11
32template <
33 class FieldMem,
34 class DerivFieldMem = FieldMem,
35 class ExecSpace = Kokkos::DefaultExecutionSpace>
36class RK2 : public ITimeStepper<FieldMem, DerivFieldMem, ExecSpace>
37{
39
40public:
41 using typename base_type::IdxRange;
42
43 using typename base_type::ValConstField;
44 using typename base_type::ValField;
45
46 using typename base_type::DerivConstField;
47 using typename base_type::DerivField;
48
49private:
50 IdxRange const m_idx_range;
51
52public:
54
55public:
60 explicit RK2(IdxRange idx_range) : m_idx_range(idx_range) {}
61
77 void update(
78 ExecSpace const& exec_space,
79 ValField y,
80 double dt,
81 std::function<void(DerivField, ValConstField)> dy_calculator,
82 std::function<void(ValField, DerivConstField, double)> y_update) const final
83 {
84 DerivFieldMem k1_alloc(m_idx_range);
85 DerivFieldMem k2_alloc(m_idx_range);
86 FieldMem y_prime_alloc(m_idx_range);
87
88 DerivField k1(k1_alloc);
89 DerivField k2(k2_alloc);
90 ValField y_prime(y_prime_alloc);
91
92 // Save initial conditions
93 base_type::copy(y_prime, get_const_field(y));
94
95 // --------- Calculate k1 ------------
96 // Calculate k1 = f(y)
97 dy_calculator(k1, get_const_field(y));
98
99 // --------- Calculate k2 ------------
100 // Calculate y_new := y_n + h/2*k_1
101 y_update(y_prime, get_const_field(k1), 0.5 * dt);
102
103 // Calculate k2 = f(y_new)
104 dy_calculator(k2, get_const_field(y_prime));
105
106 // ----------- Update y --------------
107 // Calculate y_{n+1} := y_n + h*k_2
108 y_update(y, get_const_field(k2), dt);
109 }
110};
See DerivFieldMemImplementation.
Definition derivative_field.hpp:10
See DerivFieldImplementation.
Definition derivative_field.hpp:20
The superclass from which all timestepping methods inherit.
Definition itimestepper.hpp:23
typename FieldMem::discrete_domain_type IdxRange
The type of the index range on which the values of the function are defined.
Definition itimestepper.hpp:46
typename DerivFieldMem::span_type DerivField
The type of the derivatives of the function being evolved.
Definition itimestepper.hpp:56
typename DerivFieldMem::view_type DerivConstField
The constant type of the derivatives values of the function being evolved.
Definition itimestepper.hpp:59
typename FieldMem::span_type ValField
The type of the values of the function being evolved.
Definition itimestepper.hpp:50
void update(ValField y, double dt, std::function< void(DerivField, ValConstField)> dy_calculator) const
Carry out one step of the timestepping scheme.
Definition itimestepper.hpp:78
void copy(ValField copy_to, ValConstField copy_from) const
Make a copy of the values of the function being evolved.
Definition itimestepper.hpp:147
typename FieldMem::view_type ValConstField
The constant type of the values of the function being evolved.
Definition itimestepper.hpp:53
A class which provides an implementation of a second-order Runge-Kutta method.
Definition rk2.hpp:37
RK2(IdxRange idx_range)
Create a RK2 object.
Definition rk2.hpp:60
typename DerivFieldMem::view_type DerivConstField
The constant type of the derivatives values of the function being evolved.
Definition itimestepper.hpp:59
typename FieldMem::span_type ValField
The type of the values of the function being evolved.
Definition itimestepper.hpp:50
typename FieldMem::view_type ValConstField
The constant type of the values of the function being evolved.
Definition itimestepper.hpp:53
void update(ExecSpace const &exec_space, ValField y, double dt, std::function< void(DerivField, ValConstField)> dy_calculator, std::function< void(ValField, DerivConstField, double)> y_update) const final
Carry out one step of the Runge-Kutta scheme.
Definition rk2.hpp:77