Anasazi Version of the Day
Loading...
Searching...
No Matches
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"
23namespace Anasazi {
24
25
42template <class ScalarType, class MV, class OP>
43class StatusTestCombo : public StatusTest<ScalarType,MV,OP> {
44
45 private:
46 typedef Teuchos::Array< Teuchos::RCP< StatusTest<ScalarType,MV,OP> > > STPArray;
47
48 public:
49
58
59
60#ifndef DOXYGEN_SHOULD_SKIP_THIS
61
62 typedef Teuchos::Array< Teuchos::RCP< StatusTest<ScalarType,MV,OP> > > t_arr;
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
75
78 StatusTestCombo(ComboType type, Teuchos::Array< Teuchos::RCP< StatusTest<ScalarType,MV,OP> > > tests) :
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
130 type_ = type;
131 state_ = Undefined;
132 }
133
135 ComboType getComboType() const {return type_;}
136
140 void setTests(Teuchos::Array<Teuchos::RCP<StatusTest<ScalarType,MV,OP> > > tests) {
141 tests_ = tests;
142 state_ = Undefined;
143 }
144
146 Teuchos::Array<Teuchos::RCP<StatusTest<ScalarType,MV,OP> > > getTests() const {return tests_;}
147
152 void addTest(Teuchos::RCP<StatusTest<ScalarType,MV,OP> > test) {
153 tests_.push_back(test);
154 state_ = Undefined;
155 }
156
161 void removeTest(const Teuchos::RCP<StatusTest<ScalarType,MV,OP> > &test);
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
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
205template <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
217template <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
238template <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
248template <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
258template <class ScalarType, class MV, class OP>
259std::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
281template <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
316template <class ScalarType, class MV, class OP>
317TestStatus 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
352template <class ScalarType, class MV, class OP>
353TestStatus 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
387template <class ScalarType, class MV, class OP>
388TestStatus 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 */
Declaration and definition of Anasazi::StatusTest.
Types and exceptions used within Anasazi solvers and interfaces.
The Eigensolver is a templated virtual base class that defines the basic interface that any eigensolv...
Status test for forming logical combinations of other status tests.
void setComboType(ComboType type)
Set the maximum number of iterations. This also resets the test status to Undefined.
std::vector< int > whichVecs() const
Get the indices for the vectors that passed the test.
void addTest(Teuchos::RCP< StatusTest< ScalarType, MV, OP > > test)
Add a test to the combination.
Teuchos::Array< Teuchos::RCP< StatusTest< ScalarType, MV, OP > > > getTests() const
Get the tests.
void setTests(Teuchos::Array< Teuchos::RCP< StatusTest< ScalarType, MV, OP > > > tests)
Set the tests This also resets the test status to Undefined.
ComboType
Enumerated type to list the types of StatusTestCombo combo types.
TestStatus checkStatus(Eigensolver< ScalarType, MV, OP > *solver)
int howMany() const
Get the number of vectors that passed the test.
virtual ~StatusTestCombo()
Destructor.
void removeTest(const Teuchos::RCP< StatusTest< ScalarType, MV, OP > > &test)
Removes a test from the combination, if it exists in the tester.
ComboType getComboType() const
Get the maximum number of iterations.
void reset()
Informs the status test that it should reset its internal configuration to the uninitialized state.
void clearStatus()
Clears the results of the last status test.
StatusTestCombo(ComboType type, Teuchos::Array< Teuchos::RCP< StatusTest< ScalarType, MV, OP > > > tests)
Constructor specifying the StatusTestCombo::ComboType and the tests.
StatusTestCombo()
Default constructor has no tests and initializes to StatusTestCombo::ComboType StatusTestCombo::OR.
std::ostream & print(std::ostream &os, int indent=0) const
Output formatted description of stopping test to output stream.
TestStatus getStatus() const
Return the result of the most recent checkStatus call.
Common interface of stopping criteria for Anasazi's solvers.
Namespace Anasazi contains the classes, structs, enums and utilities used by the Anasazi package.
TestStatus
Enumerated type used to pass back information from a StatusTest.