From d27917781cbe7ab7312d6726861dcdb48d0fc2e3 Mon Sep 17 00:00:00 2001 From: nebmit <76664673+nebmit@users.noreply.github.com> Date: Tue, 5 Dec 2023 18:36:55 +0100 Subject: [PATCH] refactor!: modify constructors to use keyword arguments --- julia/TUG/src/Boundary.jl | 2 +- julia/TUG/src/DynamicSimulation.jl | 10 +-- julia/TUG/src/Simulation.jl | 108 +----------------------- julia/TUG/src/TUG.jl | 2 +- julia/TUG/test/TestBoundary.jl | 2 +- julia/TUG/test/TestDynamicSimulation.jl | 30 ++++--- julia/TUG/test/TestSimulation.jl | 57 ++++++++++--- 7 files changed, 71 insertions(+), 140 deletions(-) diff --git a/julia/TUG/src/Boundary.jl b/julia/TUG/src/Boundary.jl index 9abd377..4dac5d4 100644 --- a/julia/TUG/src/Boundary.jl +++ b/julia/TUG/src/Boundary.jl @@ -118,7 +118,7 @@ struct Boundary{T} rows::UInt32 boundaries::Vector{Vector{BoundaryElement{T}}} - function Boundary(grid::Grid{T})::Boundary{T} where {T} + function Boundary{T}(grid::Grid{T})::Boundary{T} where {T} dim = grid.dim cols = grid.cols rows = grid.rows diff --git a/julia/TUG/src/DynamicSimulation.jl b/julia/TUG/src/DynamicSimulation.jl index 7fa8f70..147d911 100644 --- a/julia/TUG/src/DynamicSimulation.jl +++ b/julia/TUG/src/DynamicSimulation.jl @@ -15,7 +15,7 @@ Allows the manipulation and running of simulations on different grid states in p - `timestep::T`: The timestep for each iteration. # Constructor -- `DynamicSimulation(grid, bc, approach, timestep)` creates a new dynamic simulation with specified parameters. +- `DynamicSimulation(grid, bc; approach, timestep, workers)` creates a new dynamic simulation with specified parameters. """ struct DynamicSimulation{T} <: AbstractSimulation{T} grid::Grid{T} @@ -28,11 +28,11 @@ struct DynamicSimulation{T} <: AbstractSimulation{T} workerPool::WorkerPool - function DynamicSimulation( + function DynamicSimulation{T}( grid::Grid{T}, - bc::Boundary{T}, - approach::APPROACH, - timestep::T, + bc::Boundary{T}; + approach::APPROACH = BTCS, + timestep::T = 0.1, workers::Vector{Int} = workers(), )::DynamicSimulation{T} where {T} timestep, iterations = adjustTimestep(grid, approach, timestep, 1, false) diff --git a/julia/TUG/src/Simulation.jl b/julia/TUG/src/Simulation.jl index ee1c660..2e5eba6 100644 --- a/julia/TUG/src/Simulation.jl +++ b/julia/TUG/src/Simulation.jl @@ -21,7 +21,7 @@ of iterations, output options, and simulation approach. - `csvOutput::CSV_OUTPUT`: Option for CSV file output level. # Constructor -- `Simulation(grid, bc, approach, iterations, timestep, consoleOutput, csvOutput)` creates a new simulation with specified parameters. +- `Simulation(grid, bc; approach, iterations, timestep, consoleOutput, csvOutput)` creates a new simulation with specified parameters. """ struct Simulation{T} <: AbstractSimulation{T} grid::Grid{T} @@ -34,9 +34,9 @@ struct Simulation{T} <: AbstractSimulation{T} consoleOutput::CONSOLE_OUTPUT csvOutput::CSV_OUTPUT - function Simulation( + function Simulation{T}( grid::Grid{T}, - bc::Boundary{T}, + bc::Boundary{T}; approach::APPROACH = BTCS, iterations::Int = 1, timestep::T = 0.1, @@ -115,105 +115,3 @@ function run(simulation::Simulation{T})::Nothing where {T} end end end - -""" - setIterations(simulation::Simulation{T}, iterations::Int)::Simulation{T} where {T} - -Sets the number of iterations for the given simulation. - -# Arguments -- `simulation::Simulation{T}`: The simulation object. -- `iterations::Int`: The new number of iterations to be set. - -# Returns -A new `Simulation` object with updated iterations. -""" -function setIterations(simulation::Simulation{T}, iterations::Int)::Simulation{T} where {T} - return Simulation( - simulation.grid, - simulation.bc, - simulation.approach, - iterations, - simulation.timestep, - simulation.consoleOutput, - simulation.csvOutput, - ) -end - -""" - setOutputConsole(simulation::Simulation{T}, consoleOutput::CONSOLE_OUTPUT)::Simulation{T} where {T} - -Sets the console output level for the simulation. - -# Arguments -- `simulation::Simulation{T}`: The simulation object. -- `consoleOutput::CONSOLE_OUTPUT`: The new console output level. - -# Returns -A new `Simulation` object with updated console output setting. -""" -function setOutputConsole( - simulation::Simulation{T}, - consoleOutput::CONSOLE_OUTPUT, -)::Simulation{T} where {T} - return Simulation( - simulation.grid, - simulation.bc, - simulation.approach, - simulation.iterations, - simulation.timestep, - consoleOutput, - simulation.csvOutput, - ) -end - -""" - setOutputCSV(simulation::Simulation{T}, csvOutput::CSV_OUTPUT)::Simulation{T} where {T} - -Sets the CSV output level for the simulation. - -# Arguments -- `simulation::Simulation{T}`: The simulation object. -- `csvOutput::CSV_OUTPUT`: The new CSV output level. - -# Returns -A new `Simulation` object with updated CSV output setting. -""" -function setOutputCSV( - simulation::Simulation{T}, - csvOutput::CSV_OUTPUT, -)::Simulation{T} where {T} - return Simulation( - simulation.grid, - simulation.bc, - simulation.approach, - simulation.iterations, - simulation.timestep, - simulation.consoleOutput, - csvOutput, - ) -end - -""" - setTimestep(simulation::Simulation{T}, timestep::T)::Simulation{T} where {T} - -Sets the timestep for the simulation. - -# Arguments -- `simulation::Simulation{T}`: The simulation object. -- `timestep::T`: The new timestep to be set. - -# Returns -A new `Simulation` object with updated timestep. -""" -function setTimestep(simulation::Simulation{T}, timestep::T)::Simulation{T} where {T} - return Simulation( - simulation.grid, - simulation.bc, - simulation.approach, - simulation.iterations, - timestep, - simulation.consoleOutput, - simulation.csvOutput, - ) -end diff --git a/julia/TUG/src/TUG.jl b/julia/TUG/src/TUG.jl index 094f6a3..448dd97 100644 --- a/julia/TUG/src/TUG.jl +++ b/julia/TUG/src/TUG.jl @@ -49,7 +49,7 @@ export APPROACH, include("Simulation.jl") export Simulation -export run, setTimestep, setIterations, setOutputConsole, setOutputCSV +export run include("DynamicSimulation.jl") diff --git a/julia/TUG/test/TestBoundary.jl b/julia/TUG/test/TestBoundary.jl index a95d11e..2213361 100644 --- a/julia/TUG/test/TestBoundary.jl +++ b/julia/TUG/test/TestBoundary.jl @@ -14,7 +14,7 @@ @testset "Boundary" begin grid = TUG.Grid{Float64}(25, 20, zeros(25, 20), ones(25, 20)) - boundary = TUG.Boundary(grid) + boundary = TUG.Boundary{Float64}(grid) @test boundary.dim == 2 @test boundary.rows == 25 diff --git a/julia/TUG/test/TestDynamicSimulation.jl b/julia/TUG/test/TestDynamicSimulation.jl index 6b369ff..e48b0ae 100644 --- a/julia/TUG/test/TestDynamicSimulation.jl +++ b/julia/TUG/test/TestDynamicSimulation.jl @@ -1,8 +1,8 @@ @testset "DynamicSimulation.jl" begin @testset "Constructor" begin grid = TUG.Grid{Float64}(5, ones(1, 5)) - boundary = TUG.Boundary(grid) - simulation = TUG.DynamicSimulation(grid, boundary, BTCS, 0.1) + boundary = TUG.Boundary{Float64}(grid) + simulation = TUG.DynamicSimulation{Float64}(grid, boundary; timestep = 0.1) @test simulation.grid == grid @test simulation.bc == boundary @test simulation.approach == BTCS @@ -10,8 +10,9 @@ @test simulation.timestep == 0.1 grid = TUG.Grid{Float64}(5, ones(1, 5)) - boundary = TUG.Boundary(grid) - simulation = TUG.DynamicSimulation(grid, boundary, FTCS, 0.01) + boundary = TUG.Boundary{Float64}(grid) + simulation = + TUG.DynamicSimulation{Float64}(grid, boundary; approach = FTCS, timestep = 0.01) @test simulation.grid == grid @test simulation.bc == boundary @test simulation.approach == FTCS @@ -19,8 +20,9 @@ @test simulation.timestep == 0.01 grid = TUG.Grid{Float64}(5, ones(1, 5)) - boundary = TUG.Boundary(grid) - simulation = TUG.DynamicSimulation(grid, boundary, FTCS, 2.33) + boundary = TUG.Boundary{Float64}(grid) + simulation = + TUG.DynamicSimulation{Float64}(grid, boundary; approach = FTCS, timestep = 2.33) @test simulation.grid == grid @test simulation.bc == boundary @test simulation.approach == FTCS @@ -30,8 +32,8 @@ @testset "1D-Run" begin grid = TUG.Grid{Float64}(5, ones(1, 5)) TUG.setConcentrations!(grid, [1.0 1.0 20.0 1.0 1.0]) - boundary = TUG.Boundary(grid) - simulation = TUG.DynamicSimulation(grid, boundary, BTCS, 0.01) + boundary = TUG.Boundary{Float64}(grid) + simulation = TUG.DynamicSimulation{Float64}(grid, boundary; timestep = 0.01) TUG.createGrid(simulation) for _ = 1:20 TUG.next(simulation) @@ -46,9 +48,9 @@ grid = TUG.Grid{Float64}(5, ones(1, 5)) TUG.setConcentrations!(grid, [1.0 1.0 20.0 1.0 1.0]) - boundary = TUG.Boundary(grid) + boundary = TUG.Boundary{Float64}(grid) TUG.setBoundarySideConstant!(boundary, LEFT, 5.0) - simulation = TUG.DynamicSimulation(grid, boundary, BTCS, 0.01) + simulation = TUG.DynamicSimulation{Float64}(grid, boundary; timestep = 0.01) TUG.createGrid(simulation) for _ = 1:20 TUG.next(simulation) @@ -73,8 +75,8 @@ 1.0 1.0 20.0 1.0 1.0 ], ) - boundary = TUG.Boundary(grid) - simulation = TUG.DynamicSimulation(grid, boundary, BTCS, 0.01) + boundary = TUG.Boundary{Float64}(grid) + simulation = TUG.DynamicSimulation{Float64}(grid, boundary; timestep = 0.01) TUG.createGrid(simulation) for _ = 1:20 TUG.next(simulation) @@ -103,9 +105,9 @@ 1.0 1.0 20.0 1.0 1.0 ], ) - boundary = TUG.Boundary(grid) + boundary = TUG.Boundary{Float64}(grid) TUG.setBoundarySideConstant!(boundary, LEFT, 5.0) - simulation = TUG.DynamicSimulation(grid, boundary, BTCS, 0.01) + simulation = TUG.DynamicSimulation{Float64}(grid, boundary; timestep = 0.01) TUG.createGrid(simulation) for _ = 1:20 TUG.next(simulation) diff --git a/julia/TUG/test/TestSimulation.jl b/julia/TUG/test/TestSimulation.jl index 3367454..e864655 100644 --- a/julia/TUG/test/TestSimulation.jl +++ b/julia/TUG/test/TestSimulation.jl @@ -1,8 +1,8 @@ @testset "Simulation.jl" begin @testset "Constructor" begin grid = TUG.Grid{Float64}(5, ones(1, 5)) - boundary = TUG.Boundary(grid) - simulation = TUG.Simulation(grid, boundary) + boundary = TUG.Boundary{Float64}(grid) + simulation = TUG.Simulation{Float64}(grid, boundary) @test simulation.grid == grid @test simulation.bc == boundary @test simulation.approach == BTCS @@ -12,9 +12,16 @@ @test simulation.csvOutput == CSV_OUTPUT_OFF grid = TUG.Grid{Float64}(5, ones(1, 5)) - boundary = TUG.Boundary(grid) - simulation = - TUG.Simulation(grid, boundary, FTCS, 2, 0.2, CONSOLE_OUTPUT_ON, CSV_OUTPUT_ON) + boundary = TUG.Boundary{Float64}(grid) + simulation = TUG.Simulation{Float64}( + grid, + boundary; + approach = FTCS, + iterations = 2, + timestep = 0.2, + consoleOutput = CONSOLE_OUTPUT_ON, + csvOutput = CSV_OUTPUT_ON, + ) @test simulation.grid == grid @test simulation.bc == boundary @test simulation.approach == FTCS @@ -26,8 +33,14 @@ @testset "1D-Run" begin grid = TUG.Grid{Float64}(5, ones(1, 5)) TUG.setConcentrations!(grid, [1.0 1.0 20.0 1.0 1.0]) - boundary = TUG.Boundary(grid) - simulation = TUG.Simulation(grid, boundary, BTCS, 20, 0.01) + boundary = TUG.Boundary{Float64}(grid) + simulation = TUG.Simulation{Float64}( + grid, + boundary; + approach = BTCS, + iterations = 20, + timestep = 0.01, + ) TUG.run(simulation) expected_concentrations = [1.281106278320615 3.5643693033301567 14.309048836698485 3.5643693033301598 1.281106278320616] @@ -35,9 +48,15 @@ grid = TUG.Grid{Float64}(5, ones(1, 5)) TUG.setConcentrations!(grid, [1.0 1.0 20.0 1.0 1.0]) - boundary = TUG.Boundary(grid) + boundary = TUG.Boundary{Float64}(grid) TUG.setBoundarySideConstant!(boundary, LEFT, 5.0) - simulation = TUG.Simulation(grid, boundary, BTCS, 20, 0.01) + simulation = TUG.Simulation{Float64}( + grid, + boundary; + approach = BTCS, + iterations = 20, + timestep = 0.01, + ) TUG.run(simulation) expected_concentrations = [2.4416160635284823 3.6810808789967466 14.317333805802393 3.5648326408458035 1.2811288426376255] @@ -55,8 +74,14 @@ 1.0 1.0 20.0 1.0 1.0 ], ) - boundary = TUG.Boundary(grid) - simulation = TUG.Simulation(grid, boundary, BTCS, 20, 0.01) + boundary = TUG.Boundary{Float64}(grid) + simulation = TUG.Simulation{Float64}( + grid, + boundary; + approach = BTCS, + iterations = 20, + timestep = 0.01, + ) TUG.run(simulation) expected_concentrations = [ 1.141904802011076 3.591390417498421 14.249599956958917 3.5913904174984217 1.1419048020110782 @@ -78,9 +103,15 @@ 1.0 1.0 20.0 1.0 1.0 ], ) - boundary = TUG.Boundary(grid) + boundary = TUG.Boundary{Float64}(grid) TUG.setBoundarySideConstant!(boundary, LEFT, 5.0) - simulation = TUG.Simulation(grid, boundary, BTCS, 20, 0.01) + simulation = TUG.Simulation{Float64}( + grid, + boundary; + approach = BTCS, + iterations = 20, + timestep = 0.01, + ) TUG.run(simulation) expected_concentrations = [ 1.9866377371338924 3.67421468453773 14.255058363518529 3.5916629034159486 1.1419105589005596