Advertisement
Guest User

Untitled

a guest
Nov 7th, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Julia 1.52 KB | None | 0 0
  1. using Plots
  2. gr()
  3. const g = 1.0
  4. const dx = 0.02
  5.  
  6. function v0(x)
  7.     if x >= 0.0
  8.         return [1.0, 0.0, 0.0]
  9.     else
  10.         return [2.0, 0.0, 0.0]
  11.     end
  12. end
  13.    
  14.    
  15. # V[x, t, q] where q=0 is h, q=1 is hu, q=2 is hv
  16. function initialize_v()
  17.     V = zeros(Int(round(2.0/dx)), 60, 3)
  18.  
  19.     for i in 1:size(V)[1]
  20.         V[i, 1, :] = v0(-1.0 + (i-1)*dx)
  21.     end
  22.     return V
  23. end
  24.  
  25.  
  26. # numerical flux
  27. function F(Vin)
  28.     return [Vin[2], Vin[2]^2/Vin[1] + g * Vin[1]^2/2, Vin[2]*Vin[3]/Vin[1]]
  29. end
  30.  
  31.  
  32. function lambda_max(V, i, n)
  33.     if i+1 <= size(V)[1]
  34.         l = abs(0.5*(V[i, n, 2]/V[i, n, 1] + (V[i+1, n, 2]/V[i+1, n, 1])))
  35.         l += sqrt(g * 0.5 * (V[i, n, 1] + V[i+1, n, 1]))
  36.     else
  37.         l = abs(V[i, n, 2]/V[i, n, 1]) + sqrt(g * V[i, n, 1])
  38.     end
  39.     return l
  40. end
  41.  
  42.  
  43. # flux at i + 1/2 , n
  44. function Fin(V, i, n)
  45.     lambda = lambda_max(V, i, n)
  46.     if i+1 <= size(V)[1]
  47.         return 0.5*(F(V[i, n, :]) + F(V[i+1, n, :])) - 0.5 * lambda * (V[i+1, n, :] - V[i, n, :])
  48.     else
  49.         return F(V[i, n, :])
  50.     end
  51. end
  52.  
  53.  
  54. function main(dx)
  55.     V = initialize_v()
  56.     dts = zeros(60)
  57.     for n in 1:60
  58.         lambdas = [lambda_max(V, i, n) for i = 1:100]
  59.         dt = 0.8 * minimum(dx ./ lambdas)
  60.         dts[n] = dt
  61.         V[1, n+1, :] = V[1, n, :]
  62.         for i in 2:100
  63.             V[i, n+1, :] = V[i, n, :] - dt/dx * (Fin(V, i, n) - Fin(V, i-1, n))
  64.         end
  65.         if sum(dts) > 0.5
  66.             return V[:, 1:n, :], dts[1:n]
  67.         end
  68.     end
  69.     return V, dts
  70. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement