Skip to content

Commit

Permalink
changing code that depends on the built-in fmt, removing use of fmt/o…
Browse files Browse the repository at this point in the history
…stream.h because it seems to break
  • Loading branch information
mtao committed Sep 12, 2024
1 parent 19d9b26 commit 26702a7
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 40 deletions.
4 changes: 2 additions & 2 deletions src/polysolve/Utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ namespace polysolve

void StopWatch::log_msg()
{
const static std::string log_fmt_text =
const static auto log_fmt_text =
fmt::format("[{}] {{}} {{:.3g}}s", fmt::format(fmt::fg(fmt::terminal_color::magenta), "timing"));

if (!m_name.empty())
Expand All @@ -64,7 +64,7 @@ namespace polysolve

void log_and_throw_error(spdlog::logger &logger, const std::string &msg)
{
logger.error(msg);
logger.error("{}", msg);
throw std::runtime_error(msg);
}

Expand Down
2 changes: 1 addition & 1 deletion src/polysolve/Utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ namespace polysolve
template <typename... Args>
[[noreturn]] void log_and_throw_error(spdlog::logger &logger, const std::string &msg, const Args &...args)
{
log_and_throw_error(logger, fmt::format(msg, args...));
log_and_throw_error(logger, fmt::format(fmt::runtime(msg), args...));
}

Eigen::SparseMatrix<double> sparse_identity(int rows, int cols);
Expand Down
51 changes: 23 additions & 28 deletions src/polysolve/nonlinear/Criteria.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@ namespace polysolve::nonlinear

void Criteria::print(std::ostream &os) const
{
os << fmt::format(
os << print_message();

Check warning on line 32 in src/polysolve/nonlinear/Criteria.cpp

View check run for this annotation

Codecov / codecov/patch

src/polysolve/nonlinear/Criteria.cpp#L32

Added line #L32 was not covered by tests
}
std::string Criteria::print_message() const {
return fmt::format(
"iters={:d} Δf={:g} ‖∇f‖={:g} ‖Δx‖={:g} Δx⋅∇f(x)={:g}",
iterations, fDelta, gradNorm, xDelta, xDeltaDotGrad);
}
Expand Down Expand Up @@ -61,47 +64,39 @@ namespace polysolve::nonlinear
return Status::Continue;
}

std::ostream &operator<<(std::ostream &os, const Status &s)
{
std::string_view status_message(Status s) {
switch (s)
{
case Status::NotStarted:
os << "Solver not started";
break;
return "Solver not started";

Check warning on line 71 in src/polysolve/nonlinear/Criteria.cpp

View check run for this annotation

Codecov / codecov/patch

src/polysolve/nonlinear/Criteria.cpp#L71

Added line #L71 was not covered by tests
case Status::Continue:
os << "Convergence criteria not reached";
break;
return "Convergence criteria not reached";

Check warning on line 73 in src/polysolve/nonlinear/Criteria.cpp

View check run for this annotation

Codecov / codecov/patch

src/polysolve/nonlinear/Criteria.cpp#L73

Added line #L73 was not covered by tests
case Status::IterationLimit:
os << "Iteration limit reached";
break;
return "Iteration limit reached";

Check warning on line 75 in src/polysolve/nonlinear/Criteria.cpp

View check run for this annotation

Codecov / codecov/patch

src/polysolve/nonlinear/Criteria.cpp#L75

Added line #L75 was not covered by tests
case Status::XDeltaTolerance:
os << "Change in parameter vector too small";
break;
return "Change in parameter vector too small";

Check warning on line 77 in src/polysolve/nonlinear/Criteria.cpp

View check run for this annotation

Codecov / codecov/patch

src/polysolve/nonlinear/Criteria.cpp#L77

Added line #L77 was not covered by tests
case Status::FDeltaTolerance:
os << "Change in cost function value too small";
break;
return "Change in cost function value too small";

Check warning on line 79 in src/polysolve/nonlinear/Criteria.cpp

View check run for this annotation

Codecov / codecov/patch

src/polysolve/nonlinear/Criteria.cpp#L79

Added line #L79 was not covered by tests
case Status::GradNormTolerance:
os << "Gradient vector norm too small";
break;
return "Gradient vector norm too small";
case Status::ObjectiveCustomStop:
os << "Objective function specified to stop";
break;
return "Objective function specified to stop";

Check warning on line 83 in src/polysolve/nonlinear/Criteria.cpp

View check run for this annotation

Codecov / codecov/patch

src/polysolve/nonlinear/Criteria.cpp#L83

Added line #L83 was not covered by tests
case Status::NanEncountered:
os << "Objective or gradient function returned NaN";
break;
return "Objective or gradient function returned NaN";

Check warning on line 85 in src/polysolve/nonlinear/Criteria.cpp

View check run for this annotation

Codecov / codecov/patch

src/polysolve/nonlinear/Criteria.cpp#L85

Added line #L85 was not covered by tests
case Status::NotDescentDirection:
os << "Search direction not a descent direction";
break;
return "Search direction not a descent direction";
case Status::LineSearchFailed:
os << "Line search failed";
break;
return "Line search failed";

Check warning on line 89 in src/polysolve/nonlinear/Criteria.cpp

View check run for this annotation

Codecov / codecov/patch

src/polysolve/nonlinear/Criteria.cpp#L89

Added line #L89 was not covered by tests
case Status::UpdateDirectionFailed:
os << "Update direction could not be computed";
break;
return "Update direction could not be computed";
default:
os << "Unknown status";
break;
return "Unknown status";

Check warning on line 93 in src/polysolve/nonlinear/Criteria.cpp

View check run for this annotation

Codecov / codecov/patch

src/polysolve/nonlinear/Criteria.cpp#L93

Added line #L93 was not covered by tests
}
}

std::ostream &operator<<(std::ostream &os, const Status &s)

Check warning on line 97 in src/polysolve/nonlinear/Criteria.cpp

View check run for this annotation

Codecov / codecov/patch

src/polysolve/nonlinear/Criteria.cpp#L97

Added line #L97 was not covered by tests
{
os << status_message(s);

Check warning on line 99 in src/polysolve/nonlinear/Criteria.cpp

View check run for this annotation

Codecov / codecov/patch

src/polysolve/nonlinear/Criteria.cpp#L99

Added line #L99 was not covered by tests
return os;
}

Expand All @@ -110,4 +105,4 @@ namespace polysolve::nonlinear
c.print(os);
return os;
}
} // namespace polysolve::nonlinear
} // namespace polysolve::nonlinear
7 changes: 6 additions & 1 deletion src/polysolve/nonlinear/Criteria.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include <cstddef>
#include <iostream>
#include <string_view>

namespace polysolve::nonlinear
{
Expand Down Expand Up @@ -43,11 +44,15 @@ namespace polysolve::nonlinear
void reset();

void print(std::ostream &os) const;
std::string print_message() const;
};

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

std::string_view status_message(Status s);
std::string criteria_message(const Criteria& s);

std::ostream &operator<<(std::ostream &os, const Status &s);

std::ostream &operator<<(std::ostream &os, const Criteria &c);
} // namespace polysolve::nonlinear
} // namespace polysolve::nonlinear
16 changes: 8 additions & 8 deletions src/polysolve/nonlinear/Solver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ namespace polysolve::nonlinear

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

update_solver_info(objFunc(x));
objFunc.post_step(PostStepData(m_current.iterations, solver_info, x, grad));
Expand Down Expand Up @@ -354,12 +354,12 @@ namespace polysolve::nonlinear
m_status = Status::UpdateDirectionFailed;
log_and_throw_error(
m_logger, "[{}][{}] {} on last strategy; stopping",
current_name, m_line_search->name(), m_status);
current_name, m_line_search->name(), status_message(m_status));

Check warning on line 357 in src/polysolve/nonlinear/Solver.cpp

View check run for this annotation

Codecov / codecov/patch

src/polysolve/nonlinear/Solver.cpp#L357

Added line #L357 was not covered by tests
}

m_logger.debug(
"[{}][{}] {}; reverting to {}", current_name, m_line_search->name(),
Status::UpdateDirectionFailed, descent_strategy_name());
status_message(Status::UpdateDirectionFailed), descent_strategy_name());
m_status = Status::Continue;
continue;
}
Expand All @@ -378,15 +378,15 @@ namespace polysolve::nonlinear
m_status = Status::NotDescentDirection;
log_and_throw_error(
m_logger, "[{}][{}] {} on last strategy (‖Δx‖={:g}; ‖g‖={:g}; Δx⋅g={:g}≥0); stopping",
current_name, m_line_search->name(), m_status, delta_x.norm(), compute_grad_norm(x, grad),
current_name, m_line_search->name(), status_message(m_status), delta_x.norm(), compute_grad_norm(x, grad),

Check warning on line 381 in src/polysolve/nonlinear/Solver.cpp

View check run for this annotation

Codecov / codecov/patch

src/polysolve/nonlinear/Solver.cpp#L381

Added line #L381 was not covered by tests
m_current.xDeltaDotGrad);
}
else
{
m_status = Status::Continue;
m_logger.debug(
"[{}][{}] {} (‖Δx‖={:g}; ‖g‖={:g}; Δx⋅g={:g}≥0); reverting to {}",
current_name, m_line_search->name(), Status::NotDescentDirection,
current_name, m_line_search->name(), status_message(Status::NotDescentDirection),
delta_x.norm(), compute_grad_norm(x, grad), m_current.xDeltaDotGrad,
descent_strategy_name());
}
Expand Down Expand Up @@ -477,7 +477,7 @@ namespace polysolve::nonlinear

m_logger.debug(
"[{}][{}] {} (stopping criteria: {})",
descent_strategy_name(), m_line_search->name(), m_current, m_stop);
descent_strategy_name(), m_line_search->name(), m_current.print_message(), m_stop.print_message());

if (++m_current.iterations >= m_stop.iterations)
m_status = Status::IterationLimit;
Expand All @@ -499,8 +499,8 @@ namespace polysolve::nonlinear
m_logger.log(
succeeded ? spdlog::level::info : spdlog::level::err,
"[{}][{}] Finished: {} took {:g}s ({}) (stopping criteria: {})",
descent_strategy_name(), m_line_search->name(), m_status, tot_time,
m_current, m_stop);
descent_strategy_name(), m_line_search->name(), status_message(m_status), tot_time,
m_current.print_message(), m_stop.print_message());

log_times();
update_solver_info(objFunc(x));
Expand Down

0 comments on commit 26702a7

Please sign in to comment.