|
| 1 | +function matricize(VoV::Vector{Vector{FT}}) where {FT} |
| 2 | + Matrix(reduce(hcat, VoV)) |
| 3 | +end |
| 4 | + |
| 5 | +mutable struct POD{FT} <: AbstractDRProblem |
| 6 | + snapshots::Union{Vector{Vector{FT}}, Matrix{FT}} |
| 7 | + nmodes::Int |
| 8 | + rbasis::Matrix{FT} |
| 9 | + renergy::FT |
| 10 | + |
| 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)) |
| 15 | + end |
| 16 | + |
| 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)) |
| 21 | + end |
| 22 | + |
| 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)) |
| 25 | + end |
| 26 | +end |
| 27 | + |
| 28 | +function reduce!(pod::POD{FT}, ::SVD) where {FT} |
| 29 | + op_matrix = pod.snapshots |
| 30 | + if typeof(pod.snapshots) == Vector{Vector{FT}} |
| 31 | + op_matrix = matricize(pod.snapshots) |
| 32 | + 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]) |
| 37 | + nothing |
| 38 | +end |
| 39 | + |
| 40 | +function reduce!(pod::POD{FT}, ::TSVD) where {FT} |
| 41 | + op_matrix = pod.snapshots |
| 42 | + if typeof(pod.snapshots) == Vector{Vector{FT}} |
| 43 | + op_matrix = matricize(pod.snapshots) |
| 44 | + 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]) |
| 47 | + pod.rbasis .= u |
| 48 | + nothing |
| 49 | +end |
| 50 | + |
| 51 | +function reduce!(pod::POD{FT}, ::RSVD) where {FT} |
| 52 | + op_matrix = pod.snapshots |
| 53 | + if typeof(pod.snapshots) == Vector{Vector{FT}} |
| 54 | + op_matrix = matricize(pod.snapshots) |
| 55 | + 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]) |
| 58 | + pod.rbasis .= u |
| 59 | + nothing |
| 60 | +end |
| 61 | + |
| 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") |
| 68 | +end |
0 commit comments