MueLu  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
MueLu_SteepestDescentSolver_def.hpp
Go to the documentation of this file.
1 // @HEADER
2 //
3 // ***********************************************************************
4 //
5 // MueLu: A package for multigrid based preconditioning
6 // Copyright 2012 Sandia Corporation
7 //
8 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
9 // the U.S. Government retains certain rights in this software.
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
39 // Jonathan Hu (jhu@sandia.gov)
40 // Andrey Prokopenko (aprokop@sandia.gov)
41 // Ray Tuminaro (rstumin@sandia.gov)
42 //
43 // ***********************************************************************
44 //
45 // @HEADER
46 #ifndef MUELU_STEEPESTDESCENTSOLVER_DEF_HPP
47 #define MUELU_STEEPESTDESCENTSOLVER_DEF_HPP
48 
50 #include <Xpetra_CrsMatrixWrap.hpp>
51 #include <Xpetra_MatrixMatrix.hpp>
52 
53 #include "MueLu_Constraint.hpp"
54 #include "MueLu_Monitor.hpp"
55 #include "MueLu_Utilities.hpp"
56 
58 
59 namespace MueLu {
60 
61  using Teuchos::rcp_const_cast;
62 
63  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
65  : nIts_(Its), stepLength_(StepLength)
66  { }
67 
68  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
69  void SteepestDescentSolver<Scalar, LocalOrdinal, GlobalOrdinal, Node>::Iterate(const Matrix& Aref, const Constraint& C, const Matrix& P0, RCP<Matrix>& P) const {
70  PrintMonitor m(*this, "SD iterations");
71 
72  RCP<const Matrix> A = rcpFromRef(Aref);
73  RCP<Matrix> AP, G;
74 
75  Teuchos::FancyOStream& mmfancy = this->GetOStream(Statistics2);
76 
78 
79  RCP<CrsMatrix> Ptmp_ = CrsMatrixFactory::Build(C.GetPattern());
80  Ptmp_->fillComplete(P0.getDomainMap(), P0.getRangeMap());
81  RCP<Matrix> Ptmp = rcp(new CrsMatrixWrap(Ptmp_));
82 
83  // Initial P0 would only be used for multiplication
84  P = rcp_const_cast<Matrix>(rcpFromRef(P0));
85 
86  for (size_t k = 0; k < nIts_; k++) {
87  AP = Xpetra::MatrixMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node>::Multiply(*A, false, *P, false, mmfancy, true, false);
88 #if 0
89  // gradient = -2 A^T * A * P
90  SC stepLength = 2*stepLength_;
92  C.Apply(*G, *Ptmp);
93 #else
94  // gradient = - A * P
95  SC stepLength = stepLength_;
96  Utilities::MyOldScaleMatrix(*AP, D, true, false, false);
97  C.Apply(*AP, *Ptmp);
98 #endif
99 
100  RCP<Matrix> newP;
102  newP->fillComplete(P->getDomainMap(), P->getRangeMap() );
103  P = newP;
104  }
105  }
106 }
107 
108 #endif //ifndef MUELU_STEEPESTDESCENTSOLVER_DECL_HPP
static Teuchos::ArrayRCP< Scalar > GetMatrixDiagonal(const Xpetra::Matrix< Scalar, LocalOrdinal, GlobalOrdinal, Node > &A)
static void MyOldScaleMatrix(Xpetra::Matrix< Scalar, LocalOrdinal, GlobalOrdinal, Node > &Op, const Teuchos::ArrayRCP< const Scalar > &scalingVector, bool doInverse=true, bool doFillComplete=true, bool doOptimizeStorage=true)
Constraint space information for the potential prolongator.
RCP< const CrsGraph > GetPattern() const
Print even more statistics.
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
static void TwoMatrixAdd(const Matrix &A, bool transposeA, SC alpha, Matrix &B, SC beta)
void Apply(const Matrix &P, Matrix &Projected) const
Apply constraint.
static void Multiply(const Matrix &A, bool transposeA, const Matrix &B, bool transposeB, Matrix &C, bool call_FillComplete_on_result=true, bool doOptimizeStorage=true, const std::string &label=std::string(), const RCP< ParameterList > &params=null)
Scalar SC
void Iterate(const Matrix &A, const Constraint &C, const Matrix &P0, RCP< Matrix > &P) const
Iterate.
SteepestDescentSolver(size_t Its, SC StepLength=Teuchos::ScalarTraits< Scalar >::one())