Skip to content

Commit

Permalink
style: format
Browse files Browse the repository at this point in the history
  • Loading branch information
oameye committed Dec 31, 2024
1 parent 8570d9f commit 08360c1
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 72 deletions.
10 changes: 3 additions & 7 deletions benchmark/evaluate_drift.jl
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,10 @@ function H_p(x, p) # ℜ² → ℜ²
return Matrix([H_pu H_pv]')
end

sys_m = SgmamSystem{false, 2}(H_x, H_p)
sys_m = SgmamSystem{false,2}(H_x, H_p)

x_init_m = Matrix([xx yy]')


function KPO_SA(x, p, t)
u, v = x
return SA[fu(u, v), fv(u, v)]
Expand All @@ -72,16 +71,13 @@ using ModelingToolkit
D = Differential(t)
sts = @variables u(t) v(t)

eqs = [
D(u) ~ fu(u, v),
D(v) ~ fv(u, v)
]
eqs = [D(u) ~ fu(u, v), D(v) ~ fv(u, v)]
@named sys1 = System(eqs, t)
sys1 = structural_simplify(sys1)
prob = ODEProblem(sys1, sts .=> zeros(2), (0.0, 100.0), (); jac=true)
ds = CoupledODEs(prob)
jac = jacobian(ds)
jac([1,1], (), 0.0)
jac([1, 1], (), 0.0)

sgSys′ = SgmamSystem(ds);

Expand Down
2 changes: 1 addition & 1 deletion examples/KPO_sgMAM.jl
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ function H_p(x, p) # ℜ² → ℜ²
return Matrix([H_pu H_pv]')
end

sys = SgmamSystem{false, 2}(H_x, H_p)
sys = SgmamSystem{false,2}(H_x, H_p)

# setup
Nt = 500 # number of discrete time steps
Expand Down
12 changes: 5 additions & 7 deletions src/largedeviations/sgMAM.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ This system operates in an extended phase space where the Hamiltonian is assumed
quadratic in the extended momentum. The phase space coordinates `x` are doubled to
form a 2n-dimensional extended phase space.
"""
struct SgmamSystem{IIP,D,Hx, Hp}
struct SgmamSystem{IIP,D,Hx,Hp}
H_x::Hx
H_p::Hp

Expand All @@ -32,21 +32,19 @@ struct SgmamSystem{IIP,D,Hx, Hp}
end
return Hp
end
return new{isinplace(ds),dimension(ds), typeof(H_x), typeof(H_p)}(H_x, H_p)
return new{isinplace(ds),dimension(ds),typeof(H_x),typeof(H_p)}(H_x, H_p)
end
function SgmamSystem{IIP, D}(H_x::Function, H_p::Function) where {IIP, D}

new{IIP, D, typeof(H_x), typeof(H_p)}(H_x, H_p)
function SgmamSystem{IIP,D}(H_x::Function, H_p::Function) where {IIP,D}
return new{IIP,D,typeof(H_x),typeof(H_p)}(H_x, H_p)
end
end

function prettyprint(mlp::SgmamSystem{IIP, D}) where {IIP,D}
function prettyprint(mlp::SgmamSystem{IIP,D}) where {IIP,D}
return "Doubled $D-dimensional phase space containing $(IIP ? "in-place" : "out-of-place") functions"
end

Base.show(io::IO, mlp::SgmamSystem) = print(io, prettyprint(mlp))


"""
$(TYPEDSIGNATURES)
Expand Down
105 changes: 50 additions & 55 deletions test/largedeviations/sgMAM.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,59 +10,54 @@ using Test
η = 0
α = -1

function fu(u, v)
return (-4 * γ * ω * u - 2 * λ * v - 4 * (ω0 - ω^2) * v - 3 * α * v * (u^2 + v^2)) /
(8 * ω)
end
function fv(u, v)
return (-4 * γ * ω * v - 2 * λ * u + 4 * (ω0 - ω^2) * u + 3 * α * u * (u^2 + v^2)) /
(8 * ω)
end

dfvdv(u, v) = (-4 * γ * ω + 6 * α * u * v) / (8 * ω)
dfudu(u, v) = (-4 * γ * ω - 6 * α * u * v) / (8 * ω)
dfvdu(u, v) = (-2 * λ + 4 * (ω0 - ω^2) + 9 * α * u^2 + 3 * α * v^2) / (8 * ω)
dfudv(u, v) = (-2 * λ - 4 * (ω0 - ω^2) - 3 * α * u^2 - 9 * α * v^2) / (8 * ω)

function H_x(x, p) # ℜ² → ℜ²
u, v = eachrow(x)
pu, pv = eachrow(p)

H_u = @. pu * dfudu(u, v) + pv * dfvdu(u, v)
H_v = @. pu * dfudv(u, v) + pv * dfvdv(u, v)
return Matrix([H_u H_v]')
end
function H_p(x, p) # ℜ² → ℜ²
u, v = eachrow(x)
pu, pv = eachrow(p)

H_pu = @. pu + fu(u, v)
H_pv = @. pv + fv(u, v)
return Matrix([H_pu H_pv]')
end


@independent_variables t
D = Differential(t)
sts = @variables u(t) v(t)

eqs = [
D(u) ~ fu(u, v),
D(v) ~ fv(u, v)
]
@named sysMTK = System(eqs, t)
sysMTK = structural_simplify(sysMTK)
prob = ODEProblem(sysMTK, sts .=> zeros(2), (0.0, 100.0), (); jac=true)
ds = CoupledODEs(prob)

sys = SgmamSystem{false, 2}(H_x, H_p)
sys′ = SgmamSystem(ds);

Nt = 500 # number of discrete time steps
p_r = rand(2, Nt)
x_r = rand(2, Nt)

@test sys′.H_x(x_r, p_r) sys.H_x(x_r, p_r)
@test sys′.H_p(x_r, p_r) sys.H_p(x_r, p_r)

function fu(u, v)
return (-4 * γ * ω * u - 2 * λ * v - 4 * (ω0 - ω^2) * v - 3 * α * v * (u^2 + v^2)) /
(8 * ω)
end
function fv(u, v)
return (-4 * γ * ω * v - 2 * λ * u + 4 * (ω0 - ω^2) * u + 3 * α * u * (u^2 + v^2)) /
(8 * ω)
end

dfvdv(u, v) = (-4 * γ * ω + 6 * α * u * v) / (8 * ω)
dfudu(u, v) = (-4 * γ * ω - 6 * α * u * v) / (8 * ω)
dfvdu(u, v) = (-2 * λ + 4 * (ω0 - ω^2) + 9 * α * u^2 + 3 * α * v^2) / (8 * ω)
dfudv(u, v) = (-2 * λ - 4 * (ω0 - ω^2) - 3 * α * u^2 - 9 * α * v^2) / (8 * ω)

function H_x(x, p) # ℜ² → ℜ²
u, v = eachrow(x)
pu, pv = eachrow(p)

H_u = @. pu * dfudu(u, v) + pv * dfvdu(u, v)
H_v = @. pu * dfudv(u, v) + pv * dfvdv(u, v)
return Matrix([H_u H_v]')
end
function H_p(x, p) # ℜ² → ℜ²
u, v = eachrow(x)
pu, pv = eachrow(p)

H_pu = @. pu + fu(u, v)
H_pv = @. pv + fv(u, v)
return Matrix([H_pu H_pv]')
end

@independent_variables t
D = Differential(t)
sts = @variables u(t) v(t)

eqs = [D(u) ~ fu(u, v), D(v) ~ fv(u, v)]
@named sysMTK = System(eqs, t)
sysMTK = structural_simplify(sysMTK)
prob = ODEProblem(sysMTK, sts .=> zeros(2), (0.0, 100.0), (); jac=true)
ds = CoupledODEs(prob)

sys = SgmamSystem{false,2}(H_x, H_p)
sys′ = SgmamSystem(ds)

Nt = 500 # number of discrete time steps
p_r = rand(2, Nt)
x_r = rand(2, Nt)

@test sys′.H_x(x_r, p_r) sys.H_x(x_r, p_r)
@test sys′.H_p(x_r, p_r) sys.H_p(x_r, p_r)
end
4 changes: 2 additions & 2 deletions test/largedeviations/string_method.jl
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ yy = @. (xb[2] - xa[2]) * s + xa[2] + 4 * s * (1 - s) * xsaddle[2] + 0.01 * sin(
return StateSpaceSet([H_pu H_pv])
end

sys_sss = SgmamSystem{false, 2}(H_x, H_p)
sys_sss = SgmamSystem{false,2}(H_x, H_p)

x_init_sss = StateSpaceSet([xx yy])

Expand All @@ -75,7 +75,7 @@ yy = @. (xb[2] - xa[2]) * s + xa[2] + 4 * s * (1 - s) * xsaddle[2] + 0.01 * sin(
return Matrix([H_pu H_pv]')
end

sys_m = SgmamSystem{false, 2}(H_x, H_p)
sys_m = SgmamSystem{false,2}(H_x, H_p)

x_init_m = Matrix([xx yy]')

Expand Down

0 comments on commit 08360c1

Please sign in to comment.