Ifpack2 Templated Preconditioning Package  Version 1.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
Ifpack2_Details_Chebyshev_decl.hpp
Go to the documentation of this file.
1 /*
2 //@HEADER
3 // ***********************************************************************
4 //
5 // Ifpack2: Tempated Object-Oriented Algebraic Preconditioner Package
6 // Copyright (2009) Sandia Corporation
7 //
8 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
9 // license for use of this work by or on behalf of the U.S. Government.
10 //
11 // Redistribution and use in source and binary forms, with or without
12 // modification, are permitted provided that the following conditions are
13 // met:
14 //
15 // 1. Redistributions of source code must retain the above copyright
16 // notice, this list of conditions and the following disclaimer.
17 //
18 // 2. Redistributions in binary form must reproduce the above copyright
19 // notice, this list of conditions and the following disclaimer in the
20 // documentation and/or other materials provided with the distribution.
21 //
22 // 3. Neither the name of the Corporation nor the names of the
23 // contributors may be used to endorse or promote products derived from
24 // this software without specific prior written permission.
25 //
26 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
27 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
30 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
31 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
32 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
33 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
36 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 //
38 // Questions? Contact Michael A. Heroux (maherou@sandia.gov)
39 //
40 // ***********************************************************************
41 //@HEADER
42 */
43 
44 // ***********************************************************************
45 //
46 // Ifpack2: Templated Object-Oriented Algebraic Preconditioner Package
47 // Copyright (2004) Sandia Corporation
48 //
49 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
50 // license for use of this work by or on behalf of the U.S. Government.
51 //
52 // This library is free software; you can redistribute it and/or modify
53 // it under the terms of the GNU Lesser General Public License as
54 // published by the Free Software Foundation; either version 2.1 of the
55 // License, or (at your option) any later version.
56 //
57 // This library is distributed in the hope that it will be useful, but
58 // WITHOUT ANY WARRANTY; without even the implied warranty of
59 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
60 // Lesser General Public License for more details.
61 //
62 // You should have received a copy of the GNU Lesser General Public
63 // License along with this library; if not, write to the Free Software
64 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
65 // USA
66 // Questions? Contact Michael A. Heroux (maherou@sandia.gov)
67 //
68 // ***********************************************************************
69 
70 #ifndef IFPACK2_DETAILS_CHEBYSHEV_DECL_HPP
71 #define IFPACK2_DETAILS_CHEBYSHEV_DECL_HPP
72 
79 
80 #include <Ifpack2_ConfigDefs.hpp>
82 #include <Teuchos_Describable.hpp>
83 #include <Tpetra_CrsMatrix.hpp>
84 
85 namespace Ifpack2 {
92 namespace Details {
127 template<class ScalarType, class MV, class MAT>
129 public:
131 
132  typedef ScalarType ST;
134  typedef typename STS::magnitudeType MT;
135  typedef Tpetra::Operator<typename MV::scalar_type,
136  typename MV::local_ordinal_type,
137  typename MV::global_ordinal_type,
138  typename MV::node_type> OP;
139  typedef Tpetra::Vector<typename MV::scalar_type,
140  typename MV::local_ordinal_type,
141  typename MV::global_ordinal_type,
142  typename MV::node_type> V;
143  typedef Tpetra::Map<typename MV::local_ordinal_type,
144  typename MV::global_ordinal_type,
145  typename MV::node_type> map_type;
147 
153 
161 
268 
297  void compute ();
298 
315  MT apply (const MV& B, MV& X);
316 
317  ST getLambdaMaxForApply() const;
318 
321 
323  bool hasTransposeApply () const;
324 
326  void print (std::ostream& out);
327 
329 
331 
333  std::string description() const;
334 
336  void
338  const Teuchos::EVerbosityLevel verbLevel =
341 private:
343 
344 
346 
358 
364  Teuchos::ArrayRCP<size_t> diagOffsets_;
365 
371  bool savedDiagOffsets_;
372 
374 
376 
379  Teuchos::RCP<MV> V_;
382  Teuchos::RCP<MV> W_;
383 
387  ST computedLambdaMax_;
391  ST computedLambdaMin_;
392 
394 
396 
399  ST lambdaMaxForApply_;
402  ST lambdaMinForApply_;
405  ST eigRatioForApply_;
406 
408 
410 
413  Teuchos::RCP<const V> userInvDiag_;
416  ST userLambdaMax_;
419  ST userLambdaMin_;
422  ST userEigRatio_;
426  ST minDiagVal_;
428  int numIters_;
430  int eigMaxIters_;
432  bool zeroStartingSolution_;
439  bool assumeMatrixUnchanged_;
441  bool textbookAlgorithm_;
443  bool computeMaxResNorm_;
444 
446 
448 
450  void checkConstructorInput () const;
451 
464  void
465  makeTempMultiVectors (Teuchos::RCP<MV>& V1,
466  Teuchos::RCP<MV>& W,
467  const MV& X);
468 
470  static void
471  computeResidual (MV& R, const MV& B, const MAT& A, const MV& X,
472  const Teuchos::ETransp mode = Teuchos::NO_TRANS);
473 
479  static void solve (MV& Z, const V& D_inv, const MV& R);
480 
486  static void solve (MV& Z, const ST alpha, const V& D_inv, const MV& R);
487 
496  makeInverseDiagonal (const MAT& A, const bool useDiagOffsets=false) const;
497 
502  Teuchos::RCP<V> makeRangeMapVector (const V& D) const;
503 
522  void
523  textbookApplyImpl (const MAT& A,
524  const MV& B,
525  MV& X,
526  const int numIters,
527  const ST lambdaMax,
528  const ST lambdaMin,
529  const ST eigRatio,
530  const V& D_inv) const;
531 
554  void
555  ifpackApplyImpl (const MAT& A,
556  const MV& B,
557  MV& X,
558  const int numIters,
559  const ST lambdaMax,
560  const ST lambdaMin,
561  const ST eigRatio,
562  const V& D_inv);
563 
566  static ST
567  powerMethod (const MAT& A, const V& D_inv, const int numIters);
568 
570  static MT maxNormInf (const MV& X);
571 
572  // mfh 22 Jan 2013: This code builds correctly, but does not
573  // converge. I'm leaving it in place in case someone else wants to
574  // work on it.
575 #if 0
576  void
599  mlApplyImpl (const MAT& A,
600  const MV& B,
601  MV& X,
602  const int numIters,
603  const ST lambdaMax,
604  const ST lambdaMin,
605  const ST eigRatio,
606  const V& D_inv)
607  {
608  const ST zero = Teuchos::as<ST> (0);
609  const ST one = Teuchos::as<ST> (1);
610  const ST two = Teuchos::as<ST> (2);
611 
612  MV pAux (B.getMap (), B.getNumVectors ()); // Result of A*X
613  MV dk (B.getMap (), B.getNumVectors ()); // Solution update
614  MV R (B.getMap (), B.getNumVectors ()); // Not in original ML; need for B - pAux
615 
616  ST beta = Teuchos::as<ST> (1.1) * lambdaMax;
617  ST alpha = lambdaMax / eigRatio;
618 
619  ST delta = (beta - alpha) / two;
620  ST theta = (beta + alpha) / two;
621  ST s1 = theta / delta;
622  ST rhok = one / s1;
623 
624  // Diagonal: ML replaces entries containing 0 with 1. We
625  // shouldn't have any entries like that in typical test problems,
626  // so it's OK not to do that here.
627 
628  // The (scaled) matrix is the identity: set X = D_inv * B. (If A
629  // is the identity, then certainly D_inv is too. D_inv comes from
630  // A, so if D_inv * A is the identity, then we still need to apply
631  // the "preconditioner" D_inv to B as well, to get X.)
632  if (lambdaMin == one && lambdaMin == lambdaMax) {
633  solve (X, D_inv, B);
634  return;
635  }
636 
637  // The next bit of code is a direct translation of code from ML's
638  // ML_Cheby function, in the "normal point scaling" section, which
639  // is in lines 7365-7392 of ml_smoother.c.
640 
641  if (! zeroStartingSolution_) {
642  // dk = (1/theta) * D_inv * (B - (A*X))
643  A.apply (X, pAux); // pAux = A * X
644  R = B;
645  R.update (-one, pAux, one); // R = B - pAux
646  dk.elementWiseMultiply (one/theta, D_inv, R, zero); // dk = (1/theta)*D_inv*R
647  X.update (one, dk, one); // X = X + dk
648  } else {
649  dk.elementWiseMultiply (one/theta, D_inv, B, zero); // dk = (1/theta)*D_inv*B
650  X = dk;
651  }
652 
653  ST rhokp1, dtemp1, dtemp2;
654  for (int k = 0; k < numIters-1; ++k) {
655  A.apply (X, pAux);
656  rhokp1 = one / (two*s1 - rhok);
657  dtemp1 = rhokp1*rhok;
658  dtemp2 = two*rhokp1/delta;
659  rhok = rhokp1;
660 
661  R = B;
662  R.update (-one, pAux, one); // R = B - pAux
663  // dk = dtemp1 * dk + dtemp2 * D_inv * (B - pAux)
664  dk.elementWiseMultiply (dtemp2, D_inv, B, dtemp1);
665  X.update (one, dk, one); // X = X + dk
666  }
667  }
668  #endif // 0
669 
670  };
671 
672  } // namespace Details
673  } // namespace Ifpack2
674 
675 #endif // IFPACK2_DETAILS_CHEBYSHEV_DECL_HPP
MT apply(const MV &B, MV &X)
Solve Ax=b for x with Chebyshev iteration with left diagonal scaling.
Definition: Ifpack2_Chebyshev_def.hpp:199
void describe(Teuchos::FancyOStream &out, const Teuchos::EVerbosityLevel verbLevel=Teuchos::Describable::verbLevel_default) const
Print a description of the Chebyshev solver to out.
Definition: Ifpack2_Chebyshev_def.hpp:364
Chebyshev(Teuchos::RCP< const MAT > A)
Definition: Ifpack2_Details_Chebyshev_def.hpp:149
void print(std::ostream &out)
Print instance data to the given output stream.
Definition: Ifpack2_Details_Chebyshev_def.hpp:648
Left-scaled Chebyshev iteration.
Definition: Ifpack2_Details_Chebyshev_decl.hpp:128
static const EVerbosityLevel verbLevel_default
void setParameters(Teuchos::ParameterList &plist)
Set (or reset) parameters.
Definition: Ifpack2_Chebyshev_def.hpp:75
void compute()
(Re)compute the left scaling D_inv, and estimate min and max eigenvalues of D_inv * A...
Definition: Ifpack2_Chebyshev_def.hpp:270
bool hasTransposeApply() const
Whether it&#39;s possible to apply the transpose of this operator.
Definition: Ifpack2_Chebyshev_def.hpp:115
std::string description() const
A single-line description of the Chebyshev solver.
Definition: Ifpack2_Chebyshev_def.hpp:339
Teuchos::RCP< const MAT > getMatrix() const
Get the matrix given to the constructor.
Definition: Ifpack2_Chebyshev_def.hpp:93