Skip to content

Commit 857d93a

Browse files
Format at last
1 parent 11fcb3b commit 857d93a

6 files changed

Lines changed: 82 additions & 81 deletions

File tree

examples/POD/lorenz.jl

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,38 +3,37 @@ using Plots
33
using OrdinaryDiffEq
44

55
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]
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]
1010
end
1111

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())
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())
1717
sol
1818
end
1919

2020
sol = lorenz_prob()
2121
solution = Array(sol)
22-
plot(solution[1,:],solution[2,:],solution[3,:])
22+
plot(solution[1, :], solution[2, :], solution[3, :])
2323
# savefig("lorenz_attractor.png")
2424

2525
## Two way POD
26-
reducer = POD(solution,2)
27-
reduce!(reducer,SVD())
26+
reducer = POD(solution, 2)
27+
reduce!(reducer, SVD())
2828
bases = reducer.rbasis
29-
plot(bases[:,1],bases[:,2],label="POD2")
29+
plot(bases[:, 1], bases[:, 2], label = "POD2")
3030
# savefig("pod2.png")
3131

32-
3332
## One way POD
34-
reducer = POD(solution,1)
35-
reduce!(reducer,SVD())
33+
reducer = POD(solution, 1)
34+
reduce!(reducer, SVD())
3635
bases = reducer.rbasis
37-
plot(bases[:,1],label="POD1")
36+
plot(bases[:, 1], label = "POD1")
3837
# savefig("pod1.png")
39-
plot(solution[:,3],label='z')
38+
plot(solution[:, 3], label = 'z')
4039
# savefig("z.png")

src/DataReduction/POD.jl

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

55
mutable struct POD{FT} <: AbstractDRProblem
6-
snapshots::Union{Vector{Vector{FT}},Matrix{FT}}
6+
snapshots::Union{Vector{Vector{FT}}, Matrix{FT}}
77
nmodes::Int
88
rbasis::Matrix{FT}
99
renergy::FT
1010

11-
function POD(snaps::Vector{Vector{FT}},nmodes::Int) where {FT}
12-
errorhandle(matricize(snaps),nmodes)
13-
new{eltype(snaps[1])}(snaps,nmodes,Array{FT,2}(undef,size(snaps,1),nmodes),FT(0))
11+
function POD(snaps::Vector{Vector{FT}}, nmodes::Int) where {FT}
12+
errorhandle(matricize(snaps), nmodes)
13+
new{eltype(snaps[1])}(snaps, nmodes, Array{FT, 2}(undef, size(snaps, 1), nmodes),
14+
FT(0))
1415
end
1516

16-
function POD(snaps::Matrix{FT},nmodes::Int) where {FT}
17-
errorhandle(snaps,nmodes)
18-
new{eltype(snaps)}(snaps,nmodes,Array{FT,2}(undef,size(snaps,1),nmodes),FT(0))
17+
function POD(snaps::Matrix{FT}, nmodes::Int) where {FT}
18+
errorhandle(snaps, nmodes)
19+
new{eltype(snaps)}(snaps, nmodes, Array{FT, 2}(undef, size(snaps, 1), nmodes),
20+
FT(0))
1921
end
2022

21-
function POD(snaps::Adjoint{FT,Matrix{FT}},nmodes::Int) where {FT}
22-
POD(Matrix(snaps),nmodes,Array{FT,2}(undef,size(snaps,1),nmodes),FT(0))
23+
function POD(snaps::Adjoint{FT, Matrix{FT}}, nmodes::Int) where {FT}
24+
POD(Matrix(snaps), nmodes, Array{FT, 2}(undef, size(snaps, 1), nmodes), FT(0))
2325
end
2426
end
2527

26-
function reduce!(pod::POD{FT},::SVD) where {FT}
28+
function reduce!(pod::POD{FT}, ::SVD) where {FT}
2729
op_matrix = pod.snapshots
2830
if typeof(pod.snapshots) == Vector{Vector{FT}}
2931
op_matrix = matricize(pod.snapshots)
3032
end
31-
u,s,v = svd(op_matrix)
32-
pod.rbasis .= u[:,1:pod.nmodes]
33-
sr = s[1:pod.nmodes]
34-
pod.renergy = sum(s)/(sum(s) + (size(op_matrix,1)-pod.nmodes)*s[end])
33+
u, s, v = svd(op_matrix)
34+
pod.rbasis .= u[:, 1:(pod.nmodes)]
35+
sr = s[1:(pod.nmodes)]
36+
pod.renergy = sum(s) / (sum(s) + (size(op_matrix, 1) - pod.nmodes) * s[end])
3537
nothing
3638
end
3739

38-
function reduce!(pod::POD{FT},::TSVD) where {FT}
40+
function reduce!(pod::POD{FT}, ::TSVD) where {FT}
3941
op_matrix = pod.snapshots
4042
if typeof(pod.snapshots) == Vector{Vector{FT}}
4143
op_matrix = matricize(pod.snapshots)
4244
end
43-
u,s,v = tsvd(op_matrix,pod.nmodes)
44-
pod.renergy = sum(s)/(sum(s) + (size(op_matrix,1)-pod.nmodes)*s[end])
45+
u, s, v = tsvd(op_matrix, pod.nmodes)
46+
pod.renergy = sum(s) / (sum(s) + (size(op_matrix, 1) - pod.nmodes) * s[end])
4547
pod.rbasis .= u
4648
nothing
4749
end
4850

49-
function reduce!(pod::POD{FT},::RSVD) where {FT}
51+
function reduce!(pod::POD{FT}, ::RSVD) where {FT}
5052
op_matrix = pod.snapshots
5153
if typeof(pod.snapshots) == Vector{Vector{FT}}
5254
op_matrix = matricize(pod.snapshots)
5355
end
54-
u,s,v = rsvd(op_matrix,pod.nmodes)
55-
pod.renergy = sum(s)/(sum(s) + (size(op_matrix,1)-pod.nmodes)*s[end])
56+
u, s, v = rsvd(op_matrix, pod.nmodes)
57+
pod.renergy = sum(s) / (sum(s) + (size(op_matrix, 1) - pod.nmodes) * s[end])
5658
pod.rbasis .= u
5759
nothing
5860
end
5961

60-
function Base.show(io::IO,pod::POD)
61-
print(io,"POD \n")
62-
print(io,"Reduction Order = ",pod.nmodes,"\n")
63-
print(io,"Snapshot size = (", size(pod.snapshots,1),",",size(pod.snapshots[1],2),")\n")
64-
print(io,"Relative Energy = ", pod.renergy,"\n")
62+
function Base.show(io::IO, pod::POD)
63+
print(io, "POD \n")
64+
print(io, "Reduction Order = ", pod.nmodes, "\n")
65+
print(io, "Snapshot size = (", size(pod.snapshots, 1), ",", size(pod.snapshots[1], 2),
66+
")\n")
67+
print(io, "Relative Energy = ", pod.renergy, "\n")
6568
end

src/ErrorHandle.jl

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

src/ModelOrderReduction.jl

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
module ModelOrderReduction
22
#========================Data Reduction=========================================#
3-
include("Types.jl")
4-
include("ErrorHandle.jl")
3+
include("Types.jl")
4+
include("ErrorHandle.jl")
55

6-
using LinearAlgebra
7-
using TSVD
8-
using RandomizedLinAlg
6+
using LinearAlgebra
7+
using TSVD
8+
using RandomizedLinAlg
99

10-
include("DataReduction/POD.jl")
10+
include("DataReduction/POD.jl")
1111

12-
export SVD, TSVD, RSVD
13-
export POD, reduce!, matricize
12+
export SVD, TSVD, RSVD
13+
export POD, reduce!, matricize
1414
#========================Model Reduction========================================#
1515

1616
#===============================================================================#

src/Types.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
abstract type AbstractReductionProblem end
1+
abstract type AbstractReductionProblem end
22
abstract type AbstractMORProblem <: AbstractReductionProblem end
33
abstract type AbstractDRProblem <: AbstractReductionProblem end
44

test/DataReduction.jl

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,26 @@
11
#--------- Data Reduction -----------------#
22

33
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]
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]
88
end
99

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())
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())
1515
sol
1616
end
1717

1818
@testset "POD-Utils" begin
1919
solution = lorenz_prob()
2020
VoV = solution.u
2121
M = matricize(VoV)
22-
@test size(M,1) == length(VoV[1]) # Parameters
23-
@test size(M,2) == length(VoV) # Time
22+
@test size(M, 1) == length(VoV[1]) # Parameters
23+
@test size(M, 2) == length(VoV) # Time
2424
end
2525

2626
@testset "POD - Attractor Test" begin
@@ -29,32 +29,31 @@ end
2929

3030
order = 2
3131
solver = SVD()
32-
reducer = POD(solution,order)
33-
reduce!(reducer,solver)
32+
reducer = POD(solution, order)
33+
reduce!(reducer, solver)
3434

3535
reducer.renergy
3636

3737
# Ad-hoc tests. To be checked with Chris.
38-
@test size(reducer.rbasis,2) == reducer.nmodes
39-
@test size(reducer.rbasis,1) == size(solution,1)
38+
@test size(reducer.rbasis, 2) == reducer.nmodes
39+
@test size(reducer.rbasis, 1) == size(solution, 1)
4040
@test reducer.renergy > 0.9
4141

4242
order = 2
4343
solver = TSVD()
44-
reducer = POD(solution,order)
45-
reduce!(reducer,solver)
44+
reducer = POD(solution, order)
45+
reduce!(reducer, solver)
4646

4747
# Ad-hoc tests. To be checked with Chris.
48-
@test size(reducer.rbasis,2) == reducer.nmodes
49-
@test size(reducer.rbasis,1) == size(solution,1)
48+
@test size(reducer.rbasis, 2) == reducer.nmodes
49+
@test size(reducer.rbasis, 1) == size(solution, 1)
5050

5151
order = 2
5252
solver = RSVD()
53-
reducer = POD(solution,order)
54-
reduce!(reducer,solver)
55-
53+
reducer = POD(solution, order)
54+
reduce!(reducer, solver)
5655

5756
# Ad-hoc tests. To be checked with Chris.
58-
@test size(reducer.rbasis,2) == reducer.nmodes
59-
@test size(reducer.rbasis,1) == size(solution,1)
57+
@test size(reducer.rbasis, 2) == reducer.nmodes
58+
@test size(reducer.rbasis, 1) == size(solution, 1)
6059
end

0 commit comments

Comments
 (0)