7#include <deal.II/base/config.h>
8#include <deal.II/base/timer.h>
9#include <deal.II/lac/vector_memory.h>
13 using namespace dealii;
20 template <
typename VectorType_>
class Newton
25 Newton(
double abstol_ = 1e-10,
double reltol_ = 1e-6,
double assemble_threshold_ = 0.,
uint max_steps_ = 21,
26 uint n_stepsize_iterations_ = 21)
55 GrowingVectorMemory<VectorType> mem;
56 typename VectorMemory<VectorType>::Pointer Du(mem);
63 if (!std::isfinite(
residual_vector.l2_norm()))
throw std::runtime_error(
"Initial residual non-finite!");
65 double old_residual = 0.;
66 double assembly_time = 0.;
75 assembly_time += timer_assembly.wall_time();
82 }
catch (std::exception &) {
83 throw std::runtime_error(
"Linear solve failed!");
92 std::cerr <<
"Residual non-finite!" << std::endl;
93 throw std::runtime_error(
"Residual non-finite!");
98 while (
resnorm >= old_residual) {
103 u.add(1. /
double(1 << step_size), *Du);
113 throw std::runtime_error(
"Newton did not converge!");
198 GrowingVectorMemory<VectorType> mem;
199 typename VectorMemory<VectorType>::Pointer tmp(mem);
200 tmp->reinit(u,
true);
202 using std::abs, std::max, std::sqrt;
203 tbb::parallel_for(tbb::blocked_range<uint>(0, tmp->size()), [&](tbb::blocked_range<uint> r) {
204 for (uint n = r.begin(); n < r.end(); ++n) {
205 (*tmp)[n] = abs(err[n]) / (abstol + max(abs(u_prev[n]), abs(u[n])) * reltol);
208 return tmp->l2_norm() / sqrt(tmp->size());
209 return tmp->linfty_norm();
220 if (step > max_steps)
return false;
221 if (step == 0)
return true;
222 double err = get_EEst();
223 converged = err <= 1.;
A newton solver, using local error estimates for each vector component.
Definition newton.hh:21
Newton(double abstol_=1e-10, double reltol_=1e-6, double assemble_threshold_=0., uint max_steps_=21, uint n_stepsize_iterations_=21)
Definition newton.hh:25
uint num_newton_calls() const
Definition newton.hh:144
bool ignore_nonconv
Definition newton.hh:181
double average_time_newton() const
Definition newton.hh:136
double get_EEst()
Calculate the current error estimate.
Definition newton.hh:192
uint n_stepsize_iterations
Definition newton.hh:178
bool check()
Check if the iteration should go on.
Definition newton.hh:218
std::function< void(const VectorType &)> update_jacobian
Definition newton.hh:156
const VectorType & get_iterate() const
Definition newton.hh:134
void set_ignore_nonconv(bool x)
Make the newton algorithm return a success even if the evolution did not fulfill the convergence crit...
Definition newton.hh:152
std::function< void(VectorType &, const VectorType &)> residual
Definition newton.hh:154
double get_error()
Get the latest error.
Definition newton.hh:161
const VectorType & get_initial() const
Definition newton.hh:133
uint max_steps
Definition newton.hh:178
double assemble_threshold
Definition newton.hh:177
double resnorm
Definition newton.hh:182
VectorType iterate_vector
Definition newton.hh:176
uint get_step()
Get the number of steps taken in the latest iteration.
Definition newton.hh:166
const VectorType & get_residual() const
Definition newton.hh:132
uint get_jacobians()
Get the number of jacobians created in the latest iteration.
Definition newton.hh:171
void reinit(const VectorType &u)
Should it be necessary, one can force a recalculation of the jacobian hereby.
Definition newton.hh:35
VectorType initial_vector
Definition newton.hh:175
double threshold(const double thr)
Set the jacobian recalculation threshold, i.e. below which reduction we force a jacobian update.
Definition newton.hh:125
VectorType residual_vector
Definition newton.hh:174
bool converged
Definition newton.hh:180
std::function< void(VectorType &, const VectorType &)> lin_solve
Definition newton.hh:155
VectorType_ VectorType
Definition newton.hh:23
void operator()(VectorType &iterate)
Perform a newton iteration on the input vector.
Definition newton.hh:42
double abstol
Definition newton.hh:177
std::vector< double > timings_newton
Definition newton.hh:185
uint step
Definition newton.hh:183
uint jacobians
Definition newton.hh:183
double reltol
Definition newton.hh:177
Definition complex_math.hh:14
unsigned int uint
Definition utils.hh:22