WarpTwin
Documentation for WarpTwin models and classes.
Loading...
Searching...
No Matches
RK4Integrator.hpp
Go to the documentation of this file.
1/******************************************************************************
2* Copyright (c) ATTX LLC 2024. All Rights Reserved.
3*
4* This software and associated documentation (the "Software") are the
5* proprietary and confidential information of ATTX, LLC. The Software is
6* furnished under a license agreement between ATTX and the user organization
7* and may be used or copied only in accordance with the terms of the agreement.
8* Refer to 'license/attx_license.adoc' for standard license terms.
9*
10* EXPORT CONTROL NOTICE: THIS SOFTWARE MAY INCLUDE CONTENT CONTROLLED UNDER THE
11* INTERNATIONAL TRAFFIC IN ARMS REGULATIONS (ITAR) OR THE EXPORT ADMINISTRATION
12* REGULATIONS (EAR99). No part of the Software may be used, reproduced, or
13* transmitted in any form or by any means, for any purpose, without the express
14* written permission of ATTX, LLC.
15******************************************************************************/
16/*
17RK4 header file
18---------------
19
20Author: Alex Reynolds
21*/
22#ifndef INTEGRATOR_RK4_HPP
23#define INTEGRATOR_RK4_HPP
24
25#include <array>
26
28#include "Integrator.hpp"
29
30namespace warpos {
31
32 template <uint32 N>
33 class RK4Integrator : public Integrator<N> {
34 public:
35 RK4Integrator(Rates<N>& rate_calculator);
36
42 int16 step(floating_point start_time, floating_point end_time, const std::array<floating_point, N> &start_state, std::array<floating_point, N> &out_state) override;
43
53 void configureForStep(floating_point start_time, floating_point end_time, const std::array<floating_point, N> &start_state);
57 int16 calculateK1(std::array<floating_point, N> &state_for_k2);
62 int16 calculateK2(const std::array<floating_point, N> &state_in_k2, std::array<floating_point, N> &state_for_k3);
67 int16 calculateK3(const std::array<floating_point, N> &state_in_k2, std::array<floating_point, N> &state_for_k4);
71 int16 calculateK4(const std::array<floating_point, N> &state_in_k4);
74 void getValueEndStep(std::array<floating_point, N> &end_state);
75 protected:
77 floating_point _full_step_size = 1.0;
78 floating_point _half_step_size = 0.5;
79 floating_point _step_average;
80
82 floating_point _start_time;
83 floating_point _end_time;
84 std::array<floating_point, N> _start_state;
85
87 std::array<floating_point, N> _k1;
88 std::array<floating_point, N> _k2;
89 std::array<floating_point, N> _k3;
90 std::array<floating_point, N> _k4;
91
95 };
96
97 template <uint32 N>
99 : Integrator<N>(rate_calculator) {
101 _full_step_size = 0.0;
102 _half_step_size = 0.0;
103 _step_average = 0.0;
104 _start_time = 0.0;
105 _end_time = 0.0;
106
108 for(uint32 i = 0; i < N; i++) {
109 _start_state[i] = 0.0;
110 _k1[i] = 0.0;
111 _k2[i] = 0.0;
112 _k3[i] = 0.0;
113 _k4[i] = 0.0;
114 }
115 }
116
117 template <uint32 N>
118 void RK4Integrator<N>::configureForStep(floating_point start_time, floating_point end_time,
119 const std::array<floating_point, N> &start_state) {
121 _full_step_size = end_time - start_time;
124 _start_time = start_time;
125 _end_time = end_time;
126 for(uint32 i = 0; i < N; i++) {
127 _start_state[i] = start_state[i];
128 }
129 }
130
131 template <uint32 N>
132 int16 RK4Integrator<N>::calculateK1(std::array<floating_point, N> &state_for_k2) {
135
137 for(uint32 i = 0; i < N; i++) {
138 state_for_k2[i] = _start_state[i] + _half_step_size*_k1[i];
139 }
140 return _error;
141 }
142
143 template <uint32 N>
144 int16 RK4Integrator<N>::calculateK2(const std::array<floating_point, N> &state_in_k2,
145 std::array<floating_point, N> &state_for_k3) {
147 _error = Integrator<N>::_rate_calculator.calculateRates(_start_time + _half_step_size, state_in_k2, _k2);
148
150 for(uint32 i = 0; i < N; i++) {
151 state_for_k3[i] = _start_state[i] + _half_step_size*_k2[i];
152 }
153 return _error;
154 }
155
156 template <uint32 N>
157 int16 RK4Integrator<N>::calculateK3(const std::array<floating_point, N> &state_in_k3,
158 std::array<floating_point, N> &state_for_k4) {
160 _error = Integrator<N>::_rate_calculator.calculateRates(_start_time + _half_step_size, state_in_k3, _k3);
161
163 for(uint32 i = 0; i < N; i++) {
164 state_for_k4[i] = _start_state[i] + _full_step_size*_k3[i];
165 }
166 return _error;
167 }
168
169 template <uint32 N>
170 int16 RK4Integrator<N>::calculateK4(const std::array<floating_point, N> &state_in_k4) {
172 _error = Integrator<N>::_rate_calculator.calculateRates(_start_time + _full_step_size, state_in_k4, _k4);
173 return _error;
174 }
175
176 template <uint32 N>
177 void RK4Integrator<N>::getValueEndStep(std::array<floating_point, N> &end_state) {
178 for(uint32 i = 0; i < N; i++) {
179 end_state[i] = _start_state[i] + _step_average*(_k1[i] + 2.0*_k2[i] + 2.0*_k3[i] + _k4[i]);
180 }
181 }
182
183 template <uint32 N>
184 int16 RK4Integrator<N>::step(floating_point start_time, floating_point end_time,
185 const std::array<floating_point, N> &start_state,
186 std::array<floating_point, N> &out_state) {
190 configureForStep(start_time, end_time, start_state);
191 _error = calculateK1(out_state);
193 if (_error > 0) {return _error;} else {_saved_warning = _error;}
194 _error = calculateK2(out_state, out_state);
195 if (_error > 0) {return _error;} else {_saved_warning = _error;}
196 _error = calculateK3(out_state, out_state);
197 if (_error > 0) {return _error;} else {_saved_warning = _error;}
198 _error = calculateK4(out_state);
199 if (_error > 0) {return _error;} else {_saved_warning = _error;}
200 getValueEndStep(out_state);
201
202 return _saved_warning;
203 }
204
205}
206
207#endif
Integrator(Rates< N > &rate_calculator)
Constructor for the integrator.
Definition Integrator.hpp:60
Rates< N > & _rate_calculator
Reference to dynamics object that calculates rates.
Definition Integrator.hpp:56
std::array< floating_point, N > _start_state
Definition RK4Integrator.hpp:84
std::array< floating_point, N > _k3
Definition RK4Integrator.hpp:89
floating_point _step_average
Definition RK4Integrator.hpp:79
floating_point _full_step_size
Step size for integrator.
Definition RK4Integrator.hpp:77
floating_point _end_time
Definition RK4Integrator.hpp:83
int16 _error
Error code.
Definition RK4Integrator.hpp:93
std::array< floating_point, N > _k2
Definition RK4Integrator.hpp:88
floating_point _start_time
Initial state tracking.
Definition RK4Integrator.hpp:82
void getValueEndStep(std::array< floating_point, N > &end_state)
Calculation of final integrated step at the end of state.
Definition RK4Integrator.hpp:177
int16 calculateK1(std::array< floating_point, N > &state_for_k2)
Function to calculate k1.
Definition RK4Integrator.hpp:132
int16 calculateK2(const std::array< floating_point, N > &state_in_k2, std::array< floating_point, N > &state_for_k3)
Function to calculate k2.
Definition RK4Integrator.hpp:144
int16 calculateK4(const std::array< floating_point, N > &state_in_k4)
Function to calculate k4.
Definition RK4Integrator.hpp:170
std::array< floating_point, N > _k1
RK4 step rate values k1, k2, k3, k4 and var to calc average rate.
Definition RK4Integrator.hpp:87
floating_point _half_step_size
Definition RK4Integrator.hpp:78
int16 calculateK3(const std::array< floating_point, N > &state_in_k2, std::array< floating_point, N > &state_for_k4)
Function to calculate k3.
Definition RK4Integrator.hpp:157
RK4Integrator(Rates< N > &rate_calculator)
Definition RK4Integrator.hpp:98
int16 step(floating_point start_time, floating_point end_time, const std::array< floating_point, N > &start_state, std::array< floating_point, N > &out_state) override
Definition RK4Integrator.hpp:184
void configureForStep(floating_point start_time, floating_point end_time, const std::array< floating_point, N > &start_state)
Function to configure a full integration step.
Definition RK4Integrator.hpp:118
int16 _saved_warning
Definition RK4Integrator.hpp:94
std::array< floating_point, N > _k4
Definition RK4Integrator.hpp:90
Definition Rates.hpp:28
#define NO_ERROR
Error code in the case where matrix math executed successfully.
Definition clockwerkerrors.h:34
Definition DeadReckon.cpp:20