Advertisement
creamygoat

Heun’s Integrator with Adjustable Step Size

Apr 27th, 2013
337
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.80 KB | None | 0 0
  1. def Acceleration(Pos, Gravitators):
  2.   {Calculate net acceration Acc}
  3.   return Acc
  4.  
  5. def Integrate(Pos, Velocity, Duration, InitStepSize, Tolerance):
  6.  
  7.   TimeRemaining = Duration
  8.  
  9.   h = InitStepSize
  10.   NextH = h
  11.   P = Pos
  12.   V = Velocity
  13.  
  14.   while TimeRemaining > 0.0:
  15.  
  16.     # Euler method
  17.    
  18.     A = Acceleration(P)
  19.     PE = P + h * V
  20.     VE = V + h * A
  21.    
  22.     # Heun's improvement
  23.    
  24.     P = P + h * 0.5 * (V + VE)
  25.     V = V + h * 0.5 * (A + Acceleration(PE)
  26.    
  27.     # Adjust the delta time required by considering the
  28.     # Long term error caused by Euler's method.
  29.     LTE = VLength(PE - P) + Duration * VLength(VE - V)
  30.     NextH = h * sqrt(Tolerance / LTE) # Might want to protect from overflow
  31.     TimeRemaining -= h
  32.     h = min(NextH, TimeRemaining)
  33.    
  34.   return P, V
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement