| 1 |
|
|---|
| 2 | #Toolkit #1: serial sparse arrays
|
|---|
| 3 | using SparseArrays
|
|---|
| 4 |
|
|---|
| 5 | #Matrix
|
|---|
| 6 | mutable struct IssmMatrix #{{{
|
|---|
| 7 | M::Int64
|
|---|
| 8 | N::Int64
|
|---|
| 9 | rows::Vector{Int64}
|
|---|
| 10 | cols::Vector{Int64}
|
|---|
| 11 | vals::Vector{Float64}
|
|---|
| 12 | matrix::SparseMatrixCSC{Float64,Int64}
|
|---|
| 13 | end #}}}
|
|---|
| 14 | function IssmMatrix(M::Int64,N::Int64)#{{{
|
|---|
| 15 | return IssmMatrix(M, N, Vector{Int64}(undef,0), Vector{Int64}(undef,0), Vector{Float64}(undef,0), spzeros(0,0))
|
|---|
| 16 | end#}}}
|
|---|
| 17 | function AddValues!(matrix::IssmMatrix,m::Int64,midx::Vector{Int64},n::Int64,nidx::Vector{Int64},values::Matrix{Float64})#{{{
|
|---|
| 18 |
|
|---|
| 19 | #This is inefficient now, but it will work
|
|---|
| 20 | for i in 1:m
|
|---|
| 21 | if(midx[i]==-1) continue end
|
|---|
| 22 | for j in 1:n
|
|---|
| 23 | if(nidx[j]==-1) continue end
|
|---|
| 24 | push!(matrix.rows, midx[i])
|
|---|
| 25 | push!(matrix.cols, nidx[j])
|
|---|
| 26 | push!(matrix.vals, values[i,j])
|
|---|
| 27 | end
|
|---|
| 28 | end
|
|---|
| 29 |
|
|---|
| 30 | end#}}}
|
|---|
| 31 | function GetSize(matrix::IssmMatrix)#{{{
|
|---|
| 32 |
|
|---|
| 33 | return size(matrix.matrix)
|
|---|
| 34 |
|
|---|
| 35 | end#}}}
|
|---|
| 36 | function Assemble!(matrix::IssmMatrix)#{{{
|
|---|
| 37 |
|
|---|
| 38 | matrix.matrix = sparse(matrix.rows, matrix.cols, matrix.vals, matrix.M, matrix.N)
|
|---|
| 39 |
|
|---|
| 40 | end#}}}
|
|---|
| 41 |
|
|---|
| 42 | #Vector
|
|---|
| 43 | mutable struct IssmVector #{{{
|
|---|
| 44 | vector::Vector{Float64}
|
|---|
| 45 | end #}}}
|
|---|
| 46 | function IssmVector(M::Int64)#{{{
|
|---|
| 47 | return IssmVector(zeros(M))
|
|---|
| 48 | end#}}}
|
|---|
| 49 | function GetSize(vector::IssmVector)#{{{
|
|---|
| 50 |
|
|---|
| 51 | return length(vector.vector)
|
|---|
| 52 |
|
|---|
| 53 | end#}}}
|
|---|
| 54 | function AddValues!(vector::IssmVector,m::Int64,midx::Vector{Int64},values::Vector{Float64})#{{{
|
|---|
| 55 |
|
|---|
| 56 | #This is inefficient now, but it will work
|
|---|
| 57 | for i in 1:m
|
|---|
| 58 | if(midx[i]==-1) continue end
|
|---|
| 59 | vector.vector[midx[i]] += values[i]
|
|---|
| 60 | end
|
|---|
| 61 |
|
|---|
| 62 | end#}}}
|
|---|
| 63 | function SetValues!(vector::IssmVector,m::Int64,midx::Vector{Int64},values::Vector{Float64})#{{{
|
|---|
| 64 |
|
|---|
| 65 | #This is inefficient now, but it will work
|
|---|
| 66 | for i in 1:m
|
|---|
| 67 | if(midx[i]==-1) continue end
|
|---|
| 68 | vector.vector[midx[i]] = values[i]
|
|---|
| 69 | end
|
|---|
| 70 |
|
|---|
| 71 | end#}}}
|
|---|
| 72 | function IsEmpty(vector::IssmVector)#{{{
|
|---|
| 73 |
|
|---|
| 74 | return GetSize(vector)==0
|
|---|
| 75 |
|
|---|
| 76 | end#}}}
|
|---|
| 77 | function Duplicate(vector::IssmVector)#{{{
|
|---|
| 78 |
|
|---|
| 79 | #Copy data structure
|
|---|
| 80 | M=GetSize(vector)
|
|---|
| 81 | return IssmVector(M)
|
|---|
| 82 |
|
|---|
| 83 | end#}}}
|
|---|
| 84 | function VecCopy!(x::IssmVector,y::IssmVector)#{{{
|
|---|
| 85 |
|
|---|
| 86 | y.vector = x.vector
|
|---|
| 87 |
|
|---|
| 88 | end#}}}
|
|---|
| 89 | function Assemble!(vector::IssmVector)#{{{
|
|---|
| 90 |
|
|---|
| 91 | #Nothing to do for this toolkit
|
|---|
| 92 |
|
|---|
| 93 | end#}}}
|
|---|
| 94 | function ToSerial(vector::IssmVector)#{{{
|
|---|
| 95 |
|
|---|
| 96 | return vector.vector
|
|---|
| 97 |
|
|---|
| 98 | end#}}}
|
|---|
| 99 | function Norm(x::IssmVector,type::Int64)#{{{
|
|---|
| 100 |
|
|---|
| 101 | norm = 0
|
|---|
| 102 |
|
|---|
| 103 | if type==2
|
|---|
| 104 | for i in 1:length(x.vector)
|
|---|
| 105 | norm += x.vector[i]^2
|
|---|
| 106 | end
|
|---|
| 107 | norm = sqrt(norm)
|
|---|
| 108 | elseif type==3
|
|---|
| 109 | #Infinite norm
|
|---|
| 110 | for i in 1:length(x.vector)
|
|---|
| 111 | if(abs(x.vector[i])>norm) norm = abs(x.vector[i]) end
|
|---|
| 112 | end
|
|---|
| 113 | else
|
|---|
| 114 | error("type ",type," not supported yet")
|
|---|
| 115 | end
|
|---|
| 116 |
|
|---|
| 117 | return norm
|
|---|
| 118 |
|
|---|
| 119 | end#}}}
|
|---|
| 120 |
|
|---|
| 121 | #Operations
|
|---|
| 122 | function MatMult!(A::IssmMatrix,x::IssmVector,y::IssmVector) #{{{
|
|---|
| 123 |
|
|---|
| 124 | y.vector = A.matrix*x.vector
|
|---|
| 125 |
|
|---|
| 126 | end#}}}
|
|---|
| 127 | function AXPY!(y::IssmVector,alpha::Float64,x::IssmVector) #{{{
|
|---|
| 128 |
|
|---|
| 129 | y.vector = alpha*x.vector + y.vector
|
|---|
| 130 |
|
|---|
| 131 | end#}}}
|
|---|
| 132 | function Solverx(A::IssmMatrix, b::IssmVector, xold::IssmVector) #{{{
|
|---|
| 133 |
|
|---|
| 134 | #Initialize output
|
|---|
| 135 | #x = IssmVector(GetSize(xold))
|
|---|
| 136 |
|
|---|
| 137 | return Solverx(A, b)
|
|---|
| 138 |
|
|---|
| 139 | end#}}}
|
|---|
| 140 | function Solverx(A::IssmMatrix, b::IssmVector) #{{{
|
|---|
| 141 |
|
|---|
| 142 | #Initialize output
|
|---|
| 143 | x = IssmVector(0)
|
|---|
| 144 |
|
|---|
| 145 | #Solve linear system
|
|---|
| 146 | x.vector = A.matrix\b.vector
|
|---|
| 147 |
|
|---|
| 148 | return x
|
|---|
| 149 |
|
|---|
| 150 | end#}}}
|
|---|