From c9c0f02a5abfd0a8429e2abf15a01cea481564ba Mon Sep 17 00:00:00 2001 From: philippun Date: Fri, 11 Aug 2023 15:15:08 +0200 Subject: [PATCH] adjusted XTREME csv output --- examples/BTCS_2D_proto_example.cpp | 2 +- include/tug/Simulation.hpp | 2 +- src/Boundary.cpp | 4 ++++ src/Simulation.cpp | 15 +++++++++------ test/TestUtils.cpp | 2 +- test/testSimulation.cpp | 25 +++++++++++++++++-------- 6 files changed, 33 insertions(+), 17 deletions(-) diff --git a/examples/BTCS_2D_proto_example.cpp b/examples/BTCS_2D_proto_example.cpp index 1114596..03916b0 100644 --- a/examples/BTCS_2D_proto_example.cpp +++ b/examples/BTCS_2D_proto_example.cpp @@ -70,7 +70,7 @@ int main(int argc, char *argv[]) { simulation.setIterations(100); // set kind of output [CSV_OUTPUT_OFF (default), CSV_OUTPUT_ON, CSV_OUTPUT_VERBOSE] - simulation.setOutputCSV(CSV_OUTPUT_VERBOSE); + simulation.setOutputCSV(CSV_OUTPUT_XTREME); // **** RUN SIMULATION **** diff --git a/include/tug/Simulation.hpp b/include/tug/Simulation.hpp index 772c0e6..1d678c2 100644 --- a/include/tug/Simulation.hpp +++ b/include/tug/Simulation.hpp @@ -16,7 +16,7 @@ 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 // produce csv output with all concentration matrices and simulation environment + CSV_OUTPUT_XTREME // produce csv output with all concentration matrices and boundary conditions at beginning }; enum CONSOLE_OUTPUT { diff --git a/src/Boundary.cpp b/src/Boundary.cpp index c164860..8f248cf 100644 --- a/src/Boundary.cpp +++ b/src/Boundary.cpp @@ -115,6 +115,10 @@ VectorXd Boundary::getBoundarySideValues(BC_SIDE side) { VectorXd values(length); for (int i = 0; i < length; i++) { + if (getBoundaryElementType(side, i) == tug::bc::BC_TYPE_CLOSED) { + values(i) = -1; + continue; + } values(i) = getBoundaryElementValue(side, i); } diff --git a/src/Simulation.cpp b/src/Simulation.cpp index 53a34d5..fc35513 100644 --- a/src/Simulation.cpp +++ b/src/Simulation.cpp @@ -150,6 +150,7 @@ string Simulation::createCSVfile() { while (filesystem::exists(filename)) { appendIdent += 1; appendIdentString = to_string(appendIdent); + // ?? TODO why double filename? filename = filename = approachString + "_" + row + "_" + col + "_" + numIterations + "-" + appendIdentString + ".csv"; } @@ -166,9 +167,11 @@ string Simulation::createCSVfile() { //boundary right //boundary top //boundary bottom - file << row << endl; - file << col << endl; - file << numIterations << endl; + IOFormat one_row(StreamPrecision, DontAlignCols, "", " "); + file << bc.getBoundarySideValues(BC_SIDE_LEFT).format(one_row) << endl; + file << bc.getBoundarySideValues(BC_SIDE_RIGHT).format(one_row) << endl; + file << bc.getBoundarySideValues(BC_SIDE_TOP).format(one_row) << endl; + file << bc.getBoundarySideValues(BC_SIDE_BOTTOM).format(one_row) << endl; // TODO // file << to_string(bc.printBoundarySide) << endl; file << endl << endl; @@ -218,7 +221,7 @@ void Simulation::run() { if (console_output == CONSOLE_OUTPUT_VERBOSE && i > 0) { printConcentrationsConsole(); } - if (csv_output == CSV_OUTPUT_VERBOSE) { + if (csv_output >= CSV_OUTPUT_VERBOSE) { printConcentrationsCSV(filename); } @@ -229,7 +232,7 @@ void Simulation::run() { auto milliseconds = std::chrono::duration_cast(end - begin); // MDL: meaningful stdout messages - std::cout << ":: run() finished in " << milliseconds.count() << "ms" << endl; + std::cout << "\n:: run() finished in " << milliseconds.count() << "ms" << endl; } else if (approach == BTCS_APPROACH) { @@ -237,7 +240,7 @@ void Simulation::run() { if (console_output == CONSOLE_OUTPUT_VERBOSE && i > 0) { printConcentrationsConsole(); } - if (csv_output == CSV_OUTPUT_VERBOSE && i > 0) { + if (csv_output >= CSV_OUTPUT_VERBOSE) { printConcentrationsCSV(filename); } diff --git a/test/TestUtils.cpp b/test/TestUtils.cpp index 616b402..ef0d3db 100644 --- a/test/TestUtils.cpp +++ b/test/TestUtils.cpp @@ -43,5 +43,5 @@ bool checkSimilarityV2(MatrixXd a, MatrixXd b, double maxDiff) { MatrixXd diff = a - b; double maxCoeff = diff.maxCoeff(); - return maxCoeff < maxDiff; + return abs(maxCoeff) < maxDiff; } \ No newline at end of file diff --git a/test/testSimulation.cpp b/test/testSimulation.cpp index 44d6f98..c70a475 100644 --- a/test/testSimulation.cpp +++ b/test/testSimulation.cpp @@ -7,7 +7,7 @@ // include the configured header file #include -static Grid setupSimulation() { +static Grid setupSimulation(APPROACH approach, double timestep, int iterations) { int row = 11; int col = 11; int domain_row = 10; @@ -46,9 +46,10 @@ static Grid setupSimulation() { // Simulation - Simulation sim = Simulation(grid, bc, FTCS_APPROACH); - sim.setTimestep(0.001); - sim.setIterations(7000); + Simulation sim = Simulation(grid, bc, approach); + sim.setOutputConsole(CONSOLE_OUTPUT_ON); + sim.setTimestep(timestep); + sim.setIterations(iterations); sim.run(); // RUN @@ -56,21 +57,29 @@ static Grid setupSimulation() { } -TEST_CASE("equality to reference matrix") { +TEST_CASE("equality to reference matrix with FTCS") { // set string from the header file string test_path = testSimulationCSVDir; MatrixXd reference = CSV2Eigen(test_path); - Grid grid = setupSimulation(); + Grid grid = setupSimulation(FTCS_APPROACH, 0.001, 7000); CHECK(checkSimilarity(reference, grid.getConcentrations(), 0.1) == true); } +TEST_CASE("equality to reference matrix with BTCS") { + // set string from the header file + string test_path = testSimulationCSVDir; + MatrixXd reference = CSV2Eigen(test_path); + Grid grid = setupSimulation(BTCS_APPROACH, 1, 7); + CHECK(checkSimilarityV2(reference, grid.getConcentrations(), 0.01) == true); +} + TEST_CASE("Initialize environment"){ int rc = 12; Grid grid(rc, rc); Boundary boundary(grid); - CHECK_NOTHROW(Simulation sim(grid, boundary, FTCS_APPROACH)); - } + CHECK_NOTHROW(Simulation sim(grid, boundary, BTCS_APPROACH)); +} TEST_CASE("Simulation environment"){ int rc = 12;