Anasazi  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
AnasaziStatusTestCombo.hpp
Go to the documentation of this file.
1 // @HEADER
2 // *****************************************************************************
3 // Anasazi: Block Eigensolvers Package
4 //
5 // Copyright 2004 NTESS and the Anasazi contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9 //
10 
11 #ifndef ANASAZI_STATUS_TEST_COMBO_HPP
12 #define ANASAZI_STATUS_TEST_COMBO_HPP
13 
20 #include "AnasaziTypes.hpp"
21 #include "AnasaziStatusTest.hpp"
22 #include "Teuchos_Array.hpp"
23 namespace Anasazi {
24 
25 
42 template <class ScalarType, class MV, class OP>
43 class StatusTestCombo : public StatusTest<ScalarType,MV,OP> {
44 
45  private:
47 
48  public:
49 
51  enum ComboType
52  {
53  OR,
54  AND,
57  };
58 
59 
60 #ifndef DOXYGEN_SHOULD_SKIP_THIS
61 
63  typedef std::vector< Teuchos::RCP< StatusTest<ScalarType,MV,OP> > > st_vector;
64  typedef typename st_vector::iterator iterator;
65  typedef typename st_vector::const_iterator const_iterator;
66 
67 #endif // DOXYGEN_SHOULD_SKIP_THIS
68 
70 
71 
74  StatusTestCombo() : state_(Undefined) {}
75 
79  state_(Undefined),
80  type_(type)
81  {
82  setTests(tests);
83  };
84 
86  virtual ~StatusTestCombo() {};
88 
90 
91 
96 
99  return state_;
100  }
101 
103 
110  std::vector<int> whichVecs() const {
111  return ind_;
112  }
113 
115  //
116  // See whichVecs()
117  int howMany() const {
118  return ind_.size();
119  }
120 
122 
124 
125 
129  void setComboType(ComboType type) {
130  type_ = type;
131  state_ = Undefined;
132  }
133 
135  ComboType getComboType() const {return type_;}
136 
141  tests_ = tests;
142  state_ = Undefined;
143  }
144 
147 
153  tests_.push_back(test);
154  state_ = Undefined;
155  }
156 
162 
164 
166 
167 
171  void reset();
172 
174 
179  void clearStatus();
180 
182 
184 
185 
187  std::ostream& print(std::ostream& os, int indent = 0) const;
188 
190  private:
191 
193  TestStatus evalAND(Eigensolver<ScalarType,MV,OP>* solver);
194  TestStatus evalSEQOR(Eigensolver<ScalarType,MV,OP>* solver);
195  TestStatus evalSEQAND(Eigensolver<ScalarType,MV,OP>* solver);
196 
197  TestStatus state_;
198  ComboType type_;
199  STPArray tests_;
200  std::vector<int> ind_;
201 
202 };
203 
204 
205 template <class ScalarType, class MV, class OP>
207 {
208  typename STPArray::iterator iter1;
209  iter1 = std::find(tests_.begin(),tests_.end(),test);
210  if (iter1 != tests_.end()) {
211  tests_.erase(iter1);
212  state_ = Undefined;
213  }
214 }
215 
216 
217 template <class ScalarType, class MV, class OP>
219  clearStatus();
220  switch (type_) {
221  case OR:
222  state_ = evalOR(solver);
223  break;
224  case AND:
225  state_ = evalAND(solver);
226  break;
227  case SEQOR:
228  state_ = evalSEQOR(solver);
229  break;
230  case SEQAND:
231  state_ = evalSEQAND(solver);
232  break;
233  }
234  return state_;
235 }
236 
237 
238 template <class ScalarType, class MV, class OP>
240  ind_.resize(0);
241  state_ = Undefined;
242  typedef typename STPArray::iterator iter;
243  for (iter i=tests_.begin(); i != tests_.end(); i++) {
244  (*i)->reset();
245  }
246 }
247 
248 template <class ScalarType, class MV, class OP>
250  ind_.resize(0);
251  state_ = Undefined;
252  typedef typename STPArray::iterator iter;
253  for (iter i=tests_.begin(); i != tests_.end(); i++) {
254  (*i)->clearStatus();
255  }
256 }
257 
258 template <class ScalarType, class MV, class OP>
259 std::ostream& StatusTestCombo<ScalarType,MV,OP>::print(std::ostream& os, int indent) const {
260  std::string ind(indent,' ');
261  os << ind << "- StatusTestCombo: ";
262  switch (state_) {
263  case Passed:
264  os << "Passed" << std::endl;
265  break;
266  case Failed:
267  os << "Failed" << std::endl;
268  break;
269  case Undefined:
270  os << "Undefined" << std::endl;
271  break;
272  }
273  // print children, with extra indention
274  typedef typename STPArray::const_iterator const_iter;
275  for (const_iter i=tests_.begin(); i != tests_.end(); i++) {
276  (*i)->print(os,indent+2);
277  }
278  return os;
279 }
280 
281 template <class ScalarType, class MV, class OP>
283  state_ = Failed;
284  typedef typename STPArray::iterator iter;
285  for (iter i=tests_.begin(); i != tests_.end(); i++) {
286  TestStatus r = (*i)->checkStatus(solver);
287  if (i == tests_.begin()) {
288  ind_ = (*i)->whichVecs();
289  // sort ind_ for use below
290  std::sort(ind_.begin(),ind_.end());
291  }
292  else {
293  // to use set_union, ind_ must have room for the result, which will have size() <= end.size() + iwv.size()
294  // also, ind and iwv must be in ascending order; only ind_ is
295  // lastly, the return from set_union points to the last element in the union, which tells us how big the union is
296  std::vector<int> iwv = (*i)->whichVecs();
297  std::sort(iwv.begin(),iwv.end());
298  std::vector<int> tmp(ind_.size() + iwv.size());
299  std::vector<int>::iterator end;
300  end = std::set_union(ind_.begin(),ind_.end(),iwv.begin(),iwv.end(),tmp.begin());
301  tmp.resize(end - tmp.begin());
302  // ind_ will be sorted coming from set_union
303  ind_ = tmp;
304  }
305  if (r == Passed) {
306  state_ = Passed;
307  }
308  else {
309  TEUCHOS_TEST_FOR_EXCEPTION(r != Failed,StatusTestError,
310  "Anasazi::StatusTestCombo::evalOR(): child test gave invalid return");
311  }
312  }
313  return state_;
314 }
315 
316 template <class ScalarType, class MV, class OP>
317 TestStatus StatusTestCombo<ScalarType,MV,OP>::evalSEQOR( Eigensolver<ScalarType,MV,OP>* solver ) {
318  state_ = Failed;
319  typedef typename STPArray::iterator iter;
320  for (iter i=tests_.begin(); i != tests_.end(); i++) {
321  TestStatus r = (*i)->checkStatus(solver);
322  if (i == tests_.begin()) {
323  ind_ = (*i)->whichVecs();
324  // sort ind_ for use below
325  std::sort(ind_.begin(),ind_.end());
326  }
327  else {
328  // to use set_union, ind_ must have room for the result, which will have size() <= end.size() + iwv.size()
329  // also, ind and iwv must be in ascending order; only ind_ is
330  // lastly, the return from set_union points to the last element in the union, which tells us how big the union is
331  std::vector<int> iwv = (*i)->whichVecs();
332  std::sort(iwv.begin(),iwv.end());
333  std::vector<int> tmp(ind_.size() + iwv.size());
334  std::vector<int>::iterator end;
335  end = std::set_union(ind_.begin(),ind_.end(),iwv.begin(),iwv.end(),tmp.begin());
336  tmp.resize(end - tmp.begin());
337  // ind_ will be sorted coming from set_union
338  ind_ = tmp;
339  }
340  if (r == Passed) {
341  state_ = Passed;
342  break;
343  }
344  else {
345  TEUCHOS_TEST_FOR_EXCEPTION(r != Failed,StatusTestError,
346  "Anasazi::StatusTestCombo::evalSEQOR(): child test gave invalid return");
347  }
348  }
349  return state_;
350 }
351 
352 template <class ScalarType, class MV, class OP>
353 TestStatus StatusTestCombo<ScalarType,MV,OP>::evalAND( Eigensolver<ScalarType,MV,OP>* solver ) {
354  state_ = Passed;
355  typedef typename STPArray::iterator iter;
356  for (iter i=tests_.begin(); i != tests_.end(); i++) {
357  TestStatus r = (*i)->checkStatus(solver);
358  if (i == tests_.begin()) {
359  ind_ = (*i)->whichVecs();
360  // sort ind_ for use below
361  std::sort(ind_.begin(),ind_.end());
362  }
363  else {
364  // to use set_intersection, ind_ must have room for the result, which will have size() <= end.size() + iwv.size()
365  // also, ind and iwv must be in ascending order; only ind_ is
366  // lastly, the return from set_intersection points to the last element in the intersection, which tells us how big the intersection is
367  std::vector<int> iwv = (*i)->whichVecs();
368  std::sort(iwv.begin(),iwv.end());
369  std::vector<int> tmp(ind_.size() + iwv.size());
370  std::vector<int>::iterator end;
371  end = std::set_intersection(ind_.begin(),ind_.end(),iwv.begin(),iwv.end(),tmp.begin());
372  tmp.resize(end - tmp.begin());
373  // ind_ will be sorted coming from set_intersection
374  ind_ = tmp;
375  }
376  if (r == Failed) {
377  state_ = Failed;
378  }
379  else {
380  TEUCHOS_TEST_FOR_EXCEPTION(r != Passed,StatusTestError,
381  "Anasazi::StatusTestCombo::evalAND(): child test gave invalid return");
382  }
383  }
384  return state_;
385 }
386 
387 template <class ScalarType, class MV, class OP>
388 TestStatus StatusTestCombo<ScalarType,MV,OP>::evalSEQAND( Eigensolver<ScalarType,MV,OP>* solver ) {
389  state_ = Passed;
390  typedef typename STPArray::iterator iter;
391  for (iter i=tests_.begin(); i != tests_.end(); i++) {
392  TestStatus r = (*i)->checkStatus(solver);
393  if (i == tests_.begin()) {
394  ind_ = (*i)->whichVecs();
395  // sort ind_ for use below
396  std::sort(ind_.begin(),ind_.end());
397  }
398  else {
399  // to use set_intersection, ind_ must have room for the result, which will have size() <= end.size() + iwv.size()
400  // also, ind and iwv must be in ascending order; only ind_ is
401  // lastly, the return from set_intersection points to the last element in the intersection, which tells us how big the intersection is
402  std::vector<int> iwv = (*i)->whichVecs();
403  std::sort(iwv.begin(),iwv.end());
404  std::vector<int> tmp(ind_.size() + iwv.size());
405  std::vector<int>::iterator end;
406  end = std::set_intersection(ind_.begin(),ind_.end(),iwv.begin(),iwv.end(),tmp.begin());
407  tmp.resize(end - tmp.begin());
408  // ind_ will be sorted coming from set_intersection
409  ind_ = tmp;
410  }
411  if (r == Failed) {
412  state_ = Failed;
413  break;
414  }
415  else {
416  TEUCHOS_TEST_FOR_EXCEPTION(r != Passed,StatusTestError,
417  "Anasazi::StatusTestCombo::evalAND(): child test gave invalid return");
418  }
419  }
420  return state_;
421 }
422 
423 
424 
425 } // end of Anasazi namespace
426 
427 #endif /* ANASAZI_STATUS_TEST_COMBO_HPP */
void clearStatus()
Clears the results of the last status test.
void setTests(Teuchos::Array< Teuchos::RCP< StatusTest< ScalarType, MV, OP > > > tests)
Set the tests This also resets the test status to Undefined.
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
StatusTestCombo(ComboType type, Teuchos::Array< Teuchos::RCP< StatusTest< ScalarType, MV, OP > > > tests)
Constructor specifying the StatusTestCombo::ComboType and the tests.
Status test for forming logical combinations of other status tests.
virtual ~StatusTestCombo()
Destructor.
std::vector< int > whichVecs() const
Get the indices for the vectors that passed the test.
void reset()
Informs the status test that it should reset its internal configuration to the uninitialized state...
TestStatus
Enumerated type used to pass back information from a StatusTest.
std::ostream & print(std::ostream &os, int indent=0) const
Output formatted description of stopping test to output stream.
TestStatus checkStatus(Eigensolver< ScalarType, MV, OP > *solver)
Teuchos::Array< Teuchos::RCP< StatusTest< ScalarType, MV, OP > > > getTests() const
Get the tests.
void removeTest(const Teuchos::RCP< StatusTest< ScalarType, MV, OP > > &test)
Removes a test from the combination, if it exists in the tester.
std::vector< Teuchos::RCP< StatusTest< ScalarType, MV, OP > > >::const_iterator const_iterator
void push_back(const value_type &x)
int howMany() const
Get the number of vectors that passed the test.
ComboType getComboType() const
Get the maximum number of iterations.
void setComboType(ComboType type)
Set the maximum number of iterations. This also resets the test status to Undefined.
Types and exceptions used within Anasazi solvers and interfaces.
ComboType
Enumerated type to list the types of StatusTestCombo combo types.
Common interface of stopping criteria for Anasazi&#39;s solvers.
std::vector< Teuchos::RCP< StatusTest< ScalarType, MV, OP > > >::iterator iterator
void addTest(Teuchos::RCP< StatusTest< ScalarType, MV, OP > > test)
Add a test to the combination.
The Eigensolver is a templated virtual base class that defines the basic interface that any eigensolv...
StatusTestCombo()
Default constructor has no tests and initializes to StatusTestCombo::ComboType StatusTestCombo::OR.
Declaration and definition of Anasazi::StatusTest.
TestStatus getStatus() const
Return the result of the most recent checkStatus call.