Line data Source code
1 : /*
2 : * Copyright (c) 2010-2015: G-CSC, Goethe University Frankfurt
3 : * Author: Andreas Vogel, modifications nested Newton: Markus Knodel
4 : *
5 : * This file is part of UG4.
6 : *
7 : * UG4 is free software: you can redistribute it and/or modify it under the
8 : * terms of the GNU Lesser General Public License version 3 (as published by the
9 : * Free Software Foundation) with the following additional attribution
10 : * requirements (according to LGPL/GPL v3 §7):
11 : *
12 : * (1) The following notice must be displayed in the Appropriate Legal Notices
13 : * of covered and combined works: "Based on UG4 (www.ug4.org/license)".
14 : *
15 : * (2) The following notice must be displayed at a prominent place in the
16 : * terminal output of covered works: "Based on UG4 (www.ug4.org/license)".
17 : *
18 : * (3) The following bibliography is recommended for citation and must be
19 : * preserved in all covered files:
20 : * "Reiter, S., Vogel, A., Heppner, I., Rupp, M., and Wittum, G. A massively
21 : * parallel geometric multigrid solver on hierarchically distributed grids.
22 : * Computing and visualization in science 16, 4 (2013), 151-164"
23 : * "Vogel, A., Reiter, S., Rupp, M., Nägel, A., and Wittum, G. UG4 -- a novel
24 : * flexible software system for simulating pde based models on high performance
25 : * computers. Computing and visualization in science 16, 4 (2013), 165-179"
26 : *
27 : * This program is distributed in the hope that it will be useful,
28 : * but WITHOUT ANY WARRANTY; without even the implied warranty of
29 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30 : * GNU Lesser General Public License for more details.
31 : */
32 :
33 : #ifndef __H__UG__LIB_DISC__OPERATOR__NON_LINEAR_OPERATOR__NEWTON_SOLVER__NEWTON__
34 : #define __H__UG__LIB_DISC__OPERATOR__NON_LINEAR_OPERATOR__NEWTON_SOLVER__NEWTON__
35 :
36 : #include <cmath>
37 :
38 : #include "lib_algebra/operator/interface/operator_inverse.h"
39 : #include "lib_algebra/operator/interface/linear_operator_inverse.h"
40 : #include "lib_algebra/operator/debug_writer.h"
41 :
42 : // modul intern headers
43 : #include "lib_disc/assemble_interface.h"
44 : #include "lib_disc/operator/non_linear_operator/assembled_non_linear_operator.h"
45 : #include "lib_disc/operator/linear_operator/assembled_linear_operator.h"
46 : #include "../line_search.h"
47 : #include "newton_update_interface.h"
48 : #include "lib_algebra/operator/debug_writer.h"
49 :
50 : //#include "nestedNewtonRFSwitch.h"
51 :
52 : //#if ENABLE_NESTED_NEWTON_RESOLFUNC_UPDATE
53 : //
54 : //#include "newtonUpdaterGeneric.h"
55 : //
56 : //#endif
57 :
58 : namespace ug {
59 :
60 : /// Newton solver for assembling based discretizations
61 : template <typename TAlgebra>
62 : class NewtonSolver
63 : : public IOperatorInverse<typename TAlgebra::vector_type>,
64 : public DebugWritingObject<TAlgebra>
65 : {
66 : public:
67 : /// Algebra type
68 : typedef TAlgebra algebra_type;
69 :
70 : /// Vector type
71 : typedef typename TAlgebra::vector_type vector_type;
72 :
73 : /// Matrix type
74 : typedef typename TAlgebra::matrix_type matrix_type;
75 :
76 : public:
77 : /// constructor setting operator
78 : NewtonSolver(SmartPtr<IOperator<vector_type> > N);
79 :
80 : /// constructor using assembling
81 : NewtonSolver(SmartPtr<IAssemble<TAlgebra> > spAss);
82 :
83 : /// default constructor
84 : NewtonSolver();
85 :
86 : /// constructor
87 : NewtonSolver(SmartPtr<ILinearOperatorInverse<vector_type> > LinearSolver,
88 : SmartPtr<IConvergenceCheck<vector_type> > spConvCheck,
89 : SmartPtr<ILineSearch<vector_type> > spLineSearch);
90 :
91 : /// sets the linear solver
92 0 : void set_linear_solver(SmartPtr<ILinearOperatorInverse<vector_type> > LinearSolver) {m_spLinearSolver = LinearSolver;}
93 :
94 : /// sets the convergence check
95 : void set_convergence_check(SmartPtr<IConvergenceCheck<vector_type> > spConvCheck);
96 :
97 : /// sets the line search
98 0 : void set_line_search(SmartPtr<ILineSearch<vector_type> > spLineSearch) {m_spLineSearch = spLineSearch;}
99 0 : void disable_line_search() {m_spLineSearch = SPNULL;}
100 0 : SmartPtr<ILineSearch<vector_type> > line_search() {return m_spLineSearch;}
101 :
102 : /// This operator inverts the Operator N: Y -> X
103 : virtual bool init(SmartPtr<IOperator<vector_type> > N);
104 :
105 : /// prepare Operator
106 : virtual bool prepare(vector_type& u);
107 :
108 : /// apply Operator, i.e. N^{-1}(0) = u
109 : virtual bool apply(vector_type& u);
110 :
111 : /// returns information about configuration parameters
112 : /**
113 : * this should return necessary information about parameters and possibly
114 : * calling config_string of subcomponents.
115 : *
116 : * \returns std::string necessary information about configuration parameters
117 : */
118 :
119 : virtual std::string config_string() const;
120 :
121 : /// prints average linear solver convergence
122 : void print_average_convergence() const;
123 :
124 : /// information on convergence history
125 : /// \{
126 : size_t num_newton_steps() const;
127 : int num_linsolver_calls(size_t call) const;
128 : int num_linsolver_steps(size_t call) const;
129 : double average_linear_steps(size_t call) const;
130 : int total_linsolver_calls() const;
131 : int total_linsolver_steps() const;
132 : double total_average_linear_steps() const;
133 0 : int last_num_newton_steps() const {return m_lastNumSteps;}
134 : /// \}
135 :
136 : /// resets average linear solver convergence
137 : void clear_average_convergence();
138 :
139 : /// add inner step update (applied before every linear solver step)
140 0 : void add_inner_step_update(SmartPtr<INewtonUpdate > NU)
141 0 : {m_innerStepUpdate.push_back(NU);}
142 :
143 : /// clears inner step update
144 0 : void clear_inner_step_update(SmartPtr<INewtonUpdate > NU)
145 0 : {m_innerStepUpdate.clear();}
146 :
147 : /// add outer step update (applied before every Newton step)
148 0 : void add_step_update(SmartPtr<INewtonUpdate > NU)
149 0 : {m_stepUpdate.push_back(NU);}
150 :
151 : /// clears outer step update
152 0 : void clear_step_update(SmartPtr<INewtonUpdate > NU)
153 0 : {m_stepUpdate.clear();}
154 :
155 : /// sets the frequency of reassembling of the Jacobian (0 == 1 == in every step, i.e. classically)
156 0 : void set_reassemble_J_freq(int freq)
157 0 : {m_reassembe_J_freq = freq;};
158 :
159 : //#if ENABLE_NESTED_NEWTON_RESOLFUNC_UPDATE
160 0 : void setNewtonUpdater( SmartPtr<NewtonUpdaterGeneric<vector_type> > nU )
161 : {
162 0 : m_newtonUpdater = nU;
163 0 : }
164 : //#endif
165 :
166 0 : bool createNewtonUpdater()
167 : {
168 0 : if( m_newtonUpdater != SPNULL )
169 : {
170 0 : m_newtonUpdater = SmartPtr<NewtonUpdaterGeneric<vector_type> >
171 0 : (new NewtonUpdaterGeneric<vector_type>{});
172 :
173 0 : return true;
174 : }
175 :
176 : return false;
177 :
178 : }
179 :
180 :
181 : private:
182 : /// help functions for debug output
183 : /// \{
184 : void write_debug(const vector_type& vec, std::string filename);
185 : void write_debug(const matrix_type& mat, std::string filename);
186 : /// \}
187 :
188 : private:
189 : /// linear solver
190 : SmartPtr<ILinearOperatorInverse<vector_type> > m_spLinearSolver;
191 :
192 : /// Convergence Check
193 : SmartPtr<IConvergenceCheck<vector_type> > m_spConvCheck;
194 :
195 : /// LineSearch
196 : SmartPtr<ILineSearch<vector_type> > m_spLineSearch;
197 :
198 : /// Update
199 : std::vector<SmartPtr<INewtonUpdate> > m_innerStepUpdate;
200 : std::vector<SmartPtr<INewtonUpdate> > m_stepUpdate;
201 :
202 : /// assembling routine
203 : SmartPtr<AssembledOperator<algebra_type> > m_N;
204 : /// jacobi operator
205 : SmartPtr<AssembledLinearOperator<algebra_type> > m_J;
206 : /// assembling
207 : SmartPtr<IAssemble<TAlgebra> > m_spAss;
208 : /// how often to reassemble the Jacobian (0 == 1 == in every step, i.e. classically)
209 : int m_reassembe_J_freq;
210 :
211 : /// call counter
212 : int m_dgbCall;
213 : int m_lastNumSteps;
214 :
215 : /// convergence history of linear solver
216 : /// \{
217 : std::vector<int> m_vTotalLinSolverSteps;
218 : std::vector<int> m_vLinSolverCalls;
219 : std::vector<number> m_vNonLinSolverRates;
220 : std::vector<number> m_vLinSolverRates;
221 : /// \}
222 :
223 : //#if ENABLE_NESTED_NEWTON_RESOLFUNC_UPDATE
224 :
225 : SmartPtr<NewtonUpdaterGeneric<vector_type> > m_newtonUpdater;
226 :
227 : //#endif
228 :
229 : };
230 :
231 : }
232 :
233 : #include "newton_impl.h"
234 :
235 : #endif /* __H__UG__LIB_DISC__OPERATOR__NON_LINEAR_OPERATOR__NEWTON_SOLVER__NEWTON__ */
|