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 // Belos: Block Linear Solvers Package
4 //
5 // Copyright 2004-2016 NTESS and the Belos contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9 
10 #include <BelosStatusTestCombo.hpp>
16 
17 namespace Belos {
18 
25  template<class Scalar, class MV, class OP>
27  public:
30 
33 
36  tagged_tests_ = Teuchos::rcp(new std::map<std::string, Teuchos::RCP<base_test> > );
37  };
38 
40  virtual ~StatusTestFactory() { };
41 
44  Teuchos::RCP<base_test> status_test;
45 
46  std::string test_type = "???";
47 
48  // every defined test in the parmater list needs a type specifier using the "Test Type" parameter
49  if(Teuchos::isParameterType<std::string>(p, "Test Type")) {
50  test_type = Teuchos::get<std::string>(p, "Test Type");
51  } else {
52  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.");
53  }
54 
55  if (test_type == "Combo")
56  status_test = this->buildComboTest(p);
57  else if (test_type == "MaxIters")
58  status_test = this->buildMaxItersTest(p);
59  else if (test_type == "ResidualNorm")
60  status_test = this->buildResidualNormTest(p);
61  else if (test_type == "PartialResidualNorm")
62  status_test = this->buildPartialResidualNormTest(p);
63  else {
64  std::ostringstream msg;
65  msg << "Error - the test type \"" << test_type << "\" is invalid!";
66  TEUCHOS_TEST_FOR_EXCEPTION(true, std::logic_error, msg.str());
67  }
68 
69  // collect tagged status tests
70  if ( Teuchos::isParameterType<std::string>(p, "Tag") ) {
71  (*tagged_tests_)[Teuchos::getParameter<std::string>(p, "Tag")] = status_test;
72  }
73 
74  return status_test;
75  }
76 
77  static typename StatusTestCombo<Scalar,MV,OP>::ComboType stringToComboType ( const std::string& comboString ) {
78  typedef typename combo_test::ComboType comboType;
79  comboType userCombo;
80  if (comboString == "AND") userCombo = combo_test::AND;
81  else if(comboString == "OR") userCombo = combo_test::OR;
82  else if(comboString == "SEQ") userCombo = combo_test::SEQ;
83  else TEUCHOS_TEST_FOR_EXCEPTION(true, std::logic_error, "StatusTestFactory:stringToComboType: The \"Combo Type\" must be \"AND\", \"OR\" or \"SEQ\".");
84  return userCombo;
85  }
86 
88 
89  private:
90 
93 
95  typedef typename combo_test::ComboType comboType;
96 
97  std::string combo_type_string = Teuchos::get<std::string>(p, "Combo Type");
98  int number_of_tests = Teuchos::get<int>(p, "Number of Tests");
99 
100  comboType combo_type = stringToComboType(combo_type_string);
101 
102  Teuchos::RCP<combo_test> status_test = Teuchos::rcp(new combo_test(combo_type));
103 
104  for (int i=0; i<number_of_tests; ++i) {
105  std::ostringstream subtest_name;
106  subtest_name << "Test " << i;
107  Teuchos::ParameterList& subtest_list = p.sublist(subtest_name.str(), true);
108 
109  Teuchos::RCP<base_test> subtest = this->buildStatusTests(subtest_list); // todo add support for tagged entries
110  status_test->addStatusTest(subtest);
111  }
112 
113  return status_test;
114  }
115 
117  int max_iters = Teuchos::get<int>(p, "Maximum Iterations");
118  Teuchos::RCP<max_iter_test> status_test = Teuchos::rcp(new max_iter_test(max_iters));
119  return status_test;
120  }
121 
123  typedef StatusTestGenResNorm<Scalar,MV,OP> res_norm_test;
124  typename Teuchos::ScalarTraits<Scalar>::magnitudeType tolerance = p.get("Convergence Tolerance", 1.0e-8);
125  int quorum = p.get<int>("Deflation Quorum", -1);
126  bool showMaxResNormOnly = p.get<bool>("Show Maximum Residual Norm Only",false);
127 
128  std::string residual_type_string = p.get<std::string>("Residual Type", "Explicit");
129  std::string residual_norm_string = p.get<std::string>("Residual Norm", "TwoNorm");
130 
131  std::string scaling_type_string = p.get<std::string>("Scaling Type", "Norm of Initial Residual");
132  std::string scaling_norm_string = p.get<std::string>("Scaling Norm", "TwoNorm");
133 
134  typename res_norm_test::ResType residual_type;
135  if (residual_type_string == "Explicit") residual_type = res_norm_test::Explicit;
136  else if(residual_type_string == "Implicit") residual_type = res_norm_test::Implicit;
137  else TEUCHOS_TEST_FOR_EXCEPTION(true, std::logic_error, "Belos::StatusTestFactory::buildResidualNormTest: The \"Residual Type\" must be \"Explicit\" or \"Implicit\".");
138 
139  NormType residual_norm = this->stringToNormType(residual_norm_string);
140  ScaleType scaling_type = this->stringToScaleType(scaling_type_string);
141  NormType scaling_norm = this->stringToNormType(scaling_norm_string);
142 
143  Teuchos::RCP<res_norm_test> status_test = Teuchos::rcp(new res_norm_test(tolerance,quorum,showMaxResNormOnly));
144  status_test->defineResForm(residual_type, residual_norm);
145  status_test->defineScaleForm(scaling_type, scaling_norm);
146  return status_test;
147  }
148 
150  typedef StatusTestGenResSubNorm<Scalar,MV,OP> res_partialnorm_test;
151  typename Teuchos::ScalarTraits<Scalar>::magnitudeType tolerance = p.get("Convergence Tolerance", 1.0e-8);
152  int quorum = p.get<int>("Deflation Quorum", -1);
153  int subIdx = p.get<int>("Block index",-1);
154  bool showMaxResNormOnly = p.get<bool>("Show Maximum Residual Norm Only",false);
155 
156  TEUCHOS_TEST_FOR_EXCEPTION(subIdx < 0, std::logic_error, "Belos::StatusTestFactory::buildPartialResidualNormTest: The \"Block Index\" must not be smaller than 0.");
157 
158  std::string residual_norm_string = p.get<std::string>("Residual Norm", "TwoNorm");
159 
160  std::string scaling_type_string = p.get<std::string>("Scaling Type", "Norm of Initial Residual");
161  std::string scaling_norm_string = p.get<std::string>("Scaling Norm", "TwoNorm");
162 
163  NormType residual_norm = this->stringToNormType(residual_norm_string);
164  ScaleType scaling_type = this->stringToScaleType(scaling_type_string);
165  NormType scaling_norm = this->stringToNormType(scaling_norm_string);
166 
167  Teuchos::RCP<res_partialnorm_test> status_test = Teuchos::rcp(new res_partialnorm_test(tolerance,subIdx,quorum,showMaxResNormOnly));
168  status_test->defineResForm(residual_norm);
169  status_test->defineScaleForm(scaling_type, scaling_norm);
170  return status_test;
171  }
172 
173  static NormType stringToNormType (const std::string& normType) {
174  const char* validNames[] = {
175  "OneNorm",
176  "TwoNorm",
177  "InfNorm"
178  };
179  const int numValidNames = 3;
180  const NormType correspondingOutputs[] = {
184  };
185  for (int k = 0; k < numValidNames; ++k)
186  {
187  if (normType == validNames[k])
188  return correspondingOutputs[k];
189  }
190  TEUCHOS_TEST_FOR_EXCEPTION (true, std::logic_error,
191  "Invalid norm type \"" << normType
192  << "\".");
193  }
194 
195  static ScaleType stringToScaleType (const std::string& scaleType) {
196  const char* validNames[] = {
197  "Norm of Initial Residual",
198  "Norm of Preconditioned Initial Residual",
199  "Norm of RHS",
200  "Norm of Right-Hand Side",
201  "Norm of Full Initial Residual",
202  "Norm of Full Preconditioned Initial Residual",
203  "Norm of Full Scaled Initial Residual",
204  "Norm of Full Scaled Preconditioned Initial Residual",
205  "None"
206  };
207  const int numValidNames = 9;
208  const ScaleType correspondingOutputs[] = {
218  };
219  for (int k = 0; k < numValidNames; ++k)
220  {
221  if (scaleType == validNames[k])
222  return correspondingOutputs[k];
223  }
224  TEUCHOS_TEST_FOR_EXCEPTION (true, std::logic_error,
225  "Invalid residual scaling type \"" << scaleType
226  << "\".");
227  }
228  };
229 
230 } // 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:88
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:65
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...