Skip to content

Commit 96cb182

Browse files
committed
Simplify Runge Kutta solver
1 parent 97e7c06 commit 96cb182

File tree

1 file changed

+9
-22
lines changed

1 file changed

+9
-22
lines changed
Lines changed: 9 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,12 @@
11

2-
function runge_kutta_2(H, t, ψ0)
3-
= H(ψ0)
4-
H2ψ = H(Hψ)
5-
return (ψ0 + t *+ (t^2 / 2) * H2ψ)
6-
end
7-
8-
function runge_kutta_4(H, t, ψ0)
9-
k1 = H(ψ0)
10-
k2 = k1 + (t / 2) * H(k1)
11-
k3 = k1 + (t / 2) * H(k2)
12-
k4 = k1 + t * H(k3)
13-
return ψ0 + (t / 6) * (k1 + 2 * k2 + 2 * k3 + k4)
14-
end
15-
16-
function runge_kutta_solver(H, time, ψ; order=4, kws...)
17-
if order == 4
18-
= runge_kutta_4(H, time, ψ)
19-
elseif order == 2
20-
= runge_kutta_2(H, time, ψ)
21-
else
22-
error("For runge_kutta_solver, must specify `order` keyword")
2+
function runge_kutta_solver(H, t, ψ; order=4, kws...)
3+
# For linear ODE, Runge-Kutta is a Taylor series.
4+
# Pattern below derived as:
5+
# exp(tH)ψ = ψ + tHψ + (tH)^2(ψ)/2! + (tH)^3(ψ)/3! + ...
6+
# = ψ + (tH) * (ψ + (tH)/2 * (ψ + (tH)/3 * (ψ + ...)))
7+
eHψ = copy(ψ)
8+
for ord in reverse(1:order)
9+
eHψ = (t/ord)*H(eHψ) + ψ
2310
end
24-
return , (;)
11+
return eHψ, (;)
2512
end

0 commit comments

Comments
 (0)