diff --git a/include/marco/Runtime/Printers/CSV/Options.h b/include/marco/Runtime/Printers/CSV/Options.h index ce7546aaa..428634c36 100644 --- a/include/marco/Runtime/Printers/CSV/Options.h +++ b/include/marco/Runtime/Printers/CSV/Options.h @@ -7,6 +7,9 @@ namespace marco::runtime::printing { bool scientificNotation = false; unsigned int precision = 9; + + // The frequency at which the simulation state should be printed. + unsigned int snapshotFrequency = 1; }; PrintOptions& printOptions(); diff --git a/include/marco/Runtime/Simulation/Runtime.h b/include/marco/Runtime/Simulation/Runtime.h index af3cfb0c7..7325b7e38 100644 --- a/include/marco/Runtime/Simulation/Runtime.h +++ b/include/marco/Runtime/Simulation/Runtime.h @@ -34,6 +34,10 @@ namespace marco::runtime std::vector derOrders; + + public: + unsigned int iterationNum = 0; + private: Printer* printer; }; diff --git a/lib/Drivers/EulerForward/Driver.cpp b/lib/Drivers/EulerForward/Driver.cpp index ab2343036..49e38ed9f 100644 --- a/lib/Drivers/EulerForward/Driver.cpp +++ b/lib/Drivers/EulerForward/Driver.cpp @@ -30,6 +30,8 @@ namespace marco::runtime double time; do { + getSimulation()->iterationNum++; + // Compute the next values of the state variables. if (marco::runtime::simulation::getOptions().debug) { std::cerr << "[Euler Forward] Updating state variables" << std::endl; @@ -55,11 +57,14 @@ namespace marco::runtime std::cerr << "[Euler Forward] Printing values" << std::endl; } - // Print the values. - getSimulation()->getPrinter()->printValues(); + // Print the values at a specified frequency. + getSimulation()->getPrinter()->printValues(); } while (std::abs(simulation::getOptions().endTime - time) >= eulerforward::getOptions().timeStep); + getSimulation()->iterationNum++; + getSimulation()->getPrinter()->printValues(); + if (marco::runtime::simulation::getOptions().debug) { std::cerr << "[Euler Forward] Simulation finished" << std::endl; } diff --git a/lib/Printers/CSV/CLI.cpp b/lib/Printers/CSV/CLI.cpp index 2a8d393f6..a93859f15 100644 --- a/lib/Printers/CSV/CLI.cpp +++ b/lib/Printers/CSV/CLI.cpp @@ -8,17 +8,19 @@ std::string CommandLineOptions::getTitle() const { return "Formatting"; } void CommandLineOptions::printCommandLineOptions(std::ostream &os) const { // clang-format off - os << " --scientific-notation Print the values using the scientific notation." << std::endl; - os << " --precision= Set the number of decimals to be printed. Defaults to " << printOptions().precision << "." << std::endl; + os << " --scientific-notation Print the values using the scientific notation." << std::endl; + os << " --precision= Set the number of decimals to be printed. Defaults to " << printOptions().precision << "." << std::endl; + os << " --snapshot-frequency= The frequency at which to print the current simulation state. Defaults every " << printOptions().snapshotFrequency << " timesteps." << std::endl; // clang-format on } void CommandLineOptions::parseCommandLineOptions( const argh::parser &options) const { - // clang-format off - printOptions().scientificNotation = options["scientific-notation"]; - options("precision") >> printOptions().precision; - // clang-format on + // clang-format off + printOptions().scientificNotation = options["scientific-notation"]; + options("snapshot-frequency") >> printOptions().snapshotFrequency; + options("precision") >> printOptions().precision; + // clang-format on } } // namespace marco::runtime::printing diff --git a/lib/Printers/CSV/Printer.cpp b/lib/Printers/CSV/Printer.cpp index 4cff2843d..908d1a7ee 100644 --- a/lib/Printers/CSV/Printer.cpp +++ b/lib/Printers/CSV/Printer.cpp @@ -135,6 +135,9 @@ static void printValues(const Simulation& simulation) auto& options = printOptions(); std::cout.precision(options.precision); + // Handle snapshot frequency. + if ( simulation.iterationNum % options.snapshotFrequency > 0 ) return; + if (options.scientificNotation) { std::cout << std::scientific; } else { diff --git a/lib/Simulation/Runtime.cpp b/lib/Simulation/Runtime.cpp index 0d3bdc5a8..c0954b5c0 100644 --- a/lib/Simulation/Runtime.cpp +++ b/lib/Simulation/Runtime.cpp @@ -270,6 +270,7 @@ namespace dynamicModelEnd(); SIMULATION_PROFILER_DYNAMIC_MODEL_STOP; + // Tell the printer that the simulation has finished. simulation.getPrinter()->simulationEnd();