Skip to content

Commit ad8c991

Browse files
authored
docs: steady state sweeps example (#379)
* docs: add noise spectrum example * add Linear solve compat * docs: add SciMLExt page * fix spelling * add setp function from ModelingToolkit to ab_initio_noise example * add squeezing example to linear response tutorial * add steady_state_sweep to the manual * docs: steady state sweeps examples * compare sss * build: tag 0.13 * fix: correct parameter passing in steady_state_sweep example
1 parent 9d57e0f commit ad8c991

File tree

5 files changed

+81
-2
lines changed

5 files changed

+81
-2
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "HarmonicBalance"
22
uuid = "e13b9ff6-59c3-11ec-14b1-f3d2cc6c135e"
33
authors = ["Jan Kosata <kosataj@phys.ethz.ch>", "Javier del Pino <jdelpino@phys.ethz.ch>", "Orjan Ameye <orjan.ameye@hotmail.com>"]
4-
version = "0.12.5"
4+
version = "0.13.0"
55

66
[deps]
77
BijectiveHilbert = "91e7fc40-53cd-4118-bd19-d7fcd1de2a54"

docs/Project.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
[deps]
2+
BenchmarkTools = "6e4b80f9-dd63-53aa-95a3-0cdb28fa8baf"
23
DSP = "717857b8-e6f2-59f4-9121-6e50c889abd2"
34
Documenter = "e30172f5-a6a5-5a46-863b-614d45cd2de4"
45
DocumenterCitations = "daee34ce-89f3-4625-b898-19384cb65244"
56
DocumenterVitepress = "4710194d-e776-4893-9690-8d956a29c365"
67
HarmonicBalance = "e13b9ff6-59c3-11ec-14b1-f3d2cc6c135e"
8+
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
79
Literate = "98b081ad-f1c9-55d3-8b20-4c87d4299306"
810
ModelingToolkit = "961ee093-0014-501f-94e3-6117800e7a78"
11+
OrdinaryDiffEq = "1dea7af3-3e70-54e6-95c3-0bf5283fa5ed"
912
OrdinaryDiffEqTsit5 = "b1df2697-797e-41e3-8120-5422d3b24e4a"
1013
Plots = "91a5bcdd-55d7-5caf-9e0b-520d859cae80"
1114
StaticArrays = "90137ffa-7385-5640-81b9-e52037218182"

examples/Project.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
[deps]
2+
BenchmarkTools = "6e4b80f9-dd63-53aa-95a3-0cdb28fa8baf"
23
DSP = "717857b8-e6f2-59f4-9121-6e50c889abd2"
34
HarmonicBalance = "e13b9ff6-59c3-11ec-14b1-f3d2cc6c135e"
45
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
56
Literate = "98b081ad-f1c9-55d3-8b20-4c87d4299306"
67
ModelingToolkit = "961ee093-0014-501f-94e3-6117800e7a78"
78
NonlinearSolve = "8913a72c-1f9b-4ce2-8d82-65094dcecaec"
9+
OrdinaryDiffEq = "1dea7af3-3e70-54e6-95c3-0bf5283fa5ed"
810
OrdinaryDiffEqRosenbrock = "43230ef6-c299-4910-a778-202eb28ce4ce"
911
OrdinaryDiffEqTsit5 = "b1df2697-797e-41e3-8120-5422d3b24e4a"
1012
Plots = "91a5bcdd-55d7-5caf-9e0b-520d859cae80"

examples/steady_state_sweep.jl

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
# # Steady state sweeps
2+
3+
using HarmonicBalance, SteadyStateDiffEq, ModelingToolkit
4+
using BenchmarkTools, Plots, StaticArrays, OrdinaryDiffEq, LinearAlgebra
5+
using HarmonicBalance: OrderedDict
6+
7+
@variables α ω ω0 F γ η t x(t);
8+
9+
diff_eq = DifferentialEquation(
10+
d(x, t, 2) + ω0^2 * x + α * x^3 + γ * d(x, t) + η * d(x, t) * x^2 ~ F * cos* t), x
11+
)
12+
add_harmonic!(diff_eq, x, ω)
13+
harmonic_eq = get_harmonic_equations(diff_eq)
14+
15+
fixed = (ω0 => 1.0, γ => 1e-2, F => 0.02, α => 1.0, η => 0.3)
16+
ω_span = (0.8, 1.3);
17+
ω_range = range(ω_span..., 201);
18+
varied = ω => ω_range
19+
20+
result_HB = get_steady_states(harmonic_eq, varied, fixed)
21+
plot(result_HB, "sqrt(u1^2+v1^2)")
22+
23+
# ## Steady state sweep using `SteadyStateDiffEq.jl`
24+
param = OrderedDict(merge(Dict(fixed), Dict=> 1.1)))
25+
x0 = [0, 0.0];
26+
prob_ss = SteadyStateProblem(
27+
harmonic_eq, x0, param; in_place=false, u0_constructor=x -> SVector(x...)
28+
)
29+
30+
varied = 6 => ω_range
31+
result_ss = steady_state_sweep(
32+
prob_ss, DynamicSS(Rodas5()); varied, abstol=1e-5, reltol=1e-5
33+
)
34+
35+
plot(result_HB, "sqrt(u1^2+v1^2)")
36+
plot!(ω_range, norm.(result_ss); c=:gray, ls=:dash)
37+
38+
# ## Adiabatic evolution
39+
40+
timespan = (0.0, 50_000)
41+
sweep = AdiabaticSweep=> (0.8, 1.3), timespan) # linearly interpolate between two values at two times
42+
ode_problem = ODEProblem(harmonic_eq, fixed; u0=[0.01; 0.0], timespan, sweep)
43+
time_soln = solve(ode_problem, Tsit5(); saveat=250)
44+
45+
plot(result_HB, "sqrt(u1^2+v1^2)")
46+
plot(time_soln.t, norm.(time_soln.u))
47+
48+
# ## using follow_branch
49+
50+
followed_branch, Ys = follow_branch(1, result_HB; y="√(u1^2+v1^2)")
51+
Y_followed_gr =
52+
real.([Ys[param_idx][branch] for (param_idx, branch) in enumerate(followed_branch)]);
53+
54+
plot(result_HB, "sqrt(u1^2+v1^2)")
55+
plot!(ω_range, Y_followed_gr; c=:gray, ls=:dash)
56+
57+
# ## comparison
58+
59+
@btime result_ss = steady_state_sweep(
60+
prob_ss, DynamicSS(Rodas5()); varied, abstol=1e-5, reltol=1e-5
61+
)
62+
63+
@btime time_soln = solve(ode_problem, Tsit5(); saveat=250)
64+
65+
@btime begin
66+
followed_branch, Ys = follow_branch(1, result_HB; y="√(u1^2+v1^2)")
67+
Y_followed_gr =
68+
real.([Ys[param_idx][branch] for (param_idx, branch) in enumerate(followed_branch)])
69+
end
70+
71+
# Plotting them together gives:
72+
plot(ω_range, norm.(result_ss))
73+
plot!(ω_range, norm.(time_soln.u))
74+
plot!(ω_range, Y_followed_gr)

ext/SteadyStateDiffEqExt.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ function HarmonicBalance.steady_state_sweep(
6565
param_val = tunable_parameters(p)
6666
zeros = norm(prob_np.f.f.f.f.f_oop(sol_nn.u, param_val, 0))
6767
jac = prob_np.f.jac.f.f.f_oop(sol_nn.u, param_val, 0)
68-
eigval = jac isa Vector ? jac : eigvals(jac) # eigvals favourable supports FD.Dual
68+
eigval = jac isa AbstractVector ? jac : eigvals(jac) # eigvals favourable supports FD.Dual
6969

7070
if !isapprox(zeros, 0; atol=1e-5) || any-> λ > 0, real.(eigval))
7171
sol_ss = solve(remake(prob_ss; p, u0), alg_ss; abstol=1e-5, reltol=1e-5)

0 commit comments

Comments
 (0)