TugJulia/julia/tug/Grid.jl
2023-11-19 20:30:58 +01:00

60 lines
1.9 KiB
Julia

using LinearAlgebra
struct Grid{T}
cols::Int
rows::Int
dim::Int
domainCol::T
domainRow::T
deltaCol::T
deltaRow::T
concentrations::Ref{Matrix{T}}
alphaX::Ref{Matrix{T}}
alphaY::Ref{Matrix{T}}
# Constructor for 1D-Grid
function Grid{T}(length::Int) where {T}
if length <= 3
throw(ArgumentError("Given grid length too small. Must be greater than 3."))
end
new{T}(length, 1, 1, T(length), 0, T(1), 0, Ref(fill(T(0), 1, length)), Ref(fill(T(0), 1, length)), Ref(fill(T(0), 1, length)))
end
# Constructor for 2D-Grid
function Grid{T}(row::Int, col::Int) where {T}
if row <= 3 || col <= 3
throw(ArgumentError("Given grid dimensions too small. Must each be greater than 3."))
end
new{T}(col, row, 2, T(col), T(row), T(1), T(1), Ref(fill(T(0), row, col)), Ref(fill(T(0), row, col)), Ref(fill(T(0), row, col)))
end
end
function setConcentrations!(grid::Grid{T}, new_concentrations::Matrix{T}) where {T}
grid.concentrations[] = new_concentrations
end
function setAlpha!(grid::Grid{T}, alpha::Matrix{T}) where {T}
if grid.dim != 1
error("Grid is not one dimensional, you should probably use the 2D setter function!")
end
if size(alpha, 1) != 1 || size(alpha, 2) != grid.cols
error("Given matrix of alpha coefficients mismatch with Grid dimensions!")
end
grid.alphaX[] = alpha
end
function setAlpha!(grid::Grid{T}, alphaX::Matrix{T}, alphaY::Matrix{T}) where {T}
if grid.dim != 2
error("Grid is not two dimensional, you should probably use the 1D setter function!")
end
if size(alphaX) != (grid.rows, grid.cols) || size(alphaY) != (grid.rows, grid.cols)
error("Given matrices of alpha coefficients mismatch with Grid dimensions!")
end
grid.alphaX[] = alphaX
grid.alphaY[] = alphaY
end