94 const typename Teuchos::ScalarTraits<typename V::scalar_type>::magnitudeType tolerance = 1e-7,
95 const int eigNormalizationFreq = 1,
96 Teuchos::RCP<Teuchos::FancyOStream> out = Teuchos::null,
97 const bool computeSpectralRadius =
true)
99 typedef typename V::scalar_type ST;
100 typedef Teuchos::ScalarTraits<typename V::scalar_type> STS;
101 typedef typename Teuchos::ScalarTraits<typename V::scalar_type>::magnitudeType MT;
103 bool verbose = (out != Teuchos::null);
107 *out <<
" powerMethodWithInitGuess:" << endl;
110 const ST zero =
static_cast<ST
> (0.0);
111 const ST one =
static_cast<ST
> (1.0);
113 ST lambdaMaxOld = one;
117 TEUCHOS_TEST_FOR_EXCEPTION
118 (norm == zero, std::runtime_error,
119 "Ifpack2::PowerMethod::powerMethodWithInitGuess: The initial guess "
120 "has zero norm. This could be either because Tpetra::Vector::"
121 "randomize filled the vector with zeros (if that was used to "
122 "compute the initial guess), or because the norm2 method has a "
123 "bug. The first is not impossible, but unlikely.");
126 *out <<
" Original norm1(x): " << x->norm1()
127 <<
", norm2(x): " << norm << endl;
130 x->scale(one / norm);
133 *out <<
" norm1(x.scale(one/norm)): " << x->norm1() << endl;
137 y = Teuchos::rcp(
new V(A.getRangeMap()));
146 if(computeSpectralRadius) {
150 *out <<
" PowerMethod using spectral radius route" << endl;
152 for(
int iter = 0; iter < numIters-1; ++iter) {
154 *out <<
" Iteration " << iter << endl;
157 x->elementWiseMultiply(STS::one(), D_inv, *y, STS::zero());
159 if(((iter+1) % eigNormalizationFreq == 0) && (iter < numIters-2)) {
163 *out <<
" norm is zero; returning zero" << endl;
164 *out <<
" Power method terminated after "<< iter <<
" iterations." << endl;
168 lambdaMaxOld = lambdaMax;
169 lambdaMax = pow(norm, Teuchos::ScalarTraits<MT>::one() / eigNormalizationFreq);
170 if(Teuchos::ScalarTraits<ST>::magnitude(lambdaMax-lambdaMaxOld) < tolerance * Teuchos::ScalarTraits<ST>::magnitude(lambdaMax)) {
172 *out <<
" lambdaMax: " << lambdaMax << endl;
173 *out <<
" Power method terminated after "<< iter <<
" iterations." << endl;
177 *out <<
" lambdaMaxOld: " << lambdaMaxOld << endl;
178 *out <<
" lambdaMax: " << lambdaMax << endl;
179 *out <<
" |lambdaMax-lambdaMaxOld|/|lambdaMax|: " << Teuchos::ScalarTraits<ST>::magnitude(lambdaMax-lambdaMaxOld)/Teuchos::ScalarTraits<ST>::magnitude(lambdaMax) << endl;
182 x->scale(one / norm);
186 *out <<
" lambdaMax: " << lambdaMax << endl;
192 *out <<
" norm is zero; returning zero" << endl;
193 *out <<
" Power method terminated after "<< numIters <<
" iterations." << endl;
197 x->scale(one / norm);
199 y->elementWiseMultiply(STS::one(), D_inv, *y, STS::zero());
200 lambdaMax = y->norm2();
206 *out <<
" PowerMethod using largest eigenvalue route" << endl;
208 for (
int iter = 0; iter < numIters-1; ++iter) {
210 *out <<
" Iteration " << iter << endl;
213 y->elementWiseMultiply(STS::one(), D_inv, *y, STS::zero());
215 if(((iter+1) % eigNormalizationFreq == 0) && (iter < numIters-2)) {
216 xDinvAx = x->dot(*y);
217 if(xDinvAx == zero) {
219 *out <<
" xDinvAx is zero; returning zero" << endl;
220 *out <<
" Power method terminated after "<< iter <<
" iterations." << endl;
224 lambdaMaxOld = lambdaMax;
225 lambdaMax = pow(xDinvAx, Teuchos::ScalarTraits<MT>::one() / eigNormalizationFreq);
226 if(Teuchos::ScalarTraits<ST>::magnitude(lambdaMax-lambdaMaxOld) < tolerance * Teuchos::ScalarTraits<ST>::magnitude(lambdaMax)) {
228 *out <<
" lambdaMax: " << lambdaMax << endl;
229 *out <<
" Power method terminated after "<< iter <<
" iterations." << endl;
233 *out <<
" lambdaMaxOld: " << lambdaMaxOld << endl;
234 *out <<
" lambdaMax: " << lambdaMax << endl;
235 *out <<
" |lambdaMax-lambdaMaxOld|/|lambdaMax|: " << Teuchos::ScalarTraits<ST>::magnitude(lambdaMax-lambdaMaxOld)/Teuchos::ScalarTraits<ST>::magnitude(lambdaMax) << endl;
240 x->scale(one / norm);
244 *out <<
" lambdaMax: " << lambdaMax << endl;
250 *out <<
" norm is zero; returning zero" << endl;
251 *out <<
" Power method terminated after "<< numIters <<
" iterations." << endl;
255 x->scale(one / norm);
257 y->elementWiseMultiply(STS::one(), D_inv, *y, STS::zero());
258 lambdaMax = y->dot(*x);
262 *out <<
" lambdaMax: " << lambdaMax << endl;
263 *out <<
" Power method terminated after "<< numIters <<
" iterations." << endl;
285 typedef typename V::device_type::execution_space dev_execution_space;
286 typedef typename V::local_ordinal_type LO;
290 if(nonnegativeRealParts) {
291 auto x_lcl = x.getLocalViewDevice (Tpetra::Access::ReadWrite);
292 auto x_lcl_1d = Kokkos::subview (x_lcl, Kokkos::ALL (), 0);
294 const LO lclNumRows =
static_cast<LO
> (x.getLocalLength ());
295 Kokkos::RangePolicy<dev_execution_space, LO> range (0, lclNumRows);
296 PositivizeVector<
decltype (x_lcl_1d), LO> functor (x_lcl_1d);
297 Kokkos::parallel_for (range, functor);
324 const typename Teuchos::ScalarTraits<typename V::scalar_type>::magnitudeType tolerance = 1e-7,
325 const int eigNormalizationFreq = 1,
326 Teuchos::RCP<Teuchos::FancyOStream> out = Teuchos::null,
327 const bool computeSpectralRadius =
true)
329 typedef typename V::scalar_type ST;
330 typedef Teuchos::ScalarTraits<typename V::scalar_type> STS;
332 bool verbose = (out != Teuchos::null);
335 *out <<
"powerMethod:" << std::endl;
338 const ST zero =
static_cast<ST
> (0.0);
340 Teuchos::RCP<V> x = Teuchos::rcp(
new V(A.getDomainMap ()));
346 ST lambdaMax =
powerMethodWithInitGuess (A, D_inv, numIters, x, y, tolerance, eigNormalizationFreq, out, computeSpectralRadius);
355 if(STS::real (lambdaMax) < STS::real (zero)) {
357 *out <<
"real(lambdaMax) = " << STS::real (lambdaMax) <<
" < 0; "
358 "try again with a different random initial guess" << std::endl;
368 lambdaMax =
powerMethodWithInitGuess (A, D_inv, numIters, x, y, tolerance, eigNormalizationFreq, out, computeSpectralRadius);
V::scalar_type powerMethodWithInitGuess(const OperatorType &A, const V &D_inv, const int numIters, Teuchos::RCP< V > x, Teuchos::RCP< V > y, const typename Teuchos::ScalarTraits< typename V::scalar_type >::magnitudeType tolerance=1e-7, const int eigNormalizationFreq=1, Teuchos::RCP< Teuchos::FancyOStream > out=Teuchos::null, const bool computeSpectralRadius=true)
Use the power method to estimate the maximum eigenvalue of A*D_inv, given an initial guess vector x.
Definition Ifpack2_PowerMethod.hpp:89
V::scalar_type powerMethod(const OperatorType &A, const V &D_inv, const int numIters, Teuchos::RCP< V > y, const typename Teuchos::ScalarTraits< typename V::scalar_type >::magnitudeType tolerance=1e-7, const int eigNormalizationFreq=1, Teuchos::RCP< Teuchos::FancyOStream > out=Teuchos::null, const bool computeSpectralRadius=true)
Use the power method to estimate the maximum eigenvalue of A*D_inv.
Definition Ifpack2_PowerMethod.hpp:320
void computeInitialGuessForPowerMethod(V &x, const bool nonnegativeRealParts)
Fill x with random initial guess for power method.
Definition Ifpack2_PowerMethod.hpp:283