From d38e14d6f48968b53f15935c2ada03b63a3abd33 Mon Sep 17 00:00:00 2001 From: Hannes Signer Date: Thu, 3 Aug 2023 14:57:44 +0200 Subject: [PATCH] change: all variables as pass by reference --- examples/reference-FTCS_2D_closed.cpp | 5 +++++ include/tug/Simulation.hpp | 8 ++++---- src/FTCS.cpp | 11 +++++------ src/Simulation.cpp | 10 ++++------ test/CMakeLists.txt | 2 +- test/testSimulation.cpp | 8 +++----- 6 files changed, 22 insertions(+), 22 deletions(-) diff --git a/examples/reference-FTCS_2D_closed.cpp b/examples/reference-FTCS_2D_closed.cpp index 1deb307..2323531 100644 --- a/examples/reference-FTCS_2D_closed.cpp +++ b/examples/reference-FTCS_2D_closed.cpp @@ -1,5 +1,8 @@ #include #include "Eigen/Core" +#include + +using namespace std; int main(int argc, char *argv[]) { int row = 11; @@ -50,4 +53,6 @@ int main(int argc, char *argv[]) { // RUN sim.run(); + cout << grid.getConcentrations() << endl; + } \ No newline at end of file diff --git a/include/tug/Simulation.hpp b/include/tug/Simulation.hpp index 91ef322..a3cee5a 100644 --- a/include/tug/Simulation.hpp +++ b/include/tug/Simulation.hpp @@ -37,7 +37,7 @@ class Simulation { * @param bc * @param aproach */ - Simulation(Grid grid, Boundary bc, APPROACH approach); + Simulation(Grid &grid, Boundary &bc, APPROACH approach); /** * @brief @@ -107,7 +107,7 @@ class Simulation { * * @return Grid */ - Grid run(); + void run(); private: @@ -117,8 +117,8 @@ class Simulation { CONSOLE_OUTPUT console_output; TIME_MEASURE time_measure; - Grid grid; - Boundary bc; + Grid &grid; + Boundary &bc; APPROACH approach; }; diff --git a/src/FTCS.cpp b/src/FTCS.cpp index 3e2ae38..e33c713 100644 --- a/src/FTCS.cpp +++ b/src/FTCS.cpp @@ -239,7 +239,7 @@ MatrixXd FTCS_1D(Grid &grid, Boundary &bc, double ×tep) { } -MatrixXd FTCS_2D(Grid &grid, Boundary &bc, double ×tep) { +void FTCS_2D(Grid &grid, Boundary &bc, double ×tep) { int rowMax = grid.getRow(); int colMax = grid.getCol(); double deltaRow = grid.getDeltaRow(); @@ -384,17 +384,16 @@ MatrixXd FTCS_2D(Grid &grid, Boundary &bc, double ×tep) { ) ; - - return concentrations_t1; + grid.setConcentrations(concentrations_t1); } -MatrixXd FTCS(Grid &grid, Boundary &bc, double ×tep) { +void FTCS(Grid &grid, Boundary &bc, double ×tep) { if (grid.getDim() == 1) { - return FTCS_1D(grid, bc, timestep); + FTCS_1D(grid, bc, timestep); } else { - return FTCS_2D(grid, bc, timestep); + FTCS_2D(grid, bc, timestep); } } diff --git a/src/Simulation.cpp b/src/Simulation.cpp index a975db3..d07065f 100644 --- a/src/Simulation.cpp +++ b/src/Simulation.cpp @@ -10,9 +10,7 @@ using namespace std; -Simulation::Simulation(Grid grid, Boundary bc, APPROACH approach) : grid(grid), bc(bc) { - //probably to DEBUG assignment of grid and bc - this->grid = grid; +Simulation::Simulation(Grid &grid, Boundary &bc, APPROACH approach) : grid(grid), bc(bc) { this->approach = approach; //TODO calculate max time step @@ -146,7 +144,7 @@ void Simulation::printConcentrationsCSV(string filename) { file.close(); } -Grid Simulation::run() { +void Simulation::run() { string filename; if (this->console_output > CONSOLE_OUTPUT_OFF) { printConcentrationsConsole(); @@ -165,7 +163,7 @@ Grid Simulation::run() { printConcentrationsCSV(filename); } - grid.setConcentrations(FTCS(grid, bc, timestep)); + FTCS(grid, bc, timestep); } } else if (approach == BTCS_APPROACH) { @@ -191,5 +189,5 @@ Grid Simulation::run() { printConcentrationsCSV(filename); } - return grid; + } diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 9431fc2..7366a31 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -11,7 +11,7 @@ if(NOT DOCTEST_LIB) FetchContent_MakeAvailable(DocTest) endif() -add_executable(testTug setup.cpp testBoundaryCondition.cpp testDiffusion.cpp testSimulation.cpp) +add_executable(testTug setup.cpp testSimulation.cpp) # testBoundaryCondition.cpp testDiffusion.cpp target_link_libraries(testTug doctest tug) # get relative path of the CSV file diff --git a/test/testSimulation.cpp b/test/testSimulation.cpp index 8336cb7..aaac3fa 100644 --- a/test/testSimulation.cpp +++ b/test/testSimulation.cpp @@ -49,19 +49,17 @@ static Grid setupSimulation() { Simulation sim = Simulation(grid, bc, FTCS_APPROACH); sim.setTimestep(0.001); sim.setIterations(7000); - // sim.setOutputCSV(CSV_OUTPUT_ON); - // sim.setOutputConsole(CONSOLE_OUTPUT_ON); - + sim.run(); // RUN - return sim.run(); + return grid; + } TEST_CASE("equality to reference matrix") { // set string from the header file string test_path = testSimulationCSVDir; MatrixXd reference = CSV2Eigen(test_path); - cout << test_path << endl; Grid grid = setupSimulation(); CHECK(checkSimilarity(reference, grid.getConcentrations(), 0.1) == true); }