File tree Expand file tree Collapse file tree 1 file changed +9
-22
lines changed
src/solvers/local_solvers Expand file tree Collapse file tree 1 file changed +9
-22
lines changed Original file line number Diff line number Diff line change 11
2- function runge_kutta_2 (H, t, ψ0)
3- Hψ = H (ψ0)
4- H2ψ = H (Hψ)
5- return (ψ0 + t * Hψ + (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- Hψ = runge_kutta_4 (H, time, ψ)
19- elseif order == 2
20- Hψ = 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 Hψ , (;)
11+ return eHψ , (;)
2512end
You can’t perform that action at this time.
0 commit comments