From 6a98a23439fe9836cea8c459aaf34a77edf5e9ab Mon Sep 17 00:00:00 2001 From: nebmit <76664673+nebmit@users.noreply.github.com> Date: Tue, 12 Mar 2024 15:04:38 +0100 Subject: [PATCH] feat(Grid): add domain values as optional arguments [skip ci] --- julia/TUG/src/Grid.jl | 69 +++++++++++++++++++++++++++---------------- 1 file changed, 43 insertions(+), 26 deletions(-) diff --git a/julia/TUG/src/Grid.jl b/julia/TUG/src/Grid.jl index 92347e8..0387f1e 100644 --- a/julia/TUG/src/Grid.jl +++ b/julia/TUG/src/Grid.jl @@ -12,11 +12,11 @@ Represents a computational grid with concentration values and alpha coefficients It can be either 1D or 2D and includes dimensions, domain sizes, delta values, and matrices for concentrations and alpha coefficients. # Fields -- `cols::Int`: Number of columns in the grid. - `rows::Int`: Number of rows in the grid. +- `cols::Int`: Number of columns in the grid. - `dim::Int`: Dimension of the grid (1 for 1D, 2 for 2D). -- `domainCol::T`, `domainRow::T`: Size of the grid's domain in column and row direction. -- `deltaCol::T`, `deltaRow::T`: Delta values for columns and rows. +- `domainRow::T`, `domainCol::T`: Size of the grid's domain in column and row direction. +- `deltaRow::T`, `deltaCol::T`: Delta values for columns and rows. - `concentrations::Ref{Matrix{T}}`: Reference to the matrix holding concentration values. - `alphaX::Ref{Matrix{T}}`: Reference to the matrix of alpha coefficients in the X direction. - `alphaY::Union{Ref{Matrix{T}},Nothing}`: Reference to the matrix of alpha coefficients in the Y direction (if applicable). @@ -25,23 +25,31 @@ It can be either 1D or 2D and includes dimensions, domain sizes, delta values, a # Constructors - `Grid(length, alphaX)` creates a 1D grid with the given length and alphaX matrix. - `Grid(rows, cols, alphaX, alphaY)` creates a 2D grid with the given rows, columns, and alphaX and alphaY matrices. -- `Grid(rows, cols, dim, domainCol, domainRow, deltaCol, deltaRow, concentrations, alphaX, alphaY, alphaX_t, alphaY_t)` creates a grid with the given parameters. +- `Grid(rows, cols, dim, domainRow, domainCol, deltaRow, deltaCol, concentrations, alphaX, alphaY, alphaX_t, alphaY_t)` creates a grid with the given parameters. """ struct Grid{T} - cols::Int rows::Int + cols::Int dim::Int - domainCol::T domainRow::T - deltaCol::T + domainCol::T deltaRow::T + deltaCol::T concentrations::Matrix{T} alphaX::Matrix{T} alphaY::Union{Matrix{T},Nothing} alphaX_t::Matrix{T} alphaY_t::Union{Matrix{T},Nothing} - function Grid{T}(length::Int, alphaX::Matrix{T})::Grid{T} where {T} + function Grid{T}( + length::Int, + alphaX::Matrix{T}; + domainLength::T = T(-1), + )::Grid{T} where {T} + if domainLength < 0 + domainLength = T(length) + end + if length <= 3 throw(ArgumentError("Given grid length too small. Must be greater than 3.")) end @@ -56,13 +64,13 @@ struct Grid{T} alphaX_t = alphaX' new{T}( + 1, length, 1, - 1, - T(length), 0, - T(1), + T(domainLength), 0, + T(domainLength / length), fill(T(0), 1, length), alphaX, nothing, @@ -75,8 +83,17 @@ struct Grid{T} rows::Int, cols::Int, alphaX::Matrix{T}, - alphaY::Matrix{T}, + alphaY::Matrix{T}; + domainRow::T = T(-1), + domainCol::T = T(-1), )::Grid{T} where {T} + if domainRow < 0 + domainRow = T(rows) + end + if domainCol < 0 + domainCol = T(cols) + end + if rows <= 3 || cols <= 3 throw( ArgumentError( @@ -96,13 +113,13 @@ struct Grid{T} alphaY_t = alphaY' new{T}( - cols, rows, + cols, 2, - T(cols), - T(rows), - T(1), - T(1), + T(domainRow), + T(domainCol), + T(domainRow / rows), + T(domainCol / cols), fill(T(0), rows, cols), alphaX, alphaY, @@ -115,10 +132,10 @@ struct Grid{T} rows::Int, cols::Int, dim::Int, - domainCol::T, domainRow::T, - deltaCol::T, + domainCol::T, deltaRow::T, + deltaCol::T, concentrations::Matrix{T}, alphaX::Matrix{T}, alphaY::Union{Matrix{T},Nothing}, @@ -126,13 +143,13 @@ struct Grid{T} alphaY_t::Union{Matrix{T},Nothing}, )::Grid{T} where {T} new{T}( - cols, rows, + cols, dim, - domainCol, domainRow, - deltaCol, + domainCol, deltaRow, + deltaCol, concentrations, alphaX, alphaY, @@ -160,10 +177,10 @@ function clone(grid::Grid{T})::Grid{T} where {T} 1, grid.cols, grid.dim, - grid.domainCol, grid.domainRow, - grid.deltaCol, + grid.domainCol, grid.deltaRow, + grid.deltaCol, copy(grid.concentrations), copy(grid.alphaX), nothing, @@ -175,10 +192,10 @@ function clone(grid::Grid{T})::Grid{T} where {T} grid.rows, grid.cols, grid.dim, - grid.domainCol, grid.domainRow, - grid.deltaCol, + grid.domainCol, grid.deltaRow, + grid.deltaCol, copy(grid.concentrations), copy(grid.alphaX), copy(grid.alphaY),