Add benchmark

This commit is contained in:
Max Lübke 2023-09-06 13:20:40 +02:00
parent 7d05320f24
commit fab0f35ed0
7 changed files with 183 additions and 3 deletions

View File

@ -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()

150
naaice/BTCS_2D_NAAICE.cpp Normal file
View File

@ -0,0 +1,150 @@
#include <Eigen/Eigen>
#include <cstdint>
#include <fstream>
#include <iostream>
#include <ostream>
#include <stdexcept>
#include <string>
#include <string_view>
#include <tug/Simulation.hpp>
#include <vector>
#include "files.hpp"
template <typename T> 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 <typename T>
std::vector<T> tokenize(const std::string &input, char delimiter) {
std::vector<T> tokens;
std::istringstream tokenStream(input);
std::string token;
while (std::getline(tokenStream, token, delimiter)) {
tokens.push_back(parseString<T>(token));
}
return tokens;
}
template <typename T>
std::vector<std::vector<T>> 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<std::vector<T>> csv_data;
std::string line;
while (std::getline(in_file, line)) {
csv_data.push_back(tokenize<T>(line, ','));
}
in_file.close();
return csv_data;
}
template <typename T>
Eigen::MatrixXd CMVecToRMMatrix(const std::vector<std::vector<T>> &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<double>(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<double>(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();
}

9
naaice/CMakeLists.txt Normal file
View File

@ -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)

5
naaice/alphax.csv Normal file
View File

@ -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
1 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
2 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 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
4 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
5 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

7
naaice/files.hpp.in Normal file
View File

@ -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_

5
naaice/init_conc.csv Normal file
View File

@ -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
1 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
2 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
3 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
4 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
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

View File

@ -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()