Skip to content

Commit 57dbead

Browse files
authored
Merge pull request #94 from daniel-zint/dzint/optional_unicode
Add CMake option for removing unicode from logging messages.
2 parents 71271fb + 8f285ea commit 57dbead

5 files changed

Lines changed: 51 additions & 13 deletions

File tree

CMakeLists.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ endif()
7474

7575
# Polysolve options
7676
option(POLYSOLVE_WITH_SANITIZERS "Enable sanitizers in compilation targets" OFF)
77+
option(POLYSOLVE_WITH_UNICODE "Use Unicode in logging messages" ON)
7778

7879
# Polysolve options for enabling/disabling optional libraries
7980
option(POLYSOLVE_WITH_ACCELERATE "Enable Apple Accelerate" ${POLYSOLVE_ON_APPLE_SILICON})
@@ -322,6 +323,14 @@ endif()
322323
include(polysolve_warnings)
323324
target_link_libraries(polysolve_linear PRIVATE polysolve::warnings)
324325

326+
# Unicode
327+
if(POLYSOLVE_WITH_UNICODE)
328+
target_compile_definitions(polysolve_linear PUBLIC POLYSOLVE_WITH_UNICODE)
329+
if(MSVC)
330+
target_compile_options(polysolve_linear PUBLIC /utf-8)
331+
endif()
332+
endif()
333+
325334
# ---------
326335
# Nonlinear
327336
# ---------

src/polysolve/Utils.hpp

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#pragma once
1+
#pragma once
22

33
#include "Types.hpp"
44

@@ -62,4 +62,24 @@ namespace polysolve
6262

6363
double extract_param(const std::string &key, const std::string &name, const json &json);
6464

65+
namespace log
66+
{
67+
#ifdef POLYSOLVE_WITH_UNICODE
68+
inline std::string f0() { return "f₀"; }
69+
inline std::string dot() { return ""; }
70+
inline std::string ge() { return ""; }
71+
inline std::string delta(const std::string &x) { return std::string("Δ") + x; }
72+
inline std::string norm(const std::string &x) { return std::string("") + x + ""; }
73+
inline std::string grad(const std::string &x) { return std::string("") + x; }
74+
#else
75+
inline std::string f0() { return "f0"; }
76+
inline std::string dot() { return "*"; }
77+
inline std::string ge() { return ">="; }
78+
inline std::string delta(const std::string &x) { return std::string("d") + x; }
79+
inline std::string norm(const std::string &x) { return std::string("norm(") + x + ")"; }
80+
inline std::string grad(const std::string &x) { return std::string("grad(") + x + ")"; }
81+
#endif // POLYSOLVE_WITH_UNICODE
82+
83+
} // namespace log
84+
6585
} // namespace polysolve

src/polysolve/nonlinear/Criteria.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include "Criteria.hpp"
44

55
#include <spdlog/fmt/fmt.h>
6+
#include <polysolve/Utils.hpp>
67

78
namespace polysolve::nonlinear
89
{
@@ -33,8 +34,12 @@ namespace polysolve::nonlinear
3334
}
3435
std::string Criteria::print_message() const {
3536
return fmt::format(
36-
"iters={:d} Δf={:g} ‖∇f‖={:g} ‖Δx‖={:g} Δx⋅∇f(x)={:g}",
37-
iterations, fDelta, gradNorm, xDelta, xDeltaDotGrad);
37+
"iters={:d} {}={:g} {}={:g} {}={:g} {}={:g}",
38+
iterations,
39+
log::delta("f"), fDelta,
40+
log::norm(log::grad("f")), gradNorm,
41+
log::norm(log::delta("x")), xDelta,
42+
log::delta("x") + log::dot() + log::grad("f(x)"), xDeltaDotGrad);
3843
}
3944

4045
Status checkConvergence(const Criteria &stop, const Criteria &current)

src/polysolve/nonlinear/Solver.cpp

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -282,8 +282,8 @@ namespace polysolve::nonlinear
282282
stop_watch.start();
283283

284284
m_logger.debug(
285-
"Starting {} with {} solve f₀={:g} (stopping criteria: {})",
286-
descent_strategy_name(), m_line_search->name(), objFunc(x), m_stop.print_message());
285+
"Starting {} with {} solve {}={:g} (stopping criteria: {})",
286+
descent_strategy_name(), m_line_search->name(), log::f0(), objFunc(x), m_stop.print_message());
287287

288288
update_solver_info(objFunc(x));
289289
objFunc.post_step(PostStepData(m_current.iterations, solver_info, x, grad));
@@ -377,17 +377,21 @@ namespace polysolve::nonlinear
377377
{
378378
m_status = Status::NotDescentDirection;
379379
log_and_throw_error(
380-
m_logger, "[{}][{}] {} on last strategy (‖Δx‖={:g}; ‖g‖={:g}; Δx⋅g={:g}≥0); stopping",
381-
current_name, m_line_search->name(), status_message(m_status), delta_x.norm(), compute_grad_norm(x, grad),
382-
m_current.xDeltaDotGrad);
380+
m_logger, "[{}][{}] {} on last strategy ({}={:g}; {}={:g}; {}={:g}≥0); stopping",
381+
current_name, m_line_search->name(), status_message(m_status),
382+
log::norm(log::delta("x")), delta_x.norm(),
383+
log::norm("g"), compute_grad_norm(x, grad),
384+
log::delta("x") + log::dot() + "g", m_current.xDeltaDotGrad);
383385
}
384386
else
385387
{
386388
m_status = Status::Continue;
387389
m_logger.debug(
388-
"[{}][{}] {} (‖Δx‖={:g}; ‖g‖={:g}; Δx⋅g={:g}0); reverting to {}",
390+
"[{}][{}] {} ({}={:g}; {}={:g}; {}={:g}{}0); reverting to {}",
389391
current_name, m_line_search->name(), status_message(Status::NotDescentDirection),
390-
delta_x.norm(), compute_grad_norm(x, grad), m_current.xDeltaDotGrad,
392+
log::norm(log::delta("x")), delta_x.norm(),
393+
log::norm("g"), compute_grad_norm(x, grad),
394+
log::delta("x") + log::dot() + "g", m_current.xDeltaDotGrad, log::ge(),
391395
descent_strategy_name());
392396
}
393397
continue;
@@ -403,9 +407,9 @@ namespace polysolve::nonlinear
403407
// --- Variable update ---------------------------------------------
404408

405409
m_logger.trace(
406-
"[{}][{}] pre LS iter={:d} f={:g} ‖∇f‖={:g}",
410+
"[{}][{}] pre LS iter={:d} f={:g} {}={:g}",
407411
descent_strategy_name(), m_line_search->name(),
408-
m_current.iterations, energy, m_current.gradNorm);
412+
m_current.iterations, energy, log::norm(log::grad("f")), m_current.gradNorm);
409413

410414
// Perform a line_search to compute step scale
411415
double rate;

src/polysolve/nonlinear/line_search/Backtracking.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ namespace polysolve::nonlinear::line_search
5252
continue;
5353
}
5454

55-
m_logger.trace("ls it: {} ΔE: {}", cur_iter, new_energy - old_energy);
55+
m_logger.trace("ls it: {} {}: {}", cur_iter, log::delta("E"), new_energy - old_energy);
5656

5757
if (criteria(delta_x, objFunc, use_grad_norm, old_energy, old_grad, new_x, new_energy, step_size))
5858
{

0 commit comments

Comments
 (0)