Skip to content

Commit 1dd7d07

Browse files
authored
Merge pull request #220 from JuliaDynamics/hw/update_benchmarking
update benchmark script
2 parents f0bd1e2 + afabc3d commit 1dd7d07

File tree

7 files changed

+165
-461
lines changed

7 files changed

+165
-461
lines changed

benchmark/benchmark_cases.jl

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
"""
2+
BenchmarkCase
3+
4+
Represents a single benchmark case for NetworkDynamics.
5+
6+
Fields:
7+
- name: String identifier for the benchmark
8+
- constructor: Function that creates the Network for a given N
9+
- Ns: Vector of network sizes to benchmark
10+
- description: Documentation string
11+
"""
12+
struct BenchmarkCase
13+
name::String
14+
constructor::Function
15+
Ns::Vector{Int}
16+
description::String
17+
end
18+
19+
# Standard benchmark cases
20+
BENCHMARK_CASES = [
21+
BenchmarkCase(
22+
"diffusion_static_edge",
23+
N -> begin
24+
g = watts_strogatz(N, N ÷ 2, 0.0; rng=StableRNG(1))
25+
(g, diffusion_vertex(), diffusion_edge())
26+
end,
27+
# [10,20],
28+
[100, 300, 1000, 3000],
29+
"Diffusion network with static edges"
30+
),
31+
32+
BenchmarkCase(
33+
"diffusion_ode_edge",
34+
N -> begin
35+
g = watts_strogatz(N, N ÷ 2, 0.0; rng=StableRNG(1))
36+
(g, diffusion_vertex(), diffusion_dedge())
37+
end,
38+
# [10,20],
39+
[100, 300, 1000, 3000],
40+
"Diffusion network with ODE edges"
41+
),
42+
43+
BenchmarkCase(
44+
"kuramoto_homogeneous",
45+
N -> begin
46+
g = watts_strogatz(N, 3, 0.8; rng=StableRNG(1))
47+
(g, kuramoto_vertex_2d(), static_kuramoto_edge())
48+
end,
49+
# [10,20],
50+
[100, 1_000, 10_000, 100_000],
51+
"Homogeneous Kuramoto oscillators"
52+
),
53+
54+
BenchmarkCase(
55+
"kuramoto_heterogeneous",
56+
N -> begin
57+
g = watts_strogatz(N, 3, 0.8; rng=StableRNG(1))
58+
rng = StableRNG(1)
59+
vtypes = [kuramoto_vertex_1d(), kuramoto_vertex_2d()]
60+
vertices = vtypes[shuffle(rng, vcat([1 for _ in 1:N÷2], [2 for _ in 1:N÷2]))]
61+
(g, vertices, static_kuramoto_edge())
62+
end,
63+
# [10,20],
64+
[100, 1_000, 10_000, 100_000],
65+
"Heterogeneous Kuramoto oscillators"
66+
),
67+
68+
BenchmarkCase(
69+
"powergrid",
70+
N -> begin
71+
g = watts_strogatz(N, 3, 0.8; rng=StableRNG(1))
72+
rng = StableRNG(1)
73+
vtypes = [pqnode(), generator()]
74+
vertices = vtypes[shuffle(rng, vcat([1 for _ in 1:N÷2], [2 for _ in 1:N÷2]))]
75+
(g, vertices, piline())
76+
end,
77+
# [10,20],
78+
[100, 1_000, 10_000, 100_000],
79+
"Power grid network with PQ nodes and generators"
80+
),
81+
82+
BenchmarkCase(
83+
"powergrid_inhomogeneous_pq",
84+
N -> begin
85+
g = watts_strogatz(N, 3, 0.8; rng=StableRNG(1))
86+
rng = StableRNG(1)
87+
88+
# Create fixed P/Q values for half the nodes
89+
pq_count = N ÷ 2
90+
P_vals = -1*rand(rng, pq_count)
91+
Q_vals = -1*rand(rng, pq_count)
92+
93+
# Create vertex models with baked-in P/Q values
94+
pq_vertices = [pqnode_inhomogeneous(P_vals[i], Q_vals[i]) for i in 1:pq_count]
95+
gen_vertices = [generator() for _ in 1:(N-pq_count)]
96+
97+
# Shuffle the vertex types
98+
vertices = shuffle(rng, vcat(pq_vertices, gen_vertices))
99+
100+
(g, vertices, piline())
101+
end,
102+
[100, 1_000, 10_000, 100_000],
103+
"Power grid with heterogeneous PQ nodes having fixed P/Q values"
104+
)
105+
]

benchmark/benchmark_compat.jl

Lines changed: 4 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -1,88 +1,14 @@
11
#=
2-
Helper functions to create compatible states between NetworkDynamics.jl and NetworkDynamics.jl v0.8
2+
Helper functions to create compatible states between newer and older versions of NetworkDynamics.jl
3+
for cross version benchmarking.
34
=#
45

5-
if pkgversion(NetworkDynamics) < v"0.9.0"
6-
@info "Define compatibility functions"
7-
struct Network end
8-
struct SequentialExecution{P} end
9-
struct KAExecution{P} end
10-
struct PolyesterExecution{P} end
11-
struct ThreadedExecution{P} end
12-
struct KAAggregator; f; end
13-
struct SequentialAggregator; f; end
14-
struct PolyesterAggregator; f; end
15-
struct ThreadedAggregator; f; end
16-
function Network(g, v, e; execution = SequentialExecution{true}(), aggregator=SequentialAggregator(+))
17-
if execution isa PolyesterExecution{true} && aggregator isa PolyesterAggregator
18-
network_dynamics(v, e, g; parallel=true)
19-
elseif execution isa SequentialExecution{true} && aggregator isa SequentialAggregator
20-
network_dynamics(v, e, g; parallel=false)
21-
else
22-
error("execution type not supported")
23-
end
24-
end
25-
elseif pkgversion(NetworkDynamics) < v"0.9.1"
26-
isdynamic = NetworkDynamics.isdynamic
27-
else
28-
isdynamic(_) = true
29-
end
30-
31-
function _syms_old_order(nd::Network)
32-
syms = []
33-
for (i,cf) in enumerate(nd.im.vertexm)
34-
isdynamic(cf) || continue
35-
append!(syms, collect(VIndex(i, 1:dim(cf))))
36-
end
37-
for (i,cf) in enumerate(nd.im.edgem)
38-
isdynamic(cf) || continue
39-
append!(syms, collect(EIndex(i, 1:dim(cf))))
40-
end
41-
syms
42-
end
43-
function _psyms_old_order(nd::Network)
44-
syms = []
45-
for (i,cf) in enumerate(nd.im.vertexm)
46-
append!(syms, collect(VPIndex(i, 1:pdim(cf))))
47-
end
48-
for (i,cf) in enumerate(nd.im.edgem)
49-
append!(syms, collect(EPIndex(i, 1:pdim(cf))))
50-
end
51-
syms
52-
end
536
function randx0(nd::Network)
547
rng = StableRNG(1)
55-
_x0 = rand(rng, dim(nd))
56-
57-
s = NWState(nd; utype=typeof(_x0))
58-
for (i,sym) in enumerate(_syms_old_order(nd::Network))
59-
s[sym] = _x0[i]
60-
end
61-
s[:]
62-
end
63-
randx0(nd::ODEFunction) = rand(StableRNG(1), length(nd.syms))
64-
function legacy_order(nd::Network, _dx)
65-
s = NWState(nd, _dx)
66-
dx = similar(_dx)
67-
for (i,sym) in enumerate(_syms_old_order(nd::Network))
68-
dx[i] = s[sym]
69-
end
70-
dx
8+
rand(rng, dim(nd))
719
end
72-
legacy_order(nd::ODEFunction, dx) = dx
7310

7411
function randp(nd::Network)
7512
rng = StableRNG(1)
76-
_p = rand(rng, pdim(nd))
77-
p = NWParameter(nd; ptype=typeof(_p))
78-
for (i, sym) in enumerate(_psyms_old_order(nd))
79-
p[sym] = _p[i]
80-
end
81-
p[:]
82-
end
83-
function randp(nd::ODEFunction)
84-
rng = StableRNG(1)
85-
g = nd.f.graph
86-
_p = rand(rng, nv(g) + ne(g))
87-
p = (_p[1:nv(g)], _p[nv(g)+1:end])
13+
rand(rng, pdim(nd))
8814
end

benchmark/benchmark_models.jl

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,3 +109,15 @@ Base.@propagate_inbounds function gen!(dv, v, isum, p, T)
109109
end
110110
generator() = VertexModel(; f=gen!, sym=[:u_r, :u_i, , ], g=StateMask(1:2),
111111
psym=[:H, :P, :D, , :E_f, :T_d_dash, :T_q_dash, :X_d_dash, :X_q_dash, :X_d, :X_q])
112+
113+
function pqnode_inhomogeneous(P, Q)
114+
Base.@propagate_inbounds function pq!(du, u, isum, _, t)
115+
ic = Complex(isum[1], isum[2])
116+
S = Complex(P, Q)
117+
uc = S/conj(ic)
118+
du[1] = real(uc)
119+
du[2] = imag(uc)
120+
nothing
121+
end
122+
VertexModel(; f=pq!, sym=[:u_r, :u_i], g=StateMask(1:2), psym=[], mass_matrix=0)
123+
end

benchmark/benchmark_models_v0.8.jl

Lines changed: 0 additions & 46 deletions
This file was deleted.

benchmark/benchmark_models_v0.9.jl

Lines changed: 0 additions & 109 deletions
This file was deleted.

benchmark/benchmark_utils.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,10 +244,11 @@ function plot_over_N(target, baseline=nothing)
244244
comp = target
245245
end
246246

247-
fig = Makie.Figure(size=(2000,2000))
247+
fig = Makie.Figure(size=(2000,2500))
248248

249249
bmkeys = [
250250
"powergrid",
251+
"powergrid_inhomogeneous_pq",
251252
"diffusion_static_edge",
252253
"diffusion_ode_edge",
253254
"kuramoto_homogeneous",

0 commit comments

Comments
 (0)