Anasazi  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
AnasaziStatusTestWithOrdering.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_ORDEREDRESNORM_HPP
12 #define ANASAZI_STATUS_TEST_ORDEREDRESNORM_HPP
13 
21 #include "AnasaziStatusTest.hpp"
22 #include "Teuchos_ScalarTraits.hpp"
23 
47 namespace Anasazi {
48 
49 
50 template <class ScalarType, class MV, class OP>
51 class StatusTestWithOrdering : public StatusTest<ScalarType,MV,OP> {
52 
53  private:
54  typedef typename Teuchos::ScalarTraits<ScalarType>::magnitudeType MagnitudeType;
56 
57  public:
58 
60 
61 
64 
68 
70 
71 
76 
78  TestStatus getStatus() const { return state_; }
79 
81 
86  std::vector<int> whichVecs() const {
87  return ind_;
88  }
89 
91  int howMany() const {
92  return ind_.size();
93  }
94 
96 
98 
99 
105  void setQuorum(int quorum) {
106  state_ = Undefined;
107  quorum_ = quorum;
108  }
109 
112  int getQuorum() const {
113  return quorum_;
114  }
115 
117 
119 
120 
126  void reset() {
127  ind_.resize(0);
128  state_ = Undefined;
129  test_->reset();
130  }
131 
133 
138  void clearStatus() {
139  ind_.resize(0);
140  state_ = Undefined;
141  test_->clearStatus();
142  }
143 
148  void setAuxVals(const std::vector<typename Teuchos::ScalarTraits<ScalarType>::magnitudeType> &vals) {
149  rvals_ = vals;
150  ivals_.resize(rvals_.size(),MT::zero());
151  state_ = Undefined;
152  }
153 
158  void setAuxVals(const std::vector<typename Teuchos::ScalarTraits<ScalarType>::magnitudeType> &rvals, const std::vector<typename Teuchos::ScalarTraits<ScalarType>::magnitudeType> &ivals) {
159  rvals_ = rvals;
160  ivals_ = ivals;
161  state_ = Undefined;
162  }
163 
168  void getAuxVals(std::vector<typename Teuchos::ScalarTraits<ScalarType>::magnitudeType> &rvals, std::vector<typename Teuchos::ScalarTraits<ScalarType>::magnitudeType> &ivals) const {
169  rvals = rvals_;
170  ivals = ivals_;
171  }
172 
174 
176 
177 
179  std::ostream& print(std::ostream& os, int indent = 0) const;
180 
182  private:
183  TestStatus state_;
184  std::vector<int> ind_;
185  int quorum_;
186  std::vector<MagnitudeType> rvals_, ivals_;
189 };
190 
191 
192 template <class ScalarType, class MV, class OP>
194  : state_(Undefined), ind_(0), quorum_(quorum), rvals_(0), ivals_(0), sorter_(sorter), test_(test)
195 {
196  TEUCHOS_TEST_FOR_EXCEPTION(sorter_ == Teuchos::null, StatusTestError, "StatusTestWithOrdering::constructor() was passed null pointer for constituent SortManager.");
197  TEUCHOS_TEST_FOR_EXCEPTION(test_ == Teuchos::null, StatusTestError, "StatusTestWithOrdering::constructor() was passed null pointer for constituent StatusTest.");
198 }
199 
200 template <class ScalarType, class MV, class OP>
202 
203  // Algorithm
204  // we PASS iff the "most signficant" values of "all values" PASS
205  // "most significant" is measured by sorter
206  // auxilliary values are automatically PASSED
207  // constituent status test determines who else passes
208  // "all values" mean {auxilliary values} UNION {solver's ritz values}
209  //
210  // test_->checkStatus() calls the constituent status test
211  // cwhch = test_->whichVecs() gets the passing indices from the constituent test
212  // solval = solver->getRitzValues() gets the solver's ritz values
213  // allval = {solval auxval} contains all values
214  // sorter_->sort(allval,perm) sort all values (we just need the perm vector)
215  //
216  // allpass = {cwhch numsolval+1 ... numsolval+numAux}
217  // mostsig = {perm[1] ... perm[quorum]}
218  // whichVecs = mostsig INTERSECT allpass
219  // if size(whichVecs) >= quorum,
220  // PASS
221  // else
222  // FAIL
223  //
224  // finish: this needs to be better tested and revisited for efficiency.
225 
226  // call the constituent solver manager
227  test_->checkStatus(solver);
228  std::vector<int> cwhch( test_->whichVecs() );
229 
230  // get the ritzvalues from solver
231  std::vector<Value<ScalarType> > solval = solver->getRitzValues();
232  int numsolval = solval.size();
233  int numauxval = rvals_.size();
234  int numallval = numsolval + numauxval;
235 
236  if (numallval == 0) {
237  ind_.resize(0);
238  return Failed;
239  }
240 
241  // make space for all values, real and imaginary parts
242  std::vector<MagnitudeType> allvalr(numallval), allvali(numallval);
243  // separate the real and imaginary parts of solver ritz values
244  for (int i=0; i<numsolval; ++i) {
245  allvalr[i] = solval[i].realpart;
246  allvali[i] = solval[i].imagpart;
247  }
248  // put the auxiliary values at the end of the solver ritz values
249  std::copy(rvals_.begin(),rvals_.end(),allvalr.begin()+numsolval);
250  std::copy(ivals_.begin(),ivals_.end(),allvali.begin()+numsolval);
251 
252  // sort all values
253  std::vector<int> perm(numallval);
254  sorter_->sort(allvalr,allvali,Teuchos::rcpFromRef(perm),numallval);
255 
256  // make the set of passing values: allpass = {cwhch -1 ... -numauxval}
257  std::vector<int> allpass(cwhch.size() + numauxval);
258  std::copy(cwhch.begin(),cwhch.end(),allpass.begin());
259  for (int i=0; i<numauxval; i++) {
260  allpass[cwhch.size()+i] = -(i+1);
261  }
262 
263  // make list, with length quorum, of most significant values, if there are that many
264  int numsig = quorum_ < numallval ? quorum_ : numallval;
265  // int numsig = cwhch.size() + numauxval;
266  std::vector<int> mostsig(numsig);
267  for (int i=0; i<numsig; ++i) {
268  mostsig[i] = perm[i];
269  // if perm[i] >= numsolval, then it corresponds to the perm[i]-numsolval aux val
270  // the first aux val gets index -numauxval, the second -numauxval+1, and so forth
271  if (mostsig[i] >= numsolval) {
272  mostsig[i] = mostsig[i]-numsolval-numauxval;
273  }
274  }
275 
276  // who passed?
277  // to use set_intersection, ind_ must have room for the result, which will have size() <= min( allpass.size(), mostsig.size() )
278  // also, allpass and mostsig must be in ascending order; neither are
279  // lastly, the return from set_intersection points to the last element in the intersection, which tells us how big the intersection is
280  ind_.resize(numsig);
281  std::vector<int>::iterator end;
282  std::sort(mostsig.begin(),mostsig.end());
283  std::sort(allpass.begin(),allpass.end());
284  end = std::set_intersection(mostsig.begin(),mostsig.end(),allpass.begin(),allpass.end(),ind_.begin());
285  ind_.resize(end - ind_.begin());
286 
287  // did we pass, overall
288  if (ind_.size() >= (unsigned int)quorum_) {
289  state_ = Passed;
290  }
291  else {
292  state_ = Failed;
293  }
294  return state_;
295 }
296 
297 
298 template <class ScalarType, class MV, class OP>
299 std::ostream& StatusTestWithOrdering<ScalarType,MV,OP>::print(std::ostream& os, int indent) const {
300  // build indent string
301  std::string ind(indent,' ');
302  // print header
303  os << ind << "- StatusTestWithOrdering: ";
304  switch (state_) {
305  case Passed:
306  os << "Passed" << std::endl;
307  break;
308  case Failed:
309  os << "Failed" << std::endl;
310  break;
311  case Undefined:
312  os << "Undefined" << std::endl;
313  break;
314  }
315  // print parameters, namely, quorum
316  os << ind << " Quorum: " << quorum_ << std::endl;
317  // print any auxilliary values
318  os << ind << " Auxiliary values: ";
319  if (rvals_.size() > 0) {
320  for (unsigned int i=0; i<rvals_.size(); i++) {
321  os << "(" << rvals_[i] << ", " << ivals_[i] << ") ";
322  }
323  os << std::endl;
324  }
325  else {
326  os << "[empty]" << std::endl;
327  }
328  // print which vectors have passed
329  if (state_ != Undefined) {
330  os << ind << " Which vectors: ";
331  if (ind_.size() > 0) {
332  for (unsigned int i=0; i<ind_.size(); i++) os << ind_[i] << " ";
333  os << std::endl;
334  }
335  else {
336  os << "[empty]" << std::endl;
337  }
338  }
339  // print constituent test
340  test_->print(os,indent+2);
341  return os;
342 }
343 
344 
345 } // end of Anasazi namespace
346 
347 #endif /* ANASAZI_STATUS_TEST_ORDEREDRESNORM_HPP */
virtual std::vector< Value< ScalarType > > getRitzValues()=0
Get the Ritz values from the previous iteration.
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
Exception thrown to signal error in a status test during Anasazi::StatusTest::checkStatus().
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)
void getAuxVals(std::vector< typename Teuchos::ScalarTraits< ScalarType >::magnitudeType > &rvals, std::vector< typename Teuchos::ScalarTraits< ScalarType >::magnitudeType > &ivals) const
Get the auxiliary eigenvalues.
std::vector< int > whichVecs() const
Get the indices for the vectors that passed the test.
StatusTestWithOrdering(Teuchos::RCP< StatusTest< ScalarType, MV, OP > > test, Teuchos::RCP< SortManager< typename Teuchos::ScalarTraits< ScalarType >::magnitudeType > > sorter, int quorum=-1)
Constructor.
void setAuxVals(const std::vector< typename Teuchos::ScalarTraits< ScalarType >::magnitudeType > &rvals, const std::vector< typename Teuchos::ScalarTraits< ScalarType >::magnitudeType > &ivals)
Set the auxiliary eigenvalues.
A status test for testing the norm of the eigenvectors residuals along with a set of auxiliary eigenv...
TestStatus getStatus() const
Return the result of the most recent checkStatus call, or undefined if it has not been run...
void reset()
Informs the status test that it should reset its internal configuration to the uninitialized state...
void setAuxVals(const std::vector< typename Teuchos::ScalarTraits< ScalarType >::magnitudeType > &vals)
Set the auxiliary eigenvalues.
Anasazi&#39;s templated pure virtual class for managing the sorting of approximate eigenvalues computed b...
void clearStatus()
Clears the results of the last status test.
Common interface of stopping criteria for Anasazi&#39;s solvers.
int howMany() const
Get the number of vectors that passed the test.
The Eigensolver is a templated virtual base class that defines the basic interface that any eigensolv...
Declaration and definition of Anasazi::StatusTest.