Skip to content

Commit e6f5599

Browse files
author
Rahul Manavalan
committed
Changes from prev commit.
nmodes is now forced to be an integer. Silly transpose error fix Update relative energy for truncated SVD algos TODO: Modify type of POD, similar to the type of snapshots
1 parent d7e4593 commit e6f5599

2 files changed

Lines changed: 44 additions & 33 deletions

File tree

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

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)

0 commit comments

Comments
 (0)