diff --git a/src/BTCSv2.cpp b/src/BTCS.cpp similarity index 98% rename from src/BTCSv2.cpp rename to src/BTCS.cpp index 2ff52b8..0e0e822 100644 --- a/src/BTCSv2.cpp +++ b/src/BTCS.cpp @@ -7,9 +7,12 @@ * */ -#include "FTCS.cpp" +#include "Schemes.hpp" +#include "TugUtils.hpp" + #include #include +#include #define NUM_THREADS_BTCS 10 @@ -434,7 +437,7 @@ static void BTCS_2D(Grid &grid, Boundary &bc, double timestep, } // entry point for EigenLU solver; differentiate between 1D and 2D grid -static void BTCS_LU(Grid &grid, Boundary &bc, double timestep, int numThreads) { +void BTCS_LU(Grid &grid, Boundary &bc, double timestep, int numThreads) { if (grid.getDim() == 1) { BTCS_1D(grid, bc, timestep, EigenLUAlgorithm); } else if (grid.getDim() == 2) { @@ -446,8 +449,7 @@ static void BTCS_LU(Grid &grid, Boundary &bc, double timestep, int numThreads) { } // entry point for Thomas algorithm solver; differentiate 1D and 2D grid -static void BTCS_Thomas(Grid &grid, Boundary &bc, double timestep, - int numThreads) { +void BTCS_Thomas(Grid &grid, Boundary &bc, double timestep, int numThreads) { if (grid.getDim() == 1) { BTCS_1D(grid, bc, timestep, ThomasAlgorithm); } else if (grid.getDim() == 2) { @@ -456,4 +458,4 @@ static void BTCS_Thomas(Grid &grid, Boundary &bc, double timestep, throw_invalid_argument( "Error: Only 1- and 2-dimensional grids are defined!"); } -} \ No newline at end of file +} diff --git a/src/Boundary.cpp b/src/Boundary.cpp index 84b81e0..ea7c891 100644 --- a/src/Boundary.cpp +++ b/src/Boundary.cpp @@ -1,4 +1,5 @@ -#include "TugUtils.cpp" +#include "TugUtils.hpp" + #include #include #include diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index b7aceb2..15ce0e8 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -1,4 +1,4 @@ -add_library(tug Boundary.cpp Grid.cpp Simulation.cpp FTCS.cpp BTCSv2.cpp) +add_library(tug Boundary.cpp Grid.cpp Simulation.cpp FTCS.cpp BTCS.cpp) target_link_libraries(tug Eigen3::Eigen) diff --git a/src/FTCS.cpp b/src/FTCS.cpp index d48cc46..f6b8780 100644 --- a/src/FTCS.cpp +++ b/src/FTCS.cpp @@ -5,7 +5,9 @@ * */ -#include "TugUtils.cpp" +#include "Schemes.hpp" +#include "TugUtils.hpp" + #include #include #include @@ -377,7 +379,7 @@ static void FTCS_2D(Grid &grid, Boundary &bc, double ×tep, } // entry point; differentiate between 1D and 2D grid -static void FTCS(Grid &grid, Boundary &bc, double ×tep, int &numThreads) { +void FTCS(Grid &grid, Boundary &bc, double ×tep, int &numThreads) { if (grid.getDim() == 1) { FTCS_1D(grid, bc, timestep); } else if (grid.getDim() == 2) { diff --git a/src/Grid.cpp b/src/Grid.cpp index 059cd87..c19d935 100644 --- a/src/Grid.cpp +++ b/src/Grid.cpp @@ -1,4 +1,5 @@ -#include "TugUtils.cpp" +#include "TugUtils.hpp" + #include #include diff --git a/src/Schemes.hpp b/src/Schemes.hpp new file mode 100644 index 0000000..d10e0c5 --- /dev/null +++ b/src/Schemes.hpp @@ -0,0 +1,28 @@ +/** + * @file BTCSv2.cpp + * @brief Implementation of heterogenous BTCS (backward time-centered space) + * solution of diffusion equation in 1D and 2D space. Internally the + * alternating-direction implicit (ADI) method is used. Version 2, because + * Version 1 was an implementation for the homogeneous BTCS solution. + * + */ + +#ifndef SCHEMES_H_ +#define SCHEMES_H_ + +#include "TugUtils.hpp" + +#include +#include + +// entry point; differentiate between 1D and 2D grid +extern void FTCS(Grid &grid, Boundary &bc, double ×tep, int &numThreads); + +// entry point for EigenLU solver; differentiate between 1D and 2D grid +extern void BTCS_LU(Grid &grid, Boundary &bc, double timestep, int numThreads); + +// entry point for Thomas algorithm solver; differentiate 1D and 2D grid +extern void BTCS_Thomas(Grid &grid, Boundary &bc, double timestep, + int numThreads); + +#endif // SCHEMES_H_ diff --git a/src/Simulation.cpp b/src/Simulation.cpp index 31d1724..92221ee 100644 --- a/src/Simulation.cpp +++ b/src/Simulation.cpp @@ -1,20 +1,18 @@ #include #include #include +#include #include #include #include + #include -#include +#include "TugUtils.hpp" #ifndef SIMULATION_H_ #define SIMULATION_H_ -#include "BTCSv2.cpp" - -using namespace std; - Simulation::Simulation(Grid &grid, Boundary &bc, APPROACH approach) : grid(grid), bc(bc) { diff --git a/src/TugUtils.cpp b/src/TugUtils.cpp deleted file mode 100644 index ac0a41e..0000000 --- a/src/TugUtils.cpp +++ /dev/null @@ -1,39 +0,0 @@ -#include -#include -#include -#include - -using namespace std; - -// used for throwing an invalid argument message -#define throw_invalid_argument(msg) \ - throw std::invalid_argument(std::string(__FILE__) + ":" + \ - std::to_string(__LINE__) + ":" + \ - std::string(msg)) - -// used for throwing an out of range message -#define throw_out_of_range(msg) \ - throw std::out_of_range(std::string(__FILE__) + ":" + \ - std::to_string(__LINE__) + ":" + std::string(msg)) - -// get current time -#define time_marker() std::chrono::high_resolution_clock::now() - -// calculates difference between two time points -#define diff_time(start, end) \ - ({ \ - std::chrono::duration duration = \ - std::chrono::duration_cast>(end - \ - start); \ - duration.count(); \ - }) - -// calculates arithmetic or harmonic mean of alpha between two cells -static double calcAlphaIntercell(const double &alpha1, const double &alpha2, - bool useHarmonic = true) { - if (useHarmonic) { - return double(2) / ((double(1) / alpha1) + (double(1) / alpha2)); - } else { - return 0.5 * (alpha1 + alpha2); - } -} diff --git a/src/TugUtils.hpp b/src/TugUtils.hpp index 225f743..20d480b 100644 --- a/src/TugUtils.hpp +++ b/src/TugUtils.hpp @@ -1,35 +1,36 @@ -// #ifndef BTCSUTILS_H_ -// #define BTCSUTILS_H_ +#ifndef TUGUTILS_H_ +#define TUGUTILS_H_ -// #include -// #include -// #include +#include +#include +#include -// #define throw_invalid_argument(msg) \ -// throw std::invalid_argument(std::string(__FILE__) + ":" + \ -// std::to_string(__LINE__) + ":" + \ -// std::string(msg)) +#define throw_invalid_argument(msg) \ + throw std::invalid_argument(std::string(__FILE__) + ":" + \ + std::to_string(__LINE__) + ":" + \ + std::string(msg)) -// #define throw_out_of_range(msg) \ -// throw std::out_of_range(std::string(__FILE__) + ":" + \ -// std::to_string(__LINE__) + ":" + std::string(msg)) +#define throw_out_of_range(msg) \ + throw std::out_of_range(std::string(__FILE__) + ":" + \ + std::to_string(__LINE__) + ":" + std::string(msg)) -// #define time_marker() std::chrono::high_resolution_clock::now() +#define time_marker() std::chrono::high_resolution_clock::now() -// #define diff_time(start, end) \ -// ({ \ -// std::chrono::duration duration = \ -// std::chrono::duration_cast>(end - \ -// start); \ -// duration.count(); \ -// }) -// #endif // BTCSUTILS_H_ +#define diff_time(start, end) \ + ({ \ + std::chrono::duration duration = \ + std::chrono::duration_cast>(end - \ + start); \ + duration.count(); \ + }) -// // calculates arithmetic or harmonic mean of alpha between two cells -// static double calcAlphaIntercell(double &alpha1, double &alpha2, bool useHarmonic = true) { -// if (useHarmonic) { -// return double(2) / ((double(1)/alpha1) + (double(1)/alpha2)); -// } else { -// return 0.5 * (alpha1 + alpha2); -// } -// } \ No newline at end of file +// calculates arithmetic or harmonic mean of alpha between two cells +constexpr double calcAlphaIntercell(double alpha1, double alpha2, + bool useHarmonic = true) { + if (useHarmonic) { + return double(2) / ((double(1) / alpha1) + (double(1) / alpha2)); + } else { + return 0.5 * (alpha1 + alpha2); + } +} +#endif // TUGUTILS_H_ diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 5210b95..da2c1de 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -19,7 +19,7 @@ get_filename_component(testSimulationCSV "FTCS_11_11_7000.csv" REALPATH CACHE) # set relative path in header file configure_file(testSimulation.hpp.in testSimulation.hpp) # include test directory with generated header file from above -target_include_directories(testTug PUBLIC "${CMAKE_CURRENT_BINARY_DIR}") +target_include_directories(testTug PUBLIC "${CMAKE_CURRENT_BINARY_DIR}" "${PROJECT_SOURCE_DIR}/src") add_custom_target( check diff --git a/test/TestUtils.cpp b/test/TestUtils.hpp similarity index 83% rename from test/TestUtils.cpp rename to test/TestUtils.hpp index c6a3662..867cc3f 100644 --- a/test/TestUtils.cpp +++ b/test/TestUtils.hpp @@ -9,7 +9,7 @@ using namespace std; using namespace Eigen; -MatrixXd CSV2Eigen(string file2Convert) { +inline MatrixXd CSV2Eigen(string file2Convert) { vector matrixEntries; @@ -37,13 +37,13 @@ MatrixXd CSV2Eigen(string file2Convert) { matrixEntries.size() / matrixRowNumber); } -bool checkSimilarity(MatrixXd a, MatrixXd b, double precision = 1e-5) { +inline bool checkSimilarity(MatrixXd a, MatrixXd b, double precision = 1e-5) { return a.isApprox(b, precision); } -bool checkSimilarityV2(MatrixXd a, MatrixXd b, double maxDiff) { +inline bool checkSimilarityV2(MatrixXd a, MatrixXd b, double maxDiff) { MatrixXd diff = a - b; double maxCoeff = diff.maxCoeff(); return abs(maxCoeff) < maxDiff; -} \ No newline at end of file +} diff --git a/test/testFTCS.cpp b/test/testFTCS.cpp index f2cbda2..209ac8a 100644 --- a/test/testFTCS.cpp +++ b/test/testFTCS.cpp @@ -1,4 +1,5 @@ -#include <../src/FTCS.cpp> +#include + #include #include @@ -16,4 +17,4 @@ TEST_CASE("Maths") { CHECK_EQ(calcAlphaIntercell(alpha1, alpha2), harmonicMean); CHECK_EQ(calcAlphaIntercell(alpha1, alpha2, false), average); } -} \ No newline at end of file +} diff --git a/test/testSimulation.cpp b/test/testSimulation.cpp index 6a661e4..49a930d 100644 --- a/test/testSimulation.cpp +++ b/test/testSimulation.cpp @@ -1,4 +1,5 @@ -#include "TestUtils.cpp" +#include "TestUtils.hpp" + #include #include #include