Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions src/polysolve/Utils.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
#include "Utils.hpp"

#if defined(SPDLOG_FMT_EXTERNAL)
#include <fmt/color.h>
#else
#include <spdlog/fmt/bundled/color.h>
#endif

namespace polysolve
{
Expand Down Expand Up @@ -49,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 @@ -60,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 All @@ -79,4 +83,4 @@ namespace polysolve
return json[name];
}

} // namespace polysolve
} // namespace polysolve
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();
}
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";
case Status::Continue:
os << "Convergence criteria not reached";
break;
return "Convergence criteria not reached";
case Status::IterationLimit:
os << "Iteration limit reached";
break;
return "Iteration limit reached";
case Status::XDeltaTolerance:
os << "Change in parameter vector too small";
break;
return "Change in parameter vector too small";
case Status::FDeltaTolerance:
os << "Change in cost function value too small";
break;
return "Change in cost function value too small";
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";
case Status::NanEncountered:
os << "Objective or gradient function returned NaN";
break;
return "Objective or gradient function returned NaN";
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";
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";
}
}

std::ostream &operator<<(std::ostream &os, const Status &s)
{
os << status_message(s);
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
20 changes: 12 additions & 8 deletions src/polysolve/nonlinear/Solver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@
#include <jse/jse.h>

#include <spdlog/spdlog.h>
#if defined(SPDLOG_FMT_EXTERNAL)
#include <fmt/color.h>
#else
#include <spdlog/fmt/bundled/color.h>
#endif
#include <spdlog/fmt/ostr.h>

#include <finitediff.hpp>
Expand Down Expand Up @@ -279,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 @@ -350,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));
}

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 @@ -374,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),
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 @@ -473,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 @@ -495,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
4 changes: 4 additions & 0 deletions src/polysolve/nonlinear/descent_strategies/Newton.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@

#include <polysolve/Utils.hpp>

#if defined(SPDLOG_FMT_EXTERNAL)
#include <fmt/color.h>
#else
#include <spdlog/fmt/bundled/color.h>
#endif

namespace polysolve::nonlinear
{
Expand Down
4 changes: 4 additions & 0 deletions src/polysolve/nonlinear/line_search/LineSearch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@

#include <polysolve/Types.hpp>

#if defined(SPDLOG_FMT_EXTERNAL)
#include <fmt/color.h>
#else
#include <spdlog/fmt/bundled/color.h>
#endif

#include <cfenv>

Expand Down