LifeV
MultiscaleAlgorithm.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 File containing the Multiscale Algorithm
30  *
31  * @date 23-10-2009
32  * @author Cristiano Malossi <cristiano.malossi@epfl.ch>
33  *
34  * @maintainer Cristiano Malossi <cristiano.malossi@epfl.ch>
35  */
36 
37 #include <lifev/multiscale/algorithms/MultiscaleAlgorithm.hpp>
38 
39 namespace LifeV
40 {
41 namespace Multiscale
42 {
43 
45 
46 // ===================================================
47 // Constructors & Destructor
48 // ===================================================
50  M_type (),
51  M_name (),
52  M_multiscale (),
55  M_comm (),
57  M_tolerance ()
58 {
59 
60 #ifdef HAVE_LIFEV_DEBUG
61  debugStream ( 8010 ) << "MultiscaleAlgorithm::MultiscaleAlgorithm() \n";
62 #endif
63 
64 }
65 
66 // ===================================================
67 // Multiscale Algorithm Virtual Methods
68 // ===================================================
69 void
71 {
72 
73 #ifdef HAVE_LIFEV_DEBUG
74  debugStream ( 8010 ) << "MultiscaleAlgorithm::setMultiscaleProblem( multiscale ) \n";
75 #endif
76 
77  // Build coupling variables and residuals vectors
78  std::vector<Int> myGlobalElements (0);
79  MapEpetra couplingMap ( -1, static_cast<Int> ( myGlobalElements.size() ), &myGlobalElements[0], M_comm );
81 
82  M_couplingVariables.reset ( new VectorEpetra ( couplingMap, Unique ) );
83  M_couplingResiduals.reset ( new VectorEpetra ( couplingMap, Unique ) );
84 }
85 
86 void
88 {
89 
90 #ifdef HAVE_LIFEV_DEBUG
91  debugStream ( 8010 ) << "MultiscaleAlgorithm::subIterate() \n";
92 #endif
93 
94  // Algorithm Type
95  if ( M_comm->MyPID() == 0 )
96  {
97  std::cout << " MS- " << enum2String ( M_type, multiscaleAlgorithmsMap ) << " Algorithm" << std::endl;
98  }
99 }
100 
101 void
103 {
104  if ( M_comm->MyPID() == 0 )
105  {
106  std::cout << "Algorithm type = " << enum2String ( M_type, multiscaleAlgorithmsMap ) << std::endl
107  << "Algorithm name = " << M_name << std::endl
108  << "Max Sub-iterations = " << M_subiterationsMaximumNumber << std::endl
109  << "Tolerance = " << M_tolerance << std::endl << std::endl;
110  }
111 }
112 
113 // ===================================================
114 // Methods
115 // ===================================================
116 Real
118 {
119  // Compute interface residual
121 
122  // Export interface residual
123  M_multiscale->exportCouplingResiduals ( *M_couplingResiduals );
124 
125  // Norm2 of the interface residual
126  return M_couplingResiduals->norm2();
127 }
128 
129 // ===================================================
130 // Set Methods
131 // ===================================================
132 void
134 {
135  M_name = parameterList.get<std::string> ( "Algorithm Name" );
136 }
137 
138 void
140 {
141  M_subiterationsMaximumNumber = parameterList.get<UInt> ( "Subiterations Maximum Number" );
142  M_tolerance = parameterList.get<Real> ( "Tolerance" );
143 }
144 
145 // ===================================================
146 // Protected Methods
147 // ===================================================
148 void
149 MultiscaleAlgorithm::save ( const UInt& subiterationsNumber, const Real& residual ) const
150 {
151  std::ofstream output;
152  output << std::scientific << std::setprecision ( 15 );
153 
154  if ( M_comm->MyPID() == 0 )
155  {
156  std::string filename = multiscaleProblemFolder + multiscaleProblemPrefix + "_Algorithm_" + number2string ( multiscaleProblemStep ) + ".mfile";
157 
158  if ( M_multiscale->globalData()->dataTime()->isFirstTimeStep() )
159  {
160  output.open ( filename.c_str(), std::ios::trunc );
161  output << "% Algorithm Type: " << enum2String ( M_type, multiscaleAlgorithmsMap ) << std::endl;
162  output << "% Subiteration maximum number: " << M_subiterationsMaximumNumber << std::endl;
163  output << "% Tolerance: " << M_tolerance << std::endl << std::endl;
164  output << "% Time Subiterations Residual" << std::endl;
165  }
166  else
167  {
168  output.open ( filename.c_str(), std::ios::app );
169  }
170 
171  output << M_multiscale->globalData()->dataTime()->time() << " "
172  << subiterationsNumber << " " << residual << std::endl;
173 
174  output.close();
175  }
176 }
177 
178 bool
179 MultiscaleAlgorithm::checkResidual ( const UInt& subIT ) const
180 {
181  // Compute computeResidual
182  Real residual ( computeResidual() );
183 
184  // Display subIT and residual values
185  if ( M_comm->MyPID() == 0 )
186  {
187  if ( subIT > 0 )
188  {
189  std::cout << " MS- Sub-iteration n.: " << subIT << std::endl;
190  }
191  std::cout << " MS- Residual: " << residual << std::endl;
192  }
193  // Is the tolerance satisfied?
194  if ( residual <= M_tolerance )
195  {
196  save ( subIT, M_couplingResiduals->norm2() );
197  return true;
198  }
199  else
200  {
201  return false;
202  }
203 }
204 
205 } // Namespace Multiscale
206 } // Namespace LifeV
void save(const UInt &subiterationsNumber, const Real &residual) const
save on a Matlab file the information about the convergence of the algorithm.
void setAlgorithmName(const multiscaleParameterList_Type &parameterList)
Set the algorithm name.
virtual void showMe()
Display some information about the algorithm.
MultiscaleAlgorithm - The Multiscale Algorithm Interface.
virtual void setupAlgorithm()
Setup coupling variables and other quantities of the algorithm.
void updateInverseJacobian(const UInt &iQuadPt)
Epetra_Import const & importer()
Getter for the Epetra_Import.
Definition: MapEpetra.cpp:394
virtual void setAlgorithmParameters(const multiscaleParameterList_Type &parameterList)
Set the the main parameters of the algorithm (tolerance, maximum number of subiterations, etc.)
void createCouplingMap(MapEpetra &couplingMap)
Build the global map for the coupling vectors.
std::map< std::string, algorithms_Type > multiscaleAlgorithmsMap
LinearSolver::parameterList_Type multiscaleParameterList_Type
void computeCouplingResiduals()
Compute the values of the interface residuals.
double Real
Generic real data.
Definition: LifeV.hpp:175
multiscaleModelMultiscalePtr_Type M_multiscale
bool checkResidual(const UInt &subIT=0) const
Update the residual and check if the tolerance has been satisfied.
virtual void subIterate()
Perform sub-iteration on the coupling variables.
uint32_type UInt
generic unsigned integer (used mainly for addressing)
Definition: LifeV.hpp:191