Skip to content

Commit 925c757

Browse files
jkosataoameye
andauthored
Workshop fixes (#51)
* "Computer Modern" not global default font yet only the HarmonicBalance plot functions will use Computer modern by "default" * set default font by dictionary * _set_Plots_default in modules * plot! for time-dependent * plot! bug for default class * get_steady_states with all params fixed * export solve and ODEProblem to Main * version bump Co-authored-by: oameye <[email protected]>
1 parent dc97c59 commit 925c757

File tree

8 files changed

+41
-30
lines changed

8 files changed

+41
-30
lines changed

Project.toml

Lines changed: 3 additions & 3 deletions
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 <[email protected]>", "Javier del Pino <[email protected]>"]
4-
version = "0.6.0"
4+
version = "0.6.1"
55

66
[deps]
77
BijectiveHilbert = "91e7fc40-53cd-4118-bd19-d7fcd1de2a54"
@@ -36,8 +36,8 @@ Latexify = "0.15.16"
3636
Peaks = "0.4.0"
3737
Plots = "1.35.0"
3838
ProgressMeter = "1.7.2"
39-
Symbolics = "4.10.4"
40-
julia = "1.8.0"
39+
Symbolics = "4.11.0"
40+
julia = "1.8.2"
4141

4242
[extras]
4343
Pkg = "44cfe95a-1eb2-52ea-b672-e2afdf69b78f"

src/HarmonicBalance.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ module HarmonicBalance
5656

5757
include("modules/TimeEvolution.jl")
5858
using .TimeEvolution
59-
export ParameterSweep
59+
export ParameterSweep, ODEProblem, solve
6060

6161
include("modules/LimitCycles.jl")
6262
using .LimitCycles

src/hysteresis_sweep.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ function plot_1D_solutions_branch(starting_branch::Int64,res::Result; x::String,
8989

9090
Y_followed = [Ys[branch,param_idx] for (param_idx,branch) in enumerate(followed_branch)]
9191

92-
lines = plot(transform_solutions(res, x),Y_followed,c="k",marker=m; kwargs...)
92+
lines = plot(transform_solutions(res, x),Y_followed,c="k",marker=m; _set_Plots_default..., kwargs...)
9393

9494
#extract plotted data and return it
9595
xdata,ydata = [line.get_xdata() for line in lines], [line.get_ydata() for line in lines]

src/modules/LinearResponse/plotting.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ C = order == 1 ? get_jacobian_response(res, nat_var, Ω_range, branch) : get_lin
6363
C = logscale ? log.(C) : C
6464

6565
heatmap(X, Ω_range, C; color=:viridis,
66-
xlabel=latexify(string(first(keys(res.swept_parameters)))), ylabel=latexify("Ω"), kwargs...)
66+
xlabel=latexify(string(first(keys(res.swept_parameters)))), ylabel=latexify("Ω"), HarmonicBalance._set_Plots_default..., kwargs...)
6767
end
6868

6969

src/modules/TimeEvolution/ODEProblem.jl

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
using LinearAlgebra, Latexify
2-
import HarmonicBalance: transform_solutions, plot
3-
export transform_solutions, plot
2+
import HarmonicBalance: transform_solutions, plot, plot!
3+
import DifferentialEquations: ODEProblem, solve
4+
export transform_solutions, plot, plot!
5+
export ODEProblem, solve
46

57
"""
68
@@ -86,14 +88,21 @@ Plot a function `f` of a time-dependent solution `soln` of `harm_eq`.
8688
8789
Parametric plot of f[1] against f[2]
8890
91+
Also callable as plot!
8992
"""
90-
function plot(soln::OrdinaryDiffEq.ODECompositeSolution, funcs, harm_eq::HarmonicEquation; kwargs...)
91-
HarmonicBalance._set_Plots_default()
93+
function plot(soln::OrdinaryDiffEq.ODECompositeSolution, funcs, harm_eq::HarmonicEquation; add=false, kwargs...)
94+
95+
# start a new plot if needed
96+
p = add ? plot!() : plot()
97+
9298
if funcs isa String || length(funcs) == 1
93-
plot(soln.t, transform_solutions(soln, funcs, harm_eq); legend=false, xlabel="time", ylabel=latexify(funcs), kwargs...)
99+
plot!(soln.t, transform_solutions(soln, funcs, harm_eq); HarmonicBalance._set_Plots_default..., xlabel="time", ylabel=latexify(funcs), legend=false, kwargs...)
94100
elseif length(funcs) == 2 # plot of func vs func
95-
plot(transform_solutions(soln, funcs, harm_eq)...; legend=false, xlabel=latexify(funcs[1]), ylabel=latexify(funcs[2]), kwargs...)
101+
plot!(transform_solutions(soln, funcs, harm_eq)...; HarmonicBalance._set_Plots_default..., xlabel=latexify(funcs[1]), ylabel=latexify(funcs[2]), legend=false, kwargs...)
96102
else
97103
error("Invalid plotting argument: ", funcs)
98104
end
99-
end
105+
end
106+
107+
108+
plot!(soln::OrdinaryDiffEq.ODECompositeSolution, varargs...; kwargs...) = plot(soln, varargs...; add=true, kwargs...)

src/plotting_Plots.jl

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
using Plots, Latexify
22
import Plots.plot, Plots.plot!; export plot, plot!, plot_phase_diagram, savefig
33

4-
function _set_Plots_default()
5-
font = "Computer Modern"
6-
default(linewidth=2, legend=:outerright)
7-
default(fontfamily=font, titlefont=font , tickfont=font)
8-
end
4+
const _set_Plots_default = Dict{Symbol, Any}([
5+
:fontfamily => "computer modern",
6+
:titlefont => "computer modern",
7+
:tickfont => "computer modern",
8+
:linewidth => 2,
9+
:legend => :outerright])
10+
11+
912

1013
dim(res::Result) = length(size(res.solutions)) # give solution dimensionality
1114

@@ -40,11 +43,10 @@ default behaviour: plot stable solutions as full lines, unstable as dashed
4043
the x and y axes are taken automatically from `res`
4144
"""
4245
function plot(res::Result, varargs...; kwargs...)::Plots.Plot
43-
HarmonicBalance._set_Plots_default()
4446
if dim(res) == 1
45-
plot1D(res, varargs...; kwargs...)
47+
plot1D(res, varargs...; _set_Plots_default..., kwargs...)
4648
elseif dim(res) == 2
47-
plot2D(res, varargs...; kwargs...)
49+
plot2D(res, varargs...; _set_Plots_default..., kwargs...)
4850
else
4951
error("Data dimension ", dim(res), " not supported")
5052
end
@@ -56,9 +58,9 @@ $(TYPEDSIGNATURES)
5658
5759
Similar to `plot` but adds a plot onto an existing plot.
5860
"""
59-
plot!(res::Result, varargs...; kwargs...)::Plots.Plot = plot(res, varargs...; add=true, kwargs...)
60-
61-
61+
function plot!(res::Result, varargs...; kwargs...)::Plots.Plot
62+
plot(res, varargs...; add=true, _set_Plots_default..., kwargs...)
63+
end
6264
"""
6365
$(TYPEDSIGNATURES)
6466
@@ -105,7 +107,7 @@ function plot1D(res::Result; x::String="default", y::String, class="default", no
105107

106108
if class == "default"
107109
if not_class == [] # plot stable full, unstable dashed
108-
p = plot1D(res; x=x, y=y, class=["physical", "stable"], kwargs...)
110+
p = plot1D(res; x=x, y=y, class=["physical", "stable"], add=add, kwargs...)
109111
plot1D(res; x=x, y=y, class="physical", not_class="stable", add=true, style=:dash, kwargs...)
110112
return p
111113
else
@@ -167,11 +169,10 @@ Class selection done by passing `String` or `Vector{String}` as kwarg:
167169
Other kwargs are passed onto Plots.gr()
168170
"""
169171
function plot_phase_diagram(res::Result; kwargs...)::Plots.Plot
170-
_set_Plots_default()
171172
if dim(res) == 1
172-
plot_phase_diagram_1D(res; kwargs...)
173+
plot_phase_diagram_1D(res; _set_Plots_default..., kwargs...)
173174
elseif dim(res) == 2
174-
plot_phase_diagram_2D(res; kwargs...)
175+
plot_phase_diagram_2D(res; _set_Plots_default..., kwargs...)
175176
else
176177
error("Data dimension ", dim(res), " not supported")
177178
end

src/solve_homotopy.jl

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ $(TYPEDSIGNATURES)
77
Return an ordered dictionary specifying all variables and parameters of the solution
88
in `result` on `branch` at the position `index`.
99
"""
10-
function get_single_solution(res::Result; branch::Int64, index)
10+
function get_single_solution(res::Result; branch::Int64, index)::OrderedDict{Num, ComplexF64}
1111

1212
# check if the dimensionality of index matches the solutions
1313
if length(size(res.solutions)) !== length(index)
@@ -127,6 +127,7 @@ end
127127

128128
get_steady_states(p::Problem, swept, fixed; kwargs...) = get_steady_states(p, ParameterRange(swept), ParameterList(fixed); kwargs...)
129129
get_steady_states(eom::HarmonicEquation, swept, fixed; kwargs...) = get_steady_states(Problem(eom), swept, fixed; kwargs...)
130+
get_steady_states(p::Union{Problem, HarmonicEquation}, fixed; kwargs...) = get_steady_states(p, [], fixed; kwargs...)
130131

131132

132133
""" Compile the Jacobian from `prob`, inserting `fixed_parameters`.
@@ -239,7 +240,7 @@ function _get_raw_solution(problem::Problem, parameter_values::Array{ParameterVe
239240
# HomotopyContinuation accepts 1D arrays of parameter sets
240241
params_1D = reshape(parameter_values, :, 1)
241242

242-
if random_warmup
243+
if random_warmup && !isempty(sweep)
243244
complex_pert = [1E-2 * issubset(p, keys(sweep))*randn(ComplexF64) for p in problem.parameters] # complex perturbation of the warmup parameters
244245
warmup_parameters = params_1D[Int(round(length(params_1D)/2))] .* (ones(length(params_1D[1])) + complex_pert)
245246
warmup_solution = HomotopyContinuation.solve(problem.system, target_parameters=warmup_parameters, threading=threading)

test/parametron_result.jld2

0 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)