FEAT 3
Finite Element Analysis Toolbox
Loading...
Searching...
No Matches
fixed_step_linesearch.hpp
1// FEAT3: Finite Element Analysis Toolbox, Version 3
2// Copyright (C) 2010 by Stefan Turek & the FEAT group
3// FEAT3 is released under the GNU General Public License version 3,
4// see the file 'copyright.txt' in the top level directory for details.
5
6#pragma once
8#include <kernel/solver/linesearch.hpp>
9
10namespace FEAT
11{
12 namespace Solver
13 {
14
21 template<typename Functional_, typename Filter_>
22 class FixedStepLinesearch : public Linesearch<Functional_, Filter_>
23 {
24 public:
26 typedef Filter_ FilterType;
28 typedef typename Functional_::VectorTypeR VectorType;
30 typedef typename Functional_::DataType DataType;
33
34 private:
37
38 public:
55 explicit FixedStepLinesearch(Functional_& functional, Filter_& filter, const DataType step_length,
56 bool keep_iterates = false) :
57 BaseClass("FS-LS", functional, filter, keep_iterates),
58 _step_length(step_length)
59 {
61 }
62
81 explicit FixedStepLinesearch(const String& section_name, const PropertyMap* section,
82 Functional_& functional, Filter_& filter) :
83 BaseClass("FS-LS", section_name, section, functional, filter),
84 _step_length(-1)
85 {
86 this->set_max_iter(0);
88
89 // Check if we need to set the step length
90 auto step_length_p = section->query("step_length");
91 if(step_length_p.second)
92 {
93 set_step_length(std::stod(step_length_p.first));
94 }
95 else
96 {
97 XABORTM(name()+" config section is missing the mandatory step_length key!");
98 }
99
100 }
101
104 {
105 }
106
108 virtual String name() const override
109 {
110 return "Fixed-Step-Linesearch";
111 }
112
114 virtual DataType get_rel_update() const override
115 {
116 return _step_length*this->_norm_dir;
117 }
118
122 virtual void set_step_length(DataType step_length)
123 {
124 XASSERT(step_length > DataType(0));
125
126 _step_length = step_length;
127 }
128
140 virtual Status apply(VectorType& vec_cor, const VectorType& vec_dir) override
141 {
142 this->_norm_dir = vec_dir.norm2();
143 // clear solution vector
144 vec_cor.format();
145 vec_cor.axpy(vec_dir, _step_length);
146
147 this->_functional.prepare(vec_cor, this->_filter);
148
149 return Status::success;
150 }
151
163 virtual Status correct(VectorType& vec_sol, const VectorType& vec_dir) override
164 {
165 this->_norm_dir = vec_dir.norm2();
166 vec_sol.axpy(vec_dir, _step_length);
167
168 this->_functional.prepare(vec_sol, this->_filter);
169 this->_functional.eval_fval_grad(this->_fval_min, this->_vec_grad);
170 this->_filter.filter_def(this->_vec_grad);
171
172 return Status::success;
173 }
174 }; // class FixedStepLinesearch
175
194 template<typename Functional_, typename Filter_>
195 inline std::shared_ptr<FixedStepLinesearch<Functional_, Filter_>> new_fixed_step_linesearch(
196 Functional_& functional, Filter_& filter, typename Functional_::DataType step_length, bool keep_iterates = false)
197 {
198 return
199 std::make_shared<FixedStepLinesearch<Functional_, Filter_>>(functional, filter, step_length, keep_iterates);
200 }
201
220 template<typename Functional_, typename Filter_>
221 inline std::shared_ptr<FixedStepLinesearch<Functional_, Filter_>> new_fixed_step_linesearch(
222 const String& section_name, const PropertyMap* section, Functional_& functional, Filter_& filter)
223 {
224 return
225 std::make_shared<FixedStepLinesearch<Functional_, Filter_>>(section_name, section, functional, filter);
226 }
227 } // namespace Solver
228} // namespace FEAT
#define XABORTM(msg)
Abortion macro definition with custom message.
Definition: assertion.hpp:192
#define XASSERT(expr)
Assertion macro definition.
Definition: assertion.hpp:262
FEAT Kernel base header.
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.
Linesearch< Functional_, Filter_ > BaseClass
Our base class.
virtual DataType get_rel_update() const override
Get the relative update of the solver application.
Functional_::VectorTypeR VectorType
Input vector type for the functional's gradient.
virtual String name() const override
Returns a descriptive string.
virtual Status apply(VectorType &vec_cor, const VectorType &vec_dir) override
Applies the solver, setting the initial guess to zero.
virtual void set_step_length(DataType step_length)
Sets the step length.
Functional_::DataType DataType
Underlying floating point type.
Filter_ FilterType
Filter type to be applied to the gradient of the functional.
virtual Status correct(VectorType &vec_sol, const VectorType &vec_dir) override
Applies the solver, making use of an initial guess.
FixedStepLinesearch(Functional_ &functional, Filter_ &filter, const DataType step_length, bool keep_iterates=false)
Standard constructor.
DataType _step_length
The length of the step.
FixedStepLinesearch(const String &section_name, const PropertyMap *section, Functional_ &functional, Filter_ &filter)
Constructor using a PropertyMap.
void set_max_iter(Index max_iter)
Sets the maximum iteration count for the solver.
Definition: iterative.hpp:455
Linesearch base class.
Definition: linesearch.hpp:34
VectorType _vec_grad
Gradient vector.
Definition: linesearch.hpp:53
DataType _norm_dir
The 2-norm of the search direction.
Definition: linesearch.hpp:75
Functional_ & _functional
The (nonlinear) functional.
Definition: linesearch.hpp:48
DataType _fval_min
Functional functional value.
Definition: linesearch.hpp:62
Filter_ & _filter
The filter to be applied to the functional's gradient.
Definition: linesearch.hpp:50
DataType _alpha_min
Line search parameter.
Definition: linesearch.hpp:71
String class implementation.
Definition: string.hpp:46
std::shared_ptr< FixedStepLinesearch< Functional_, Filter_ > > new_fixed_step_linesearch(Functional_ &functional, Filter_ &filter, typename Functional_::DataType step_length, bool keep_iterates=false)
Creates a new FixedStepLinesearch object.
Status
Solver status return codes enumeration.
Definition: base.hpp:47
@ success
solving successful (convergence criterion fulfilled)
FEAT namespace.
Definition: adjactor.hpp:12