From a796acbc1dcb0089baa7a5ab416903c21f93c303 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Max=20L=C3=BCbke?= Date: Wed, 12 Jun 2024 12:19:46 +0200 Subject: [PATCH] BREAKING CHANGE: Rename Simulation to Diffusion chore: Cleanup of applications --- docs_sphinx/{Simulation.rst => Diffusion.rst} | 4 +- docs_sphinx/user.rst | 2 +- examples/BTCS_1D_proto_example.cpp | 51 ------ examples/BTCS_2D_proto_example.cpp | 5 +- examples/CMakeLists.txt | 17 +- examples/CRNI_2D_proto_example.cpp | 29 ---- examples/FTCS_1D_proto_example.cpp | 51 ------ examples/FTCS_2D_proto_closed_mdl.cpp | 6 +- examples/FTCS_2D_proto_example.cpp | 92 ---------- examples/FTCS_2D_proto_example_mdl.cpp | 6 +- examples/profiling_openmp.cpp | 70 -------- examples/profiling_speedup.cpp | 70 -------- examples/reference-FTCS_2D_closed.cpp | 53 ------ include/tug/Core/BaseSimulation.hpp | 135 +++++++++++++++ include/tug/Core/{ => Numeric}/BTCS.hpp | 4 +- include/tug/Core/{ => Numeric}/FTCS.hpp | 4 +- include/tug/{Simulation.hpp => Diffusion.hpp} | 157 +++--------------- naaice/BTCS_2D_NAAICE.cpp | 5 +- naaice/NAAICE_dble_vs_float.cpp | 7 +- test/CMakeLists.txt | 2 +- .../{testSimulation.cpp => testDiffusion.cpp} | 16 +- 21 files changed, 184 insertions(+), 602 deletions(-) rename docs_sphinx/{Simulation.rst => Diffusion.rst} (79%) delete mode 100644 examples/BTCS_1D_proto_example.cpp delete mode 100644 examples/CRNI_2D_proto_example.cpp delete mode 100644 examples/FTCS_1D_proto_example.cpp delete mode 100644 examples/FTCS_2D_proto_example.cpp delete mode 100644 examples/profiling_openmp.cpp delete mode 100644 examples/profiling_speedup.cpp delete mode 100644 examples/reference-FTCS_2D_closed.cpp create mode 100644 include/tug/Core/BaseSimulation.hpp rename include/tug/Core/{ => Numeric}/BTCS.hpp (99%) rename include/tug/Core/{ => Numeric}/FTCS.hpp (99%) rename include/tug/{Simulation.hpp => Diffusion.hpp} (74%) rename test/{testSimulation.cpp => testDiffusion.cpp} (91%) diff --git a/docs_sphinx/Simulation.rst b/docs_sphinx/Diffusion.rst similarity index 79% rename from docs_sphinx/Simulation.rst rename to docs_sphinx/Diffusion.rst index a461073..1ab1cc1 100644 --- a/docs_sphinx/Simulation.rst +++ b/docs_sphinx/Diffusion.rst @@ -1,4 +1,4 @@ -Simulation +Diffusion ========== .. doxygenenum:: tug::APPROACH @@ -7,4 +7,4 @@ Simulation .. doxygenenum:: tug::CONSOLE_OUTPUT .. doxygenenum:: tug::TIME_MEASURE -.. doxygenclass:: tug::Simulation +.. doxygenclass:: tug::Diffusion diff --git a/docs_sphinx/user.rst b/docs_sphinx/user.rst index ddd4941..cb9c2c8 100644 --- a/docs_sphinx/user.rst +++ b/docs_sphinx/user.rst @@ -5,4 +5,4 @@ User API Grid Boundary - Simulation + Diffusion diff --git a/examples/BTCS_1D_proto_example.cpp b/examples/BTCS_1D_proto_example.cpp deleted file mode 100644 index af9888e..0000000 --- a/examples/BTCS_1D_proto_example.cpp +++ /dev/null @@ -1,51 +0,0 @@ -#include -#include - -using namespace Eigen; -using namespace tug; - -int main(int argc, char *argv[]) { - // ************** - // **** GRID **** - // ************** - - // create a linear grid with 20 cells - int cells = 20; - Grid64 grid(cells); - - MatrixXd concentrations = MatrixXd::Constant(1, 20, 0); - concentrations(0, 0) = 2000; - // TODO add option to set concentrations with a vector in 1D case - grid.setConcentrations(concentrations); - - // ****************** - // **** BOUNDARY **** - // ****************** - - // create a boundary with constant values - Boundary bc = Boundary(grid); - bc.setBoundarySideConstant(BC_SIDE_LEFT, 0); - bc.setBoundarySideConstant(BC_SIDE_RIGHT, 0); - - // ************************ - // **** SIMULATION ENV **** - // ************************ - - // set up a simulation environment - Simulation simulation = Simulation(grid, bc); // grid,boundary - - // set the timestep of the simulation - simulation.setTimestep(0.1); // timestep - - // set the number of iterations - simulation.setIterations(100); - - // set kind of output [CSV_OUTPUT_OFF (default), CSV_OUTPUT_ON, - // CSV_OUTPUT_VERBOSE] - simulation.setOutputCSV(CSV_OUTPUT_VERBOSE); - - // **** RUN SIMULATION **** - - // run the simulation - simulation.run(); -} diff --git a/examples/BTCS_2D_proto_example.cpp b/examples/BTCS_2D_proto_example.cpp index 98acdb9..9cefd10 100644 --- a/examples/BTCS_2D_proto_example.cpp +++ b/examples/BTCS_2D_proto_example.cpp @@ -1,5 +1,5 @@ #include -#include +#include using namespace Eigen; using namespace tug; @@ -61,8 +61,7 @@ int main(int argc, char *argv[]) { // ************************ // set up a simulation environment - Simulation simulation = - Simulation(grid, bc); // grid,boundary + Diffusion simulation(grid, bc); // grid,boundary // set the timestep of the simulation simulation.setTimestep(0.1); // timestep diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index 0a3173d..3c8bf28 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -1,20 +1,7 @@ -add_executable(FTCS_1D_proto_example FTCS_1D_proto_example.cpp) -add_executable(FTCS_2D_proto_example FTCS_2D_proto_example.cpp) -add_executable(BTCS_1D_proto_example BTCS_1D_proto_example.cpp) add_executable(BTCS_2D_proto_example BTCS_2D_proto_example.cpp) -add_executable(CRNI_2D_proto_example CRNI_2D_proto_example.cpp) -add_executable(reference-FTCS_2D_closed reference-FTCS_2D_closed.cpp) -add_executable(profiling_openmp profiling_openmp.cpp) - -target_link_libraries(FTCS_1D_proto_example tug) -target_link_libraries(FTCS_2D_proto_example tug) -target_link_libraries(BTCS_1D_proto_example tug) -target_link_libraries(BTCS_2D_proto_example tug) -target_link_libraries(CRNI_2D_proto_example tug) -target_link_libraries(reference-FTCS_2D_closed tug) -target_link_libraries(profiling_openmp tug) - add_executable(FTCS_2D_proto_example_mdl FTCS_2D_proto_example_mdl.cpp) add_executable(FTCS_2D_proto_closed_mdl FTCS_2D_proto_closed_mdl.cpp) + +target_link_libraries(BTCS_2D_proto_example tug) target_link_libraries(FTCS_2D_proto_closed_mdl tug) target_link_libraries(FTCS_2D_proto_example_mdl tug) diff --git a/examples/CRNI_2D_proto_example.cpp b/examples/CRNI_2D_proto_example.cpp deleted file mode 100644 index fc3f046..0000000 --- a/examples/CRNI_2D_proto_example.cpp +++ /dev/null @@ -1,29 +0,0 @@ -#include -#include - -using namespace Eigen; -using namespace tug; - -int main(int argc, char *argv[]) { - int row = 20; - int col = 20; - Grid64 grid(row, col); - - MatrixXd concentrations = MatrixXd::Constant(row, col, 0); - concentrations(10, 10) = 2000; - grid.setConcentrations(concentrations); - - 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(grid, bc); - simulation.setTimestep(0.1); - simulation.setIterations(50); - simulation.setOutputCSV(CSV_OUTPUT_XTREME); - - simulation.run(); -} diff --git a/examples/FTCS_1D_proto_example.cpp b/examples/FTCS_1D_proto_example.cpp deleted file mode 100644 index b3dc905..0000000 --- a/examples/FTCS_1D_proto_example.cpp +++ /dev/null @@ -1,51 +0,0 @@ -#include "tug/Boundary.hpp" -#include -#include - -using namespace Eigen; -using namespace tug; - -int main(int argc, char *argv[]) { - // ************** - // **** GRID **** - // ************** - - // create a linear grid with 20 cells - int cells = 20; - Grid64 grid(cells); - - MatrixXd concentrations = MatrixXd::Constant(1, 20, 20); - grid.setConcentrations(concentrations); - - // ****************** - // **** BOUNDARY **** - // ****************** - - // create a boundary with constant values - Boundary bc = Boundary(grid); - bc.setBoundarySideConstant(BC_SIDE_LEFT, 1); - bc.setBoundarySideConstant(BC_SIDE_RIGHT, 1); - - // ************************ - // **** SIMULATION ENV **** - // ************************ - - // set up a simulation environment - Simulation simulation = - Simulation(grid, bc); // grid,boundary,simulation-approach - - // (optional) set the timestep of the simulation - simulation.setTimestep(0.1); // timestep - - // (optional) set the number of iterations - simulation.setIterations(100); - - // (optional) set kind of output [CSV_OUTPUT_OFF (default), CSV_OUTPUT_ON, - // CSV_OUTPUT_VERBOSE] - simulation.setOutputCSV(CSV_OUTPUT_OFF); - - // **** RUN SIMULATION **** - - // run the simulation - simulation.run(); -} diff --git a/examples/FTCS_2D_proto_closed_mdl.cpp b/examples/FTCS_2D_proto_closed_mdl.cpp index c6ed02f..a5bd8f4 100644 --- a/examples/FTCS_2D_proto_closed_mdl.cpp +++ b/examples/FTCS_2D_proto_closed_mdl.cpp @@ -9,7 +9,7 @@ #include #include #include -#include +#include using namespace Eigen; using namespace tug; @@ -69,8 +69,8 @@ int main(int argc, char *argv[]) { // ************************ // set up a simulation environment - Simulation simulation = - Simulation(grid, bc); // grid,boundary,simulation-approach + Diffusion simulation( + grid, bc); // grid,boundary,simulation-approach // set the timestep of the simulation simulation.setTimestep(10000); // timestep diff --git a/examples/FTCS_2D_proto_example.cpp b/examples/FTCS_2D_proto_example.cpp deleted file mode 100644 index 9b8987a..0000000 --- a/examples/FTCS_2D_proto_example.cpp +++ /dev/null @@ -1,92 +0,0 @@ -/** - * @file FTCS_2D_proto_example.cpp - * @author Hannes Signer, Philipp Ungrund - * @brief Creates a prototypical standard TUG simulation in 2D with FTCS - * approach and constant boundary condition - * - */ - -#include -#include - -using namespace Eigen; -using namespace tug; -// #include -// #define EASY_PROFILER_ENABLE ::profiler::setEnabled(true); - -int main(int argc, char *argv[]) { - // EASY_PROFILER_ENABLE; - // profiler::startListen(); - // ************** - // **** GRID **** - // ************** - // profiler::startListen(); - // create a grid with a 20 x 20 field - int row = 20; - int col = 20; - Grid64 grid(row, col); - - // (optional) set the domain, e.g.: - // grid.setDomain(20, 20); - - // (optional) set the concentrations, e.g.: - // MatrixXd concentrations = MatrixXd::Constant(20,20,1000); // - // #row,#col,value grid.setConcentrations(concentrations); - MatrixXd concentrations = MatrixXd::Constant(row, col, 0); - concentrations(0, 0) = 1999; - grid.setConcentrations(concentrations); - - // (optional) set alphas of the grid, e.g.: - // MatrixXd alphax = MatrixXd::Constant(20,20,1); // row,col,value - // MatrixXd alphay = MatrixXd::Constant(20,20,1); // row,col,value - // grid.setAlpha(alphax, alphay); - - // ****************** - // **** BOUNDARY **** - // ****************** - - // create a boundary with constant values - Boundary bc = Boundary(grid); - bc.setBoundarySideConstant(BC_SIDE_LEFT, 0); - bc.setBoundarySideConstant(BC_SIDE_RIGHT, 0); - bc.setBoundarySideConstant(BC_SIDE_TOP, 0); - bc.setBoundarySideConstant(BC_SIDE_BOTTOM, 0); - - // (optional) set boundary condition values for one side, e.g.: - // VectorXd bc_left_values = VectorXd::Constant(20,1); // length,value - // bc.setBoundaryConditionValue(BC_SIDE_LEFT, bc_left_values); // side,values - // VectorXd bc_zero_values = VectorXd::Constant(20,0); - // bc.setBoundaryConditionValue(BC_SIDE_LEFT, bc_zero_values); - // bc.setBoundaryConditionValue(BC_SIDE_RIGHT, bc_zero_values); - // VectorXd bc_front_values = VectorXd::Constant(20,2000); - // bc.setBoundaryConditionValue(BC_SIDE_TOP, bc_front_values); - // bc.setBoundaryConditionValue(BC_SIDE_BOTTOM, bc_zero_values); - - // ************************ - // **** SIMULATION ENV **** - // ************************ - - // set up a simulation environment - Simulation simulation = - Simulation(grid, bc); // grid,boundary,simulation-approach - - // set the timestep of the simulation - simulation.setTimestep(0.1); // timestep - - // set the number of iterations - simulation.setIterations(10000); - - // set kind of output [CSV_OUTPUT_OFF (default), CSV_OUTPUT_ON, - // CSV_OUTPUT_VERBOSE] - simulation.setOutputCSV(CSV_OUTPUT_VERBOSE); - - // **** RUN SIMULATION **** - - // run the simulation - - // EASY_BLOCK("SIMULATION") - simulation.run(); - // EASY_END_BLOCK; - // profiler::dumpBlocksToFile("test_profile.prof"); - // profiler::stopListen(); -} diff --git a/examples/FTCS_2D_proto_example_mdl.cpp b/examples/FTCS_2D_proto_example_mdl.cpp index fdf40d1..3f750eb 100644 --- a/examples/FTCS_2D_proto_example_mdl.cpp +++ b/examples/FTCS_2D_proto_example_mdl.cpp @@ -7,7 +7,7 @@ */ #include -#include +#include using namespace Eigen; using namespace tug; @@ -60,8 +60,8 @@ int main(int argc, char *argv[]) { // ************************ // set up a simulation environment - Simulation simulation = - Simulation(grid, bc); // grid,boundary,simulation-approach + Diffusion simulation( + grid, bc); // grid,boundary,simulation-approach // (optional) set the timestep of the simulation simulation.setTimestep(1000); // timestep diff --git a/examples/profiling_openmp.cpp b/examples/profiling_openmp.cpp deleted file mode 100644 index 8ad62e0..0000000 --- a/examples/profiling_openmp.cpp +++ /dev/null @@ -1,70 +0,0 @@ -#include -#include -#include -#include -#include -#include - -using namespace Eigen; -using namespace std; -using namespace tug; - -int main(int argc, char *argv[]) { - - int n[] = {2000}; - int threads[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; - int iterations[1] = {1}; - int repetition = 10; - - for (int l = 0; l < size(threads); l++) { - // string filename = "ftcs_openmp_" + to_string(threads[l]) + ".csv"; - ofstream myfile; - myfile.open("speedup_1000.csv", std::ios::app); - myfile << "Number threads: " << threads[l] << endl; - - for (int i = 0; i < size(n); i++) { - cout << "Grid size: " << n[i] << " x " << n[i] << endl << endl; - // myfile << "Grid size: " << n[i] << " x " << n[i] << endl << endl; - for (int j = 0; j < size(iterations); j++) { - cout << "Iterations: " << iterations[j] << endl; - // myfile << "Iterations: " << iterations[j] << endl; - for (int k = 0; k < repetition; k++) { - cout << "Wiederholung: " << k << endl; - Grid64 grid(n[i], n[i]); - grid.setDomain(1, 1); - - MatrixXd concentrations = MatrixXd::Constant(n[i], n[i], 0); - concentrations(n[i] / 2, n[i] / 2) = 1; - grid.setConcentrations(concentrations); - MatrixXd alpha = MatrixXd::Constant(n[i], n[i], 0.5); - - Boundary bc = Boundary(grid); - - Simulation sim = Simulation(grid, bc); - - if (argc == 2) { - int numThreads = atoi(argv[1]); - sim.setNumberThreads(numThreads); - } else { - sim.setNumberThreads(threads[l]); - } - - sim.setTimestep(0.01); - sim.setIterations(iterations[j]); - sim.setOutputCSV(CSV_OUTPUT_OFF); - - auto begin = std::chrono::high_resolution_clock::now(); - sim.run(); - auto end = std::chrono::high_resolution_clock::now(); - auto milliseconds = - std::chrono::duration_cast(end - - begin); - myfile << milliseconds.count() << endl; - } - } - cout << endl; - myfile << endl; - } - myfile.close(); - } -} diff --git a/examples/profiling_speedup.cpp b/examples/profiling_speedup.cpp deleted file mode 100644 index 8ad62e0..0000000 --- a/examples/profiling_speedup.cpp +++ /dev/null @@ -1,70 +0,0 @@ -#include -#include -#include -#include -#include -#include - -using namespace Eigen; -using namespace std; -using namespace tug; - -int main(int argc, char *argv[]) { - - int n[] = {2000}; - int threads[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; - int iterations[1] = {1}; - int repetition = 10; - - for (int l = 0; l < size(threads); l++) { - // string filename = "ftcs_openmp_" + to_string(threads[l]) + ".csv"; - ofstream myfile; - myfile.open("speedup_1000.csv", std::ios::app); - myfile << "Number threads: " << threads[l] << endl; - - for (int i = 0; i < size(n); i++) { - cout << "Grid size: " << n[i] << " x " << n[i] << endl << endl; - // myfile << "Grid size: " << n[i] << " x " << n[i] << endl << endl; - for (int j = 0; j < size(iterations); j++) { - cout << "Iterations: " << iterations[j] << endl; - // myfile << "Iterations: " << iterations[j] << endl; - for (int k = 0; k < repetition; k++) { - cout << "Wiederholung: " << k << endl; - Grid64 grid(n[i], n[i]); - grid.setDomain(1, 1); - - MatrixXd concentrations = MatrixXd::Constant(n[i], n[i], 0); - concentrations(n[i] / 2, n[i] / 2) = 1; - grid.setConcentrations(concentrations); - MatrixXd alpha = MatrixXd::Constant(n[i], n[i], 0.5); - - Boundary bc = Boundary(grid); - - Simulation sim = Simulation(grid, bc); - - if (argc == 2) { - int numThreads = atoi(argv[1]); - sim.setNumberThreads(numThreads); - } else { - sim.setNumberThreads(threads[l]); - } - - sim.setTimestep(0.01); - sim.setIterations(iterations[j]); - sim.setOutputCSV(CSV_OUTPUT_OFF); - - auto begin = std::chrono::high_resolution_clock::now(); - sim.run(); - auto end = std::chrono::high_resolution_clock::now(); - auto milliseconds = - std::chrono::duration_cast(end - - begin); - myfile << milliseconds.count() << endl; - } - } - cout << endl; - myfile << endl; - } - myfile.close(); - } -} diff --git a/examples/reference-FTCS_2D_closed.cpp b/examples/reference-FTCS_2D_closed.cpp deleted file mode 100644 index f08c689..0000000 --- a/examples/reference-FTCS_2D_closed.cpp +++ /dev/null @@ -1,53 +0,0 @@ -#include "Eigen/Core" -#include -#include - -using namespace std; -using namespace Eigen; -using namespace tug; - -int main(int argc, char *argv[]) { - int row = 50; - int col = 50; - int domain_row = 10; - int domain_col = 10; - - // Grid - Grid64 grid(row, col); - grid.setDomain(domain_row, domain_col); - - MatrixXd concentrations = MatrixXd::Constant(row, col, 0); - concentrations(5, 5) = 1; - grid.setConcentrations(concentrations); - - MatrixXd alpha = MatrixXd::Constant(row, col, 1); - for (int i = 0; i < 5; i++) { - for (int j = 0; j < 6; j++) { - alpha(i, j) = 0.01; - } - } - for (int i = 0; i < 5; i++) { - for (int j = 6; j < 11; j++) { - alpha(i, j) = 0.001; - } - } - for (int i = 5; i < 11; i++) { - for (int j = 6; j < 11; j++) { - alpha(i, j) = 0.1; - } - } - grid.setAlpha(alpha, alpha); - - // Boundary - Boundary bc = Boundary(grid); - - // Simulation - Simulation sim = Simulation(grid, bc); - sim.setTimestep(0.001); - sim.setIterations(10000); - sim.setOutputCSV(CSV_OUTPUT_OFF); - sim.setOutputConsole(CONSOLE_OUTPUT_OFF); - - // RUN - sim.run(); -} diff --git a/include/tug/Core/BaseSimulation.hpp b/include/tug/Core/BaseSimulation.hpp new file mode 100644 index 0000000..5c6ca11 --- /dev/null +++ b/include/tug/Core/BaseSimulation.hpp @@ -0,0 +1,135 @@ +#pragma once + +#include + +namespace tug { + +/** + * @brief Enum holding different options for .csv output. + * + */ +enum CSV_OUTPUT { + CSV_OUTPUT_OFF, /*!< do not produce csv output */ + CSV_OUTPUT_ON, /*!< produce csv output with last concentration matrix */ + CSV_OUTPUT_VERBOSE, /*!< produce csv output with all concentration matrices */ + CSV_OUTPUT_XTREME /*!< csv output like VERBOSE but additional boundary + conditions at beginning */ +}; + +/** + * @brief Enum holding different options for console output. + * + */ +enum CONSOLE_OUTPUT { + CONSOLE_OUTPUT_OFF, /*!< do not print any output to console */ + CONSOLE_OUTPUT_ON, /*!< print before and after concentrations to console */ + CONSOLE_OUTPUT_VERBOSE /*!< print all concentration matrices to console */ +}; + +/** + * @brief Enum holding different options for time measurement. + * + */ +enum TIME_MEASURE { + TIME_MEASURE_OFF, /*!< do not print any time measures */ + TIME_MEASURE_ON /*!< print time measure after last iteration */ +}; + +class BaseSimulation { +protected: + CSV_OUTPUT csv_output{CSV_OUTPUT_OFF}; + CONSOLE_OUTPUT console_output{CONSOLE_OUTPUT_OFF}; + TIME_MEASURE time_measure{TIME_MEASURE_OFF}; + + int iterations{1}; + +public: + /** + * @brief Set the option to output the results to a CSV file. Off by default. + * + * + * @param csv_output Valid output option. The following options can be set + * here: + * - CSV_OUTPUT_OFF: do not produce csv output + * - CSV_OUTPUT_ON: produce csv output with last + * concentration matrix + * - CSV_OUTPUT_VERBOSE: produce csv output with all + * concentration matrices + * - CSV_OUTPUT_XTREME: produce csv output with all + * concentration matrices and simulation environment + */ + void setOutputCSV(CSV_OUTPUT csv_output) { + if (csv_output < CSV_OUTPUT_OFF && csv_output > CSV_OUTPUT_VERBOSE) { + throw std::invalid_argument("Invalid CSV output option given!"); + } + + this->csv_output = csv_output; + } + + /** + * @brief Set the options for outputting information to the console. Off by + * default. + * + * @param console_output Valid output option. The following options can be set + * here: + * - CONSOLE_OUTPUT_OFF: do not print any output to + * console + * - CONSOLE_OUTPUT_ON: print before and after + * concentrations to console + * - CONSOLE_OUTPUT_VERBOSE: print all concentration + * matrices to console + */ + void setOutputConsole(CONSOLE_OUTPUT console_output) { + if (console_output < CONSOLE_OUTPUT_OFF && + console_output > CONSOLE_OUTPUT_VERBOSE) { + throw std::invalid_argument("Invalid console output option given!"); + } + + this->console_output = console_output; + } + + /** + * @brief Set the Time Measure option. Off by default. + * + * @param time_measure The following options are allowed: + * - TIME_MEASURE_OFF: Time of simulation is not printed + * to console + * - TIME_MEASURE_ON: Time of simulation run is printed to + * console + */ + void setTimeMeasure(TIME_MEASURE time_measure) { + if (time_measure < TIME_MEASURE_OFF && time_measure > TIME_MEASURE_ON) { + throw std::invalid_argument("Invalid time measure option given!"); + } + + this->time_measure = time_measure; + } + + /** + * @brief Set the desired iterations to be calculated. A value greater + * than zero must be specified here. Setting iterations is required. + * + * @param iterations Number of iterations to be simulated. + */ + void setIterations(int iterations) { + if (iterations <= 0) { + throw std::invalid_argument( + "Number of iterations must be greater than zero."); + } + this->iterations = iterations; + } + + /** + * @brief Return the currently set iterations to be calculated. + * + * @return int Number of iterations. + */ + int getIterations() const { return this->iterations; } + + /** + * @brief Method starts the simulation process with the previously set + * parameters. + */ + virtual void run() = 0; +}; +} // namespace tug \ No newline at end of file diff --git a/include/tug/Core/BTCS.hpp b/include/tug/Core/Numeric/BTCS.hpp similarity index 99% rename from include/tug/Core/BTCS.hpp rename to include/tug/Core/Numeric/BTCS.hpp index a1081af..3aca4a8 100644 --- a/include/tug/Core/BTCS.hpp +++ b/include/tug/Core/Numeric/BTCS.hpp @@ -10,8 +10,8 @@ #ifndef BTCS_H_ #define BTCS_H_ -#include "Matrix.hpp" -#include "TugUtils.hpp" +#include "../Matrix.hpp" +#include "../TugUtils.hpp" #include #include diff --git a/include/tug/Core/FTCS.hpp b/include/tug/Core/Numeric/FTCS.hpp similarity index 99% rename from include/tug/Core/FTCS.hpp rename to include/tug/Core/Numeric/FTCS.hpp index a3012b3..41eeb80 100644 --- a/include/tug/Core/FTCS.hpp +++ b/include/tug/Core/Numeric/FTCS.hpp @@ -8,10 +8,8 @@ #ifndef FTCS_H_ #define FTCS_H_ -#include "TugUtils.hpp" +#include "../TugUtils.hpp" -#include -#include #include #ifdef _OPENMP diff --git a/include/tug/Simulation.hpp b/include/tug/Diffusion.hpp similarity index 74% rename from include/tug/Simulation.hpp rename to include/tug/Diffusion.hpp index 7f8632d..4d2f0f0 100644 --- a/include/tug/Simulation.hpp +++ b/include/tug/Diffusion.hpp @@ -1,13 +1,12 @@ /** - * @file Simulation.hpp - * @brief API of Simulation class, that holds all information regarding a + * @file Diffusion.hpp + * @brief API of Diffusion class, that holds all information regarding a * specific simulation run like its timestep, number of iterations and output - * options. Simulation object also holds a predefined Grid and Boundary object. + * options. Diffusion object also holds a predefined Grid and Boundary object. * */ -#ifndef SIMULATION_H_ -#define SIMULATION_H_ +#pragma once #include "Boundary.hpp" #include "Grid.hpp" @@ -19,9 +18,10 @@ #include #include -#include "Core/BTCS.hpp" -#include "Core/FTCS.hpp" +#include "Core/Numeric/BTCS.hpp" +#include "Core/Numeric/FTCS.hpp" #include "Core/TugUtils.hpp" +#include "tug/Core/BaseSimulation.hpp" #ifdef _OPENMP #include @@ -51,37 +51,6 @@ enum SOLVER { tridiagonal matrices */ }; -/** - * @brief Enum holding different options for .csv output. - * - */ -enum CSV_OUTPUT { - CSV_OUTPUT_OFF, /*!< do not produce csv output */ - CSV_OUTPUT_ON, /*!< produce csv output with last concentration matrix */ - CSV_OUTPUT_VERBOSE, /*!< produce csv output with all concentration matrices */ - CSV_OUTPUT_XTREME /*!< csv output like VERBOSE but additional boundary - conditions at beginning */ -}; - -/** - * @brief Enum holding different options for console output. - * - */ -enum CONSOLE_OUTPUT { - CONSOLE_OUTPUT_OFF, /*!< do not print any output to console */ - CONSOLE_OUTPUT_ON, /*!< print before and after concentrations to console */ - CONSOLE_OUTPUT_VERBOSE /*!< print all concentration matrices to console */ -}; - -/** - * @brief Enum holding different options for time measurement. - * - */ -enum TIME_MEASURE { - TIME_MEASURE_OFF, /*!< do not print any time measures */ - TIME_MEASURE_ON /*!< print time measure after last iteration */ -}; - /** * @brief The class forms the interface for performing the diffusion simulations * and contains all the methods for controlling the desired parameters, such as @@ -94,7 +63,17 @@ enum TIME_MEASURE { */ template -class Simulation { +class Diffusion : public BaseSimulation { +private: + T timestep{-1}; + int innerIterations{1}; + int numThreads{omp_get_num_procs()}; + + Grid &grid; + Boundary &bc; + + const std::vector approach_names = {"FTCS", "BTCS", "CRNI"}; + public: /** * @brief Set up a simulation environment. The timestep and number of @@ -108,68 +87,7 @@ public: * @param bc Valid boundary condition object * @param approach Approach to solving the problem. Either FTCS or BTCS. */ - Simulation(Grid &_grid, Boundary &_bc) : grid(_grid), bc(_bc){}; - - /** - * @brief Set the option to output the results to a CSV file. Off by default. - * - * - * @param csv_output Valid output option. The following options can be set - * here: - * - CSV_OUTPUT_OFF: do not produce csv output - * - CSV_OUTPUT_ON: produce csv output with last - * concentration matrix - * - CSV_OUTPUT_VERBOSE: produce csv output with all - * concentration matrices - * - CSV_OUTPUT_XTREME: produce csv output with all - * concentration matrices and simulation environment - */ - void setOutputCSV(CSV_OUTPUT csv_output) { - if (csv_output < CSV_OUTPUT_OFF && csv_output > CSV_OUTPUT_VERBOSE) { - throw std::invalid_argument("Invalid CSV output option given!"); - } - - this->csv_output = csv_output; - } - - /** - * @brief Set the options for outputting information to the console. Off by - * default. - * - * @param console_output Valid output option. The following options can be set - * here: - * - CONSOLE_OUTPUT_OFF: do not print any output to - * console - * - CONSOLE_OUTPUT_ON: print before and after - * concentrations to console - * - CONSOLE_OUTPUT_VERBOSE: print all concentration - * matrices to console - */ - void setOutputConsole(CONSOLE_OUTPUT console_output) { - if (console_output < CONSOLE_OUTPUT_OFF && - console_output > CONSOLE_OUTPUT_VERBOSE) { - throw std::invalid_argument("Invalid console output option given!"); - } - - this->console_output = console_output; - } - - /** - * @brief Set the Time Measure option. Off by default. - * - * @param time_measure The following options are allowed: - * - TIME_MEASURE_OFF: Time of simulation is not printed - * to console - * - TIME_MEASURE_ON: Time of simulation run is printed to - * console - */ - void setTimeMeasure(TIME_MEASURE time_measure) { - if (time_measure < TIME_MEASURE_OFF && time_measure > TIME_MEASURE_ON) { - throw std::invalid_argument("Invalid time measure option given!"); - } - - this->time_measure = time_measure; - } + Diffusion(Grid &_grid, Boundary &_bc) : grid(_grid), bc(_bc){}; /** * @brief Setting the time step for each iteration step. Time step must be @@ -244,20 +162,6 @@ public: */ T getTimestep() const { return this->timestep; } - /** - * @brief Set the desired iterations to be calculated. A value greater - * than zero must be specified here. Setting iterations is required. - * - * @param iterations Number of iterations to be simulated. - */ - void setIterations(int iterations) { - if (iterations <= 0) { - throw std::invalid_argument( - "Number of iterations must be greater than zero."); - } - this->iterations = iterations; - } - /** * @brief Set the number of desired openMP Threads. * @@ -277,13 +181,6 @@ public: } } - /** - * @brief Return the currently set iterations to be calculated. - * - * @return int Number of iterations. - */ - int getIterations() const { return this->iterations; } - /** * @brief Outputs the current concentrations of the grid on the console. * @@ -393,7 +290,6 @@ public: auto begin = std::chrono::high_resolution_clock::now(); if constexpr (approach == FTCS_APPROACH) { // FTCS case - for (int i = 0; i < iterations * innerIterations; i++) { if (console_output == CONSOLE_OUTPUT_VERBOSE && i > 0) { printConcentrationsConsole(); @@ -485,20 +381,5 @@ public: << milliseconds.count() << "ms" << std::endl; } } - -private: - T timestep{-1}; - int iterations{-1}; - int innerIterations{1}; - int numThreads{omp_get_num_procs()}; - CSV_OUTPUT csv_output{CSV_OUTPUT_OFF}; - CONSOLE_OUTPUT console_output{CONSOLE_OUTPUT_OFF}; - TIME_MEASURE time_measure{TIME_MEASURE_OFF}; - - Grid &grid; - Boundary &bc; - - const std::vector approach_names = {"FTCS", "BTCS", "CRNI"}; }; } // namespace tug -#endif // SIMULATION_H_ diff --git a/naaice/BTCS_2D_NAAICE.cpp b/naaice/BTCS_2D_NAAICE.cpp index 9ed7ccb..f344893 100644 --- a/naaice/BTCS_2D_NAAICE.cpp +++ b/naaice/BTCS_2D_NAAICE.cpp @@ -5,8 +5,7 @@ #include #include #include -#include -#include +#include #include #include "files.hpp" @@ -141,7 +140,7 @@ int main(int argc, char *argv[]) { // // ************************ // set up a simulation environment - Simulation simulation = Simulation(grid, bc); // grid,boundary + Diffusion simulation(grid, bc); // grid,boundary // set the timestep of the simulation simulation.setTimestep(360); // timestep diff --git a/naaice/NAAICE_dble_vs_float.cpp b/naaice/NAAICE_dble_vs_float.cpp index ac3718c..c432f0f 100644 --- a/naaice/NAAICE_dble_vs_float.cpp +++ b/naaice/NAAICE_dble_vs_float.cpp @@ -7,11 +7,11 @@ #include #include #include -#include #include #include -#include "files.hpp" +#include +#include using namespace tug; @@ -142,8 +142,7 @@ template int doWork(int ngrid) { bc.setBoundarySideClosed(BC_SIDE_BOTTOM); // set up a simulation environment - Simulation Sim = - Simulation(grid, bc); // grid_64,boundary,simulation-approach + Diffusion Sim(grid, bc); // grid_64,boundary,simulation-approach // Sim64.setSolver(THOMAS_ALGORITHM_SOLVER); diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index b509370..78d67e2 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -10,7 +10,7 @@ FetchContent_MakeAvailable(googletest) add_executable(testTug -testSimulation.cpp +testDiffusion.cpp testGrid.cpp testFTCS.cpp testBoundary.cpp diff --git a/test/testSimulation.cpp b/test/testDiffusion.cpp similarity index 91% rename from test/testSimulation.cpp rename to test/testDiffusion.cpp index 6411412..79d0506 100644 --- a/test/testSimulation.cpp +++ b/test/testDiffusion.cpp @@ -1,7 +1,7 @@ #include "TestUtils.hpp" #include #include -#include +#include #include #include @@ -64,7 +64,7 @@ DIFFUSION_TEST(EqualityFTCS) { // Simulation - Simulation sim = Simulation(grid, bc); + Diffusion sim(grid, bc); // sim.setOutputConsole(CONSOLE_OUTPUT_ON); sim.setTimestep(timestep); sim.setIterations(iterations); @@ -84,7 +84,7 @@ DIFFUSION_TEST(EqualityBTCS) { Boundary bc = Boundary(grid); // Simulation - Simulation sim = Simulation(grid, bc); + Diffusion sim(grid, bc); // sim.setOutputConsole(CONSOLE_OUTPUT_ON); sim.setTimestep(timestep); sim.setIterations(iterations); @@ -99,16 +99,16 @@ DIFFUSION_TEST(InitializeEnvironment) { Grid64 grid(rc, rc); Boundary boundary(grid); - EXPECT_NO_THROW(Simulation sim(grid, boundary)); + EXPECT_NO_THROW(Diffusion sim(grid, boundary)); } DIFFUSION_TEST(SimulationEnvironment) { int rc = 12; Grid64 grid(rc, rc); Boundary boundary(grid); - Simulation sim(grid, boundary); + Diffusion sim(grid, boundary); - EXPECT_EQ(sim.getIterations(), -1); + EXPECT_EQ(sim.getIterations(), 1); EXPECT_NO_THROW(sim.setIterations(2000)); EXPECT_EQ(sim.getIterations(), 2000); @@ -139,7 +139,7 @@ DIFFUSION_TEST(ClosedBoundaries) { bc.setBoundarySideConstant(tug::BC_SIDE_TOP, 1.0); bc.setBoundarySideConstant(tug::BC_SIDE_BOTTOM, 1.0); - tug::Simulation sim(grid, bc); + tug::Diffusion sim(grid, bc); sim.setTimestep(1); sim.setIterations(1); @@ -166,7 +166,7 @@ DIFFUSION_TEST(ConstantInnerCell) { // inner bc.setInnerBoundary(2, 2, 0); - tug::Simulation sim(grid, bc); + tug::Diffusion sim(grid, bc); sim.setTimestep(1); sim.setIterations(1);