version 0.4.4
nonlinearsolverlogger.hh
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2021-2025 The Ikarus Developers ikarus@ibb.uni-stuttgart.de
2// SPDX-License-Identifier: LGPL-3.0-or-later
3
10#pragma once
11#include <spdlog/spdlog.h>
12
16
17namespace Ikarus {
18
23{
24public:
25 template <typename BC>
27 this->subscribe(bc, [&](NonLinearSolverMessages message, const BC::State& state) { this->update(message, state); });
28 return *this;
29 }
30
38 constexpr bool isDomainAVector = Concepts::EigenVector<typename std::remove_cvref_t<decltype(state)>::Domain>;
39 switch (message) {
41 init<isDomainAVector>();
42 break;
44 iterationEnded<isDomainAVector>();
45 break;
47 rNorm_ = state.information.residualNorm;
48 break;
50 if constexpr (not isDomainAVector)
51 lambda_ = state.domain.parameter();
52 break;
54 dNorm_ = state.information.correctionNorm;
55 break;
57 finishedSuccessfully(state.information.iterations);
58 break;
59 default:
60 break;
61 }
62 }
63
64private:
65 int iters_{0};
66 double dNorm_{0};
67 double rNorm_{0};
68 double lambda_{0};
69
70 template <bool isDomainAVector>
71 void init() {
72 iters_ = 1;
73 rNorm_ = 0.0;
74 dNorm_ = 0.0;
75 spdlog::info("Non-linear solver started:");
76 if (not isDomainAVector) {
77 spdlog::info("{:<11} {:<20} {:<20} {:<20}", "Ite", "normR", "normD", "lambda");
78 spdlog::info("-------------------------------------------------------------------------------");
79 } else {
80 spdlog::info("{:<11} {:<20} {:<20}", "Ite", "normR", "normD");
81 spdlog::info("-------------------------------------------------");
82 }
83 }
84
85 template <bool isDomainAVector>
86 void iterationEnded() {
87 if (not isDomainAVector)
88 spdlog::info("{} {:<10d} {:<20.2e} {:<20.2e} {:<20.2e}", "", iters_, rNorm_, dNorm_, lambda_);
89 else
90 spdlog::info("{} {:<10d} {:<20.2e} {:<20.2e}", "", iters_, rNorm_, dNorm_);
91 ++iters_;
92 }
93
94 void finishedSuccessfully(int numberOfIterations) { spdlog::info("Number of iterations: {}", numberOfIterations); }
95};
96} // namespace Ikarus
Implementation of the observer design pattern with broadcasters.
Enums for observer messages.
Definition: assemblermanipulatorbuildingblocks.hh:22
NonLinearSolverMessages
Enum class defining non-linear solver-related messages.
Definition: broadcastermessages.hh:22
Implements a listener.
Definition: listener.hh:28
auto subscribe(Broadcaster &broadcaster, std::function< void(typename Broadcaster::MessageType, const typename Broadcaster::State &)> callback)
Function to subscribe to a broadcaster with a given function (either a lambda, std::function or funct...
Definition: listener.hh:41
Implementation of an observer for logging non-linear solvers.
Definition: nonlinearsolverlogger.hh:23
void update(NonLinearSolverMessages message, const Concepts::NonLinearSolverState auto &state)
Implementation of the update method for logging control messages with a control routine state.
Definition: nonlinearsolverlogger.hh:37
NonLinearSolverLogger & subscribeTo(BC &bc)
Definition: nonlinearsolverlogger.hh:26
Concept defining the requirements for Eigen vectors.
Definition: utils/concepts.hh:365
Concept to check if a type represents a nonlinear solver state.
Definition: utils/concepts.hh:664
Several concepts.