Skip to content

Commit

Permalink
Added assertions for dt and changed type to Real. Provide some assert…
Browse files Browse the repository at this point in the history
…ion messages.
  • Loading branch information
AlCap23 committed Nov 28, 2019
1 parent b7fc345 commit 2fd4410
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 10 deletions.
12 changes: 7 additions & 5 deletions src/exact_dmd.jl
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,14 @@ mutable struct ExactDMD{M,L,W,F, Q, P} <: abstractKoopmanOperator

end

function ExactDMD(X::AbstractArray; dt::Float64 = 0.0)
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; dt::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)
Expand Down Expand Up @@ -75,7 +76,8 @@ end


# Update with new measurements
function update!(m::ExactDMD, x::AbstractArray, y::AbstractArray; dt::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 Down
11 changes: 6 additions & 5 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), dt::Float64 = 1.0)
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), dt::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,7 +44,7 @@ function ExtendedDMD(X::AbstractArray, Y::AbstractArray, Ψ::abstractBasis; p::A
return ExtendedDMD(Op, B, Ψ)
end

function update!(m::ExtendedDMD, x::AbstractArray, y::AbstractArray; p::AbstractArray = [], dt::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, Ψ₀, Ψ₁, dt = dt, threshold = threshold)
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

0 comments on commit 2fd4410

Please sign in to comment.