Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Δt to dt #16

Merged
merged 2 commits into from
Nov 28, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/DMD_Examples.jl
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ sol_cont = solve(prob_cont, saveat = 0.1)

plot(sol_cont)

approx_cont = ExactDMD(sol_cont[:,:], Δt = 0.1)
approx_cont = ExactDMD(sol_cont[:,:], dt = 0.1)

test = dynamics(approx_cont, discrete = false)

Expand Down
22 changes: 12 additions & 10 deletions src/exact_dmd.jl
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,23 @@ mutable struct ExactDMD{M,L,W,F, Q, P} <: abstractKoopmanOperator

end

function ExactDMD(X::AbstractArray; Δt::Float64 = 0.0)
return ExactDMD(X[:, 1:end-1], X[:, 2:end], Δt = Δt)
function ExactDMD(X::AbstractArray; dt::T = 0.0) where T <: Real
return ExactDMD(X[:, 1:end-1], X[:, 2:end], dt = dt)
end

function ExactDMD(X::AbstractArray, Y::AbstractArray; Δt::Float64 = 0.0)
@assert size(X)[2] .== size(Y)[2]
@assert size(Y)[1] .<= size(Y)[2]
function ExactDMD(X::AbstractArray, Y::AbstractArray; dt::T = 0.0) where T <: Real
@assert dt >= zero(typeof(dt)) "Provide positive dt"
@assert size(X)[2] .== size(Y)[2] "Provide consistent dimensions for data"
@assert size(Y)[1] .<= size(Y)[2] "Provide consistent dimensions for data"

# Best Frob norm approximator
à = Y*pinv(X)
# Eigen Decomposition for solution
Λ, W = eigen(Ã)

if Δt > 0.0
if dt > 0.0
# Casting Complex enforces results
ω = log.(Complex.(Λ)) / Δt
ω = log.(Complex.(Λ)) / dt
else
ω = []
end
Expand Down Expand Up @@ -75,7 +76,8 @@ end


# Update with new measurements
function update!(m::ExactDMD, x::AbstractArray, y::AbstractArray; Δt::Float64 = 0.0, threshold::Float64 = 1e-3)
function update!(m::ExactDMD, x::AbstractArray, y::AbstractArray; dt::T = 0.0, threshold::Float64 = 1e-3) where T <: Real
@assert dt >= zero(typeof(dt)) "Provide positive dt"
# Check the error
ϵ = norm(y - m.Ã*x, 2)

Expand All @@ -88,9 +90,9 @@ function update!(m::ExactDMD, x::AbstractArray, y::AbstractArray; Δt::Float64 =
m.Ã = m.Qₖ*inv(m.Pₖ)
m.λ, m.ϕ = eigen(m.Ã)

if Δt > 0.0
if dt > 0.0
# Casting Complex enforces results
ω = log.(Complex.(m.λ)) / Δt
ω = log.(Complex.(m.λ)) / dt
else
ω = []
end
Expand Down
15 changes: 8 additions & 7 deletions src/extended_dmd.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,14 @@ eigen(m::ExtendedDMD) = eigen(m.koopman)
eigvals(m::ExtendedDMD) = eigvals(m.koopman)
eigvecs(m::ExtendedDMD) = eigvecs(m.koopman)

function ExtendedDMD(X::AbstractArray, Ψ::abstractBasis; p::AbstractArray = [], B::AbstractArray = reshape([], 0,0), Δt::Float64 = 1.0)
return ExtendedDMD(X[:, 1:end-1], X[:, 2:end], Ψ, p = p, B = B, Δt = Δt)
function ExtendedDMD(X::AbstractArray, Ψ::abstractBasis; p::AbstractArray = [], B::AbstractArray = reshape([], 0,0), dt::T = 1.0) where T <: Real
return ExtendedDMD(X[:, 1:end-1], X[:, 2:end], Ψ, p = p, B = B, dt = dt)
end

function ExtendedDMD(X::AbstractArray, Y::AbstractArray, Ψ::abstractBasis; p::AbstractArray = [], B::AbstractArray = reshape([], 0,0), Δt::Float64 = 1.0)
@assert size(X)[2] .== size(Y)[2]
@assert size(Y)[1] .<= size(Y)[2]
function ExtendedDMD(X::AbstractArray, Y::AbstractArray, Ψ::abstractBasis; p::AbstractArray = [], B::AbstractArray = reshape([], 0,0), dt::T = 1.0) where T <: Real
@assert dt >= zero(typeof(dt)) "Provide positive dt"
@assert size(X)[2] .== size(Y)[2] "Provide consistent dimensions for data"
@assert size(Y)[1] .<= size(Y)[2] "Provide consistent dimensions for data"

# Based upon William et.al. , A Data-Driven Approximation of the Koopman operator

Expand All @@ -43,10 +44,10 @@ function ExtendedDMD(X::AbstractArray, Y::AbstractArray, Ψ::abstractBasis; p::A
return ExtendedDMD(Op, B, Ψ)
end

function update!(m::ExtendedDMD, x::AbstractArray, y::AbstractArray; p::AbstractArray = [], Δt::Float64 = 0.0, threshold::Float64 = 1e-3)
function update!(m::ExtendedDMD, x::AbstractArray, y::AbstractArray; p::AbstractArray = [], dt::T = 0.0, threshold::Float64 = 1e-3) where T <: Real
Ψ₀ = m.basis(x, p = p)
Ψ₁ = m.basis(y, p = p)
update!(m.koopman, Ψ₀, Ψ₁, Δt = Δt, threshold = threshold)
update!(m.koopman, Ψ₀, Ψ₁, dt = dt, threshold = threshold)
return
end

Expand Down
3 changes: 3 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ end
push!(y, A*y[end])
end
X = hcat(y...)
@test_throws AssertionError ExactDMD(X[:, 1:end-2], dt = -1.0)
estimator = ExactDMD(X[:,1:end-2])
@test isstable(estimator)
@test estimator.Ã ≈ A
Expand All @@ -48,6 +49,7 @@ end


@testset "EDMD" begin

# Test for linear system
function linear_sys(u, p, t)
x = -0.9*u[1]
Expand All @@ -64,6 +66,7 @@ end
h = [1u[1]; 1u[2]; sin(u[1]); cos(u[1]); u[1]*u[2]]
basis = Basis(h, u)

@test_throws AssertionError ExtendedDMD(sol[:,:], basis, dt = -1.0)
estimator = ExtendedDMD(sol[:,:], basis)
@test basis == estimator.basis
basis_2 = reduce_basis(estimator, threshold = 1e-5)
Expand Down