Skip to content

Commit

Permalink
Add snapshot frequency to Euler Forward driver
Browse files Browse the repository at this point in the history
  • Loading branch information
thaugdahl committed Oct 1, 2024
1 parent 4f81d64 commit 745e804
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 8 deletions.
3 changes: 3 additions & 0 deletions include/marco/Runtime/Printers/CSV/Options.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
4 changes: 4 additions & 0 deletions include/marco/Runtime/Simulation/Runtime.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ namespace marco::runtime

std::vector<int64_t> derOrders;


public:
unsigned int iterationNum = 0;

private:
Printer* printer;
};
Expand Down
9 changes: 7 additions & 2 deletions lib/Drivers/EulerForward/Driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}
Expand Down
14 changes: 8 additions & 6 deletions lib/Printers/CSV/CLI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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=<value> 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=<value> Set the number of decimals to be printed. Defaults to " << printOptions().precision << "." << std::endl;
os << " --snapshot-frequency=<value> 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

Expand Down
3 changes: 3 additions & 0 deletions lib/Printers/CSV/Printer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
1 change: 1 addition & 0 deletions lib/Simulation/Runtime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,7 @@ namespace
dynamicModelEnd();
SIMULATION_PROFILER_DYNAMIC_MODEL_STOP;


// Tell the printer that the simulation has finished.
simulation.getPrinter()->simulationEnd();

Expand Down

0 comments on commit 745e804

Please sign in to comment.