From fab0f35ed0e401f919534a4ac8776266efaf31b5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Max=20L=C3=BCbke?= Date: Wed, 6 Sep 2023 13:20:40 +0200 Subject: [PATCH] Add benchmark --- CMakeLists.txt | 6 ++ naaice/BTCS_2D_NAAICE.cpp | 150 ++++++++++++++++++++++++++++++++++++++ naaice/CMakeLists.txt | 9 +++ naaice/alphax.csv | 5 ++ naaice/files.hpp.in | 7 ++ naaice/init_conc.csv | 5 ++ src/CMakeLists.txt | 4 +- 7 files changed, 183 insertions(+), 3 deletions(-) create mode 100644 naaice/BTCS_2D_NAAICE.cpp create mode 100644 naaice/CMakeLists.txt create mode 100644 naaice/alphax.csv create mode 100644 naaice/files.hpp.in create mode 100644 naaice/init_conc.csv diff --git a/CMakeLists.txt b/CMakeLists.txt index a76e995..ec9da4c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -42,3 +42,9 @@ if(TUG_ENABLE_TESTING) endif() add_subdirectory(examples) + +option(TUG_NAAICE_EXAMPLE "Enables NAAICE examples with export of CSV files" OFF) + +if (TUG_NAAICE_EXAMPLE) + add_subdirectory(naaice) +endif() diff --git a/naaice/BTCS_2D_NAAICE.cpp b/naaice/BTCS_2D_NAAICE.cpp new file mode 100644 index 0000000..1b99849 --- /dev/null +++ b/naaice/BTCS_2D_NAAICE.cpp @@ -0,0 +1,150 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "files.hpp" + +template inline T parseString(const std::string &str) { + T result; + std::istringstream iss(str); + + if (!(iss >> result)) { + throw std::invalid_argument("Invalid input for parsing."); + } + + return result; +} + +template +std::vector tokenize(const std::string &input, char delimiter) { + std::vector tokens; + std::istringstream tokenStream(input); + std::string token; + + while (std::getline(tokenStream, token, delimiter)) { + tokens.push_back(parseString(token)); + } + + return tokens; +} + +template +std::vector> CSVToVector(const char *filename) { + std::ifstream in_file(filename); + if (!in_file.is_open()) { + throw std::runtime_error("Error opening file \'" + std::string(filename) + + "\'."); + } + + std::vector> csv_data; + + std::string line; + while (std::getline(in_file, line)) { + csv_data.push_back(tokenize(line, ',')); + } + + in_file.close(); + + return csv_data; +} + +template +Eigen::MatrixXd CMVecToRMMatrix(const std::vector> &vec, + std::uint32_t exp_rows, + std::uint32_t exp_cols) { + if (exp_rows != vec.size()) { + throw std::runtime_error( + "Mismatch in y dimension while converting to Eigen::Matrix."); + } + + Eigen::MatrixXd out_mat(exp_rows, exp_cols); + + for (std::uint32_t ri = 0; ri < exp_rows; ri++) { + const auto &vec_row = vec[ri]; + if (vec[ri].size() != exp_cols) { + throw std::runtime_error( + "Mismatch in x dimension while converting to Eigen::Matrix."); + } + for (std::uint32_t cj = 0; cj < exp_cols; cj++) { + out_mat(ri, cj) = vec_row[cj]; + } + } + + return out_mat; +} + +int main(int argc, char *argv[]) { + // EASY_PROFILER_ENABLE; + // profiler::startListen(); + // ************** + // **** GRID **** + // ************** + // profiler::startListen(); + // create a grid with a 5 x 10 field + constexpr int row = 5; + constexpr int col = 10; + Grid grid = Grid(row, col); + + // (optional) set the domain, e.g.: + grid.setDomain(0.005, 0.01); + + const auto init_values_vec = CSVToVector(INPUT_CONC_FILE); + MatrixXd concentrations = CMVecToRMMatrix(init_values_vec, row, col); + grid.setConcentrations(concentrations); + + // // (optional) set alphas of the grid, e.g.: + const auto alphax_vec = CSVToVector(INPUT_ALPHAX_FILE); + MatrixXd alphax = CMVecToRMMatrix(alphax_vec, row, col); + + constexpr double alphay_val = 5e-10; + MatrixXd alphay = MatrixXd::Constant(row, col, alphay_val); // row,col,value + grid.setAlpha(alphax, alphay); + + // // ****************** + // // **** BOUNDARY **** + // // ****************** + + // create a boundary with constant values + 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 ENV **** + // // ************************ + + // set up a simulation environment + Simulation simulation = Simulation( + grid, bc, BTCS_APPROACH); // grid,boundary,simulation-approach + + // set the timestep of the simulation + simulation.setTimestep(360); // timestep + + // set the number of iterations + simulation.setIterations(1); + + // set kind of output [CSV_OUTPUT_OFF (default), CSV_OUTPUT_ON, + // CSV_OUTPUT_VERBOSE] + simulation.setOutputCSV(CSV_OUTPUT_ON); + + simulation.setOutputConsole(CONSOLE_OUTPUT_ON); + + // // **** RUN SIMULATION **** + + // // run the simulation + + // // EASY_BLOCK("SIMULATION") + simulation.run(); + // // EASY_END_BLOCK; + // // profiler::dumpBlocksToFile("test_profile.prof"); + // // profiler::stopListen(); +} diff --git a/naaice/CMakeLists.txt b/naaice/CMakeLists.txt new file mode 100644 index 0000000..947e720 --- /dev/null +++ b/naaice/CMakeLists.txt @@ -0,0 +1,9 @@ +add_executable(naaice BTCS_2D_NAAICE.cpp) + +get_filename_component(IN_CONC_FILE "init_conc.csv" REALPATH) +get_filename_component(IN_ALPHAX_FILE "alphax.csv" REALPATH) + +configure_file(files.hpp.in files.hpp) +target_include_directories(naaice PRIVATE ${CMAKE_CURRENT_BINARY_DIR}) + +target_link_libraries(naaice PUBLIC tug) diff --git a/naaice/alphax.csv b/naaice/alphax.csv new file mode 100644 index 0000000..17db4c6 --- /dev/null +++ b/naaice/alphax.csv @@ -0,0 +1,5 @@ +9.35863547772169e-10,4.1475358910393e-10,7.40997499064542e-10,4.63898729439825e-10,5.50232032872736e-10,1.83670640387572e-10,5.84096802584827e-10,4.72976535512134e-10,2.92526249657385e-10,5.8247926668264e-10 +8.08492869278416e-10,3.5943602763582e-10,6.87254608655348e-10,1.52116895909421e-10,5.95404988038354e-10,8.90064929216169e-10,6.13143724552356e-10,1.23507722397335e-10,2.898759260308e-10,9.90528965741396e-10 +3.12359050870873e-10,6.34084914484993e-10,8.28234328492545e-10,1.60584925650619e-10,1.31777092232369e-10,7.34155903453939e-10,9.86383097898215e-10,5.42474469379522e-10,1.23030534153804e-10,2.33146838657558e-10 +7.76231796317734e-10,1.69479642040096e-10,6.74477553134784e-10,2.4903867142275e-10,2.70820381003432e-10,1.67315319390036e-10,7.1631961625535e-10,4.51548034301959e-10,2.41610987577587e-10,4.98075650655665e-10 +7.33895266707987e-10,5.82150935800746e-10,1.49088777275756e-10,5.00345975253731e-10,8.26257051993161e-10,5.28838745504618e-10,9.94136832957156e-10,7.44971914449707e-10,7.53557282453403e-10,7.54089470859617e-10 diff --git a/naaice/files.hpp.in b/naaice/files.hpp.in new file mode 100644 index 0000000..c7dd813 --- /dev/null +++ b/naaice/files.hpp.in @@ -0,0 +1,7 @@ +#ifndef FILES_H_ +#define FILES_H_ + +const char *INPUT_CONC_FILE = "@IN_CONC_FILE@"; +const char *INPUT_ALPHAX_FILE = "@IN_ALPHAX_FILE@"; + +#endif // FILES_H_ diff --git a/naaice/init_conc.csv b/naaice/init_conc.csv new file mode 100644 index 0000000..ad55817 --- /dev/null +++ b/naaice/init_conc.csv @@ -0,0 +1,5 @@ +6.92023e-07,6.92023e-07,6.92023e-07,6.92023e-07,6.92023e-07,2.02396e-08,2.02396e-08,2.02396e-08,2.02396e-08,2.02396e-08 +6.92023e-07,6.92023e-07,6.92023e-07,6.92023e-07,6.92023e-07,2.02396e-08,2.02396e-08,2.02396e-08,2.02396e-08,2.02396e-08 +6.92023e-07,6.92023e-07,6.92023e-07,6.92023e-07,6.92023e-07,2.02396e-08,2.02396e-08,2.02396e-08,2.02396e-08,2.02396e-08 +6.92023e-07,6.92023e-07,6.92023e-07,6.92023e-07,6.92023e-07,2.02396e-08,2.02396e-08,2.02396e-08,2.02396e-08,2.02396e-08 +6.92023e-07,6.92023e-07,6.92023e-07,6.92023e-07,6.92023e-07,2.02396e-08,2.02396e-08,2.02396e-08,2.02396e-08,2.02396e-08 diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 3cc79d4..04db461 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -1,8 +1,6 @@ add_library(tug Boundary.cpp Grid.cpp Simulation.cpp FTCS.cpp BTCSv2.cpp) -option(TUG_WRITE_CSV "Write CSV during Thomas algorithm with consecutive numbers for each call" OFF) - -IF(TUG_WRITE_CSV) +IF(TUG_NAAICE_EXAMPLE) target_compile_definitions(tug PRIVATE WRITE_THOMAS_CSV) endif()