11#ifndef BELFEM_FN_FEM_ANDERSON_MIXING_HPP
12#define BELFEM_FN_FEM_ANDERSON_MIXING_HPP
82 "x and r histories out of sync ( %u vs %u )",
83 (
unsigned int ) aXHistory.size(),
84 (
unsigned int ) aRHistory.size() );
87 auto tPlainStep = [ & ]()
89 for (
index_t k = 0; k < tN; ++k )
91 aXNew( k ) = aX( k ) + aBeta * aR( k );
97 auto tTrySolve = [ & ](
const uint tUse ) ->
bool
101 for (
uint j = 0; j < tUse; ++j )
104 aR : aRHistory( tUse - 2 - j );
108 for (
index_t k = 0; k < tN; ++k )
110 const real tValue = tR1( k ) - tR0( k );
111 aDeltaR( k, j ) = tValue ;
112 tNorm += tValue * tValue ;
114 tNorm = std::sqrt( tNorm );
122 aColNorm( j ) = tNorm ;
124 const real tScale = 1.0 / tNorm ;
125 for (
index_t k = 0; k < tN; ++k )
127 aDeltaR( k, j ) *= tScale ;
132 for (
index_t k = 0; k < tN; ++k )
149 if ( tLwork < 2 * tCols )
155 aDeltaR.
data(), &tLda, aRhs.
data(), &tLdb,
156 aWork.
data(), &tQuery, &tInfo );
158 "gels workspace query failed: %i", (
int ) tInfo );
164 aDeltaR.
data(), &tLda, aRhs.
data(), &tLdb,
165 aWork.
data(), &tLwork, &tInfo );
169 "gels reports an illegal argument: %i", (
int ) tInfo );
178 real tGammaMax = 0.0 ;
179 for (
uint j = 0; j < tUse; ++j )
181 const real tValue = aRhs( j ) / aColNorm( j );
182 if ( ! std::isfinite( tValue ) )
186 aGamma( j ) = tValue ;
187 tGammaMax = std::max( tGammaMax, std::abs( tValue ) );
196 uint tUse = aXHistory.size() ;
202 bool tOK = tUse > 0 ? tTrySolve( tUse ) : false ;
203 if ( ! tOK && tUse > 1 )
206 tOK = tTrySolve( tUse );
218 for (
uint j = 0; j < tUse; ++j )
220 const bool tHead = ( j + 1 == tUse );
221 const Vector< real > & tX1 = tHead ? aX : aXHistory( tUse - 2 - j );
223 const Vector< real > & tR1 = tHead ? aR : aRHistory( tUse - 2 - j );
226 const real tGamma = aGamma( j );
227 for (
index_t k = 0; k < tN; ++k )
229 aXNew( k ) -= tGamma * ( ( tX1( k ) - tX0( k ) )
230 + aBeta * ( tR1( k ) - tR0( k ) ) );
#define BELFEM_ERROR(aCheck,...)
Definition assert.hpp:264
#define BELFEM_ASSERT(aCheck,...)
Definition assert.hpp:244
T * data()
Definition cl_AR_Matrix.hpp:135
Fixed-capacity FIFO with history, for time-stepping.
Definition cl_ShiftRegister.hpp:74
T * data()
expose the underlying raw pointer ( writable version )
Definition cl_AR_Vector.hpp:182
void set_size(const size_t aNumRows)
change the size of the vector
Definition cl_AR_Vector.hpp:237
size_t length() const
get the length of the vector
Definition cl_AR_Vector.hpp:257
Least-squares or minimum-norm solution of a full-rank system (LAPACK ?gels).
Definition cl_IFB_LINE3.hpp:21
constexpr real gAndersonGammaMax
Definition fn_FEM_anderson_mixing.hpp:32
uint anderson_mixing_step(const Vector< real > &aX, const Vector< real > &aR, const ShiftRegister< Vector< real > > &aXHistory, const ShiftRegister< Vector< real > > &aRHistory, const real aBeta, Matrix< real > &aDeltaR, Vector< real > &aRhs, Vector< real > &aWork, Vector< real > &aGamma, Vector< real > &aColNorm, Vector< real > &aXNew)
one type-II Anderson mixing step (Walker & Ni 2011) for the fixed-point iteration x <- G( x ) with re...
Definition fn_FEM_anderson_mixing.hpp:65
int_t leading_dimension(const Vector< T > &A)
logical length of a vector operand, as passed to LAPACK as LDB; vector storage is contiguous under bo...
Definition lapacktools.hpp:143
int_t work_size(const float &aValue)
lapack reports the optimal work size in the first entry of the work array, which stays real valued fo...
Definition lapacktools.hpp:191
void gels(const char *trans, const int_t *m, const int_t *n, const int_t *nrhs, T *a, const int_t *lda, T *b, const int_t *ldb, T *work, const int_t *lwork, int_t *info)
Definition fn_gels.hpp:117
USER GUIDES:
Definition cl_Capacitor.cpp:16
unsigned int uint
Definition typedefs.hpp:30
constexpr real BELFEM_EPSILON
Definition typedefs.hpp:90
uint32_t index_t
Definition typedefs.hpp:52
double real
Definition typedefs.hpp:36
int32_t int_t
Definition typedefs.hpp:51