Skip to content

Commit 11fcb3b

Browse files
Merge pull request #1 from dynamic-queries/Main
Main
2 parents 9dd802a + c2837a7 commit 11fcb3b

11 files changed

Lines changed: 47 additions & 44 deletions

File tree

Project.toml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,7 @@ version = "0.1.0"
55

66
[deps]
77
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
8-
MAT = "23992714-dd62-5051-b70f-ba57cb901cac"
9-
MultivariateStats = "6f286f6a-111f-5878-ab1e-185364afe411"
108
OrdinaryDiffEq = "1dea7af3-3e70-54e6-95c3-0bf5283fa5ed"
11-
RDatasets = "ce6b1742-4840-55fa-b093-852dadbb1d8b"
129
RandomizedLinAlg = "0448d7d9-159c-5637-8537-fd72090fea46"
1310
TSVD = "9449cd9e-2762-5aa3-a617-5413e99d722e"
1411

examples/POD/lorenz.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ function lorenz_prob()
1818
end
1919

2020
sol = lorenz_prob()
21-
solution = Matrix(reduce(hcat,sol.u)')
22-
plot(solution[:,1],solution[:,2],solution[:,3])
21+
solution = Array(sol)
22+
plot(solution[1,:],solution[2,:],solution[3,:])
2323
# savefig("lorenz_attractor.png")
2424

2525
## Two way POD

src/DataReduction/DiffusionMaps.jl

Whitespace-only changes.

src/DataReduction/POD.jl

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,65 @@
11
function matricize(VoV::Vector{Vector{FT}}) where {FT}
2-
Matrix(reduce(hcat,VoV)')
2+
Matrix(reduce(hcat,VoV))
33
end
44

5-
mutable struct POD{FT,IT} <: AbstractDRProblem
5+
mutable struct POD{FT} <: AbstractDRProblem
66
snapshots::Union{Vector{Vector{FT}},Matrix{FT}}
7-
nmodes::IT
7+
nmodes::Int
88
rbasis::Matrix{FT}
9-
energy::FT
9+
renergy::FT
1010

11-
function POD(snaps::Vector{Vector{FT}},nmodes::IT) where {FT,IT}
11+
function POD(snaps::Vector{Vector{FT}},nmodes::Int) where {FT}
1212
errorhandle(matricize(snaps),nmodes)
13-
new{eltype(snaps[1]),typeof(nmodes)}(snaps,nmodes,Array{FT,2}(undef,size(snaps,1),nmodes),FT(0))
13+
new{eltype(snaps[1])}(snaps,nmodes,Array{FT,2}(undef,size(snaps,1),nmodes),FT(0))
1414
end
1515

16-
function POD(snaps::Matrix{FT},nmodes::IT) where {FT,IT}
16+
function POD(snaps::Matrix{FT},nmodes::Int) where {FT}
1717
errorhandle(snaps,nmodes)
18-
new{eltype(snaps),typeof(nmodes)}(snaps,nmodes,Array{FT,2}(undef,size(snaps,1),nmodes),FT(0))
18+
new{eltype(snaps)}(snaps,nmodes,Array{FT,2}(undef,size(snaps,1),nmodes),FT(0))
1919
end
2020

21-
function POD(snaps::Adjoint{FT,Matrix{FT}},nmodes::IT) where {FT,IT}
21+
function POD(snaps::Adjoint{FT,Matrix{FT}},nmodes::Int) where {FT}
2222
POD(Matrix(snaps),nmodes,Array{FT,2}(undef,size(snaps,1),nmodes),FT(0))
2323
end
2424
end
2525

26-
function reduce!(pod::POD{FT,IT},::SVD) where {FT,IT}
26+
function reduce!(pod::POD{FT},::SVD) where {FT}
2727
op_matrix = pod.snapshots
2828
if typeof(pod.snapshots) == Vector{Vector{FT}}
2929
op_matrix = matricize(pod.snapshots)
3030
end
3131
u,s,v = svd(op_matrix)
3232
pod.rbasis .= u[:,1:pod.nmodes]
3333
sr = s[1:pod.nmodes]
34-
pod.energy = sum(sr)/sum(s)
34+
pod.renergy = sum(s)/(sum(s) + (size(op_matrix,1)-pod.nmodes)*s[end])
3535
nothing
3636
end
3737

38-
function reduce!(pod::POD{FT,IT},::TSVD) where {FT,IT}
38+
function reduce!(pod::POD{FT},::TSVD) where {FT}
3939
op_matrix = pod.snapshots
4040
if typeof(pod.snapshots) == Vector{Vector{FT}}
4141
op_matrix = matricize(pod.snapshots)
4242
end
4343
u,s,v = tsvd(op_matrix,pod.nmodes)
44-
pod.energy = NaN
44+
pod.renergy = sum(s)/(sum(s) + (size(op_matrix,1)-pod.nmodes)*s[end])
4545
pod.rbasis .= u
4646
nothing
4747
end
4848

49-
function reduce!(pod::POD{FT,IT},::RSVD) where {FT,IT}
49+
function reduce!(pod::POD{FT},::RSVD) where {FT}
5050
op_matrix = pod.snapshots
5151
if typeof(pod.snapshots) == Vector{Vector{FT}}
5252
op_matrix = matricize(pod.snapshots)
5353
end
5454
u,s,v = rsvd(op_matrix,pod.nmodes)
55-
pod.energy = NaN
55+
pod.renergy = sum(s)/(sum(s) + (size(op_matrix,1)-pod.nmodes)*s[end])
5656
pod.rbasis .= u
5757
nothing
5858
end
5959

6060
function Base.show(io::IO,pod::POD)
6161
print(io,"POD \n")
6262
print(io,"Reduction Order = ",pod.nmodes,"\n")
63-
print(io,"Snapshot size = (", length(pod.snapshots),",",length(pod.snapshots[1]),")\n")
64-
print(io,"Energy = ", pod.energy,"\n")
63+
print(io,"Snapshot size = (", size(pod.snapshots,1),",",size(pod.snapshots[1],2),")\n")
64+
print(io,"Relative Energy = ", pod.renergy,"\n")
6565
end

src/DataReduction/VAE.jl

Whitespace-only changes.

src/ErrorHandle.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
function errorhandle(data::Matrix{FT},modes::IT) where {FT,IT}
22
@assert size(data,1)>1 "State vector is expected to be vector valued."
33
s = size(data,2)
4-
@assert (modes>0)&(modes<s) "Number of modes should be [1,$(s)]."
4+
@assert (modes>0)&(modes<s) "Number of modes should be in {1,2,...,$(s-1)}."
55
end

src/ModelOrderReduction.jl

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,10 @@ module ModelOrderReduction
88
using RandomizedLinAlg
99

1010
include("DataReduction/POD.jl")
11-
include("DataReduction/DiffusionMaps.jl")
12-
include("DataReduction/VAE.jl")
1311

1412
export SVD, TSVD, RSVD
1513
export POD, reduce!, matricize
1614
#========================Model Reduction========================================#
17-
include("ModelReduction/LiftAndLearn.jl")
1815

1916
#===============================================================================#
2017
end

src/ModelReduction/LiftAndLearn.jl

Whitespace-only changes.

test/DataReduction.jl

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,43 @@
11
#--------- Data Reduction -----------------#
22

3-
@testset "POD - Attractor Test" begin
4-
function lorenz_prob()
5-
function lorenz!(du,u,p,t)
6-
du[1] = p[1]*(u[2]-u[1])
7-
du[2] = u[1]*(p[2]-u[3]) - u[2]
8-
du[3] = u[1]*u[2] - p[3]*u[3]
9-
end
10-
11-
u0 = [1,0,0]
12-
p = [10,28,8/3]
13-
tspan = (0,100)
14-
prob = ODEProblem(lorenz!,u0,tspan,p)
15-
sol = solve(prob,Tsit5())
16-
sol
3+
function lorenz_prob()
4+
function lorenz!(du,u,p,t)
5+
du[1] = p[1]*(u[2]-u[1])
6+
du[2] = u[1]*(p[2]-u[3]) - u[2]
7+
du[3] = u[1]*u[2] - p[3]*u[3]
178
end
189

10+
u0 = [1,0,0]
11+
p = [10,28,8/3]
12+
tspan = (0,100)
13+
prob = ODEProblem(lorenz!,u0,tspan,p)
14+
sol = solve(prob,Tsit5())
15+
sol
16+
end
17+
18+
@testset "POD-Utils" begin
19+
solution = lorenz_prob()
20+
VoV = solution.u
21+
M = matricize(VoV)
22+
@test size(M,1) == length(VoV[1]) # Parameters
23+
@test size(M,2) == length(VoV) # Time
24+
end
25+
26+
@testset "POD - Attractor Test" begin
1927
sol = lorenz_prob()
20-
solution = Matrix(reduce(hcat,sol.u)')
28+
solution = Array(sol)
2129

2230
order = 2
2331
solver = SVD()
2432
reducer = POD(solution,order)
2533
reduce!(reducer,solver)
2634

35+
reducer.renergy
36+
2737
# Ad-hoc tests. To be checked with Chris.
2838
@test size(reducer.rbasis,2) == reducer.nmodes
2939
@test size(reducer.rbasis,1) == size(solution,1)
30-
@test reducer.energy > 0.9
40+
@test reducer.renergy > 0.9
3141

3242
order = 2
3343
solver = TSVD()
@@ -43,6 +53,7 @@
4353
reducer = POD(solution,order)
4454
reduce!(reducer,solver)
4555

56+
4657
# Ad-hoc tests. To be checked with Chris.
4758
@test size(reducer.rbasis,2) == reducer.nmodes
4859
@test size(reducer.rbasis,1) == size(solution,1)

test/ModelReduction.jl

Whitespace-only changes.

0 commit comments

Comments
 (0)