Skip to content

Commit 9dd802a

Browse files
author
Rahul Manavalan
committed
Reduced Bases VoV->Matrix
Considering that we need the reduced bases in Matrix form down the line, for POD based MOR methods, a change in the DS is made.
1 parent 39b4cbb commit 9dd802a

6 files changed

Lines changed: 64 additions & 18 deletions

File tree

Project.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@ 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"
810
OrdinaryDiffEq = "1dea7af3-3e70-54e6-95c3-0bf5283fa5ed"
11+
RDatasets = "ce6b1742-4840-55fa-b093-852dadbb1d8b"
912
RandomizedLinAlg = "0448d7d9-159c-5637-8537-fd72090fea46"
1013
TSVD = "9449cd9e-2762-5aa3-a617-5413e99d722e"
1114

examples/POD/lorenz.jl

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
using ModelOrderReduction
2+
using Plots
3+
using OrdinaryDiffEq
4+
5+
function lorenz_prob()
6+
function lorenz!(du,u,p,t)
7+
du[1] = p[1]*(u[2]-u[1])
8+
du[2] = u[1]*(p[2]-u[3]) - u[2]
9+
du[3] = u[1]*u[2] - p[3]*u[3]
10+
end
11+
12+
u0 = [1,0,0]
13+
p = [10,28,8/3]
14+
tspan = (0,100)
15+
prob = ODEProblem(lorenz!,u0,tspan,p)
16+
sol = solve(prob,Tsit5())
17+
sol
18+
end
19+
20+
sol = lorenz_prob()
21+
solution = Matrix(reduce(hcat,sol.u)')
22+
plot(solution[:,1],solution[:,2],solution[:,3])
23+
# savefig("lorenz_attractor.png")
24+
25+
## Two way POD
26+
reducer = POD(solution,2)
27+
reduce!(reducer,SVD())
28+
bases = reducer.rbasis
29+
plot(bases[:,1],bases[:,2],label="POD2")
30+
# savefig("pod2.png")
31+
32+
33+
## One way POD
34+
reducer = POD(solution,1)
35+
reduce!(reducer,SVD())
36+
bases = reducer.rbasis
37+
plot(bases[:,1],label="POD1")
38+
# savefig("pod1.png")
39+
plot(solution[:,3],label='z')
40+
# savefig("z.png")

src/DataReduction/POD.jl

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,25 @@
1-
# Leverage PCA from MultivariateStats.jl ?
2-
31
function matricize(VoV::Vector{Vector{FT}}) where {FT}
42
Matrix(reduce(hcat,VoV)')
53
end
64

75
mutable struct POD{FT,IT} <: AbstractDRProblem
86
snapshots::Union{Vector{Vector{FT}},Matrix{FT}}
97
nmodes::IT
10-
rbasis::Vector{Vector{FT}}
8+
rbasis::Matrix{FT}
119
energy::FT
1210

1311
function POD(snaps::Vector{Vector{FT}},nmodes::IT) where {FT,IT}
1412
errorhandle(matricize(snaps),nmodes)
15-
new{eltype(snaps[1]),typeof(nmodes)}(snaps,nmodes)
13+
new{eltype(snaps[1]),typeof(nmodes)}(snaps,nmodes,Array{FT,2}(undef,size(snaps,1),nmodes),FT(0))
1614
end
1715

1816
function POD(snaps::Matrix{FT},nmodes::IT) where {FT,IT}
1917
errorhandle(snaps,nmodes)
20-
new{eltype(snaps),typeof(nmodes)}(snaps,nmodes)
18+
new{eltype(snaps),typeof(nmodes)}(snaps,nmodes,Array{FT,2}(undef,size(snaps,1),nmodes),FT(0))
19+
end
20+
21+
function POD(snaps::Adjoint{FT,Matrix{FT}},nmodes::IT) where {FT,IT}
22+
POD(Matrix(snaps),nmodes,Array{FT,2}(undef,size(snaps,1),nmodes),FT(0))
2123
end
2224
end
2325

@@ -27,10 +29,9 @@ function reduce!(pod::POD{FT,IT},::SVD) where {FT,IT}
2729
op_matrix = matricize(pod.snapshots)
2830
end
2931
u,s,v = svd(op_matrix)
30-
vr = v[:,1:pod.nmodes]
32+
pod.rbasis .= u[:,1:pod.nmodes]
3133
sr = s[1:pod.nmodes]
3234
pod.energy = sum(sr)/sum(s)
33-
pod.rbasis = [vr[:,i] for i=1:pod.nmodes]
3435
nothing
3536
end
3637

@@ -41,7 +42,7 @@ function reduce!(pod::POD{FT,IT},::TSVD) where {FT,IT}
4142
end
4243
u,s,v = tsvd(op_matrix,pod.nmodes)
4344
pod.energy = NaN
44-
pod.rbasis = [v[:,i] for i=1:pod.nmodes]
45+
pod.rbasis .= u
4546
nothing
4647
end
4748

@@ -52,7 +53,7 @@ function reduce!(pod::POD{FT,IT},::RSVD) where {FT,IT}
5253
end
5354
u,s,v = rsvd(op_matrix,pod.nmodes)
5455
pod.energy = NaN
55-
pod.rbasis = [v[:,i] for i=1:pod.nmodes]
56+
pod.rbasis .= u
5657
nothing
5758
end
5859

src/ModelOrderReduction.jl

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,19 @@ module ModelOrderReduction
22
#========================Data Reduction=========================================#
33
include("Types.jl")
44
include("ErrorHandle.jl")
5-
5+
66
using LinearAlgebra
77
using TSVD
88
using RandomizedLinAlg
99

1010
include("DataReduction/POD.jl")
11-
include("DataReduction/DifussionMaps.jl")
11+
include("DataReduction/DiffusionMaps.jl")
1212
include("DataReduction/VAE.jl")
1313

1414
export SVD, TSVD, RSVD
1515
export POD, reduce!, matricize
1616
#========================Model Reduction========================================#
17+
include("ModelReduction/LiftAndLearn.jl")
1718

1819
#===============================================================================#
1920
end

test/DataReduction.jl

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#--------- Data Reduction -----------------#
2-
@testset "POD" begin
2+
3+
@testset "POD - Attractor Test" begin
34
function lorenz_prob()
45
function lorenz!(du,u,p,t)
56
du[1] = p[1]*(u[2]-u[1])
@@ -24,8 +25,8 @@
2425
reduce!(reducer,solver)
2526

2627
# Ad-hoc tests. To be checked with Chris.
27-
@test length(reducer.rbasis) == reducer.nmodes
28-
@test length(reducer.rbasis[1]) == size(solution,2)
28+
@test size(reducer.rbasis,2) == reducer.nmodes
29+
@test size(reducer.rbasis,1) == size(solution,1)
2930
@test reducer.energy > 0.9
3031

3132
order = 2
@@ -34,15 +35,15 @@
3435
reduce!(reducer,solver)
3536

3637
# Ad-hoc tests. To be checked with Chris.
37-
@test length(reducer.rbasis) == reducer.nmodes
38-
@test length(reducer.rbasis[1]) == size(solution,2)
38+
@test size(reducer.rbasis,2) == reducer.nmodes
39+
@test size(reducer.rbasis,1) == size(solution,1)
3940

4041
order = 2
4142
solver = RSVD()
4243
reducer = POD(solution,order)
4344
reduce!(reducer,solver)
4445

4546
# Ad-hoc tests. To be checked with Chris.
46-
@test length(reducer.rbasis) == reducer.nmodes
47-
@test length(reducer.rbasis[1]) == size(solution,2)
47+
@test size(reducer.rbasis,2) == reducer.nmodes
48+
@test size(reducer.rbasis,1) == size(solution,1)
4849
end

0 commit comments

Comments
 (0)