8#include <kernel/solver/iterative.hpp>
32 template<
typename Functional_,
typename Filter_>
41 typedef typename Functional_::DataType
DataType;
111 bool keep_iterates =
false) :
134 iterates =
new std::deque<VectorType>;
158 Functional_& functional, Filter_& filter) :
159 BaseClass(plot_name, section_name, section),
177 auto keep_iterates_p = section->
query(
"keep_iterates");
178 if(keep_iterates_p.second)
180 iterates =
new std::deque<VectorType>;
183 auto tol_curvature_p = section->
query(
"tol_curvature");
184 if(tol_curvature_p.second)
189 auto tol_decrease_p = section->
query(
"tol_decrease");
190 if(tol_decrease_p.second)
195 auto tol_step_p = section->
query(
"tol_step");
196 if(tol_step_p.second)
217 +
", evals: "+
stringify(_functional.get_num_func_evals())+
" (func) "
279 _vec_tmp = this->_functional.create_vector_r();
280 _vec_pn = this->_functional.create_vector_r();
281 _vec_grad = this->_functional.create_vector_r();
302 _fval_0 = Math::huge<DataType>();
380 vec_def.scale(this->_vec_grad,
DataType(-1));
394 this->_vec_grad.scale(vec_def,
DataType(-1));
435 this->_vec_grad.format(
DataType(0));
469 this->_vec_initial_sol.copy(vec_sol);
471 this->_vec_pn.copy(vec_dir);
472 this->_norm_dir = this->_vec_pn.norm2();
475 this->_vec_pn.scale(this->_vec_pn,
DataType(1)/this->_vec_pn.max_abs_element());
477 this->_vec_pn.scale(this->_vec_pn,
DataType(1)/this->_vec_pn.norm2());
480 this->_fval_min = this->
_fval_0;
481 this->_delta_0 = this->_vec_pn.dot(this->_vec_grad);
492 this->_norm_sol = vec_sol.norm2();
549 Statistics::add_solver_expression(
#define XABORTM(msg)
Abortion macro definition with custom message.
#define XASSERT(expr)
Assertion macro definition.
A class organizing a tree of key-value pairs.
std::pair< String, bool > query(String key_path) const
Queries a value by its key path.
Abstract base-class for iterative solvers.
DataType _def_init
initial defect
Status get_status() const
Returns the status.
String get_plot_name() const
Returns the plot name of the solver.
Index _iter_digits
iteration count digits for plotting
Index get_num_iter() const
Returns number of performed iterations.
void _set_comm_by_matrix(const Matrix_ &matrix)
Sets the communicator for the solver from a matrix.
void _print_line(const String &line) const
Prints a line.
bool is_diverged() const
checks for divergence
String _plot_name
name of the solver in plots
Index _num_iter
number of performed iterations
bool _plot_iter(Status st=Status::progress) const
Plot the current iteration?
DataType _def_cur
current defect
Index _max_iter
maximum number of iterations
virtual ~Linesearch()
Virtual destructor.
VectorType _vec_grad
Gradient vector.
void set_tol_curvature(DataType tol_curvature)
Sets the tolerance for the sufficient decrease in curvature.
virtual void init_symbolic() override
Symbolic initialization method.
DataType _norm_dir
The 2-norm of the search direction.
Functional_ & _functional
The (nonlinear) functional.
IterativeSolver< typename Functional_::VectorTypeR > BaseClass
Our base class.
VectorType _vec_initial_sol
Initial solution.
void set_grad_from_defect(const VectorType &vec_def)
Gets a defect vector from the final gradient.
void get_defect_from_grad(VectorType &vec_def) const
Sets the initial gradient from a defect vector.
virtual void trim_func_grad(DataType &func)
Trims the function value and gradient according to some threshold.
Functional_::VectorTypeR VectorType
Input vector type for the functional's gradient.
DataType _trim_threshold
Threshold for trimming function value and gradient.
DataType _fval_0
Initial functional value.
void set_initial_fval(DataType f0)
Sets the intitial functional value.
void set_tol_decrease(DataType tol_decrease)
Sets the tolerance for the sufficient decrease in functional value.
const VectorType & get_initial_sol() const
Gets the initial solution the linesearch started with.
DataType _tol_curvature
Tolerance for sufficient decrease in the norm of the gradient (Wolfe conditions)
void set_tol_step(DataType tol_step)
Sets the step length tolerance.
DataType _norm_sol
The 2-norm of the iterate.
virtual void done_symbolic() override
Symbolic finalization method.
virtual Status _startup(DataType &alpha, DataType &fval, DataType &delta, const VectorType &vec_sol, const VectorType &vec_dir)
Performs the startup of the iteration.
VectorType _vec_pn
descend direction vector, normalized for better numerical stability
Linesearch(const String &plot_name, Functional_ &functional, Filter_ &filter, bool keep_iterates=false)
Standard constructor.
virtual DataType get_rel_update() const
Get the relative update of the solver application.
std::deque< VectorType > * iterates
For debugging purposes, it is possible to save all iterates to this.
virtual void reset()
Resets various member variables in case the solver is reused.
DataType _delta_0
Initial <vec_dir, vec_grad>
DataType get_final_fval() const
Gets the functional value of the last iteration.
DataType _fval_min
Functional functional value.
Filter_ FilterType
Filter type to be applied to the gradient of the functional.
virtual String get_summary() const override
Returns a summary string.
Filter_ & _filter
The filter to be applied to the functional's gradient.
DataType _tol_decrease
Tolerance for sufficient decrease in the functional value (Wolfe conditions)
DataType _alpha_0
Initial line search parameter.
DataType _alpha_min
Line search parameter.
void set_dir_scaling(const bool b)
Determines if search direction scaling is to be used.
DataType _tol_step
Tolerance for the update step.
Functional_::DataType DataType
Underlying floating point type.
VectorType _vec_tmp
temporary vector
virtual Status _check_convergence(const DataType fval, const DataType df, const DataType alpha)
Performs the line search convergence checks using the strong Wolfe conditions.
Linesearch(const String &plot_name, const String §ion_name, const PropertyMap *section, Functional_ &functional, Filter_ &filter)
Constructor using a PropertyMap.
virtual void init_symbolic()
Symbolic initialization method.
virtual void done_symbolic()
Symbolic finalization method.
virtual String name() const =0
Returns a descriptive string.
String class implementation.
String pad_front(size_type len, char c=' ') const
Pads the front of the string up to a desired length.
T_ abs(T_ x)
Returns the absolute value.
bool isfinite(T_ x)
Checks whether a value is finite.
Status
Solver status return codes enumeration.
@ success
solving successful (convergence criterion fulfilled)
@ progress
continue iteration (internal use only)
@ max_iter
solver reached maximum iterations
@ diverged
solver diverged (divergence criterion fulfilled)
@ aborted
premature abort (solver aborted due to internal errors or preconditioner failure)
String stringify(const T_ &item)
Converts an item into a String.
String stringify_fp_sci(DataType_ value, int precision=0, int width=0, bool sign=false)
Prints a floating point value to a string in scientific notation.
std::uint64_t Index
Index data type.