Skip to content

Commit 3323595

Browse files
author
Rahul Manavalan
committed
Tests for SVD solvers interface
Ad-hoc tests are now implemented with the Lorenz attractor. Regression tests with MultivariateStats.jl coming up. Code Formatting to be done. Include a Benchmark from the data in #2.
1 parent 79b0ac1 commit 3323595

7 files changed

Lines changed: 73 additions & 75 deletions

File tree

Project.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
name = "ModelOrderReduction"
22
uuid = "207801d6-6cee-43a9-ad0c-f0c64933efa0"
3-
authors = ["Bowen S. Zhu","Rahul Manavalan"]
3+
authors = ["Bowen S. Zhu", "Rahul Manavalan"]
44
version = "0.1.0"
55

66
[deps]
77
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
88
OrdinaryDiffEq = "1dea7af3-3e70-54e6-95c3-0bf5283fa5ed"
9+
RandomizedLinAlg = "0448d7d9-159c-5637-8537-fd72090fea46"
910
TSVD = "9449cd9e-2762-5aa3-a617-5413e99d722e"
1011

1112
[compat]

src/DataReduction/PCA.jl

Lines changed: 0 additions & 41 deletions
This file was deleted.

src/DataReduction/POD.jl

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,29 @@ function reduce!(pod::POD{FT,IT},::SVD) where {FT,IT}
3131
sr = s[1:pod.nmodes]
3232
pod.energy = sum(sr)/sum(s)
3333
pod.rbasis = [vr[:,i] for i=1:pod.nmodes]
34+
nothing
3435
end
3536

3637
function reduce!(pod::POD{FT,IT},::TSVD) where {FT,IT}
37-
38+
op_matrix = pod.snapshots
39+
if typeof(pod.snapshots) == Vector{Vector{FT}}
40+
op_matrix = matricize(pod.snapshots)
41+
end
42+
u,s,v = tsvd(op_matrix,pod.nmodes)
43+
pod.energy = NaN
44+
pod.rbasis = [v[:,i] for i=1:pod.nmodes]
45+
nothing
3846
end
3947

4048
function reduce!(pod::POD{FT,IT},::RSVD) where {FT,IT}
41-
49+
op_matrix = pod.snapshots
50+
if typeof(pod.snapshots) == Vector{Vector{FT}}
51+
op_matrix = matricize(pod.snapshots)
52+
end
53+
u,s,v = rsvd(op_matrix,pod.nmodes)
54+
pod.energy = NaN
55+
pod.rbasis = [v[:,i] for i=1:pod.nmodes]
56+
nothing
4257
end
4358

4459
function Base.show(io::IO,pod::POD)

src/ModelOrderReduction.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@ module ModelOrderReduction
22
#========================Data Reduction=========================================#
33
include("Types.jl")
44
include("ErrorHandle.jl")
5+
56
using LinearAlgebra
7+
using TSVD
8+
using RandomizedLinAlg
9+
610
include("DataReduction/POD.jl")
711
include("DataReduction/DifussionMaps.jl")
812
include("DataReduction/VAE.jl")

test/DataReduction.jl

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#--------- Data Reduction -----------------#
2+
@testset "POD" begin
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]
8+
end
9+
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+
sol = lorenz_prob()
19+
solution = Matrix(reduce(hcat,sol.u)')
20+
21+
order = 2
22+
solver = SVD()
23+
reducer = POD(solution,order)
24+
reduce!(reducer,solver)
25+
26+
# Ad-hoc tests. To be checked with Chris.
27+
@test length(reducer.rbasis) == reducer.nmodes
28+
@test length(reducer.rbasis[1]) == size(solution,2)
29+
@test reducer.energy > 0.9
30+
31+
order = 2
32+
solver = TSVD()
33+
reducer = POD(solution,order)
34+
reduce!(reducer,solver)
35+
36+
# Ad-hoc tests. To be checked with Chris.
37+
@test length(reducer.rbasis) == reducer.nmodes
38+
@test length(reducer.rbasis[1]) == size(solution,2)
39+
40+
order = 2
41+
solver = RSVD()
42+
reducer = POD(solution,order)
43+
reduce!(reducer,solver)
44+
45+
# Ad-hoc tests. To be checked with Chris.
46+
@test length(reducer.rbasis) == reducer.nmodes
47+
@test length(reducer.rbasis[1]) == size(solution,2)
48+
end

test/ModelReduction.jl

Whitespace-only changes.

test/runtests.jl

Lines changed: 2 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2,36 +2,7 @@ using ModelOrderReduction
22
using Test
33
using OrdinaryDiffEq
44

5-
#--------- Data Reduction -----------------#
6-
@testset "POD" begin
7-
function lorenz_prob()
8-
function lorenz!(du,u,p,t)
9-
du[1] = p[1]*(u[2]-u[1])
10-
du[2] = u[1]*(p[2]-u[3]) - u[2]
11-
du[3] = u[1]*u[2] - p[3]*u[3]
12-
end
13-
14-
u0 = [1,0,0]
15-
p = [10,28,8/3]
16-
tspan = (0,100)
17-
prob = ODEProblem(lorenz!,u0,tspan,p)
18-
sol = solve(prob,Tsit5())
19-
sol
20-
end
21-
22-
sol = lorenz_prob()
23-
solution = Matrix(reduce(hcat,sol.u)')
24-
25-
order = 2
26-
solver = SVD()
27-
reducer = POD(solution,order)
28-
reduce!(reducer,solver)
29-
30-
# Ad-hoc tests. To be checked with Chris.
31-
@test length(reducer.rbasis) == reducer.nmodes
32-
@test length(reducer.rbasis[1]) == size(solution,2)
33-
@test reducer.energy > 0.9
34-
35-
end
5+
include("DataReduction.jl")
6+
include("ModelReduction.jl")
367

378
#---------- Model Reduction ----------------#

0 commit comments

Comments
 (0)