Skip to content

Commit 369fd73

Browse files
rfrankeOpenModelica-Hudson
authored andcommitted
Exploit structured logging for Newton solver
1 parent a94610a commit 369fd73

File tree

3 files changed

+45
-30
lines changed

3 files changed

+45
-30
lines changed

SimulationRuntime/cpp/Include/Core/Utils/extension/logger.hpp

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,17 @@
99
#define LOGGER_HPP_
1010

1111
#ifdef USE_LOGGER
12-
#define LOGGER_WRITE(msg, cat, lvl) Logger::write(msg, cat, lvl)
13-
#define LOGGER_WRITE_TUPLE(msg, mode) Logger::write(msg, mode)
14-
#define LOGGER_WRITE_BEGIN(msg, cat, lvl) Logger::writeBegin(msg, cat, lvl)
12+
// check for log settings to avoid construction of unused log strings
13+
#define LOGGER_IS_SET(cat, lvl) Logger::getInstance()->isSet(cat, lvl)
14+
#define LOGGER_WRITE(msg, cat, lvl) \
15+
if (LOGGER_IS_SET(cat, lvl)) Logger::write(msg, cat, lvl)
16+
#define LOGGER_WRITE_TUPLE(msg, mode) \
17+
if (LOGGER_IS_SET(mode.first, mode.second)) Logger::write(msg, mode)
18+
#define LOGGER_WRITE_BEGIN(msg, cat, lvl) \
19+
if (LOGGER_IS_SET(cat, lvl)) Logger::writeBegin(msg, cat, lvl)
1520
#define LOGGER_WRITE_END(cat, lvl) Logger::writeEnd(cat, lvl)
1621
#else
22+
#define LOGGER_IS_SET(cat, lvl) false
1723
#define LOGGER_WRITE(msg, cat, lvl)
1824
#define LOGGER_WRITE_TUPLE(msg, mode)
1925
#define LOGGER_WRITE_BEGIN(msg, cat, lvl)
@@ -42,19 +48,19 @@ class BOOST_EXTENSION_LOGGER_DECL Logger
4248

4349
static inline void write(std::string msg, LogCategory cat, LogLevel lvl)
4450
{
45-
if (instance && instance->isOutput(cat, lvl))
51+
if (instance && instance->isSet(cat, lvl))
4652
instance->writeInternal(msg, cat, lvl, true);
4753
}
4854

4955
static inline void writeBegin(std::string msg, LogCategory cat, LogLevel lvl)
5056
{
51-
if (instance && instance->isOutput(cat, lvl))
57+
if (instance && instance->isSet(cat, lvl))
5258
instance->writeInternal(msg, cat, lvl, false);
5359
}
5460

5561
static inline void writeEnd(LogCategory cat, LogLevel lvl)
5662
{
57-
if (instance && instance->isOutput(cat, lvl))
63+
if (instance && instance->isSet(cat, lvl))
5864
instance->writeInternal("", cat, lvl, true);
5965
}
6066

@@ -78,14 +84,14 @@ class BOOST_EXTENSION_LOGGER_DECL Logger
7884
return std::pair<LogCategory, LogLevel>(cat, lvl);
7985
}
8086

81-
bool isOutput(LogCategory cat, LogLevel lvl) const
87+
bool isSet(LogCategory cat, LogLevel lvl) const
8288
{
8389
return _isEnabled && _settings.modes[cat] >= lvl;
8490
}
8591

86-
bool isOutput(std::pair<LogCategory,LogLevel> mode) const
92+
bool isSet(std::pair<LogCategory,LogLevel> mode) const
8793
{
88-
return isOutput(mode.first, mode.second);
94+
return isSet(mode.first, mode.second);
8995
}
9096

9197
protected:

SimulationRuntime/cpp/Include/Solver/Newton/Newton.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,5 +89,6 @@ class Newton : public IAlgLoopSolver
8989
*_jac, ///< Temp - Jacobian
9090
*_zeroVec;
9191
long int *_iHelp;
92+
LogCategory _lc; ///< LC_NLS or LC_LS
9293

9394
};/** @} */ // end of solverNewton

SimulationRuntime/cpp/Solver/Newton/Newton.cpp

Lines changed: 29 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,22 @@
1414
#include <Core/Math/ILapack.h> // needed for solution of linear system with Lapack
1515
#include <Core/Math/Constants.h> // definitializeion of constants like uround
1616

17+
#ifdef USE_LOGGER
18+
#define LOG_VEC(algLoop, name, vec, lc, ll) LogVec(algLoop, name, vec, lc, ll)
19+
#else
20+
#define LOG_VEC(algLoop, name, vec, lc, ll)
21+
#endif
22+
1723
template <typename S, typename T>
18-
static inline void LogSysVec(IAlgLoop* algLoop, S name, T vec[]) {
19-
if (Logger::getInstance()->isOutput(LC_NLS, LL_DEBUG)) {
24+
static inline void LogVec(IAlgLoop* algLoop, S name, T vec[],
25+
LogCategory lc, LogLevel ll) {
26+
if (LOGGER_IS_SET(lc, ll)) {
2027
std::stringstream ss;
21-
ss << "Newton: eq" << to_string(algLoop->getEquationIndex());
22-
ss << ", time " << algLoop->getSimTime() << ": " << name << " = {";
28+
ss << name << " = {";
2329
for (int i = 0; i < algLoop->getDimReal(); i++)
2430
ss << (i > 0? ", ": "") << vec[i];
2531
ss << "}";
26-
Logger::write(ss.str(), LC_NLS, LL_DEBUG);
32+
LOGGER_WRITE(ss.str(), lc, ll);
2733
}
2834
}
2935

@@ -46,6 +52,7 @@ Newton::Newton(IAlgLoop* algLoop, INonLinSolverSettings* settings)
4652
, _dimSys (0)
4753
, _firstCall (true)
4854
, _iterationStatus (CONTINUE)
55+
, _lc (algLoop->isLinear()? LC_LS: LC_NLS)
4956
{
5057
}
5158

@@ -123,11 +130,10 @@ void Newton::initialize()
123130
_iterationStatus = SOLVERERROR;
124131
}
125132
}
126-
if (Logger::getInstance()->isOutput(LC_NLS, LL_DEBUG)) {
127-
Logger::write("Newton: eq" + to_string(_algLoop->getEquationIndex())
128-
+ " initialized", LC_NLS, LL_DEBUG);
129-
LogSysVec(_algLoop, "names", _yNames);
130-
}
133+
LOGGER_WRITE_BEGIN("Newton: eq" + to_string(_algLoop->getEquationIndex()) +
134+
" initialized", _lc, LL_DEBUG);
135+
LOG_VEC(_algLoop, "names", _yNames, _lc, LL_DEBUG);
136+
LOGGER_WRITE_END(_lc, LL_DEBUG);
131137
}
132138

133139
void Newton::solve()
@@ -153,6 +159,10 @@ void Newton::solve()
153159
// Reset status flag
154160
_iterationStatus = CONTINUE;
155161

162+
LOGGER_WRITE_BEGIN("Newton: eq" + to_string(_algLoop->getEquationIndex()) +
163+
" at time " + to_string(_algLoop->getSimTime()) + ":",
164+
_lc, LL_DEBUG);
165+
156166
while (_iterationStatus == CONTINUE) {
157167
// Check stopping criterion
158168
if (!_algLoop->isLinear()) {
@@ -205,8 +215,8 @@ void Newton::solve()
205215

206216
// Determination of Jacobian for non-linear system
207217
else {
208-
LogSysVec(_algLoop, "y" + to_string(totSteps), _y);
209-
LogSysVec(_algLoop, "f" + to_string(totSteps), _f);
218+
LOG_VEC(_algLoop, "y" + to_string(totSteps), _y, _lc, LL_DEBUG);
219+
LOG_VEC(_algLoop, "f" + to_string(totSteps), _f, _lc, LL_DEBUG);
210220
double phi = 0.0; // line search function
211221
for (int i = 0; i < _dimSys; ++i) {
212222
phi += _f[i] * _f[i];
@@ -305,14 +315,10 @@ void Newton::solve()
305315
phiHelp += _fHelp[i] * _fHelp[i];
306316
}
307317
}
308-
if (Logger::getInstance()->isOutput(LC_NLS, LL_DEBUG)) {
309-
std::stringstream ss;
310-
ss << "Newton: eq" << to_string(_algLoop->getEquationIndex());
311-
ss << ", time " << _algLoop->getSimTime();
312-
ss << ": lambda = " << lambda;
313-
ss << ", phi = " << phi << " --> " << phiHelp;
314-
Logger::write(ss.str(), LC_NLS, LL_DEBUG);
315-
}
318+
LOGGER_WRITE("lambda = " + to_string(lambda) +
319+
", phi = " + to_string(phi) +
320+
" --> " + to_string(phiHelp),
321+
_lc, LL_DEBUG);
316322
}
317323
// check for sufficient decrease
318324
if (phiHelp <= (1.0 - alpha * lambda) * phi)
@@ -329,7 +335,9 @@ void Newton::solve()
329335
"error solving nonlinear system (iteration limit: " + to_string(totSteps) + ")");
330336
}
331337
} // end while
332-
LogSysVec(_algLoop, "y*", _y);
338+
339+
LOG_VEC(_algLoop, "y*", _y, _lc, LL_DEBUG);
340+
LOGGER_WRITE_END(_lc, LL_DEBUG);
333341
}
334342

335343
IAlgLoopSolver::ITERATIONSTATUS Newton::getIterationStatus()

0 commit comments

Comments
 (0)