Skip to content

Instantly share code, notes, and snippets.

Created March 3, 2018 21:07
Show Gist options
  • Save anonymous/5c8766cc3f5b8eb6eb08cfb1c5a39c75 to your computer and use it in GitHub Desktop.
Save anonymous/5c8766cc3f5b8eb6eb08cfb1c5a39c75 to your computer and use it in GitHub Desktop.
import math
import arraymancer
const
dz = 0.01
z = 100
spaceSteps = int(z / dz)
timeSteps = 50000
dt = 0.12 / timeSteps
alpha = 2.0
startingTemp = 30.0
oscillations = 20.0
var
Ts = startingTemp * ones[float](timeSteps, spaceSteps)
for j in 0 ..< timeSteps:
Ts[j, 0] = startingTemp - oscillations * sin(2.0 * PI * j.float / timeSteps)
proc f(T: Tensor[float]): Tensor[float] =
let d2T_dz2 = (T[_, 0..^3] - 2.0 * T[_, 1..^2] + T[_, 2..^1]) / dz^2
return alpha * d2T_dz2
proc eulerSolve(Ts: var Tensor[float]) =
for t in 0 ..< timeSteps-1:
Ts[t+1, 1..^2] = Ts[t, 1..^2] + dt * f(Ts[t, _])
Ts[t+1, ^1] = Ts[t+1, ^2]
Ts.eulerSolve()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment