Skip to content

Commit

Permalink
Add snapshot frequency to Euler Forward driver and print disable flag.
Browse files Browse the repository at this point in the history
Add snapshot frequency for euler forward and printer disable flag for CSV printer
  • Loading branch information
thaugdahl committed Oct 14, 2024
1 parent 4b256af commit e99ac65
Show file tree
Hide file tree
Showing 8 changed files with 107 additions and 102 deletions.
1 change: 1 addition & 0 deletions include/marco/Runtime/Printers/CSV/Options.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ namespace marco::runtime::printing
{
bool scientificNotation = false;
unsigned int precision = 9;
bool disablePrinting = false;
};

PrintOptions& printOptions();
Expand Down
1 change: 1 addition & 0 deletions include/marco/Runtime/Simulation/Runtime.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ namespace marco::runtime

std::vector<int64_t> derOrders;


private:
Printer* printer;
};
Expand Down
1 change: 1 addition & 0 deletions include/marco/Runtime/Solvers/EulerForward/Options.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ namespace marco::runtime::eulerforward
struct Options
{
double timeStep = 0.1;
std::size_t snapshotSteps = 1;
};

Options& getOptions();
Expand Down
4 changes: 3 additions & 1 deletion lib/Drivers/EulerForward/CLI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,16 @@ std::string CommandLineOptions::getTitle() const { return "Euler forward"; }

void CommandLineOptions::printCommandLineOptions(std::ostream &os) const {
// clang-format off
os << " --time-step=<value> Set the time step (in seconds). Defaults to " << getOptions().timeStep << "." << std::endl;
os << " --time-step=<value> Set the time step (in seconds). Defaults to " << getOptions().timeStep << "." << std::endl;
os << " --snapshot-steps=<value> Print simulation state every <value> time steps. Defaults to " << getOptions().snapshotSteps << "." << std::endl;
// clang-format on
}

void CommandLineOptions::parseCommandLineOptions(
const argh::parser &options) const {
// clang-format off
options("time-step") >> getOptions().timeStep;
options("snapshot-steps") >> getOptions().snapshotSteps;
// clang-format on
}
} // namespace marco::runtime::eulerforward
Expand Down
104 changes: 53 additions & 51 deletions lib/Drivers/EulerForward/Driver.cpp
Original file line number Diff line number Diff line change
@@ -1,77 +1,79 @@
#include "marco/Runtime/Drivers/EulerForward/Driver.h"
#include "marco/Runtime/Drivers/EulerForward/CLI.h"
#include "marco/Runtime/Solvers/EulerForward/Options.h"
#include "marco/Runtime/Solvers/EulerForward/Profiler.h"
#include "marco/Runtime/Simulation/Options.h"
#include "marco/Runtime/Simulation/Profiler.h"
#include "marco/Runtime/Simulation/Runtime.h"
#include "marco/Runtime/Solvers/EulerForward/Options.h"
#include "marco/Runtime/Solvers/EulerForward/Profiler.h"
#include <iostream>

namespace marco::runtime
{
EulerForward::EulerForward(Simulation* simulation)
: Driver(simulation)
{
}
namespace marco::runtime {
EulerForward::EulerForward(Simulation *simulation) : Driver(simulation) {}

#ifdef CLI_ENABLE
std::unique_ptr<cli::Category> EulerForward::getCLIOptions()
{
return std::make_unique<eulerforward::CommandLineOptions>();
}
std::unique_ptr<cli::Category> EulerForward::getCLIOptions() {
return std::make_unique<eulerforward::CommandLineOptions>();
}
#endif // CLI_ENABLE

int EulerForward::run()
{
if (marco::runtime::simulation::getOptions().debug) {
std::cerr << "[Euler Forward] Starting simulation" << std::endl;
}

double time;
int EulerForward::run() {
if (marco::runtime::simulation::getOptions().debug) {
std::cerr << "[Euler Forward] Starting simulation" << std::endl;
}

do {
// Compute the next values of the state variables.
if (marco::runtime::simulation::getOptions().debug) {
std::cerr << "[Euler Forward] Updating state variables" << std::endl;
}
double time;
std::size_t iterationStep = 0;

EULER_FORWARD_PROFILER_STATEVAR_START;
updateStateVariables(eulerforward::getOptions().timeStep);
EULER_FORWARD_PROFILER_STATEVAR_STOP;
do {
iterationStep++;

// Move to the next step.
if (marco::runtime::simulation::getOptions().debug) {
std::cerr << "[Euler Forward] Updating time and non-state variables" << std::endl;
}
// Compute the next values of the state variables.
if (marco::runtime::simulation::getOptions().debug) {
std::cerr << "[Euler Forward] Updating state variables" << std::endl;
}

EULER_FORWARD_PROFILER_NONSTATEVAR_START;
time = getTime() + eulerforward::getOptions().timeStep;
setTime(time);
EULER_FORWARD_PROFILER_STATEVAR_START;
updateStateVariables(eulerforward::getOptions().timeStep);
EULER_FORWARD_PROFILER_STATEVAR_STOP;

updateNonStateVariables();
EULER_FORWARD_PROFILER_NONSTATEVAR_STOP;
// Move to the next step.
if (marco::runtime::simulation::getOptions().debug) {
std::cerr << "[Euler Forward] Updating time and non-state variables"
<< std::endl;
}

if (marco::runtime::simulation::getOptions().debug) {
std::cerr << "[Euler Forward] Printing values" << std::endl;
}
EULER_FORWARD_PROFILER_NONSTATEVAR_START;
time = getTime() + eulerforward::getOptions().timeStep;
setTime(time);

// Print the values.
getSimulation()->getPrinter()->printValues();
} while (std::abs(simulation::getOptions().endTime - time) >=
eulerforward::getOptions().timeStep);
updateNonStateVariables();
EULER_FORWARD_PROFILER_NONSTATEVAR_STOP;

if (marco::runtime::simulation::getOptions().debug) {
std::cerr << "[Euler Forward] Simulation finished" << std::endl;
std::cerr << "[Euler Forward] Printing values" << std::endl;
}

return EXIT_SUCCESS;
// Print the values at a specified frequency.
if (iterationStep % eulerforward::getOptions().snapshotSteps == 0) {
getSimulation()->getPrinter()->printValues();
}

} while (std::abs(simulation::getOptions().endTime - time) >=
eulerforward::getOptions().timeStep);

iterationStep++;
getSimulation()->getPrinter()->printValues();

if (marco::runtime::simulation::getOptions().debug) {
std::cerr << "[Euler Forward] Simulation finished" << std::endl;
}

return EXIT_SUCCESS;
}
} // namespace marco::runtime

namespace marco::runtime
{
std::unique_ptr<Driver> getDriver(Simulation* simulation)
{
return std::make_unique<EulerForward>(simulation);
}
namespace marco::runtime {
std::unique_ptr<Driver> getDriver(Simulation *simulation) {
return std::make_unique<EulerForward>(simulation);
}
} // namespace marco::runtime
10 changes: 6 additions & 4 deletions lib/Printers/CSV/CLI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,18 @@ 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 << " --disable-printing Disables output. Useful for testing performance without I/O overhead. Defaults to " << printOptions().disablePrinting << "." << 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;
printOptions().scientificNotation = options["scientific-notation"];
options("precision") >> printOptions().precision;
printOptions().disablePrinting = options["disable-printing"];
// clang-format on
}
} // namespace marco::runtime::printing
Expand Down
87 changes: 41 additions & 46 deletions lib/Printers/CSV/Printer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,26 +11,23 @@
using namespace ::marco::runtime;
using namespace ::marco::runtime::printing;

static void printDerWrapOpening(int64_t order)
{
static void printDerWrapOpening(int64_t order) {
for (int64_t i = 0; i < order; ++i) {
PRINT_PROFILER_STRING_START;
std::cout << "der(";
PRINT_PROFILER_STRING_STOP;
}
}

static void printDerWrapClosing(int64_t order)
{
static void printDerWrapClosing(int64_t order) {
for (int64_t i = 0; i < order; ++i) {
PRINT_PROFILER_STRING_START;
std::cout << ')';
PRINT_PROFILER_STRING_STOP;
}
}

static void printName(char* name, int64_t rank, const int64_t* indices)
{
static void printName(char *name, int64_t rank, const int64_t *indices) {
PRINT_PROFILER_STRING_START;
std::cout << name;
PRINT_PROFILER_STRING_STOP;
Expand Down Expand Up @@ -58,8 +55,12 @@ static void printName(char* name, int64_t rank, const int64_t* indices)
}
}

static void printHeader(const Simulation& simulation)
{
static void printHeader(const Simulation &simulation) {

if (printOptions().disablePrinting) {
return;
}

PRINT_PROFILER_STRING_START;
std::cout << '"' << "time" << '"';
PRINT_PROFILER_STRING_STOP;
Expand All @@ -85,7 +86,7 @@ static void printHeader(const Simulation& simulation)
}

assert(baseVar != -1);
char* name = simulation.variablesNames[baseVar];
char *name = simulation.variablesNames[baseVar];

if (rank == 0) {
// Print only the variable name.
Expand All @@ -104,7 +105,7 @@ static void printHeader(const Simulation& simulation)
// Print the name of the array and the indices, for each possible
// combination of printable indices.

for (const auto& range : simulation.variablesPrintableIndices[var]) {
for (const auto &range : simulation.variablesPrintableIndices[var]) {
auto beginIt = MultidimensionalRangeIterator::begin(range);
auto endIt = MultidimensionalRangeIterator::end(range);

Expand All @@ -130,9 +131,13 @@ static void printHeader(const Simulation& simulation)
PRINT_PROFILER_STRING_STOP;
}

static void printValues(const Simulation& simulation)
{
auto& options = printOptions();
static void printValues(const Simulation &simulation) {
auto &options = printOptions();

if (options.disablePrinting) {
return;
}

std::cout.precision(options.precision);

if (options.scientificNotation) {
Expand Down Expand Up @@ -173,7 +178,7 @@ static void printValues(const Simulation& simulation)
PRINT_PROFILER_FLOAT_STOP;
} else {
// Print the components of the array variable.
for (const auto& range : simulation.variablesPrintableIndices[var]) {
for (const auto &range : simulation.variablesPrintableIndices[var]) {
auto beginIt = MultidimensionalRangeIterator::begin(range);
auto endIt = MultidimensionalRangeIterator::end(range);

Expand All @@ -197,44 +202,34 @@ static void printValues(const Simulation& simulation)
PRINT_PROFILER_STRING_STOP;
}

namespace marco::runtime::printing
{
CSVPrinter::CSVPrinter(Simulation* simulation)
: Printer(simulation)
{
}
namespace marco::runtime::printing {
CSVPrinter::CSVPrinter(Simulation *simulation) : Printer(simulation) {}

#ifdef CLI_ENABLE
std::unique_ptr<cli::Category> CSVPrinter::getCLIOptions()
{
return std::make_unique<CommandLineOptions>();
}
std::unique_ptr<cli::Category> CSVPrinter::getCLIOptions() {
return std::make_unique<CommandLineOptions>();
}
#endif // CLI_ENABLE

void CSVPrinter::simulationBegin()
{
SIMULATION_PROFILER_PRINTING_START;
::printHeader(*getSimulation());
SIMULATION_PROFILER_PRINTING_STOP;
}
void CSVPrinter::simulationBegin() {
SIMULATION_PROFILER_PRINTING_START;
::printHeader(*getSimulation());
SIMULATION_PROFILER_PRINTING_STOP;
}

void CSVPrinter::printValues()
{
SIMULATION_PROFILER_PRINTING_START;
::printValues(*getSimulation());
SIMULATION_PROFILER_PRINTING_STOP;
}
void CSVPrinter::printValues() {
SIMULATION_PROFILER_PRINTING_START;
::printValues(*getSimulation());
SIMULATION_PROFILER_PRINTING_STOP;
}

void CSVPrinter::simulationEnd()
{
// Do nothing.
}
void CSVPrinter::simulationEnd() {
// Do nothing.
}
} // namespace marco::runtime::printing

namespace marco::runtime
{
std::unique_ptr<Printer> getPrinter(Simulation* simulation)
{
return std::make_unique<printing::CSVPrinter>(simulation);
}
namespace marco::runtime {
std::unique_ptr<Printer> getPrinter(Simulation *simulation) {
return std::make_unique<printing::CSVPrinter>(simulation);
}
} // namespace marco::runtime
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 e99ac65

Please sign in to comment.