Skip to content

Commit d0dfeaa

Browse files
author
teseoch
authored
Merge pull request #81 from polyfem/mtao/spdlog_external
Enable use of external fmt
2 parents 68aacce + 26702a7 commit d0dfeaa

File tree

7 files changed

+57
-41
lines changed

7 files changed

+57
-41
lines changed

src/polysolve/Utils.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
#include "Utils.hpp"
22

3+
#if defined(SPDLOG_FMT_EXTERNAL)
4+
#include <fmt/color.h>
5+
#else
36
#include <spdlog/fmt/bundled/color.h>
7+
#endif
48

59
namespace polysolve
610
{
@@ -49,7 +53,7 @@ namespace polysolve
4953

5054
void StopWatch::log_msg()
5155
{
52-
const static std::string log_fmt_text =
56+
const static auto log_fmt_text =
5357
fmt::format("[{}] {{}} {{:.3g}}s", fmt::format(fmt::fg(fmt::terminal_color::magenta), "timing"));
5458

5559
if (!m_name.empty())
@@ -60,7 +64,7 @@ namespace polysolve
6064

6165
void log_and_throw_error(spdlog::logger &logger, const std::string &msg)
6266
{
63-
logger.error(msg);
67+
logger.error("{}", msg);
6468
throw std::runtime_error(msg);
6569
}
6670

@@ -79,4 +83,4 @@ namespace polysolve
7983
return json[name];
8084
}
8185

82-
} // namespace polysolve
86+
} // namespace polysolve

src/polysolve/Utils.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ namespace polysolve
5555
template <typename... Args>
5656
[[noreturn]] void log_and_throw_error(spdlog::logger &logger, const std::string &msg, const Args &...args)
5757
{
58-
log_and_throw_error(logger, fmt::format(msg, args...));
58+
log_and_throw_error(logger, fmt::format(fmt::runtime(msg), args...));
5959
}
6060

6161
Eigen::SparseMatrix<double> sparse_identity(int rows, int cols);

src/polysolve/nonlinear/Criteria.cpp

Lines changed: 23 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,10 @@ namespace polysolve::nonlinear
2929

3030
void Criteria::print(std::ostream &os) const
3131
{
32-
os << fmt::format(
32+
os << print_message();
33+
}
34+
std::string Criteria::print_message() const {
35+
return fmt::format(
3336
"iters={:d} Δf={:g} ‖∇f‖={:g} ‖Δx‖={:g} Δx⋅∇f(x)={:g}",
3437
iterations, fDelta, gradNorm, xDelta, xDeltaDotGrad);
3538
}
@@ -61,47 +64,39 @@ namespace polysolve::nonlinear
6164
return Status::Continue;
6265
}
6366

64-
std::ostream &operator<<(std::ostream &os, const Status &s)
65-
{
67+
std::string_view status_message(Status s) {
6668
switch (s)
6769
{
6870
case Status::NotStarted:
69-
os << "Solver not started";
70-
break;
71+
return "Solver not started";
7172
case Status::Continue:
72-
os << "Convergence criteria not reached";
73-
break;
73+
return "Convergence criteria not reached";
7474
case Status::IterationLimit:
75-
os << "Iteration limit reached";
76-
break;
75+
return "Iteration limit reached";
7776
case Status::XDeltaTolerance:
78-
os << "Change in parameter vector too small";
79-
break;
77+
return "Change in parameter vector too small";
8078
case Status::FDeltaTolerance:
81-
os << "Change in cost function value too small";
82-
break;
79+
return "Change in cost function value too small";
8380
case Status::GradNormTolerance:
84-
os << "Gradient vector norm too small";
85-
break;
81+
return "Gradient vector norm too small";
8682
case Status::ObjectiveCustomStop:
87-
os << "Objective function specified to stop";
88-
break;
83+
return "Objective function specified to stop";
8984
case Status::NanEncountered:
90-
os << "Objective or gradient function returned NaN";
91-
break;
85+
return "Objective or gradient function returned NaN";
9286
case Status::NotDescentDirection:
93-
os << "Search direction not a descent direction";
94-
break;
87+
return "Search direction not a descent direction";
9588
case Status::LineSearchFailed:
96-
os << "Line search failed";
97-
break;
89+
return "Line search failed";
9890
case Status::UpdateDirectionFailed:
99-
os << "Update direction could not be computed";
100-
break;
91+
return "Update direction could not be computed";
10192
default:
102-
os << "Unknown status";
103-
break;
93+
return "Unknown status";
10494
}
95+
}
96+
97+
std::ostream &operator<<(std::ostream &os, const Status &s)
98+
{
99+
os << status_message(s);
105100
return os;
106101
}
107102

@@ -110,4 +105,4 @@ namespace polysolve::nonlinear
110105
c.print(os);
111106
return os;
112107
}
113-
} // namespace polysolve::nonlinear
108+
} // namespace polysolve::nonlinear

src/polysolve/nonlinear/Criteria.hpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
#include <cstddef>
44
#include <iostream>
5+
#include <string_view>
56

67
namespace polysolve::nonlinear
78
{
@@ -43,11 +44,15 @@ namespace polysolve::nonlinear
4344
void reset();
4445

4546
void print(std::ostream &os) const;
47+
std::string print_message() const;
4648
};
4749

4850
Status checkConvergence(const Criteria &stop, const Criteria &current);
4951

52+
std::string_view status_message(Status s);
53+
std::string criteria_message(const Criteria& s);
54+
5055
std::ostream &operator<<(std::ostream &os, const Status &s);
5156

5257
std::ostream &operator<<(std::ostream &os, const Criteria &c);
53-
} // namespace polysolve::nonlinear
58+
} // namespace polysolve::nonlinear

src/polysolve/nonlinear/Solver.cpp

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,11 @@
1414
#include <jse/jse.h>
1515

1616
#include <spdlog/spdlog.h>
17+
#if defined(SPDLOG_FMT_EXTERNAL)
18+
#include <fmt/color.h>
19+
#else
1720
#include <spdlog/fmt/bundled/color.h>
21+
#endif
1822
#include <spdlog/fmt/ostr.h>
1923

2024
#include <finitediff.hpp>
@@ -279,7 +283,7 @@ namespace polysolve::nonlinear
279283

280284
m_logger.debug(
281285
"Starting {} with {} solve f₀={:g} (stopping criteria: {})",
282-
descent_strategy_name(), m_line_search->name(), objFunc(x), m_stop);
286+
descent_strategy_name(), m_line_search->name(), objFunc(x), m_stop.print_message());
283287

284288
update_solver_info(objFunc(x));
285289
objFunc.post_step(PostStepData(m_current.iterations, solver_info, x, grad));
@@ -350,12 +354,12 @@ namespace polysolve::nonlinear
350354
m_status = Status::UpdateDirectionFailed;
351355
log_and_throw_error(
352356
m_logger, "[{}][{}] {} on last strategy; stopping",
353-
current_name, m_line_search->name(), m_status);
357+
current_name, m_line_search->name(), status_message(m_status));
354358
}
355359

356360
m_logger.debug(
357361
"[{}][{}] {}; reverting to {}", current_name, m_line_search->name(),
358-
Status::UpdateDirectionFailed, descent_strategy_name());
362+
status_message(Status::UpdateDirectionFailed), descent_strategy_name());
359363
m_status = Status::Continue;
360364
continue;
361365
}
@@ -374,15 +378,15 @@ namespace polysolve::nonlinear
374378
m_status = Status::NotDescentDirection;
375379
log_and_throw_error(
376380
m_logger, "[{}][{}] {} on last strategy (‖Δx‖={:g}; ‖g‖={:g}; Δx⋅g={:g}≥0); stopping",
377-
current_name, m_line_search->name(), m_status, delta_x.norm(), compute_grad_norm(x, grad),
381+
current_name, m_line_search->name(), status_message(m_status), delta_x.norm(), compute_grad_norm(x, grad),
378382
m_current.xDeltaDotGrad);
379383
}
380384
else
381385
{
382386
m_status = Status::Continue;
383387
m_logger.debug(
384388
"[{}][{}] {} (‖Δx‖={:g}; ‖g‖={:g}; Δx⋅g={:g}≥0); reverting to {}",
385-
current_name, m_line_search->name(), Status::NotDescentDirection,
389+
current_name, m_line_search->name(), status_message(Status::NotDescentDirection),
386390
delta_x.norm(), compute_grad_norm(x, grad), m_current.xDeltaDotGrad,
387391
descent_strategy_name());
388392
}
@@ -479,7 +483,7 @@ namespace polysolve::nonlinear
479483

480484
m_logger.debug(
481485
"[{}][{}] {} (stopping criteria: {})",
482-
descent_strategy_name(), m_line_search->name(), m_current, m_stop);
486+
descent_strategy_name(), m_line_search->name(), m_current.print_message(), m_stop.print_message());
483487

484488
if (++m_current.iterations >= m_stop.iterations)
485489
m_status = Status::IterationLimit;
@@ -501,8 +505,8 @@ namespace polysolve::nonlinear
501505
m_logger.log(
502506
succeeded ? spdlog::level::info : spdlog::level::err,
503507
"[{}][{}] Finished: {} took {:g}s ({}) (stopping criteria: {})",
504-
descent_strategy_name(), m_line_search->name(), m_status, tot_time,
505-
m_current, m_stop);
508+
descent_strategy_name(), m_line_search->name(), status_message(m_status), tot_time,
509+
m_current.print_message(), m_stop.print_message());
506510

507511
log_times();
508512
update_solver_info(objFunc(x));

src/polysolve/nonlinear/descent_strategies/Newton.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@
22

33
#include <polysolve/Utils.hpp>
44

5+
#if defined(SPDLOG_FMT_EXTERNAL)
6+
#include <fmt/color.h>
7+
#else
58
#include <spdlog/fmt/bundled/color.h>
9+
#endif
610

711
namespace polysolve::nonlinear
812
{

src/polysolve/nonlinear/line_search/LineSearch.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,11 @@
99

1010
#include <polysolve/Types.hpp>
1111

12+
#if defined(SPDLOG_FMT_EXTERNAL)
13+
#include <fmt/color.h>
14+
#else
1215
#include <spdlog/fmt/bundled/color.h>
16+
#endif
1317

1418
#include <cfenv>
1519

0 commit comments

Comments
 (0)