13template<
class ContainerType,
class value_type>
16 for(
unsigned i = 0; i<mMax-1;i++){
17 value_type temp = sqrt(R(i,i+1)*R(i,i+1)+R(i+1,i+1)*R(i+1,i+1));
18 value_type c = R(i,i+1)/temp;
19 value_type s = R(i+1,i+1)/temp;
23 for (
unsigned j = i+2; j < mMax; j++){
24 temp = c * R(i,j) + s * R(i+1,j);
25 R(i+1,j) = - s * R(i,j) + c * R(i+1,j);
30 double tmp = c*qi + s*qip;
37 for(
unsigned i = 0; i<mMax-1;i++)
38 for(
unsigned j = 0; j < mMax-1; j++)
59template<
class ContainerType>
90 m_R( mMax), m_mMax( mMax)
100 template<
class ...Params>
108 const ContainerType&
copyable()
const{
return m_fval;}
111 m_throw_on_fail = throw_on_fail;
143 template<
class MatrixType,
class ContainerType0,
class ContainerType1,
class ContainerType2>
144 unsigned solve( MatrixType&& f, ContainerType0& x,
const ContainerType1& b,
147 value_type damping,
unsigned restart,
bool verbose);
150 ContainerType m_g_old, m_fval, m_f_old;
151 std::vector<ContainerType> m_DG, m_Q;
152 std::vector<value_type> m_gamma;
156 bool m_throw_on_fail =
true;
160template<
class ContainerType>
161template<
class MatrixType,
class ContainerType0,
class ContainerType1,
class ContainerType2>
163 MatrixType&& func, ContainerType0& x,
const ContainerType1& b,
const ContainerType2&
weights,
164 value_type rtol, value_type atol,
unsigned max_iter,
165 value_type damping,
unsigned restart,
bool verbose )
169 MPI_Comm_rank(MPI_COMM_WORLD, &rank);
172 if(verbose)
DG_RANK0 std::cout<<
"No acceleration will occur" << std::endl;
177 value_type tol = atol+rtol*nrmb;
178 if(verbose)
DG_RANK0 std::cout <<
"Solve with mMax = "<<m_mMax<<
" rtol = "
179 <<rtol<<
" atol = "<<atol<<
" tol = " << tol <<
" max_iter = "<<max_iter
180 <<
" damping = "<<damping<<
" restart = "<<restart<<std::endl;
182 ContainerType0& m_gval = x;
184 for(
unsigned iter=0;iter < max_iter; iter++)
186 if ( restart != 0 && iter % (restart) == 0) {
188 if(verbose)
DG_RANK0 std::cout <<
"Iter = " << iter << std::endl;
191 dg::apply( std::forward<MatrixType>(func), x, m_fval);
195 if(verbose)
DG_RANK0 std::cout <<
"res_norm = " << res_norm <<
" Against tol = " << tol << std::endl;
197 if (res_norm <= tol){
198 if(verbose)
DG_RANK0 std::cout <<
"Terminate with residual norm = " << res_norm << std::endl;
204 if( m_mMax == 0)
continue;
208 std::swap(m_fval,m_f_old);
222 std::rotate(m_DG.begin(), m_DG.begin() + 1, m_DG.end());
225 detail::QRdelete1(m_Q,m_R,m_mMax);
234 for (
unsigned j = 0; j < mAA; j++) {
247 for(
int i = (
int)mAA; i>=0; i--){
249 for(
int j = i + 1; j < (int)mAA+1; j++){
250 m_gamma[i] = DG_FMA( -m_R(i,j), m_gamma[j], m_gamma[i]) ;
252 m_gamma[i] /= m_R(i,i);
255 std::swap(m_fval,m_f_old);
264 std::vector<value_type>{m_gamma.begin(), m_gamma.begin()+mAA+1},
272 <<
"After "<<max_iter<<
" Anderson iterations with rtol "<<rtol<<
" atol "<<atol<<
" damping "<<damping<<
" restart "<<restart);
283template<
class ContainerType>
#define _ping_
Definition: exceptions.h:12
#define DG_DEVICE
Expands to __host__ __device__ if compiled with nvcc else is empty.
Definition: functions.h:9
void apply(get_value_type< ContainerType1 > alpha, MatrixType &&M, const ContainerType1 &x, get_value_type< ContainerType1 > beta, ContainerType2 &y)
; (alias for dg::blas2::symv)
Definition: blas2.h:461
void copy(const ContainerTypeIn &source, ContainerTypeOut &target)
Definition: blas1.h:164
void axpby(get_value_type< ContainerType > alpha, const ContainerType1 &x, get_value_type< ContainerType > beta, ContainerType &y)
Definition: blas1.h:231
void subroutine(Subroutine f, ContainerType &&x, ContainerTypes &&... xs)
; Customizable and generic blas1 function
Definition: blas1.h:618
void scal(ContainerType &x, get_value_type< ContainerType > alpha)
Definition: blas1.h:185
void gemv(get_value_type< ContainerType1 > alpha, MatrixType &&M, const ContainerType1 &x, get_value_type< ContainerType1 > beta, ContainerType2 &y)
; (alias for symv)
Definition: blas2.h:299
get_value_type< MatrixType > dot(const ContainerType1 &x, const MatrixType &m, const ContainerType2 &y)
; Binary reproducible general dot product
Definition: blas2.h:85
std::vector< const ContainerType * > asPointers(const std::vector< ContainerType > &in)
Convert a vector of vectors to a vector of pointers.
Definition: densematrix.h:100
auto asDenseMatrix(const std::vector< const ContainerType * > &in)
Lightweight DenseMatrix for dg::blas2::gemv.
Definition: densematrix.h:70
MPI_Vector< thrust::host_vector< real_type > > weights(const aRealMPITopology2d< real_type > &g)
Nodal weight coefficients.
Definition: mpi_weights.h:22
typename TensorTraits< std::decay_t< Vector > >::value_type get_value_type
Definition: tensor_traits.h:38
This is the namespace for all functions and classes defined and used by the discontinuous Galerkin li...
Anderson Acceleration of Fixed Point/Richardson Iteration for the nonlinear equation .
Definition: andersonacc.h:61
const ContainerType & copyable() const
Return an object of same size as the object used for construction.
Definition: andersonacc.h:108
AndersonAcceleration(const ContainerType ©able)
Allocate memory for Fixed point iteration.
Definition: andersonacc.h:73
AndersonAcceleration(const ContainerType ©able, unsigned mMax)
Allocate memory for the object.
Definition: andersonacc.h:86
get_value_type< ContainerType > value_type
the value type of the time variable (float or double)
Definition: andersonacc.h:62
AndersonAcceleration()=default
Allocate nothing, Call construct method before usage.
unsigned solve(MatrixType &&f, ContainerType0 &x, const ContainerType1 &b, const ContainerType2 &weights, value_type rtol, value_type atol, unsigned max_iter, value_type damping, unsigned restart, bool verbose)
Solve the system in the given norm.
void set_throw_on_fail(bool throw_on_fail)
Set or unset a throw on failure-to-converge.
Definition: andersonacc.h:110
void construct(Params &&...ps)
Perfect forward parameters to one of the constructors.
Definition: andersonacc.h:101
ContainerType container_type
Definition: andersonacc.h:63
Indicate failure to converge.
Definition: exceptions.h:126
#define DG_RANK0
Definition: typedefs.h:147