Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add snapshot frequency to Euler Forward driver #10

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;


Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this whiteline inserted by clang-format? :D

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No :D

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
98 changes: 52 additions & 46 deletions lib/Drivers/EulerForward/Driver.cpp
Original file line number Diff line number Diff line change
@@ -1,77 +1,83 @@
#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;
}
int EulerForward::run() {
if (marco::runtime::simulation::getOptions().debug) {
std::cerr << "[Euler Forward] Starting simulation" << std::endl;
}

double time;
std::size_t iterationStep = 0;

double time;
do {
iterationStep++;
bool snapshotCondition =
iterationStep % eulerforward::getOptions().snapshotSteps == 0;

do {
// Compute the next values of the state variables.
if (marco::runtime::simulation::getOptions().debug) {
std::cerr << "[Euler Forward] Updating 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_STATEVAR_START;
updateStateVariables(eulerforward::getOptions().timeStep);
EULER_FORWARD_PROFILER_STATEVAR_STOP;
EULER_FORWARD_PROFILER_STATEVAR_START;
updateStateVariables(eulerforward::getOptions().timeStep);
EULER_FORWARD_PROFILER_STATEVAR_STOP;

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

if (snapshotCondition) {
EULER_FORWARD_PROFILER_NONSTATEVAR_START;
time = getTime() + eulerforward::getOptions().timeStep;
setTime(time);

updateNonStateVariables();
EULER_FORWARD_PROFILER_NONSTATEVAR_STOP;
}

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be done only if the snapshot steps condition holds.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pushing soon

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The macros / assertions -- introduced as sequence points?

std::cerr << "[Euler Forward] Printing values" << std::endl;
}

// Print the values.
// Print the values at a specified frequency.
if (snapshotCondition) {
getSimulation()->getPrinter()->printValues();
} while (std::abs(simulation::getOptions().endTime - time) >=
eulerforward::getOptions().timeStep);

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

return EXIT_SUCCESS;
} 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
Loading
Loading