Belos Package Browser (Single Doxygen Collection)  Development
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
BelosStatusTestFactory.hpp
Go to the documentation of this file.
1 //@HEADER
2 // ************************************************************************
3 //
4 // Belos: Block Linear Solvers Package
5 // Copyright 2004 Sandia Corporation
6 //
7 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
8 // the U.S. Government retains certain rights in this software.
9 //
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions are
12 // met:
13 //
14 // 1. Redistributions of source code must retain the above copyright
15 // notice, this list of conditions and the following disclaimer.
16 //
17 // 2. Redistributions in binary form must reproduce the above copyright
18 // notice, this list of conditions and the following disclaimer in the
19 // documentation and/or other materials provided with the distribution.
20 //
21 // 3. Neither the name of the Corporation nor the names of the
22 // contributors may be used to endorse or promote products derived from
23 // this software without specific prior written permission.
24 //
25 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
26 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
29 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 //
37 // Questions? Contact Michael A. Heroux (maherou@sandia.gov)
38 //
39 // ************************************************************************
40 //@HEADER
41 
42 #include <BelosStatusTestCombo.hpp>
48 
49 namespace Belos {
50 
57  template<class Scalar, class MV, class OP>
59  public:
62 
65 
68  tagged_tests_ = Teuchos::rcp(new std::map<std::string, Teuchos::RCP<base_test> > );
69  };
70 
72  virtual ~StatusTestFactory() { };
73 
76  Teuchos::RCP<base_test> status_test;
77 
78  std::string test_type = "???";
79 
80  // every defined test in the parmater list needs a type specifier using the "Test Type" parameter
81  if(Teuchos::isParameterType<std::string>(p, "Test Type")) {
82  test_type = Teuchos::get<std::string>(p, "Test Type");
83  } else {
84  TEUCHOS_TEST_FOR_EXCEPTION(true, std::logic_error, "Belos::StatusTestFactory::buildStatusTests: The \"Test Type\" parameter is required! Please add it to the definition of the status test to specify the type of status test.");
85  }
86 
87  if (test_type == "Combo")
88  status_test = this->buildComboTest(p);
89  else if (test_type == "MaxIters")
90  status_test = this->buildMaxItersTest(p);
91  else if (test_type == "ResidualNorm")
92  status_test = this->buildResidualNormTest(p);
93  else if (test_type == "PartialResidualNorm")
94  status_test = this->buildPartialResidualNormTest(p);
95  else {
96  std::ostringstream msg;
97  msg << "Error - the test type \"" << test_type << "\" is invalid!";
98  TEUCHOS_TEST_FOR_EXCEPTION(true, std::logic_error, msg.str());
99  }
100 
101  // collect tagged status tests
102  if ( Teuchos::isParameterType<std::string>(p, "Tag") ) {
103  (*tagged_tests_)[Teuchos::getParameter<std::string>(p, "Tag")] = status_test;
104  }
105 
106  return status_test;
107  }
108 
109  static typename StatusTestCombo<Scalar,MV,OP>::ComboType stringToComboType ( const std::string& comboString ) {
110  typedef typename combo_test::ComboType comboType;
111  comboType userCombo;
112  if (comboString == "AND") userCombo = combo_test::AND;
113  else if(comboString == "OR") userCombo = combo_test::OR;
114  else if(comboString == "SEQ") userCombo = combo_test::SEQ;
115  else TEUCHOS_TEST_FOR_EXCEPTION(true, std::logic_error, "StatusTestFactory:stringToComboType: The \"Combo Type\" must be \"AND\", \"OR\" or \"SEQ\".");
116  return userCombo;
117  }
118 
120 
121  private:
122 
125 
127  typedef typename combo_test::ComboType comboType;
128 
129  std::string combo_type_string = Teuchos::get<std::string>(p, "Combo Type");
130  int number_of_tests = Teuchos::get<int>(p, "Number of Tests");
131 
132  comboType combo_type = stringToComboType(combo_type_string);
133 
134  Teuchos::RCP<combo_test> status_test = Teuchos::rcp(new combo_test(combo_type));
135 
136  for (int i=0; i<number_of_tests; ++i) {
137  std::ostringstream subtest_name;
138  subtest_name << "Test " << i;
139  Teuchos::ParameterList& subtest_list = p.sublist(subtest_name.str(), true);
140 
141  Teuchos::RCP<base_test> subtest = this->buildStatusTests(subtest_list); // todo add support for tagged entries
142  status_test->addStatusTest(subtest);
143  }
144 
145  return status_test;
146  }
147 
149  int max_iters = Teuchos::get<int>(p, "Maximum Iterations");
150  Teuchos::RCP<max_iter_test> status_test = Teuchos::rcp(new max_iter_test(max_iters));
151  return status_test;
152  }
153 
155  typedef StatusTestGenResNorm<Scalar,MV,OP> res_norm_test;
156  typename Teuchos::ScalarTraits<Scalar>::magnitudeType tolerance = p.get("Convergence Tolerance", 1.0e-8);
157  int quorum = p.get<int>("Deflation Quorum", -1);
158  bool showMaxResNormOnly = p.get<bool>("Show Maximum Residual Norm Only",false);
159 
160  std::string residual_type_string = p.get<std::string>("Residual Type", "Explicit");
161  std::string residual_norm_string = p.get<std::string>("Residual Norm", "TwoNorm");
162 
163  std::string scaling_type_string = p.get<std::string>("Scaling Type", "Norm of Initial Residual");
164  std::string scaling_norm_string = p.get<std::string>("Scaling Norm", "TwoNorm");
165 
166  typename res_norm_test::ResType residual_type;
167  if (residual_type_string == "Explicit") residual_type = res_norm_test::Explicit;
168  else if(residual_type_string == "Implicit") residual_type = res_norm_test::Implicit;
169  else TEUCHOS_TEST_FOR_EXCEPTION(true, std::logic_error, "Belos::StatusTestFactory::buildResidualNormTest: The \"Residual Type\" must be \"Explicit\" or \"Implicit\".");
170 
171  NormType residual_norm = this->stringToNormType(residual_norm_string);
172  ScaleType scaling_type = this->stringToScaleType(scaling_type_string);
173  NormType scaling_norm = this->stringToNormType(scaling_norm_string);
174 
175  Teuchos::RCP<res_norm_test> status_test = Teuchos::rcp(new res_norm_test(tolerance,quorum,showMaxResNormOnly));
176  status_test->defineResForm(residual_type, residual_norm);
177  status_test->defineScaleForm(scaling_type, scaling_norm);
178  return status_test;
179  }
180 
182  typedef StatusTestGenResSubNorm<Scalar,MV,OP> res_partialnorm_test;
183  typename Teuchos::ScalarTraits<Scalar>::magnitudeType tolerance = p.get("Convergence Tolerance", 1.0e-8);
184  int quorum = p.get<int>("Deflation Quorum", -1);
185  int subIdx = p.get<int>("Block index",-1);
186  bool showMaxResNormOnly = p.get<bool>("Show Maximum Residual Norm Only",false);
187 
188  TEUCHOS_TEST_FOR_EXCEPTION(subIdx < 0, std::logic_error, "Belos::StatusTestFactory::buildPartialResidualNormTest: The \"Block Index\" must not be smaller than 0.");
189 
190  std::string residual_norm_string = p.get<std::string>("Residual Norm", "TwoNorm");
191 
192  std::string scaling_type_string = p.get<std::string>("Scaling Type", "Norm of Initial Residual");
193  std::string scaling_norm_string = p.get<std::string>("Scaling Norm", "TwoNorm");
194 
195  NormType residual_norm = this->stringToNormType(residual_norm_string);
196  ScaleType scaling_type = this->stringToScaleType(scaling_type_string);
197  NormType scaling_norm = this->stringToNormType(scaling_norm_string);
198 
199  Teuchos::RCP<res_partialnorm_test> status_test = Teuchos::rcp(new res_partialnorm_test(tolerance,subIdx,quorum,showMaxResNormOnly));
200  status_test->defineResForm(residual_norm);
201  status_test->defineScaleForm(scaling_type, scaling_norm);
202  return status_test;
203  }
204 
205  static NormType stringToNormType (const std::string& normType) {
206  const char* validNames[] = {
207  "OneNorm",
208  "TwoNorm",
209  "InfNorm"
210  };
211  const int numValidNames = 3;
212  const NormType correspondingOutputs[] = {
216  };
217  for (int k = 0; k < numValidNames; ++k)
218  {
219  if (normType == validNames[k])
220  return correspondingOutputs[k];
221  }
222  TEUCHOS_TEST_FOR_EXCEPTION (true, std::logic_error,
223  "Invalid norm type \"" << normType
224  << "\".");
225  }
226 
227  static ScaleType stringToScaleType (const std::string& scaleType) {
228  const char* validNames[] = {
229  "Norm of Initial Residual",
230  "Norm of Preconditioned Initial Residual",
231  "Norm of RHS",
232  "Norm of Right-Hand Side",
233  "Norm of Full Initial Residual",
234  "Norm of Full Preconditioned Initial Residual",
235  "Norm of Full Scaled Initial Residual",
236  "Norm of Full Scaled Preconditioned Initial Residual",
237  "None"
238  };
239  const int numValidNames = 9;
240  const ScaleType correspondingOutputs[] = {
250  };
251  for (int k = 0; k < numValidNames; ++k)
252  {
253  if (scaleType == validNames[k])
254  return correspondingOutputs[k];
255  }
256  TEUCHOS_TEST_FOR_EXCEPTION (true, std::logic_error,
257  "Invalid residual scaling type \"" << scaleType
258  << "\".");
259  }
260  };
261 
262 } // namespace Belos
ComboType
The test can be either the AND of all the component tests, or the OR of all the component tests...
Teuchos::RCP< std::map< std::string, Teuchos::RCP< base_test > > > getTaggedTests() const
Teuchos::RCP< base_test > buildResidualNormTest(Teuchos::ParameterList &p) const
Teuchos::RCP< std::map< std::string, Teuchos::RCP< base_test > > > tagged_tests_
maps test names (defined by the &quot;Tag&quot; parameter) to the corresponding status test ...
StatusTestMaxIters< Scalar, MV, OP > max_iter_test
ScaleType
The type of scaling to use on the residual norm value.
Definition: BelosTypes.hpp:120
T & get(ParameterList &l, const std::string &name)
StatusTest< Scalar, MV, OP > base_test
An implementation of StatusTestResNorm using a family of norms of subvectors of the residual vectors...
Teuchos::RCP< base_test > buildPartialResidualNormTest(Teuchos::ParameterList &p) const
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
Teuchos::ScalarTraits< Scalar >::magnitudeType magnitude_type
Teuchos::RCP< base_test > buildStatusTests(Teuchos::ParameterList &p) const
returns a StatusTest set from a parameter list
An implementation of StatusTestResNorm using a family of residual norms.
static StatusTestCombo< Scalar, MV, OP >::ComboType stringToComboType(const std::string &comboString)
StatusTestCombo< ScalarType, MV, OP > & addStatusTest(const Teuchos::RCP< StatusTest< ScalarType, MV, OP > > &add_test)
Add another test to this combination.
RCP< ParameterList > sublist(const RCP< ParameterList > &paramList, const std::string &name, bool mustAlreadyExist=false, const std::string &docString="")
Belos::StatusTest class for specifying a maximum number of iterations.
A pure virtual class for defining the status tests for the Belos iterative solvers.
static ScaleType stringToScaleType(const std::string &scaleType)
Belos::StatusTest for logically combining several status tests.
A Belos::StatusTest class for specifying a maximum number of iterations.
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
Teuchos::RCP< base_test > buildMaxItersTest(Teuchos::ParameterList &p) const
virtual ~StatusTestFactory()
Destructor.
static NormType stringToNormType(const std::string &normType)
NormType
The type of vector norm to compute.
Definition: BelosTypes.hpp:97
Belos::StatusTestResNorm for specifying general residual norm stopping criteria.
Belos::StatusTest for specifying an implicit residual norm stopping criteria that checks for loss of ...
A class for extending the status testing capabilities of Belos via logical combinations.
StatusTestCombo< Scalar, MV, OP > combo_test
Teuchos::RCP< base_test > buildComboTest(Teuchos::ParameterList &p) const
Factory to build a set of status tests from a parameter list.
Belos::StatusTestResSubNorm for specifying general residual norm of sub-residual vectors stopping cri...