OdeRungeKutta4.h 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. // David Eberly, Geometric Tools, Redmond WA 98052
  2. // Copyright (c) 1998-2020
  3. // Distributed under the Boost Software License, Version 1.0.
  4. // https://www.boost.org/LICENSE_1_0.txt
  5. // https://www.geometrictools.com/License/Boost/LICENSE_1_0.txt
  6. // Version: 4.0.2019.08.13
  7. #pragma once
  8. #include <Mathematics/OdeSolver.h>
  9. // The TVector template parameter allows you to create solvers with
  10. // Vector<N,Real> when the dimension N is known at compile time or
  11. // GVector<Real> when the dimension N is known at run time. Both classes
  12. // have 'int GetSize() const' that allow OdeSolver-derived classes to query
  13. // for the dimension.
  14. namespace WwiseGTE
  15. {
  16. template <typename Real, typename TVector>
  17. class OdeRungeKutta4 : public OdeSolver<Real, TVector>
  18. {
  19. public:
  20. // Construction and destruction.
  21. virtual ~OdeRungeKutta4() = default;
  22. OdeRungeKutta4(Real tDelta, std::function<TVector(Real, TVector const&)> const& F)
  23. :
  24. OdeSolver<Real, TVector>(tDelta, F)
  25. {
  26. }
  27. // Estimate x(t + tDelta) from x(t) using dx/dt = F(t,x). You may
  28. // allow xIn and xOut to be the same object.
  29. virtual void Update(Real tIn, TVector const& xIn, Real& tOut, TVector& xOut) override
  30. {
  31. // Compute the first step.
  32. Real halfTDelta = (Real)0.5 * this->mTDelta;
  33. TVector fTemp1 = this->mFunction(tIn, xIn);
  34. TVector xTemp = xIn + halfTDelta * fTemp1;
  35. // Compute the second step.
  36. Real halfT = tIn + halfTDelta;
  37. TVector fTemp2 = this->mFunction(halfT, xTemp);
  38. xTemp = xIn + halfTDelta * fTemp2;
  39. // Compute the third step.
  40. TVector fTemp3 = this->mFunction(halfT, xTemp);
  41. xTemp = xIn + this->mTDelta * fTemp3;
  42. // Compute the fourth step.
  43. Real sixthTDelta = this->mTDelta / (Real)6;
  44. tOut = tIn + this->mTDelta;
  45. TVector fTemp4 = this->mFunction(tOut, xTemp);
  46. xOut = xIn + sixthTDelta * (fTemp1 + (Real)2 * (fTemp2 + fTemp3) + fTemp4);
  47. }
  48. };
  49. }