From 01b2247344f3fc8fbbe67f49f672e4612d4e5307 Mon Sep 17 00:00:00 2001 From: nebmit <76664673+nebmit@users.noreply.github.com> Date: Mon, 4 Dec 2023 09:20:19 +0100 Subject: [PATCH] test: added package unit tests Created unit tests for Boundary, Grid, Simulation and Dynamic Simulation. [skip ci] --- julia/TUG/test/Manifest.toml | 34 +++++++++ julia/TUG/test/Project.toml | 2 + julia/TUG/test/TestBoundary.jl | 54 +++++++++++++++ julia/TUG/test/TestDynamicSimulation.jl | 78 +++++++++++++++++++++ julia/TUG/test/TestGrid.jl | 91 +++++++++++++++++++++++++ julia/TUG/test/TestSimulation.jl | 61 +++++++++++++++++ julia/TUG/test/runtests.jl | 7 ++ 7 files changed, 327 insertions(+) create mode 100644 julia/TUG/test/Manifest.toml create mode 100644 julia/TUG/test/Project.toml create mode 100644 julia/TUG/test/TestBoundary.jl create mode 100644 julia/TUG/test/TestDynamicSimulation.jl create mode 100644 julia/TUG/test/TestGrid.jl create mode 100644 julia/TUG/test/TestSimulation.jl create mode 100644 julia/TUG/test/runtests.jl diff --git a/julia/TUG/test/Manifest.toml b/julia/TUG/test/Manifest.toml new file mode 100644 index 0000000..4ad1915 --- /dev/null +++ b/julia/TUG/test/Manifest.toml @@ -0,0 +1,34 @@ +# This file is machine-generated - editing it directly is not advised + +julia_version = "1.9.3" +manifest_format = "2.0" +project_hash = "71d91126b5a1fb1020e1098d9d492de2a4438fd2" + +[[deps.Base64]] +uuid = "2a0f44e3-6c83-55bd-87e4-b1978d98bd5f" + +[[deps.InteractiveUtils]] +deps = ["Markdown"] +uuid = "b77e0a4c-d291-57a0-90e8-8db25a27a240" + +[[deps.Logging]] +uuid = "56ddb016-857b-54e1-b83d-db4d58db5568" + +[[deps.Markdown]] +deps = ["Base64"] +uuid = "d6f4376e-aef5-505a-96c1-9c027394607a" + +[[deps.Random]] +deps = ["SHA", "Serialization"] +uuid = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c" + +[[deps.SHA]] +uuid = "ea8e919c-243c-51af-8825-aaa63cd721ce" +version = "0.7.0" + +[[deps.Serialization]] +uuid = "9e88b42a-f829-5b0c-bbe9-9e923198166b" + +[[deps.Test]] +deps = ["InteractiveUtils", "Logging", "Random", "Serialization"] +uuid = "8dfed614-e22c-5e08-85e1-65c5234f0b40" diff --git a/julia/TUG/test/Project.toml b/julia/TUG/test/Project.toml new file mode 100644 index 0000000..0c36332 --- /dev/null +++ b/julia/TUG/test/Project.toml @@ -0,0 +1,2 @@ +[deps] +Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" diff --git a/julia/TUG/test/TestBoundary.jl b/julia/TUG/test/TestBoundary.jl new file mode 100644 index 0000000..a95d11e --- /dev/null +++ b/julia/TUG/test/TestBoundary.jl @@ -0,0 +1,54 @@ +@testset "Boundary.jl" begin + @testset "BoundaryElement" begin + be = TUG.BoundaryElement{Float64}() + @test TUG.getType(be) == TUG.CLOSED + @test TUG.getValue(be) == -1.0 + + be = TUG.BoundaryElement{Float64}(1.0) + @test TUG.getType(be) == TUG.CONSTANT + @test TUG.getValue(be) == 1.0 + @test TUG.getValue([be]) == [1.0] + + @test_throws ArgumentError TUG.BoundaryElement{Float64}(-1.0) + end + + @testset "Boundary" begin + grid = TUG.Grid{Float64}(25, 20, zeros(25, 20), ones(25, 20)) + boundary = TUG.Boundary(grid) + + @test boundary.dim == 2 + @test boundary.rows == 25 + @test boundary.cols == 20 + + @test TUG.getBoundaryElementType(boundary, TUG.LEFT, 1) == TUG.CLOSED + @test TUG.getBoundaryElementType(boundary, TUG.RIGHT, 1) == TUG.CLOSED + @test TUG.getBoundaryElementType(boundary, TUG.TOP, 1) == TUG.CLOSED + @test TUG.getBoundaryElementType(boundary, TUG.BOTTOM, 1) == TUG.CLOSED + + @test_throws ArgumentError TUG.getBoundaryElementType(boundary, TUG.LEFT, -1) + @test_throws ArgumentError TUG.getBoundaryElementType(boundary, TUG.RIGHT, 26) + + @test TUG.getBoundaryElementValue(boundary, TUG.LEFT, 1) == -1.0 + @test TUG.getBoundaryElementValue(boundary, TUG.RIGHT, 1) == -1.0 + @test TUG.getBoundaryElementValue(boundary, TUG.TOP, 1) == -1.0 + @test TUG.getBoundaryElementValue(boundary, TUG.BOTTOM, 1) == -1.0 + + @test_throws ArgumentError TUG.getBoundaryElementValue(boundary, TUG.BOTTOM, -1) + @test_throws ArgumentError TUG.getBoundaryElementValue(boundary, TUG.TOP, 21) + + TUG.setBoundarySideClosed!(boundary, TUG.LEFT) + TUG.setBoundarySideConstant!(boundary, TUG.RIGHT, 1.0) + TUG.setBoundarySideConstant!(boundary, TUG.TOP, 2.0) + TUG.setBoundarySideConstant!(boundary, TUG.BOTTOM, 3.0) + + @test TUG.getBoundaryElementType(boundary, TUG.LEFT, 1) == TUG.CLOSED + @test TUG.getBoundaryElementType(boundary, TUG.RIGHT, 1) == TUG.CONSTANT + @test TUG.getBoundaryElementType(boundary, TUG.TOP, 5) == TUG.CONSTANT + @test TUG.getBoundaryElementType(boundary, TUG.BOTTOM, 20) == TUG.CONSTANT + + @test all(TUG.getValue(TUG.getBoundarySide(boundary, TUG.LEFT)) .== -1.0) + @test all(TUG.getValue(TUG.getBoundarySide(boundary, TUG.RIGHT)) .== 1.0) + @test all(TUG.getValue(TUG.getBoundarySide(boundary, TUG.TOP)) .== 2.0) + @test all(TUG.getValue(TUG.getBoundarySide(boundary, TUG.BOTTOM)) .== 3.0) + end +end diff --git a/julia/TUG/test/TestDynamicSimulation.jl b/julia/TUG/test/TestDynamicSimulation.jl new file mode 100644 index 0000000..a07f4d7 --- /dev/null +++ b/julia/TUG/test/TestDynamicSimulation.jl @@ -0,0 +1,78 @@ +@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) + @test simulation.grid == grid + @test simulation.bc == boundary + @test simulation.approach == BTCS + @test simulation.iterations == 1 + @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) + @test simulation.grid == grid + @test simulation.bc == boundary + @test simulation.approach == FTCS + @test simulation.iterations == 1 + @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) + @test simulation.grid == grid + @test simulation.bc == boundary + @test simulation.approach == FTCS + @test simulation.iterations == 10 + @test simulation.timestep == 0.233 + end + @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) + TUG.createGrid(simulation) + for _ in 1:20 + TUG.next(simulation) + end + expected_concentrations = [1.281106278320615 3.5643693033301567 14.309048836698485 3.5643693033301598 1.281106278320616] + @test isapprox(TUG.getConcentrations(simulation, 1), expected_concentrations, atol=1e-6) + + 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) + TUG.setBoundarySideConstant!(boundary, LEFT, 5.0) + simulation = TUG.DynamicSimulation(grid, boundary, BTCS, 0.01) + TUG.createGrid(simulation) + for _ in 1:20 + TUG.next(simulation) + end + expected_concentrations = [2.4416160635284823 3.6810808789967466 14.317333805802393 3.5648326408458035 1.2811288426376255] + @test isapprox(TUG.getConcentrations(simulation, 1), expected_concentrations, atol=1e-6) + end + @testset "2D-Run" begin + grid = TUG.Grid{Float64}(5, 5, ones(5, 5), ones(5, 5)) + TUG.setConcentrations!(grid, [1.0 1.0 20.0 1.0 1.0; 1.0 1.0 20.0 1.0 1.0; 1.0 1.0 20.0 1.0 1.0; 1.0 1.0 20.0 1.0 1.0; 1.0 1.0 20.0 1.0 1.0]) + boundary = TUG.Boundary(grid) + simulation = TUG.DynamicSimulation(grid, boundary, BTCS, 0.01) + TUG.createGrid(simulation) + for _ in 1:20 + TUG.next(simulation) + end + expected_concentrations = [1.141904802011076 3.591390417498421 14.249599956958917 3.5913904174984217 1.1419048020110782; 1.1419048020110738 3.5913904174984173 14.2495999569589 3.5913904174984177 1.1419048020110767; 1.1419048020110725 3.591390417498413 14.249599956958875 3.5913904174984137 1.1419048020110751; 1.1419048020110738 3.5913904174984164 14.249599956958901 3.5913904174984173 1.141904802011077; 1.1419048020110774 3.5913904174984297 14.24959995695894 3.5913904174984297 1.1419048020110796] + @test isapprox(TUG.getConcentrations(simulation, 1), expected_concentrations, atol=1e-6) + + grid = TUG.Grid{Float64}(5, 5, ones(5, 5), ones(5, 5)) + TUG.setConcentrations!(grid, [1.0 1.0 20.0 1.0 1.0; 1.0 1.0 20.0 1.0 1.0; 1.0 1.0 20.0 1.0 1.0; 1.0 1.0 20.0 1.0 1.0; 1.0 1.0 20.0 1.0 1.0]) + boundary = TUG.Boundary(grid) + TUG.setBoundarySideConstant!(boundary, LEFT, 5.0) + simulation = TUG.DynamicSimulation(grid, boundary, BTCS, 0.01) + TUG.createGrid(simulation) + for _ in 1:20 + TUG.next(simulation) + end + expected_concentrations = [1.9866377371338924 3.67421468453773 14.255058363518529 3.5916629034159486 1.1419105589005596; 1.98663773713389 3.674214684537723 14.255058363518497 3.5916629034159406 1.1419105589005576; 1.9866377371338884 3.6742146845377186 14.255058363518481 3.591662903415937 1.1419105589005565; 1.9866377371338895 3.674214684537725 14.255058363518502 3.5916629034159424 1.1419105589005574; 1.9866377371338952 3.6742146845377377 14.255058363518547 3.591662903415955 1.141910558900562] + @test isapprox(TUG.getConcentrations(simulation, 1), expected_concentrations, atol=1e-6) + end +end \ No newline at end of file diff --git a/julia/TUG/test/TestGrid.jl b/julia/TUG/test/TestGrid.jl new file mode 100644 index 0000000..5a22095 --- /dev/null +++ b/julia/TUG/test/TestGrid.jl @@ -0,0 +1,91 @@ +@testset "Grid.jl" begin + @testset "1D-Grid" begin + grid = TUG.Grid{Float64}(20, zeros(1, 20)) + @test TUG.getCols(grid) == 20 + @test TUG.getRows(grid) == 1 + @test TUG.getDim(grid) == 1 + + concentrations = TUG.getConcentrations(grid) + @test size(concentrations) == (1, 20) + @test all(concentrations .== 0) + + alphaX = TUG.getAlphaX(grid) + @test size(alphaX) == (1, 20) + @test all(alphaX .== 0) + + @test_throws ErrorException TUG.getAlphaY(grid) + + alphaX_t = TUG.getAlphaX_t(grid) + @test size(alphaX_t) == (20, 1) + @test all(alphaX_t .== 0) + + @test_throws ErrorException TUG.getAlphaY_t(grid) + end + + @testset "2D-Grid" begin + grid = TUG.Grid{Float64}(25, 20, zeros(25, 20), ones(25, 20)) + @test TUG.getCols(grid) == 20 + @test TUG.getRows(grid) == 25 + @test TUG.getDim(grid) == 2 + + concentrations = TUG.getConcentrations(grid) + @test size(concentrations) == (25, 20) + @test all(concentrations .== 0) + + alphaX = TUG.getAlphaX(grid) + @test size(alphaX) == (25, 20) + @test all(alphaX .== 0) + + alphaY = TUG.getAlphaY(grid) + @test size(alphaY) == (25, 20) + @test all(alphaY .== 1) + + alphaX_t = TUG.getAlphaX_t(grid) + @test size(alphaX_t) == (20, 25) + @test all(alphaX_t .== 0) + + alphaY_t = TUG.getAlphaY_t(grid) + @test size(alphaY_t) == (20, 25) + @test all(alphaY_t .== 1) + end + + @testset "Clone" begin + grid = TUG.Grid{Float64}(20, zeros(1, 20)) + gridClone = TUG.clone(grid) + @test TUG.getCols(gridClone) == 20 + @test TUG.getRows(gridClone) == 1 + @test TUG.getDim(gridClone) == 1 + concentrations = TUG.getConcentrations(gridClone) + @test size(concentrations) == (1, 20) + @test all(concentrations .== 0) + alphaX = TUG.getAlphaX(gridClone) + @test size(alphaX) == (1, 20) + @test all(alphaX .== 0) + @test_throws ErrorException TUG.getAlphaY(gridClone) + alphaX_t = TUG.getAlphaX_t(gridClone) + @test size(alphaX_t) == (20, 1) + @test all(alphaX_t .== 0) + @test_throws ErrorException TUG.getAlphaY_t(gridClone) + + grid = TUG.Grid{Float64}(25, 20, zeros(25, 20), ones(25, 20)) + gridClone = TUG.clone(grid) + @test TUG.getCols(gridClone) == 20 + @test TUG.getRows(gridClone) == 25 + @test TUG.getDim(gridClone) == 2 + concentrations = TUG.getConcentrations(gridClone) + @test size(concentrations) == (25, 20) + @test all(concentrations .== 0) + alphaX = TUG.getAlphaX(gridClone) + @test size(alphaX) == (25, 20) + @test all(alphaX .== 0) + alphaY = TUG.getAlphaY(gridClone) + @test size(alphaY) == (25, 20) + @test all(alphaY .== 1) + alphaX_t = TUG.getAlphaX_t(gridClone) + @test size(alphaX_t) == (20, 25) + @test all(alphaX_t .== 0) + alphaY_t = TUG.getAlphaY_t(gridClone) + @test size(alphaY_t) == (20, 25) + @test all(alphaY_t .== 1) + end +end diff --git a/julia/TUG/test/TestSimulation.jl b/julia/TUG/test/TestSimulation.jl new file mode 100644 index 0000000..edf26a9 --- /dev/null +++ b/julia/TUG/test/TestSimulation.jl @@ -0,0 +1,61 @@ +@testset "Simulation.jl" begin + @testset "Constructor" begin + grid = TUG.Grid{Float64}(5, ones(1, 5)) + boundary = TUG.Boundary(grid) + simulation = TUG.Simulation(grid, boundary) + @test simulation.grid == grid + @test simulation.bc == boundary + @test simulation.approach == BTCS + @test simulation.iterations == 1 + @test simulation.timestep == 0.1 + @test simulation.consoleOutput == CONSOLE_OUTPUT_OFF + @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) + @test simulation.grid == grid + @test simulation.bc == boundary + @test simulation.approach == FTCS + @test simulation.iterations == 2 + @test simulation.timestep == 0.2 + @test simulation.consoleOutput == CONSOLE_OUTPUT_ON + @test simulation.csvOutput == CSV_OUTPUT_ON + end + @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) + TUG.run(simulation) + expected_concentrations = [1.281106278320615 3.5643693033301567 14.309048836698485 3.5643693033301598 1.281106278320616] + @test isapprox(TUG.getConcentrations(grid), expected_concentrations, atol=1e-6) + + 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) + TUG.setBoundarySideConstant!(boundary, LEFT, 5.0) + simulation = TUG.Simulation(grid, boundary, BTCS, 20, 0.01) + TUG.run(simulation) + expected_concentrations = [2.4416160635284823 3.6810808789967466 14.317333805802393 3.5648326408458035 1.2811288426376255] + @test isapprox(TUG.getConcentrations(grid), expected_concentrations, atol=1e-6) + end + @testset "2D-Run" begin + grid = TUG.Grid{Float64}(5, 5, ones(5, 5), ones(5, 5)) + TUG.setConcentrations!(grid, [1.0 1.0 20.0 1.0 1.0; 1.0 1.0 20.0 1.0 1.0; 1.0 1.0 20.0 1.0 1.0; 1.0 1.0 20.0 1.0 1.0; 1.0 1.0 20.0 1.0 1.0]) + boundary = TUG.Boundary(grid) + simulation = TUG.Simulation(grid, boundary, BTCS, 20, 0.01) + TUG.run(simulation) + expected_concentrations = [1.141904802011076 3.591390417498421 14.249599956958917 3.5913904174984217 1.1419048020110782; 1.1419048020110738 3.5913904174984173 14.2495999569589 3.5913904174984177 1.1419048020110767; 1.1419048020110725 3.591390417498413 14.249599956958875 3.5913904174984137 1.1419048020110751; 1.1419048020110738 3.5913904174984164 14.249599956958901 3.5913904174984173 1.141904802011077; 1.1419048020110774 3.5913904174984297 14.24959995695894 3.5913904174984297 1.1419048020110796] + @test isapprox(TUG.getConcentrations(grid), expected_concentrations, atol=1e-6) + + grid = TUG.Grid{Float64}(5, 5, ones(5, 5), ones(5, 5)) + TUG.setConcentrations!(grid, [1.0 1.0 20.0 1.0 1.0; 1.0 1.0 20.0 1.0 1.0; 1.0 1.0 20.0 1.0 1.0; 1.0 1.0 20.0 1.0 1.0; 1.0 1.0 20.0 1.0 1.0]) + boundary = TUG.Boundary(grid) + TUG.setBoundarySideConstant!(boundary, LEFT, 5.0) + simulation = TUG.Simulation(grid, boundary, BTCS, 20, 0.01) + TUG.run(simulation) + expected_concentrations = [1.9866377371338924 3.67421468453773 14.255058363518529 3.5916629034159486 1.1419105589005596; 1.98663773713389 3.674214684537723 14.255058363518497 3.5916629034159406 1.1419105589005576; 1.9866377371338884 3.6742146845377186 14.255058363518481 3.591662903415937 1.1419105589005565; 1.9866377371338895 3.674214684537725 14.255058363518502 3.5916629034159424 1.1419105589005574; 1.9866377371338952 3.6742146845377377 14.255058363518547 3.591662903415955 1.141910558900562] + @test isapprox(TUG.getConcentrations(grid), expected_concentrations, atol=1e-6) + end +end diff --git a/julia/TUG/test/runtests.jl b/julia/TUG/test/runtests.jl new file mode 100644 index 0000000..db404b0 --- /dev/null +++ b/julia/TUG/test/runtests.jl @@ -0,0 +1,7 @@ +using TUG +using Test + +include("TestGrid.jl") +include("TestBoundary.jl") +include("TestSimulation.jl") +include("TestDynamicSimulation.jl")