Skip to content

Commit

Permalink
Only update nonstate variables when snapshot condition holds for Eule…
Browse files Browse the repository at this point in the history
…rForward
  • Loading branch information
thaugdahl committed Oct 14, 2024
1 parent e99ac65 commit 49aade2
Show file tree
Hide file tree
Showing 2 changed files with 126 additions and 133 deletions.
16 changes: 10 additions & 6 deletions lib/Drivers/EulerForward/Driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ int EulerForward::run() {

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

// Compute the next values of the state variables.
if (marco::runtime::simulation::getOptions().debug) {
Expand All @@ -42,19 +44,21 @@ int EulerForward::run() {
<< std::endl;
}

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

updateNonStateVariables();
EULER_FORWARD_PROFILER_NONSTATEVAR_STOP;
updateNonStateVariables();
EULER_FORWARD_PROFILER_NONSTATEVAR_STOP;
}

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

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

Expand Down
243 changes: 116 additions & 127 deletions lib/Simulation/Runtime.cpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
#include "marco/Runtime/Simulation/Runtime.h"
#include "marco/Runtime/CLI/CLI.h"
#include "marco/Runtime/Drivers/Driver.h"
#include "marco/Runtime/Multithreading/CLI.h"
#include "marco/Runtime/Printers/Printer.h"
#include "marco/Runtime/Profiling/Profiling.h"
#include "marco/Runtime/Simulation/CLI.h"
#include "marco/Runtime/Simulation/Options.h"
#include "marco/Runtime/Simulation/Profiler.h"
#include "marco/Runtime/Drivers/Driver.h"
#include "marco/Runtime/Printers/Printer.h"
#include <cassert>
#include <iostream>

Expand All @@ -16,21 +16,19 @@ using namespace ::marco::runtime;
// Functions defined inside the module of the compiled model
//===---------------------------------------------------------------------===//

extern "C"
{
/// Initialize the simulation.
void init();
extern "C" {
/// Initialize the simulation.
void init();

/// Deinitialize the simulation.
void deinit();
/// Deinitialize the simulation.
void deinit();
}

//===---------------------------------------------------------------------===//
// CLI
//===---------------------------------------------------------------------===//

static void printHelp()
{
static void printHelp() {
std::cout << "Modelica simulation.\n";
std::cout << "Model: " << getModelName() << "\n";
std::cout << "Generated with MARCO compiler.\n\n";
Expand All @@ -39,7 +37,7 @@ static void printHelp()
std::cout << "OPTIONS:\n";
std::cout << " --help Display the available options.\n\n";

auto& cli = getCLI();
auto &cli = getCLI();

for (size_t i = 0; i < cli.size(); ++i) {
std::cout << cli[i].getTitle() << "\n";
Expand All @@ -53,160 +51,152 @@ static void printHelp()
// Simulation
//===---------------------------------------------------------------------===//

namespace marco::runtime
{
Printer* Simulation::getPrinter()
{
assert(printer != nullptr);
return printer;
}
namespace marco::runtime {
Printer *Simulation::getPrinter() {
assert(printer != nullptr);
return printer;
}

const Printer* Simulation::getPrinter() const
{
assert(printer != nullptr);
return printer;
}
const Printer *Simulation::getPrinter() const {
assert(printer != nullptr);
return printer;
}

void Simulation::setPrinter(Printer* newPrinter)
{
assert(newPrinter != nullptr);
printer = newPrinter;
}
void Simulation::setPrinter(Printer *newPrinter) {
assert(newPrinter != nullptr);
printer = newPrinter;
}
} // namespace marco::runtime

namespace
{
Simulation runtimeInit()
{
#ifdef MARCO_PROFILING
profilingInit();
#endif
namespace {
Simulation runtimeInit() {
#ifdef MARCO_PROFILING
profilingInit();
#endif

Simulation result;
Simulation result;

// Number of array variables of the model (both state and algebraic ones).
int64_t numOfVariables = getNumOfVariables();
// Number of array variables of the model (both state and algebraic ones).
int64_t numOfVariables = getNumOfVariables();

// Pre-fetch the names.
result.variablesNames.resize(numOfVariables);
// Pre-fetch the names.
result.variablesNames.resize(numOfVariables);

for (int64_t var = 0; var < numOfVariables; ++var) {
result.variablesNames[var] = getVariableName(var);
}
for (int64_t var = 0; var < numOfVariables; ++var) {
result.variablesNames[var] = getVariableName(var);
}

// Pre-fetch the ranks.
result.variablesRanks.resize(numOfVariables);
// Pre-fetch the ranks.
result.variablesRanks.resize(numOfVariables);

for (int64_t var = 0; var < numOfVariables; ++var) {
result.variablesRanks[var] = getVariableRank(var);
}
for (int64_t var = 0; var < numOfVariables; ++var) {
result.variablesRanks[var] = getVariableRank(var);
}

// Pre-fetch whether each variable is printable.
result.printableVariables.resize(numOfVariables);
// Pre-fetch whether each variable is printable.
result.printableVariables.resize(numOfVariables);

for (int64_t var = 0; var < numOfVariables; ++var) {
result.printableVariables[var] = isPrintable(var);
}
for (int64_t var = 0; var < numOfVariables; ++var) {
result.printableVariables[var] = isPrintable(var);
}

// Pre-fetch the printable indices.
result.variablesPrintableIndices.resize(numOfVariables);
result.variablesPrintOrder.resize(numOfVariables);
// Pre-fetch the printable indices.
result.variablesPrintableIndices.resize(numOfVariables);
result.variablesPrintOrder.resize(numOfVariables);

for (int64_t var = 0; var < numOfVariables; ++var) {
int64_t numOfPrintableRanges = getVariableNumOfPrintableRanges(var);
result.variablesPrintableIndices[var].resize(numOfPrintableRanges);
for (int64_t var = 0; var < numOfVariables; ++var) {
int64_t numOfPrintableRanges = getVariableNumOfPrintableRanges(var);
result.variablesPrintableIndices[var].resize(numOfPrintableRanges);

for (int64_t range = 0; range < numOfPrintableRanges; ++range) {
int64_t rank = result.variablesRanks[var];
result.variablesPrintableIndices[var][range].resize(rank);
for (int64_t range = 0; range < numOfPrintableRanges; ++range) {
int64_t rank = result.variablesRanks[var];
result.variablesPrintableIndices[var][range].resize(rank);

for (int64_t dim = 0; dim < rank; ++dim) {
result.variablesPrintableIndices[var][range][dim].begin =
getVariablePrintableRangeBegin(var, range, dim);
for (int64_t dim = 0; dim < rank; ++dim) {
result.variablesPrintableIndices[var][range][dim].begin =
getVariablePrintableRangeBegin(var, range, dim);

result.variablesPrintableIndices[var][range][dim].end =
getVariablePrintableRangeEnd(var, range, dim);
}
result.variablesPrintableIndices[var][range][dim].end =
getVariablePrintableRangeEnd(var, range, dim);
}

result.variablesPrintOrder[var] = var;
}

// Pre-fetch the derivatives map.
result.derivativesMap.resize(numOfVariables);
result.variablesPrintOrder[var] = var;
}

// Pre-fetch the derivatives map.
result.derivativesMap.resize(numOfVariables);

for (int64_t var = 0; var < numOfVariables; ++var) {
result.derivativesMap[var] = -1;
}
for (int64_t var = 0; var < numOfVariables; ++var) {
result.derivativesMap[var] = -1;
}

for (int64_t var = 0; var < numOfVariables; ++var) {
int64_t derivative = getDerivative(var);
for (int64_t var = 0; var < numOfVariables; ++var) {
int64_t derivative = getDerivative(var);

if (derivative != -1) {
result.derivativesMap[derivative] = var;
}
if (derivative != -1) {
result.derivativesMap[derivative] = var;
}
}

// Compute the derivative order of each variable.
result.derOrders.resize(numOfVariables);

for (int64_t var = 0; var < numOfVariables; ++var) {
int64_t currentVar = result.derivativesMap[var];
int64_t order = 0;
// Compute the derivative order of each variable.
result.derOrders.resize(numOfVariables);

while (currentVar != -1) {
++order;
currentVar = result.derivativesMap[currentVar];
}
for (int64_t var = 0; var < numOfVariables; ++var) {
int64_t currentVar = result.derivativesMap[var];
int64_t order = 0;

result.derOrders[var] = order;
while (currentVar != -1) {
++order;
currentVar = result.derivativesMap[currentVar];
}

// Determine the print ordering for the variables.
std::sort(result.variablesPrintOrder.begin(),
result.variablesPrintOrder.end(),
[&](const int64_t& x, const int64_t& y) -> bool {
int64_t xDerOrder = result.derOrders[x];
int64_t yDerOrder = result.derOrders[y];
result.derOrders[var] = order;
}

if (xDerOrder < yDerOrder) {
return true;
}
// Determine the print ordering for the variables.
std::sort(result.variablesPrintOrder.begin(),
result.variablesPrintOrder.end(),
[&](const int64_t &x, const int64_t &y) -> bool {
int64_t xDerOrder = result.derOrders[x];
int64_t yDerOrder = result.derOrders[y];

if (xDerOrder > yDerOrder) {
return false;
}
if (xDerOrder < yDerOrder) {
return true;
}

int64_t first = x;
int64_t second = y;
if (xDerOrder > yDerOrder) {
return false;
}

if (xDerOrder != 0) {
for (int64_t i = 0; i < xDerOrder; ++i) {
first = result.derivativesMap[first];
}
int64_t first = x;
int64_t second = y;

for (int64_t i = 0; i < yDerOrder; ++i) {
second = result.derivativesMap[second];
}
if (xDerOrder != 0) {
for (int64_t i = 0; i < xDerOrder; ++i) {
first = result.derivativesMap[first];
}

return std::string_view(result.variablesNames[first]) <
std::string_view(result.variablesNames[second]);
});
for (int64_t i = 0; i < yDerOrder; ++i) {
second = result.derivativesMap[second];
}
}

return result;
}
return std::string_view(result.variablesNames[first]) <
std::string_view(result.variablesNames[second]);
});

void runtimeDeinit(Simulation& simulationInfo)
{
#ifdef MARCO_PROFILING
printProfilingStats();
#endif
}
return result;
}

[[maybe_unused]] int runSimulation(int argc, char* argv[])
{
void runtimeDeinit(Simulation &simulationInfo) {
#ifdef MARCO_PROFILING
printProfilingStats();
#endif
}
} // namespace

[[maybe_unused]] int runSimulation(int argc, char *argv[]) {
// Initialize the runtime library.
Simulation simulation = runtimeInit();

Expand All @@ -220,7 +210,7 @@ namespace
// Parse the command-line arguments.
#ifdef CLI_ENABLE
SIMULATION_PROFILER_ARG_START;
auto& cli = getCLI();
auto &cli = getCLI();
cli += simulation::getCLIOptions();

#ifdef THREADS_ENABLE
Expand Down Expand Up @@ -270,7 +260,6 @@ namespace
dynamicModelEnd();
SIMULATION_PROFILER_DYNAMIC_MODEL_STOP;


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

Expand Down

0 comments on commit 49aade2

Please sign in to comment.