From fe7f5c9b4acebfb5bfdf3e1d08e29650932ad55f Mon Sep 17 00:00:00 2001 From: nebmit <76664673+nebmit@users.noreply.github.com> Date: Thu, 30 Nov 2023 14:36:27 +0100 Subject: [PATCH] test: added FTCS julia/cpp tests [skip ci] --- julia/tests/cpp_bench/FTCS_1024_1000_100.cpp | 53 +++++++++++++++ julia/tests/cpp_bench/FTCS_1_20_7000.cpp | 42 ++++++++++++ julia/tests/cpp_bench/FTCS_2027_1999_200.cpp | 66 +++++++++++++++++++ julia/tests/cpp_bench/FTCS_20_20_500.cpp | 46 +++++++++++++ julia/tests/cpp_bench/FTCS_450_670_750.cpp | 54 +++++++++++++++ julia/tests/julia_bench/FTCS_1024_1000_100.jl | 41 ++++++++++++ julia/tests/julia_bench/FTCS_1_20_7000.jl | 30 +++++++++ julia/tests/julia_bench/FTCS_2027_1999_200.jl | 38 +++++++++++ julia/tests/julia_bench/FTCS_20_20_500.jl | 34 ++++++++++ julia/tests/julia_bench/FTCS_450_670_750.jl | 42 ++++++++++++ julia/tests/test.py | 5 +- 11 files changed, 449 insertions(+), 2 deletions(-) create mode 100644 julia/tests/cpp_bench/FTCS_1024_1000_100.cpp create mode 100644 julia/tests/cpp_bench/FTCS_1_20_7000.cpp create mode 100644 julia/tests/cpp_bench/FTCS_2027_1999_200.cpp create mode 100644 julia/tests/cpp_bench/FTCS_20_20_500.cpp create mode 100644 julia/tests/cpp_bench/FTCS_450_670_750.cpp create mode 100644 julia/tests/julia_bench/FTCS_1024_1000_100.jl create mode 100644 julia/tests/julia_bench/FTCS_1_20_7000.jl create mode 100644 julia/tests/julia_bench/FTCS_2027_1999_200.jl create mode 100644 julia/tests/julia_bench/FTCS_20_20_500.jl create mode 100644 julia/tests/julia_bench/FTCS_450_670_750.jl diff --git a/julia/tests/cpp_bench/FTCS_1024_1000_100.cpp b/julia/tests/cpp_bench/FTCS_1024_1000_100.cpp new file mode 100644 index 0000000..dd7f14c --- /dev/null +++ b/julia/tests/cpp_bench/FTCS_1024_1000_100.cpp @@ -0,0 +1,53 @@ +#include +#include +#include +#include + +using namespace Eigen; +using namespace tug; +using namespace std::chrono; + +int main(int argc, char *argv[]) +{ + // **** GRID **** + int rows = 1024; + int cols = 1000; + Grid64 grid(rows, cols); + + MatrixXd concentrations = MatrixXd::Constant(rows, cols, 0.5); + concentrations(10, 10) = 15000; + concentrations(1014, 990) = 7500; + concentrations(10, 990) = 7500; + concentrations(1014, 10) = 7500; + grid.setConcentrations(concentrations); + + MatrixXd alphax = MatrixXd::Constant(rows, cols, 1.25); + MatrixXd alphay = MatrixXd::Constant(rows, cols, 1.1); + alphax.block(0, 0, 100, cols) = MatrixXd::Constant(100, cols, 0.5); + alphax.block(100, 0, 100, cols) = MatrixXd::Constant(100, cols, 0.8); + alphay.block(0, 0, rows, 200) = MatrixXd::Constant(rows, 200, 0.6); + alphay.block(0, 200, rows, 200) = MatrixXd::Constant(rows, 200, 0.9); + grid.setAlpha(alphax, alphay); + + // **** BOUNDARY **** + Boundary bc = Boundary(grid); + bc.setBoundarySideClosed(BC_SIDE_LEFT); + bc.setBoundarySideClosed(BC_SIDE_RIGHT); + bc.setBoundarySideClosed(BC_SIDE_TOP); + bc.setBoundarySideClosed(BC_SIDE_BOTTOM); + + // **** SIMULATION **** + Simulation simulation = Simulation(grid, bc); + simulation.setTimestep(0.01); + simulation.setIterations(100); + simulation.setOutputCSV(CSV_OUTPUT_ON); + simulation.setOutputConsole(CONSOLE_OUTPUT_OFF); + + // **** RUN SIMULATION **** + auto start = high_resolution_clock::now(); + simulation.run(); + auto stop = high_resolution_clock::now(); + + auto duration = duration_cast(stop - start); + std::cout << duration.count() << std::endl; +} diff --git a/julia/tests/cpp_bench/FTCS_1_20_7000.cpp b/julia/tests/cpp_bench/FTCS_1_20_7000.cpp new file mode 100644 index 0000000..52b9951 --- /dev/null +++ b/julia/tests/cpp_bench/FTCS_1_20_7000.cpp @@ -0,0 +1,42 @@ +#include +#include +#include +#include + +using namespace Eigen; +using namespace tug; +using namespace std::chrono; + +int main(int argc, char *argv[]) +{ + // **** GRID **** + int cells = 20; + Grid64 grid(cells); + + MatrixXd concentrations = MatrixXd::Constant(1, cells, 0); + concentrations(0, 0) = 2000; + grid.setConcentrations(concentrations); + + MatrixXd alpha = MatrixXd::Constant(1, cells, 1); + grid.setAlpha(alpha); + + // **** BOUNDARY **** + Boundary bc = Boundary(grid); + bc.setBoundarySideConstant(BC_SIDE_LEFT, 0); + bc.setBoundarySideConstant(BC_SIDE_RIGHT, 0); + + // **** SIMULATION **** + Simulation simulation = Simulation(grid, bc); + simulation.setTimestep(0.001); + simulation.setIterations(7000); + simulation.setOutputCSV(CSV_OUTPUT_ON); + simulation.setOutputConsole(CONSOLE_OUTPUT_OFF); + + // **** RUN SIMULATION **** + auto start = high_resolution_clock::now(); + simulation.run(); + auto stop = high_resolution_clock::now(); + + auto duration = duration_cast(stop - start); + std::cout << duration.count() << std::endl; +} diff --git a/julia/tests/cpp_bench/FTCS_2027_1999_200.cpp b/julia/tests/cpp_bench/FTCS_2027_1999_200.cpp new file mode 100644 index 0000000..0fb4573 --- /dev/null +++ b/julia/tests/cpp_bench/FTCS_2027_1999_200.cpp @@ -0,0 +1,66 @@ +#include +#include +#include +#include +#include + +using namespace Eigen; +using namespace tug; +using namespace std::chrono; + +int main(int argc, char *argv[]) +{ + // **** GRID **** + int rows = 2027; + int cols = 1999; + Grid64 grid(rows, cols); + + MatrixXd concentrations(rows, cols); + for (int i = 0; i < rows; ++i) + { + for (int j = 0; j < cols; ++j) + { + concentrations(i, j) = static_cast(i * j) / 1e6; + } + } + concentrations(10, 10) = 15000; + concentrations(2020, 1994) = 7500; + concentrations(10, 1994) = 7500; + concentrations(2020, 10) = 7500; + grid.setConcentrations(concentrations); + + // Complex alpha patterns + MatrixXd alphax = MatrixXd(rows, cols); + MatrixXd alphay = MatrixXd(rows, cols); + for (int i = 0; i < rows; ++i) + { + for (int j = 0; j < cols; ++j) + { + alphax(i, j) = std::sin(i / 100.0) * std::cos(j / 100.0); + alphay(i, j) = std::cos(i / 100.0) * std::sin(j / 100.0); + } + } + grid.setAlpha(alphax, alphay); + + // **** BOUNDARY **** + Boundary bc = Boundary(grid); + bc.setBoundarySideConstant(BC_SIDE_LEFT, 1.5); + bc.setBoundarySideConstant(BC_SIDE_RIGHT, 1.5); + bc.setBoundarySideConstant(BC_SIDE_TOP, 0.75); + bc.setBoundarySideConstant(BC_SIDE_BOTTOM, 0.75); + + // **** SIMULATION **** + Simulation simulation = Simulation(grid, bc); + simulation.setTimestep(0.005); + simulation.setIterations(200); + simulation.setOutputCSV(CSV_OUTPUT_ON); + simulation.setOutputConsole(CONSOLE_OUTPUT_OFF); + + // **** RUN SIMULATION **** + auto start = high_resolution_clock::now(); + simulation.run(); + auto stop = high_resolution_clock::now(); + + auto duration = duration_cast(stop - start); + std::cout << duration.count() << std::endl; +} diff --git a/julia/tests/cpp_bench/FTCS_20_20_500.cpp b/julia/tests/cpp_bench/FTCS_20_20_500.cpp new file mode 100644 index 0000000..e766801 --- /dev/null +++ b/julia/tests/cpp_bench/FTCS_20_20_500.cpp @@ -0,0 +1,46 @@ +#include +#include +#include +#include + +using namespace Eigen; +using namespace tug; +using namespace std::chrono; + +int main(int argc, char *argv[]) +{ + // **** GRID **** + int rows = 20; + int cols = 20; + Grid64 grid(rows, cols); + + MatrixXd concentrations = MatrixXd::Constant(rows, cols, 0); + concentrations(10, 10) = 2000; + grid.setConcentrations(concentrations); + + MatrixXd alphax = MatrixXd::Constant(rows, cols, 1); + MatrixXd alphay = MatrixXd::Constant(rows, cols, 1); + grid.setAlpha(alphax, alphay); + + // **** BOUNDARY **** + Boundary bc = Boundary(grid); + bc.setBoundarySideClosed(BC_SIDE_LEFT); + bc.setBoundarySideClosed(BC_SIDE_RIGHT); + bc.setBoundarySideClosed(BC_SIDE_TOP); + bc.setBoundarySideClosed(BC_SIDE_BOTTOM); + + // **** SIMULATION **** + Simulation simulation = Simulation(grid, bc); + simulation.setTimestep(0.1); + simulation.setIterations(500); + simulation.setOutputCSV(CSV_OUTPUT_ON); + simulation.setOutputConsole(CONSOLE_OUTPUT_OFF); + + // **** RUN SIMULATION **** + auto start = high_resolution_clock::now(); + simulation.run(); + auto stop = high_resolution_clock::now(); + + auto duration = duration_cast(stop - start); + std::cout << duration.count() << std::endl; +} diff --git a/julia/tests/cpp_bench/FTCS_450_670_750.cpp b/julia/tests/cpp_bench/FTCS_450_670_750.cpp new file mode 100644 index 0000000..734268e --- /dev/null +++ b/julia/tests/cpp_bench/FTCS_450_670_750.cpp @@ -0,0 +1,54 @@ +#include +#include +#include +#include + +using namespace Eigen; +using namespace tug; +using namespace std::chrono; + +int main(int argc, char *argv[]) +{ + // **** GRID **** + int rows = 450; + int cols = 670; + Grid64 grid(rows, cols); + + MatrixXd concentrations = MatrixXd::Constant(rows, cols, 0); + concentrations(10, 10) = 1500; + concentrations(440, 660) = 750; + concentrations(440, 10) = 750; + concentrations(10, 660) = 750; + concentrations(220, 335) = 1500; + grid.setConcentrations(concentrations); + + MatrixXd alphax = MatrixXd::Constant(rows, cols, 1); + MatrixXd alphay = MatrixXd::Constant(rows, cols, 1); + alphax.block(0, 0, 100, cols) = MatrixXd::Constant(100, cols, 0.5); + alphax.block(100, 0, 100, cols) = MatrixXd::Constant(100, cols, 0.8); + alphay.block(0, 0, rows, 200) = MatrixXd::Constant(rows, 200, 0.6); + alphay.block(0, 200, rows, 200) = MatrixXd::Constant(rows, 200, 0.9); + grid.setAlpha(alphax, alphay); + + // **** BOUNDARY **** + Boundary bc = Boundary(grid); + bc.setBoundarySideClosed(BC_SIDE_LEFT); + bc.setBoundarySideClosed(BC_SIDE_RIGHT); + bc.setBoundarySideClosed(BC_SIDE_TOP); + bc.setBoundarySideClosed(BC_SIDE_BOTTOM); + + // **** SIMULATION **** + Simulation simulation = Simulation(grid, bc); + simulation.setTimestep(0.2); + simulation.setIterations(750); + simulation.setOutputCSV(CSV_OUTPUT_ON); + simulation.setOutputConsole(CONSOLE_OUTPUT_OFF); + + // **** RUN SIMULATION **** + auto start = high_resolution_clock::now(); + simulation.run(); + auto stop = high_resolution_clock::now(); + + auto duration = duration_cast(stop - start); + std::cout << duration.count() << std::endl; +} diff --git a/julia/tests/julia_bench/FTCS_1024_1000_100.jl b/julia/tests/julia_bench/FTCS_1024_1000_100.jl new file mode 100644 index 0000000..e7bb657 --- /dev/null +++ b/julia/tests/julia_bench/FTCS_1024_1000_100.jl @@ -0,0 +1,41 @@ +include("../../tug/Simulation.jl") + +function main() + # **** GRID **** + rows::Int = 1024 + cols::Int = 1000 + + alphaX = fill(1.25, rows, cols) + alphaY = fill(1.1, rows, cols) + alphaX[1:100, :] .= 0.5 + alphaX[101:200, :] .= 0.8 + alphaY[:, 1:200] .= 0.6 + alphaY[:, 201:400] .= 0.9 + grid::Grid = Grid{Float64}(rows, cols, alphaX, alphaY) + + concentrations = fill(0.5, rows, cols) + concentrations[11, 11] = 15000 + concentrations[1015, 991] = 7500 + concentrations[11, 991] = 7500 + concentrations[1015, 11] = 7500 + setConcentrations!(grid, concentrations) + + # **** BOUNDARY **** + bc::Boundary = Boundary(grid) + setBoundarySideClosed!(bc, LEFT) + setBoundarySideClosed!(bc, RIGHT) + setBoundarySideClosed!(bc, TOP) + setBoundarySideClosed!(bc, BOTTOM) + + # **** SIMULATION **** + simulation::Simulation = Simulation(grid, bc, FTCS) + simulation = setTimestep(simulation, 0.01) + simulation = setIterations(simulation, 100) + simulation = setOutputConsole(simulation, CONSOLE_OUTPUT_OFF) + simulation = setOutputCSV(simulation, CSV_OUTPUT_ON) + + # **** RUN SIMULATION **** + print((@elapsed run(simulation)) * 1e9) +end + +main() diff --git a/julia/tests/julia_bench/FTCS_1_20_7000.jl b/julia/tests/julia_bench/FTCS_1_20_7000.jl new file mode 100644 index 0000000..780e80f --- /dev/null +++ b/julia/tests/julia_bench/FTCS_1_20_7000.jl @@ -0,0 +1,30 @@ +include("../../tug/Simulation.jl") + +function main() + # **** GRID **** + cells::Int = 20 + + alpha = fill(1.0, 1, cells) + grid::Grid = Grid{Float64}(cells, alpha) + + concentrations = fill(0.0, 1, cells) + concentrations[1] = 2000 + setConcentrations!(grid, concentrations) + + # **** BOUNDARY **** + bc::Boundary = Boundary(grid) + setBoundarySideConstant!(bc, LEFT, 0.0) + setBoundarySideConstant!(bc, RIGHT, 0.0) + + # **** SIMULATION **** + simulation::Simulation = Simulation(grid, bc, FTCS) + simulation = setTimestep(simulation, 0.001) + simulation = setIterations(simulation, 7000) + simulation = setOutputConsole(simulation, CONSOLE_OUTPUT_OFF) + simulation = setOutputCSV(simulation, CSV_OUTPUT_ON) + + # **** RUN SIMULATION **** + print((@elapsed run(simulation)) * 1e9) +end + +main() diff --git a/julia/tests/julia_bench/FTCS_2027_1999_200.jl b/julia/tests/julia_bench/FTCS_2027_1999_200.jl new file mode 100644 index 0000000..0d8c81d --- /dev/null +++ b/julia/tests/julia_bench/FTCS_2027_1999_200.jl @@ -0,0 +1,38 @@ +include("../../tug/Simulation.jl") + +function main() + # **** GRID **** + rows::Int = 2027 + cols::Int = 1999 + + alphaX = [sin(i / 100) * cos(j / 100) for i in 1:rows, j in 1:cols] + alphaY = [cos(i / 100) * sin(j / 100) for i in 1:rows, j in 1:cols] + + grid::Grid = Grid{Float64}(rows, cols, alphaX, alphaY) + + concentrations = [i * j / 1e6 for i in 1:rows, j in 1:cols] + concentrations[11, 11] = 15000 + concentrations[2021, 1995] = 7500 + concentrations[11, 1995] = 7500 + concentrations[2021, 11] = 7500 + setConcentrations!(grid, concentrations) + + # **** BOUNDARY **** + bc::Boundary = Boundary(grid) + setBoundarySideClosed!(bc, LEFT) + setBoundarySideConstant!(bc, RIGHT, 1.5) + setBoundarySideClosed!(bc, TOP) + setBoundarySideConstant!(bc, BOTTOM, 0.75) + + # **** SIMULATION **** + simulation::Simulation = Simulation(grid, bc, FTCS) + simulation = setTimestep(simulation, 0.005) + simulation = setIterations(simulation, 200) + simulation = setOutputConsole(simulation, CONSOLE_OUTPUT_OFF) + simulation = setOutputCSV(simulation, CSV_OUTPUT_ON) + + # **** RUN SIMULATION **** + print((@elapsed run(simulation)) * 1e9) +end + +main() diff --git a/julia/tests/julia_bench/FTCS_20_20_500.jl b/julia/tests/julia_bench/FTCS_20_20_500.jl new file mode 100644 index 0000000..cdc9979 --- /dev/null +++ b/julia/tests/julia_bench/FTCS_20_20_500.jl @@ -0,0 +1,34 @@ +include("../../tug/Simulation.jl") + +function main() + # **** GRID **** + rows::Int = 20 + cols::Int = 20 + + alphaX = fill(1.0, rows, cols) + alphaY = fill(1.0, rows, cols) + grid::Grid = Grid{Float64}(rows, cols, alphaX, alphaY) + + concentrations = fill(0.0, rows, cols) + concentrations[11, 11] = 2000 + setConcentrations!(grid, concentrations) + + # **** BOUNDARY **** + bc::Boundary = Boundary(grid) + setBoundarySideClosed!(bc, LEFT) + setBoundarySideClosed!(bc, RIGHT) + setBoundarySideClosed!(bc, TOP) + setBoundarySideClosed!(bc, BOTTOM) + + # **** SIMULATION **** + simulation::Simulation = Simulation(grid, bc, FTCS) + simulation = setTimestep(simulation, 0.1) + simulation = setIterations(simulation, 500) + simulation = setOutputConsole(simulation, CONSOLE_OUTPUT_OFF) + simulation = setOutputCSV(simulation, CSV_OUTPUT_ON) + + # **** RUN SIMULATION **** + print((@elapsed run(simulation)) * 1e9) +end + +main() diff --git a/julia/tests/julia_bench/FTCS_450_670_750.jl b/julia/tests/julia_bench/FTCS_450_670_750.jl new file mode 100644 index 0000000..a3fa145 --- /dev/null +++ b/julia/tests/julia_bench/FTCS_450_670_750.jl @@ -0,0 +1,42 @@ +include("../../tug/Simulation.jl") + +function main() + # **** GRID **** + rows::Int = 450 + cols::Int = 670 + + alphaX = fill(1.0, rows, cols) + alphaY = fill(1.0, rows, cols) + alphaX[1:100, :] .= 0.5 + alphaX[101:200, :] .= 0.8 + alphaY[:, 1:200] .= 0.6 + alphaY[:, 201:400] .= 0.9 + grid::Grid = Grid{Float64}(rows, cols, alphaX, alphaY) + + concentrations = fill(0.0, rows, cols) + concentrations[11, 11] = 1500 + concentrations[441, 661] = 750 + concentrations[441, 11] = 750 + concentrations[11, 661] = 750 + concentrations[221, 336] = 1500 + setConcentrations!(grid, concentrations) + + # **** BOUNDARY **** + bc::Boundary = Boundary(grid) + setBoundarySideClosed!(bc, LEFT) + setBoundarySideClosed!(bc, RIGHT) + setBoundarySideClosed!(bc, TOP) + setBoundarySideClosed!(bc, BOTTOM) + + # **** SIMULATION **** + simulation::Simulation = Simulation(grid, bc, FTCS) + simulation = setTimestep(simulation, 0.2) + simulation = setIterations(simulation, 750) + simulation = setOutputConsole(simulation, CONSOLE_OUTPUT_OFF) + simulation = setOutputCSV(simulation, CSV_OUTPUT_ON) + + # **** RUN SIMULATION **** + print((@elapsed run(simulation)) * 1e9) +end + +main() diff --git a/julia/tests/test.py b/julia/tests/test.py index 75d310e..9f1360c 100644 --- a/julia/tests/test.py +++ b/julia/tests/test.py @@ -33,7 +33,7 @@ def format_difference(diff): threshold = 1e-5 if diff != 0: if abs(diff) < threshold: - return '{:.2e}'.format(diff).rjust(6) # Scientific notation for small values + return '{:.2e}'.format(diff).rjust(6) # Scientific notation for small values else: return '{:.3f}'.format(diff).rjust(6) # Fixed-point notation for larger values else: @@ -46,7 +46,8 @@ def run_benchmark(command, runs, precompile=False): output = subprocess.run(command, capture_output=True, text=True) elapsed = time.perf_counter() - start_time if precompile: - times.append(float(output.stdout)*1e-9) # Convert from ns to s + out = output.stdout.splitlines()[-1] # Take the second to last line if there are new line symbols + times.append(float(out)*1e-9) # Convert from nanoseconds to seconds else: times.append(elapsed)