24 #ifndef INSTAT_STEP_NEWTON_SOLVER_H_
25 #define INSTAT_STEP_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>
54 template <
typename INTEGRATOR,
typename LINEARSOLVER,
typename VECTOR>
76 template<
typename PROBLEM>
110 template<
typename PROBLEM>
111 bool NonlinearSolve(PROBLEM& pde,
const VECTOR &last_time_solution, VECTOR &solution,
112 bool apply_boundary_values=
true,
113 bool force_matrix_build=
false,
int priority = 5, std::string algo_level =
"\t\t ");
145 template<
typename PROBLEM>
147 bool force_matrix_build=
false,
int priority = 5, std::string algo_level =
"\t\t ");
161 template<
typename PROBLEM>
169 INTEGRATOR &integrator_;
173 double nonlinear_global_tol_, nonlinear_tol_, nonlinear_rho_;
174 double linesearch_rho_;
175 int nonlinear_maxiter_, line_maxiter_;
180 template <
typename INTEGRATOR,
typename LINEARSOLVER,
typename VECTOR>
185 param_reader.
declare_entry(
"nonlinear_global_tol",
"1.e-12",Patterns::Double(0),
"global tolerance for the newton iteration");
186 param_reader.
declare_entry(
"nonlinear_tol",
"1.e-10",Patterns::Double(0),
"relative tolerance for the newton iteration");
187 param_reader.
declare_entry(
"nonlinear_maxiter",
"10",Patterns::Integer(0),
"maximal number of newton iterations");
188 param_reader.
declare_entry(
"nonlinear_rho",
"0.1",Patterns::Double(0),
"minimal newton reduction, if actual reduction is less, matrix is rebuild ");
190 param_reader.
declare_entry(
"line_maxiter",
"4",Patterns::Integer(0),
"maximal number of linesearch steps");
191 param_reader.
declare_entry(
"linesearch_rho",
"0.9",Patterns::Double(0),
"reduction rate for the linesearch damping paramete");
193 LINEARSOLVER::declare_params(param_reader);
198 template <
typename INTEGRATOR,
typename LINEARSOLVER,
typename VECTOR>
201 : LINEARSOLVER(param_reader), integrator_(integrator)
204 nonlinear_global_tol_ = param_reader.
get_double (
"nonlinear_global_tol");
205 nonlinear_tol_ = param_reader.
get_double (
"nonlinear_tol");
206 nonlinear_maxiter_ = param_reader.
get_integer (
"nonlinear_maxiter");
207 nonlinear_rho_ = param_reader.
get_double (
"nonlinear_rho");
209 line_maxiter_ = param_reader.
get_integer (
"line_maxiter");
210 linesearch_rho_ = param_reader.
get_double (
"linesearch_rho");
216 template <
typename INTEGRATOR,
typename LINEARSOLVER,
typename VECTOR>
223 template <
typename INTEGRATOR,
typename LINEARSOLVER,
typename VECTOR>
224 template<
typename PROBLEM>
228 LINEARSOLVER::ReInit(pde);
232 template <
typename INTEGRATOR,
typename LINEARSOLVER,
typename VECTOR>
233 template<
typename PROBLEM>
238 tmp_residual.reinit(residual);
240 GetIntegrator().AddDomainData(
"last_newton_solution",&last_time_solution);
241 GetIntegrator().AddDomainData(
"last_time_solution",&last_time_solution);
242 pde.SetStepPart(
"Old");
243 GetIntegrator().ComputeNonlinearLhs(pde,residual);
244 GetIntegrator().ComputeNonlinearRhs(pde,tmp_residual);
246 residual += tmp_residual;
248 GetIntegrator().DeleteDomainData(
"last_newton_solution");
249 GetIntegrator().DeleteDomainData(
"last_time_solution");
254 template <
typename INTEGRATOR,
typename LINEARSOLVER,
typename VECTOR>
255 template<
typename PROBLEM>
258 bool force_matrix_build,
int priority, std::string algo_level)
260 bool build_matrix = force_matrix_build;
263 std::stringstream out;
264 pde.GetOutputHandler()->InitNewtonOut(out);
267 residual.reinit(solution);
269 if(apply_boundary_values)
271 GetIntegrator().ApplyInitialBoundaryValues(pde,solution);
273 pde.GetDoFConstraints().distribute(solution);
275 GetIntegrator().AddDomainData(
"last_newton_solution",&solution);
277 GetIntegrator().ComputeNonlinearResidual(pde,residual);
280 pde.GetOutputHandler()->SetIterationNumber(0,
"PDENewton");
281 pde.GetOutputHandler()->Write(residual,
"Residual"+pde.GetType(),pde.GetDoFType());
283 double res = residual.linfty_norm();
284 double firstres = res;
285 double lastres = res;
288 out<< algo_level <<
"Newton step: " <<0<<
"\t Residual (abs.): "
289 <<pde.GetOutputHandler()->ZeroTolerance(res, 1.0)
292 out<< algo_level <<
"Newton step: " <<0<<
"\t Residual (rel.): " << std::scientific << pde.GetOutputHandler()->ZeroTolerance(firstres/firstres,1.0);
295 pde.GetOutputHandler()->Write(out,priority);
298 while(res > nonlinear_global_tol_ && res > firstres * nonlinear_tol_)
302 if(iter > nonlinear_maxiter_)
304 GetIntegrator().DeleteDomainData(
"last_newton_solution");
305 throw DOpEIterationException(
"Iteration count exceeded bounds!",
"InstatStepNewtonSolver::NonlinearSolve_Initial");
308 pde.GetOutputHandler()->SetIterationNumber(iter,
"PDENewton");
310 LINEARSOLVER::Solve(pde,GetIntegrator(),residual,du,build_matrix);
311 build_matrix =
false;
316 GetIntegrator().ComputeNonlinearResidual(pde,residual);
319 pde.GetOutputHandler()->Write(residual,
"Residual"+pde.GetType(),pde.GetDoFType());
320 pde.GetOutputHandler()->Write(du,
"Update"+pde.GetType(),pde.GetDoFType());
322 double newres = residual.linfty_norm();
324 double rho = linesearch_rho_;
329 out<< algo_level <<
"Newton step: " <<iter<<
"\t Residual (rel.): "
330 <<pde.GetOutputHandler()->ZeroTolerance(newres/firstres, 1.0)
331 <<
"\t LineSearch {"<<lineiter<<
"} ";
333 pde.GetOutputHandler()->Write(out,priority+1);
336 if(lineiter > line_maxiter_)
338 GetIntegrator().DeleteDomainData(
"last_newton_solution");
339 throw DOpEIterationException(
"Line-Iteration count exceeded bounds!",
"InstatStepNewtonSolver::NonlinearSolve_Initial");
341 solution.add(alpha*(rho-1.),du);
344 GetIntegrator().ComputeNonlinearResidual(pde,residual);
346 pde.GetOutputHandler()->Write(residual,
"Residual"+pde.GetType(),pde.GetDoFType());
348 newres = residual.linfty_norm();
351 if(res/lastres > nonlinear_rho_)
361 <<
"\t Residual (rel.): "
362 << pde.GetOutputHandler()->ZeroTolerance(res/firstres, 1.0)
368 pde.GetOutputHandler()->Write(out,priority);
372 GetIntegrator().DeleteDomainData(
"last_newton_solution");
378 template <
typename INTEGRATOR,
typename LINEARSOLVER,
typename VECTOR>
379 template<
typename PROBLEM>
382 const VECTOR &last_time_solution,
384 bool apply_boundary_values,
385 bool force_matrix_build,
390 bool build_matrix = force_matrix_build;
391 VECTOR residual, time_residual, tmp_residual;
393 std::stringstream out;
394 pde.GetOutputHandler()->InitNewtonOut(out);
397 double firstres = 0.0;
398 double lastres = 0.0;
401 residual.reinit(solution);
402 time_residual.reinit(solution);
403 tmp_residual.reinit(solution);
408 solution = last_time_solution;
410 if(apply_boundary_values)
412 GetIntegrator().ApplyInitialBoundaryValues(pde,solution);
416 GetIntegrator().AddDomainData(
"last_time_solution",&last_time_solution);
417 GetIntegrator().AddDomainData(
"last_newton_solution",&solution);
418 pde.SetStepPart(
"New");
419 GetIntegrator().ComputeNonlinearRhs(pde,tmp_residual);
421 residual += tmp_residual;
424 time_residual = residual;
428 GetIntegrator().ComputeNonlinearLhs(pde,tmp_residual);
429 residual += tmp_residual;
433 pde.GetOutputHandler()->SetIterationNumber(0,
"PDENewton");
434 pde.GetOutputHandler()->Write(residual,
"Residual"+pde.GetType(),pde.GetDoFType());
436 res = residual.linfty_norm();
441 out<<
"\t\t Newton step: " <<0<<
"\t Residual (abs.): "
442 <<pde.GetOutputHandler()->ZeroTolerance(res, 1.0)
445 out<<
"\t\t Newton step: " <<0<<
"\t Residual (rel.): "<< std::scientific << pde.GetOutputHandler()->ZeroTolerance(firstres/firstres,1.0);
449 pde.GetOutputHandler()->Write(out,priority);
450 while(res > nonlinear_global_tol_ && res > firstres * nonlinear_tol_)
453 if(iter > nonlinear_maxiter_)
458 pde.GetOutputHandler()->SetIterationNumber(iter,
"PDENewton");
459 LINEARSOLVER::Solve(pde,GetIntegrator(),residual,du,build_matrix);
460 build_matrix =
false;
464 GetIntegrator().ComputeNonlinearLhs(pde,residual);
465 residual -= time_residual;
467 pde.GetOutputHandler()->Write(residual,
"Residual"+pde.GetType(),pde.GetDoFType());
469 double newres = residual.linfty_norm();
471 double rho = linesearch_rho_;
476 out<<
"\t\t\t Linesearch step: " <<lineiter<<
"\t Residual (rel.): "
477 <<pde.GetOutputHandler()->ZeroTolerance(newres/firstres, 1.0);
478 pde.GetOutputHandler()->Write(out,priority+1);
480 if(lineiter > line_maxiter_)
484 solution.add(alpha*(rho-1.),du);
487 GetIntegrator().ComputeNonlinearLhs(pde,residual);
488 residual -= time_residual;
490 pde.GetOutputHandler()->Write(residual,
"Residual"+pde.GetType(),pde.GetDoFType());
492 newres = residual.linfty_norm();
495 if(res/lastres > nonlinear_rho_)
502 out<<
"\t\t Newton step: " <<iter<<
"\t Residual (rel.): "
503 << pde.GetOutputHandler()->ZeroTolerance(res/firstres, 1.0)
504 <<
"\t LineSearch {"<<lineiter<<
"} ";
505 pde.GetOutputHandler()->Write(out,priority);
509 GetIntegrator().DeleteDomainData(
"last_time_solution");
510 GetIntegrator().DeleteDomainData(
"last_newton_solution");
517 template <
typename INTEGRATOR,
typename LINEARSOLVER,
typename VECTOR>
bool NonlinearSolve_Initial(PROBLEM &pde, VECTOR &solution, bool apply_boundary_values=true, bool force_matrix_build=false, int priority=5, std::string algo_level="\t\t ")
Definition: instat_step_newtonsolver.h:257
Definition: dopeexception.h:76
void NonlinearLastTimeEvals(PROBLEM &pde, const VECTOR &last_time_solution, VECTOR &residual)
Definition: instat_step_newtonsolver.h:235
double get_double(const std::string &entry_name)
Definition: parameterreader.h:115
static void declare_params(ParameterReader ¶m_reader)
Definition: instat_step_newtonsolver.h:182
Definition: parameterreader.h:36
bool NonlinearSolve(PROBLEM &pde, const VECTOR &last_time_solution, VECTOR &solution, bool apply_boundary_values=true, bool force_matrix_build=false, int priority=5, std::string algo_level="\t\t ")
Definition: instat_step_newtonsolver.h:381
~InstatStepNewtonSolver()
Definition: instat_step_newtonsolver.h:218
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
InstatStepNewtonSolver(INTEGRATOR &integrator, ParameterReader ¶m_reader)
Definition: instat_step_newtonsolver.h:200
INTEGRATOR & GetIntegrator()
Definition: instat_step_newtonsolver.h:519
void ReInit(PROBLEM &pde)
Definition: instat_step_newtonsolver.h:226
Definition: instat_step_newtonsolver.h:55
int get_integer(const std::string &entry_name)
Definition: parameterreader.h:126
void SetSubsection(const std::string subsection)
Definition: parameterreader.h:93