24 #ifndef NEWTON_SOLVER_H_
25 #define NEWTON_SOLVER_H_
27 #include <deal.II/lac/vector.h>
28 #include <deal.II/lac/block_sparsity_pattern.h>
29 #include <deal.II/lac/block_sparse_matrix.h>
30 #include <deal.II/numerics/vector_tools.h>
52 template <
typename INTEGRATOR,
typename LINEARSOLVER,
typename VECTOR>
65 template<
typename PROBLEM>
95 template<
typename PROBLEM>
96 bool NonlinearSolve(PROBLEM& pde, VECTOR &solution,
bool apply_boundary_values=
true,
97 bool force_matrix_build=
false,
98 int priority = 5, std::string algo_level =
"\t\t ");
105 INTEGRATOR &integrator_;
109 double nonlinear_global_tol_, nonlinear_tol_, nonlinear_rho_;
110 double linesearch_rho_;
111 int nonlinear_maxiter_, line_maxiter_;
116 template <
typename INTEGRATOR,
typename LINEARSOLVER,
typename VECTOR>
121 param_reader.
declare_entry(
"nonlinear_global_tol",
"1.e-12",Patterns::Double(0),
"global tolerance for the newton iteration");
122 param_reader.
declare_entry(
"nonlinear_tol",
"1.e-10",Patterns::Double(0),
"relative tolerance for the newton iteration");
123 param_reader.
declare_entry(
"nonlinear_maxiter",
"10",Patterns::Integer(0),
"maximal number of newton iterations");
124 param_reader.
declare_entry(
"nonlinear_rho",
"0.1",Patterns::Double(0),
"minimal newton reduction, if actual reduction is less, matrix is rebuild ");
126 param_reader.
declare_entry(
"line_maxiter",
"4",Patterns::Integer(0),
"maximal number of linesearch steps");
127 param_reader.
declare_entry(
"linesearch_rho",
"0.9",Patterns::Double(0),
"reduction rate for the linesearch damping paramete");
129 LINEARSOLVER::declare_params(param_reader);
134 template <
typename INTEGRATOR,
typename LINEARSOLVER,
typename VECTOR>
137 : LINEARSOLVER(param_reader), integrator_(integrator)
140 nonlinear_global_tol_ = param_reader.
get_double (
"nonlinear_global_tol");
141 nonlinear_tol_ = param_reader.
get_double (
"nonlinear_tol");
142 nonlinear_maxiter_ = param_reader.
get_integer (
"nonlinear_maxiter");
143 nonlinear_rho_ = param_reader.
get_double (
"nonlinear_rho");
145 line_maxiter_ = param_reader.
get_integer (
"line_maxiter");
146 linesearch_rho_ = param_reader.
get_double (
"linesearch_rho");
152 template <
typename INTEGRATOR,
typename LINEARSOLVER,
typename VECTOR>
159 template <
typename INTEGRATOR,
typename LINEARSOLVER,
typename VECTOR>
160 template<
typename PROBLEM>
164 LINEARSOLVER::ReInit(pde);
168 template <
typename INTEGRATOR,
typename LINEARSOLVER,
typename VECTOR>
169 template<
typename PROBLEM>
173 bool apply_boundary_values,
174 bool force_matrix_build,
176 std::string algo_level)
178 bool build_matrix = force_matrix_build;
181 std::stringstream out;
182 pde.GetOutputHandler()->InitNewtonOut(out);
185 residual.reinit(solution);
187 if(apply_boundary_values)
189 GetIntegrator().ApplyInitialBoundaryValues(pde,solution);
191 pde.GetDoFConstraints().distribute(solution);
193 GetIntegrator().AddDomainData(
"last_newton_solution",&solution);
195 GetIntegrator().ComputeNonlinearResidual(pde,residual);
198 pde.GetOutputHandler()->SetIterationNumber(0,
"PDENewton");
199 pde.GetOutputHandler()->Write(residual,
"Residual"+pde.GetType(),pde.GetDoFType());
201 double res = residual.linfty_norm();
202 double firstres = res;
203 double lastres = res;
206 out<< algo_level <<
"Newton step: " <<0<<
"\t Residual (abs.): "
207 <<pde.GetOutputHandler()->ZeroTolerance(res, 1.0)
210 out<< algo_level <<
"Newton step: " <<0<<
"\t Residual (rel.): " << std::scientific << firstres/firstres;
213 pde.GetOutputHandler()->Write(out,priority);
216 while(res > nonlinear_global_tol_ && res > firstres * nonlinear_tol_)
220 if(iter > nonlinear_maxiter_)
222 GetIntegrator().DeleteDomainData(
"last_newton_solution");
226 pde.GetOutputHandler()->SetIterationNumber(iter,
"PDENewton");
228 LINEARSOLVER::Solve(pde,GetIntegrator(),residual,du,build_matrix);
229 build_matrix =
false;
234 GetIntegrator().ComputeNonlinearResidual(pde,residual);
237 pde.GetOutputHandler()->Write(residual,
"Residual"+pde.GetType(),pde.GetDoFType());
238 pde.GetOutputHandler()->Write(du,
"Update"+pde.GetType(),pde.GetDoFType());
240 double newres = residual.linfty_norm();
242 double rho = linesearch_rho_;
247 out<< algo_level <<
"Newton step: " <<iter<<
"\t Residual (rel.): "
248 <<pde.GetOutputHandler()->ZeroTolerance(newres/firstres, 1.0)
249 <<
"\t LineSearch {"<<lineiter<<
"} ";
251 pde.GetOutputHandler()->Write(out,priority+1);
254 if(lineiter > line_maxiter_)
256 GetIntegrator().DeleteDomainData(
"last_newton_solution");
259 solution.add(alpha*(rho-1.),du);
262 GetIntegrator().ComputeNonlinearResidual(pde,residual);
264 pde.GetOutputHandler()->Write(residual,
"Residual"+pde.GetType(),pde.GetDoFType());
266 newres = residual.linfty_norm();
269 if(res/lastres > nonlinear_rho_)
279 <<
"\t Residual (rel.): "
280 << pde.GetOutputHandler()->ZeroTolerance(res/firstres, 1.0)
286 pde.GetOutputHandler()->Write(out,priority);
290 GetIntegrator().DeleteDomainData(
"last_newton_solution");
296 template <
typename INTEGRATOR,
typename LINEARSOLVER,
typename VECTOR>
Definition: dopeexception.h:76
double get_double(const std::string &entry_name)
Definition: parameterreader.h:115
void ReInit(PROBLEM &pde)
Definition: newtonsolver.h:162
Definition: parameterreader.h:36
INTEGRATOR & GetIntegrator()
Definition: newtonsolver.h:298
~NewtonSolver()
Definition: newtonsolver.h:154
void declare_entry(const std::string &entry, const std::string &default_value, const Patterns::PatternBase &pattern=Patterns::Anything(), const std::string &documentation=std::string())
Definition: parameterreader.h:98
Definition: newtonsolver.h:53
int get_integer(const std::string &entry_name)
Definition: parameterreader.h:126
NewtonSolver(INTEGRATOR &integrator, ParameterReader ¶m_reader)
Definition: newtonsolver.h:136
static void declare_params(ParameterReader ¶m_reader)
Definition: newtonsolver.h:118
void SetSubsection(const std::string subsection)
Definition: parameterreader.h:93
bool NonlinearSolve(PROBLEM &pde, VECTOR &solution, bool apply_boundary_values=true, bool force_matrix_build=false, int priority=5, std::string algo_level="\t\t ")
Definition: newtonsolver.h:171