LifeV
PreconditionerLinearSolver.cpp
Go to the documentation of this file.
1 //@HEADER
2 /*
3 *******************************************************************************
4 
5  Copyright (C) 2004, 2005, 2007 EPFL, Politecnico di Milano, INRIA
6  Copyright (C) 2010 EPFL, Politecnico di Milano, Emory University
7 
8  This file is part of LifeV.
9 
10  LifeV is free software; you can redistribute it and/or modify
11  it under the terms of the GNU Lesser General Public License as published by
12  the Free Software Foundation, either version 3 of the License, or
13  (at your option) any later version.
14 
15  LifeV is distributed in the hope that it will be useful,
16  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18  Lesser General Public License for more details.
19 
20  You should have received a copy of the GNU Lesser General Public License
21  along with LifeV. If not, see <http://www.gnu.org/licenses/>.
22 
23 *******************************************************************************
24 */
25 //@HEADER
26 
27 /*!
28  @file
29  @brief LinearSolver preconditioner
30 
31  @author Gwenol Grandperrin <gwenol.grandperrin@epfl.ch>
32  @maintainer Gwenol Grandperrin <gwenol.grandperrin@epfl.ch>
33 
34  @date 17-09-2011
35  */
36 
37 #include <lifev/core/algorithm/PreconditionerLinearSolver.hpp>
38 #include <lifev/core/util/LifeDebug.hpp>
39 #include <lifev/core/util/LifeChrono.hpp>
40 #include <lifev/core/filter/GetPot.hpp>
41 
42 namespace LifeV
43 {
44 
45 // ===================================================
46 // Constructors & Destructor
47 // ===================================================
48 PreconditionerLinearSolver::PreconditionerLinearSolver ( std::shared_ptr<Epetra_Comm> comm ) :
49  Preconditioner ( comm ),
50  M_printSubiterationCount ( false ),
51  M_precName ( "" ),
52  M_precDataSection ( "" )
53 {
54 
55 }
56 
58 {
59  M_solver.reset();
60  M_preconditioner.reset();
61 }
62 
63 // ===================================================
64 // Methods
65 // ===================================================
66 
67 void
69  const GetPot& dataFile,
70  const std::string& section,
71  const std::string& subSection )
72 {
73  createLinearSolverList ( list, dataFile, section, subSection, M_displayer.comm()->MyPID() == 0 );
74 }
75 
76 void
78  const GetPot& dataFile,
79  const std::string& section,
80  const std::string& subsection,
81  const bool& verbose )
82 {
83  bool displayList = dataFile ( ( section + "/displayList" ).data(), false);
84 
85  // If this option is true, the solver will print the iteration count
86  const std::string solverParamFile = dataFile ( ( section + "/" + subsection + "/parameters_file" ).data(), "none" );
87  list = * ( Teuchos::getParametersFromXmlFile ( solverParamFile ) );
88 
89  if ( displayList && verbose )
90  {
91  std::cout << "PreconditionerLinearSolver parameters list:" << std::endl;
92  std::cout << "-----------------------------" << std::endl;
93  list.print ( std::cout );
94  std::cout << "-----------------------------" << std::endl;
95  }
96 }
97 
98 Int
100 {
101  // Setup the solver
102  M_solver.reset ( new solver_Type ( this->M_displayer.comm() ) );
103  M_solver->setParameters ( M_list.sublist ( "LinearSolver" ) );
104  M_solver->setOperator ( matrix );
105 
106  // Setup the preconditioner for the solver
107  M_preconditioner.reset ( PRECFactory::instance().createObject ( M_precName ) );
108  ASSERT ( M_preconditioner.get() != 0, " Preconditioner not set" );
109  M_preconditioner->setDataFromGetPot ( M_dataFile, M_precDataSection );
110  M_solver->setPreconditioner ( M_preconditioner );
111  M_solver->buildPreconditioner();
112  M_solver->setupSolverOperator();
113  M_solver->solver()->setUsedForPreconditioning ( M_printSubiterationCount );
114 
115  this->M_preconditionerCreated = true;
116 
117  return 0;
118 }
119 
120 void
122 {
123  M_solver.reset();
124  this->M_preconditionerCreated = false;
125 }
126 
127 Real
129 {
130  return 0.0;
131 }
132 
133 void
134 PreconditionerLinearSolver::showMe ( std::ostream& output ) const
135 {
136  M_solver->showMe (output);
137 }
138 
139 // ===================================================
140 // Epetra Operator Interface Methods
141 // ===================================================
142 Int
143 PreconditionerLinearSolver::SetUseTranspose ( const bool useTranspose )
144 {
145  return M_solver->solver()->SetUseTranspose (useTranspose);
146 }
147 
148 bool
150 {
151  return M_solver->solver()->UseTranspose();
152 }
153 
154 Int
155 PreconditionerLinearSolver::Apply ( const Epetra_MultiVector& X, Epetra_MultiVector& Y ) const
156 {
157  return M_solver->solver()->Apply ( X, Y );
158 }
159 
160 Int
161 PreconditionerLinearSolver::ApplyInverse ( const Epetra_MultiVector& X, Epetra_MultiVector& Y ) const
162 {
163  if ( M_solver )
164  {
165  M_solver->solver()->ApplyInverse ( X, Y );
167  {
168  M_displayer.leaderPrint ( "> ", M_solver->numIterations(), " subiterations\n" );
169  }
170  }
171  return 0;
172 }
173 
174 const Epetra_Map&
176 {
177  return M_solver->solver()->OperatorRangeMap();
178 }
179 
180 const Epetra_Map&
182 {
183  return M_solver->solver()->OperatorDomainMap();
184 }
185 
186 // ===================================================
187 // Set Methods
188 // ===================================================
189 void
190 PreconditionerLinearSolver::setDataFromGetPot ( const GetPot& dataFile, const std::string& section )
191 {
192  createLinearSolverList ( M_list, dataFile, section, "LinearSolver", M_displayer.comm()->MyPID() == 0 );
193  M_printSubiterationCount = this->M_list.get ( "Print Subiteration Count", false );
194  M_precName = this->M_list.get ( "Preconditioner", "ML" );
195  M_precDataSection = this->M_list.get ( "Preconditioner Data Section", "" );
196  M_dataFile = dataFile;
197 }
198 
199 void
201 {
202 
203 }
204 
205 // ===================================================
206 // Get Methods
207 // ===================================================
208 //! Return true if the preconditioner is set
209 bool
211 {
212  return M_solver != nullptr ? true : false;
213 }
214 
217 {
218  return M_solver->solver().get();
219 }
220 
223 {
224  return M_solver->solver();
225 }
226 
229 {
230  return M_solver;
231 }
232 
233 std::string
235 {
236  return "LinearSolver preconditioner";
237 }
238 
239 // ===================================================
240 // Private Methods
241 // ===================================================
242 
243 
244 } // namespace LifeV
bool isPreconditionerSet() const
Return true if the preconditioner is set.
const Epetra_Map & OperatorDomainMap() const
Return the Domain map of the operator.
std::shared_ptr< prec_raw_type > prec_type
SolverAztecOO - Class to wrap linear solver.
Int SetUseTranspose(const bool useTranspose=false)
Set the matrix to be used transposed (or not)
void setSolver(SolverAztecOO &)
Set the internal solver.
int32_type Int
Generic integer data.
Definition: LifeV.hpp:188
void updateInverseJacobian(const UInt &iQuadPt)
const Epetra_Map & OperatorRangeMap() const
Return the Range map of the operator.
prec_type preconditionerPtr()
Return a shared pointer on the preconditioner.
std::shared_ptr< solver_Type > solverPtr_Type
solverPtr_Type solverPtr()
Return a shared pointer on the solver.
virtual void showMe(std::ostream &output=std::cout) const
Show informations about the preconditioner.
#define ASSERT(X, A)
Definition: LifeAssert.hpp:90
virtual Real condest()
Return An estimation of the condition number of the preconditioner.
Int ApplyInverse(const Epetra_MultiVector &vector1, Epetra_MultiVector &vector2) const
Apply the inverse of the preconditioner on vector1 and store the result in vector2.
PreconditionerLinearSolver(std::shared_ptr< Epetra_Comm > comm=std::shared_ptr< Epetra_Comm >(new Epetra_MpiComm(MPI_COMM_WORLD)))
Default constructor.
Int Apply(const Epetra_MultiVector &vector1, Epetra_MultiVector &vector2) const
Apply the inverse of the preconditioner on vector1 and store the result in vector2.
virtual void createParametersList(list_Type &list, const GetPot &dataFile, const std::string &section, const std::string &subSection)
Create the list of parameters of the preconditioner.
double Real
Generic real data.
Definition: LifeV.hpp:175
static void createLinearSolverList(list_Type &list, const GetPot &dataFile, const std::string &section, const std::string &subSection="SolverLinear", const bool &verbose=true)
Create the list of parameters of the preconditioner.
void setDataFromGetPot(const GetPot &dataFile, const std::string &section)
Set the data of the preconditioner using a GetPot object.
virtual Int buildPreconditioner(operator_type &matrix)
Build a preconditioner based on the given matrix.
std::string preconditionerType()
Return the type of preconditioner.
virtual void resetPreconditioner()
Reset the preconditioner.
prec_raw_type * preconditioner()
Return a raw pointer on the preconditioner.
bool UseTranspose()
Return true if the preconditioner is transposed.
std::shared_ptr< operator_raw_type > operator_type