11using TSVD: tsvd
22using RandomizedLinAlg: rsvd
33
4- function matricize(VoV:: Vector{Vector{T}} ) where {T}
4+ function matricize(VoV:: Vector{Vector{T}} ):: Matrix{T} where {T}
55 return reduce(hcat, VoV)
66end
77
2626
2727_rsvd(data, n:: Int , p:: Int ) = rsvd(data, n, p)
2828
29- mutable struct POD <: AbstractDRProblem
29+ mutable struct POD{S, T <: AbstractFloat } <: AbstractDRProblem
3030 # specified
31- snapshots:: Any
32- min_renergy:: Any
31+ snapshots:: S
32+ min_renergy:: T
3333 min_nmodes:: Int
3434 max_nmodes:: Int
3535 # computed
3636 nmodes:: Int
37- rbasis:: Any
38- renergy:: Any
39- spectrum:: Any
37+ rbasis:: Union{Missing, Matrix{T}}
38+ renergy:: T
39+ spectrum:: Union{Missing, Vector{T}}
4040 # constructors
4141 function POD(
42- snaps;
43- min_renergy = 1.0 ,
42+ snaps:: S ;
43+ min_renergy:: T = 1.0 ,
4444 min_nmodes:: Int = 1 ,
4545 max_nmodes:: Int = length(snaps[1 ])
46- )
46+ ) where {S <: AbstractMatrix{T} } where {T <: AbstractFloat }
4747 nmodes = min_nmodes
4848 errorhandle(snaps, nmodes, min_renergy, min_nmodes, max_nmodes)
49- return new(snaps, min_renergy, min_nmodes, max_nmodes, nmodes, missing , 1.0 , missing )
49+ return new{S, T} (snaps, min_renergy, min_nmodes, max_nmodes, nmodes, missing , one(T) , missing )
5050 end
51- function POD(snaps, nmodes:: Int )
52- errorhandle(snaps, nmodes, 0.0 , nmodes, nmodes)
53- return new(snaps, 0.0 , nmodes, nmodes, nmodes, missing , 1.0 , missing )
51+ function POD(
52+ snaps:: S ;
53+ min_renergy:: T = 1.0 ,
54+ min_nmodes:: Int = 1 ,
55+ max_nmodes:: Int = length(snaps[1 ])
56+ ) where {T <: AbstractFloat , S <: AbstractVector{<:AbstractVector{T}} }
57+ nmodes = min_nmodes
58+ errorhandle(snaps, nmodes, min_renergy, min_nmodes, max_nmodes)
59+ return new{S, T}(snaps, min_renergy, min_nmodes, max_nmodes, nmodes, missing , one(T), missing )
60+ end
61+ function POD(snaps:: S , nmodes:: Int ) where {S <: AbstractMatrix{T} } where {T <: AbstractFloat }
62+ errorhandle(snaps, nmodes, zero(T), nmodes, nmodes)
63+ return new{S, T}(snaps, zero(T), nmodes, nmodes, nmodes, missing , one(T), missing )
64+ end
65+ function POD(snaps:: S , nmodes:: Int ) where {T <: AbstractFloat , S <: AbstractVector{<:AbstractVector{T}} }
66+ errorhandle(snaps, nmodes, zero(T), nmodes, nmodes)
67+ return new{S, T}(snaps, zero(T), nmodes, nmodes, nmodes, missing , one(T), missing )
5468 end
5569end
5670
57- function determine_truncation(s, min_nmodes, min_renergy, max_nmodes)
71+ function determine_truncation(
72+ s:: AbstractVector{T} , min_nmodes:: Int , max_nmodes:: Int , min_renergy:: T
73+ ):: Tuple{Int, T} where {T <: AbstractFloat }
5874 nmodes = min_nmodes
5975 overall_energy = sum(s)
6076 energy = sum(s[1 : nmodes]) / overall_energy
@@ -65,42 +81,43 @@ function determine_truncation(s, min_nmodes, min_renergy, max_nmodes)
6581 return nmodes, energy
6682end
6783
68- function reduce!(pod:: POD , alg:: SVD )
84+ function reduce!(pod:: POD{S, T} , alg:: SVD ):: Nothing where {S, T}
6985 u, s, v = _svd(pod. snapshots; alg. kwargs... )
7086 pod. nmodes,
7187 pod. renergy = determine_truncation(
7288 s, pod. min_nmodes, pod. max_nmodes,
7389 pod. min_renergy
7490 )
75- pod. rbasis = u[:, 1 : (pod. nmodes)]
76- pod. spectrum = s
91+ pod. rbasis = Matrix{T}( u[:, 1 : (pod. nmodes)])
92+ pod. spectrum = Vector{T}(s)
7793 return nothing
7894end
7995
80- function reduce!(pod:: POD , alg:: TSVD )
96+ function reduce!(pod:: POD{S, T} , alg:: TSVD ):: Nothing where {S, T}
8197 u, s, v = _tsvd(pod. snapshots, pod. nmodes; alg. kwargs... )
8298 n_max = min(size(u, 1 ), size(v, 1 ))
83- pod. renergy = sum(s) / (sum(s) + (n_max - pod. nmodes) * s[end ])
84- pod. rbasis = u
85- pod. spectrum = s
99+ pod. renergy = T( sum(s) / (sum(s) + (n_max - pod. nmodes) * s[end ]) )
100+ pod. rbasis = Matrix{T}(u)
101+ pod. spectrum = Vector{T}(s)
86102 return nothing
87103end
88104
89- function reduce!(pod:: POD , alg:: RSVD )
105+ function reduce!(pod:: POD{S, T} , alg:: RSVD ):: Nothing where {S, T}
90106 u, s, v = _rsvd(pod. snapshots, pod. nmodes, alg. p)
91107 n_max = min(size(u, 1 ), size(v, 1 ))
92- pod. renergy = sum(s) / (sum(s) + (n_max - pod. nmodes) * s[end ])
93- pod. rbasis = u
94- pod. spectrum = s
108+ pod. renergy = T( sum(s) / (sum(s) + (n_max - pod. nmodes) * s[end ]) )
109+ pod. rbasis = Matrix{T}(u)
110+ pod. spectrum = Vector{T}(s)
95111 return nothing
96112end
97113
98- function Base. show(io:: IO , pod:: POD )
114+ function Base. show(io:: IO , pod:: POD ):: Nothing
99115 print(io, " POD \n " )
100116 print(io, " Reduction Order = " , pod. nmodes, " \n " )
101117 print(
102118 io, " Snapshot size = (" , size(pod. snapshots, 1 ), " ," , size(pod. snapshots[1 ], 2 ),
103119 " )\n "
104120 )
105- return print(io, " Relative Energy = " , pod. renergy, " \n " )
121+ print(io, " Relative Energy = " , pod. renergy, " \n " )
122+ return nothing
106123end
0 commit comments