Skip to content

Commit e99ac65

Browse files
committed
Add snapshot frequency to Euler Forward driver and print disable flag.
Add snapshot frequency for euler forward and printer disable flag for CSV printer
1 parent 4b256af commit e99ac65

File tree

8 files changed

+107
-102
lines changed

8 files changed

+107
-102
lines changed

include/marco/Runtime/Printers/CSV/Options.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ namespace marco::runtime::printing
77
{
88
bool scientificNotation = false;
99
unsigned int precision = 9;
10+
bool disablePrinting = false;
1011
};
1112

1213
PrintOptions& printOptions();

include/marco/Runtime/Simulation/Runtime.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ namespace marco::runtime
3434

3535
std::vector<int64_t> derOrders;
3636

37+
3738
private:
3839
Printer* printer;
3940
};

include/marco/Runtime/Solvers/EulerForward/Options.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ namespace marco::runtime::eulerforward
88
struct Options
99
{
1010
double timeStep = 0.1;
11+
std::size_t snapshotSteps = 1;
1112
};
1213

1314
Options& getOptions();

lib/Drivers/EulerForward/CLI.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,16 @@ std::string CommandLineOptions::getTitle() const { return "Euler forward"; }
88

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

1516
void CommandLineOptions::parseCommandLineOptions(
1617
const argh::parser &options) const {
1718
// clang-format off
1819
options("time-step") >> getOptions().timeStep;
20+
options("snapshot-steps") >> getOptions().snapshotSteps;
1921
// clang-format on
2022
}
2123
} // namespace marco::runtime::eulerforward

lib/Drivers/EulerForward/Driver.cpp

Lines changed: 53 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,77 +1,79 @@
11
#include "marco/Runtime/Drivers/EulerForward/Driver.h"
22
#include "marco/Runtime/Drivers/EulerForward/CLI.h"
3-
#include "marco/Runtime/Solvers/EulerForward/Options.h"
4-
#include "marco/Runtime/Solvers/EulerForward/Profiler.h"
53
#include "marco/Runtime/Simulation/Options.h"
64
#include "marco/Runtime/Simulation/Profiler.h"
75
#include "marco/Runtime/Simulation/Runtime.h"
6+
#include "marco/Runtime/Solvers/EulerForward/Options.h"
7+
#include "marco/Runtime/Solvers/EulerForward/Profiler.h"
88
#include <iostream>
99

10-
namespace marco::runtime
11-
{
12-
EulerForward::EulerForward(Simulation* simulation)
13-
: Driver(simulation)
14-
{
15-
}
10+
namespace marco::runtime {
11+
EulerForward::EulerForward(Simulation *simulation) : Driver(simulation) {}
1612

1713
#ifdef CLI_ENABLE
18-
std::unique_ptr<cli::Category> EulerForward::getCLIOptions()
19-
{
20-
return std::make_unique<eulerforward::CommandLineOptions>();
21-
}
14+
std::unique_ptr<cli::Category> EulerForward::getCLIOptions() {
15+
return std::make_unique<eulerforward::CommandLineOptions>();
16+
}
2217
#endif // CLI_ENABLE
2318

24-
int EulerForward::run()
25-
{
26-
if (marco::runtime::simulation::getOptions().debug) {
27-
std::cerr << "[Euler Forward] Starting simulation" << std::endl;
28-
}
29-
30-
double time;
19+
int EulerForward::run() {
20+
if (marco::runtime::simulation::getOptions().debug) {
21+
std::cerr << "[Euler Forward] Starting simulation" << std::endl;
22+
}
3123

32-
do {
33-
// Compute the next values of the state variables.
34-
if (marco::runtime::simulation::getOptions().debug) {
35-
std::cerr << "[Euler Forward] Updating state variables" << std::endl;
36-
}
24+
double time;
25+
std::size_t iterationStep = 0;
3726

38-
EULER_FORWARD_PROFILER_STATEVAR_START;
39-
updateStateVariables(eulerforward::getOptions().timeStep);
40-
EULER_FORWARD_PROFILER_STATEVAR_STOP;
27+
do {
28+
iterationStep++;
4129

42-
// Move to the next step.
43-
if (marco::runtime::simulation::getOptions().debug) {
44-
std::cerr << "[Euler Forward] Updating time and non-state variables" << std::endl;
45-
}
30+
// Compute the next values of the state variables.
31+
if (marco::runtime::simulation::getOptions().debug) {
32+
std::cerr << "[Euler Forward] Updating state variables" << std::endl;
33+
}
4634

47-
EULER_FORWARD_PROFILER_NONSTATEVAR_START;
48-
time = getTime() + eulerforward::getOptions().timeStep;
49-
setTime(time);
35+
EULER_FORWARD_PROFILER_STATEVAR_START;
36+
updateStateVariables(eulerforward::getOptions().timeStep);
37+
EULER_FORWARD_PROFILER_STATEVAR_STOP;
5038

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

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

58-
// Print the values.
59-
getSimulation()->getPrinter()->printValues();
60-
} while (std::abs(simulation::getOptions().endTime - time) >=
61-
eulerforward::getOptions().timeStep);
49+
updateNonStateVariables();
50+
EULER_FORWARD_PROFILER_NONSTATEVAR_STOP;
6251

6352
if (marco::runtime::simulation::getOptions().debug) {
64-
std::cerr << "[Euler Forward] Simulation finished" << std::endl;
53+
std::cerr << "[Euler Forward] Printing values" << std::endl;
6554
}
6655

67-
return EXIT_SUCCESS;
56+
// Print the values at a specified frequency.
57+
if (iterationStep % eulerforward::getOptions().snapshotSteps == 0) {
58+
getSimulation()->getPrinter()->printValues();
59+
}
60+
61+
} while (std::abs(simulation::getOptions().endTime - time) >=
62+
eulerforward::getOptions().timeStep);
63+
64+
iterationStep++;
65+
getSimulation()->getPrinter()->printValues();
66+
67+
if (marco::runtime::simulation::getOptions().debug) {
68+
std::cerr << "[Euler Forward] Simulation finished" << std::endl;
6869
}
70+
71+
return EXIT_SUCCESS;
6972
}
73+
} // namespace marco::runtime
7074

71-
namespace marco::runtime
72-
{
73-
std::unique_ptr<Driver> getDriver(Simulation* simulation)
74-
{
75-
return std::make_unique<EulerForward>(simulation);
76-
}
75+
namespace marco::runtime {
76+
std::unique_ptr<Driver> getDriver(Simulation *simulation) {
77+
return std::make_unique<EulerForward>(simulation);
7778
}
79+
} // namespace marco::runtime

lib/Printers/CSV/CLI.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,18 @@ std::string CommandLineOptions::getTitle() const { return "Formatting"; }
88

99
void CommandLineOptions::printCommandLineOptions(std::ostream &os) const {
1010
// clang-format off
11-
os << " --scientific-notation Print the values using the scientific notation." << std::endl;
12-
os << " --precision=<value> Set the number of decimals to be printed. Defaults to " << printOptions().precision << "." << std::endl;
11+
os << " --scientific-notation Print the values using the scientific notation." << std::endl;
12+
os << " --precision=<value> Set the number of decimals to be printed. Defaults to " << printOptions().precision << "." << std::endl;
13+
os << " --disable-printing Disables output. Useful for testing performance without I/O overhead. Defaults to " << printOptions().disablePrinting << "." << std::endl;
1314
// clang-format on
1415
}
1516

1617
void CommandLineOptions::parseCommandLineOptions(
1718
const argh::parser &options) const {
1819
// clang-format off
19-
printOptions().scientificNotation = options["scientific-notation"];
20-
options("precision") >> printOptions().precision;
20+
printOptions().scientificNotation = options["scientific-notation"];
21+
options("precision") >> printOptions().precision;
22+
printOptions().disablePrinting = options["disable-printing"];
2123
// clang-format on
2224
}
2325
} // namespace marco::runtime::printing

lib/Printers/CSV/Printer.cpp

Lines changed: 41 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -11,26 +11,23 @@
1111
using namespace ::marco::runtime;
1212
using namespace ::marco::runtime::printing;
1313

14-
static void printDerWrapOpening(int64_t order)
15-
{
14+
static void printDerWrapOpening(int64_t order) {
1615
for (int64_t i = 0; i < order; ++i) {
1716
PRINT_PROFILER_STRING_START;
1817
std::cout << "der(";
1918
PRINT_PROFILER_STRING_STOP;
2019
}
2120
}
2221

23-
static void printDerWrapClosing(int64_t order)
24-
{
22+
static void printDerWrapClosing(int64_t order) {
2523
for (int64_t i = 0; i < order; ++i) {
2624
PRINT_PROFILER_STRING_START;
2725
std::cout << ')';
2826
PRINT_PROFILER_STRING_STOP;
2927
}
3028
}
3129

32-
static void printName(char* name, int64_t rank, const int64_t* indices)
33-
{
30+
static void printName(char *name, int64_t rank, const int64_t *indices) {
3431
PRINT_PROFILER_STRING_START;
3532
std::cout << name;
3633
PRINT_PROFILER_STRING_STOP;
@@ -58,8 +55,12 @@ static void printName(char* name, int64_t rank, const int64_t* indices)
5855
}
5956
}
6057

61-
static void printHeader(const Simulation& simulation)
62-
{
58+
static void printHeader(const Simulation &simulation) {
59+
60+
if (printOptions().disablePrinting) {
61+
return;
62+
}
63+
6364
PRINT_PROFILER_STRING_START;
6465
std::cout << '"' << "time" << '"';
6566
PRINT_PROFILER_STRING_STOP;
@@ -85,7 +86,7 @@ static void printHeader(const Simulation& simulation)
8586
}
8687

8788
assert(baseVar != -1);
88-
char* name = simulation.variablesNames[baseVar];
89+
char *name = simulation.variablesNames[baseVar];
8990

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

107-
for (const auto& range : simulation.variablesPrintableIndices[var]) {
108+
for (const auto &range : simulation.variablesPrintableIndices[var]) {
108109
auto beginIt = MultidimensionalRangeIterator::begin(range);
109110
auto endIt = MultidimensionalRangeIterator::end(range);
110111

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

133-
static void printValues(const Simulation& simulation)
134-
{
135-
auto& options = printOptions();
134+
static void printValues(const Simulation &simulation) {
135+
auto &options = printOptions();
136+
137+
if (options.disablePrinting) {
138+
return;
139+
}
140+
136141
std::cout.precision(options.precision);
137142

138143
if (options.scientificNotation) {
@@ -173,7 +178,7 @@ static void printValues(const Simulation& simulation)
173178
PRINT_PROFILER_FLOAT_STOP;
174179
} else {
175180
// Print the components of the array variable.
176-
for (const auto& range : simulation.variablesPrintableIndices[var]) {
181+
for (const auto &range : simulation.variablesPrintableIndices[var]) {
177182
auto beginIt = MultidimensionalRangeIterator::begin(range);
178183
auto endIt = MultidimensionalRangeIterator::end(range);
179184

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

200-
namespace marco::runtime::printing
201-
{
202-
CSVPrinter::CSVPrinter(Simulation* simulation)
203-
: Printer(simulation)
204-
{
205-
}
205+
namespace marco::runtime::printing {
206+
CSVPrinter::CSVPrinter(Simulation *simulation) : Printer(simulation) {}
206207

207208
#ifdef CLI_ENABLE
208-
std::unique_ptr<cli::Category> CSVPrinter::getCLIOptions()
209-
{
210-
return std::make_unique<CommandLineOptions>();
211-
}
209+
std::unique_ptr<cli::Category> CSVPrinter::getCLIOptions() {
210+
return std::make_unique<CommandLineOptions>();
211+
}
212212
#endif // CLI_ENABLE
213213

214-
void CSVPrinter::simulationBegin()
215-
{
216-
SIMULATION_PROFILER_PRINTING_START;
217-
::printHeader(*getSimulation());
218-
SIMULATION_PROFILER_PRINTING_STOP;
219-
}
214+
void CSVPrinter::simulationBegin() {
215+
SIMULATION_PROFILER_PRINTING_START;
216+
::printHeader(*getSimulation());
217+
SIMULATION_PROFILER_PRINTING_STOP;
218+
}
220219

221-
void CSVPrinter::printValues()
222-
{
223-
SIMULATION_PROFILER_PRINTING_START;
224-
::printValues(*getSimulation());
225-
SIMULATION_PROFILER_PRINTING_STOP;
226-
}
220+
void CSVPrinter::printValues() {
221+
SIMULATION_PROFILER_PRINTING_START;
222+
::printValues(*getSimulation());
223+
SIMULATION_PROFILER_PRINTING_STOP;
224+
}
227225

228-
void CSVPrinter::simulationEnd()
229-
{
230-
// Do nothing.
231-
}
226+
void CSVPrinter::simulationEnd() {
227+
// Do nothing.
232228
}
229+
} // namespace marco::runtime::printing
233230

234-
namespace marco::runtime
235-
{
236-
std::unique_ptr<Printer> getPrinter(Simulation* simulation)
237-
{
238-
return std::make_unique<printing::CSVPrinter>(simulation);
239-
}
231+
namespace marco::runtime {
232+
std::unique_ptr<Printer> getPrinter(Simulation *simulation) {
233+
return std::make_unique<printing::CSVPrinter>(simulation);
240234
}
235+
} // namespace marco::runtime

lib/Simulation/Runtime.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,7 @@ namespace
270270
dynamicModelEnd();
271271
SIMULATION_PROFILER_DYNAMIC_MODEL_STOP;
272272

273+
273274
// Tell the printer that the simulation has finished.
274275
simulation.getPrinter()->simulationEnd();
275276

0 commit comments

Comments
 (0)