OdeImplicitEuler.h 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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/GMatrix.h>
  9. #include <Mathematics/OdeSolver.h>
  10. // The TVector template parameter allows you to create solvers with
  11. // Vector<N,Real> when the dimension N is known at compile time or
  12. // GVector<Real> when the dimension N is known at run time. Both classes
  13. // have 'int GetSize() const' that allow OdeSolver-derived classes to query
  14. // for the dimension. The TMatrix parameter must be either Matrix<N,N,Real>
  15. // or GMatrix<Real> accordingly.
  16. //
  17. // The function F(t,x) has input t, a scalar, and input x, an N-vector.
  18. // The first derivative matrix with respect to x is DF(t,x), an
  19. // N-by-N matrix. Entry DF(r,c) is the derivative of F[r] with
  20. // respect to x[c].
  21. namespace WwiseGTE
  22. {
  23. template <typename Real, typename TVector, typename TMatrix>
  24. class OdeImplicitEuler : public OdeSolver<Real, TVector>
  25. {
  26. public:
  27. // Construction and destruction.
  28. virtual ~OdeImplicitEuler() = default;
  29. OdeImplicitEuler(Real tDelta,
  30. std::function<TVector(Real, TVector const&)> const& F,
  31. std::function<TMatrix(Real, TVector const&)> const& DF)
  32. :
  33. OdeSolver<Real, TVector>(tDelta, F),
  34. mDerivativeFunction(DF)
  35. {
  36. }
  37. // Estimate x(t + tDelta) from x(t) using dx/dt = F(t,x). You may
  38. // allow xIn and xOut to be the same object.
  39. virtual void Update(Real tIn, TVector const& xIn, Real& tOut, TVector& xOut) override
  40. {
  41. TVector fVector = this->mFunction(tIn, xIn);
  42. TMatrix dfMatrix = mDerivativeFunction(tIn, xIn);
  43. TMatrix dgMatrix = TMatrix::Identity() - this->mTDelta * dfMatrix;
  44. TMatrix dgInverse = Inverse(dgMatrix);
  45. fVector = dgInverse * fVector;
  46. tOut = tIn + this->mTDelta;
  47. xOut = xIn + this->mTDelta * fVector;
  48. }
  49. private:
  50. std::function<TMatrix(Real, TVector const&)> mDerivativeFunction;
  51. };
  52. }