IterationPack_AlgorithmState.cpp

Go to the documentation of this file.
00001 // @HEADER
00002 // ***********************************************************************
00003 // 
00004 // Moocho: Multi-functional Object-Oriented arCHitecture for Optimization
00005 //                  Copyright (2003) Sandia Corporation
00006 // 
00007 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
00008 // license for use of this work by or on behalf of the U.S. Government.
00009 // 
00010 // This library is free software; you can redistribute it and/or modify
00011 // it under the terms of the GNU Lesser General Public License as
00012 // published by the Free Software Foundation; either version 2.1 of the
00013 // License, or (at your option) any later version.
00014 //  
00015 // This library is distributed in the hope that it will be useful, but
00016 // WITHOUT ANY WARRANTY; without even the implied warranty of
00017 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018 // Lesser General Public License for more details.
00019 //  
00020 // You should have received a copy of the GNU Lesser General Public
00021 // License along with this library; if not, write to the Free Software
00022 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
00023 // USA
00024 // Questions? Contact Roscoe A. Bartlett (rabartl@sandia.gov) 
00025 // 
00026 // ***********************************************************************
00027 // @HEADER
00028 
00029 #include <ostream>
00030 #include <iomanip>
00031 #include <typeinfo>
00032 
00033 #include "IterationPack_AlgorithmState.hpp"
00034 #include "Teuchos_TestForException.hpp"
00035 
00036 namespace {
00037 namespace {
00038 template< class T >
00039 inline
00040 T my_max( const T& v1, const T& v2 ) { return v1 > v2 ? v1 : v2; }
00041 } // end namespace
00042 inline void output_spaces(std::ostream& out, int spaces)
00043 { for(int i = 0; i < spaces; ++i) out << ' '; }
00044 }
00045 
00046 namespace IterationPack {
00047 
00048 AlgorithmState::iq_id_type AlgorithmState::set_iter_quant(
00049   const std::string& iq_name, const IQ_ptr& iq)
00050 {
00051   TEST_FOR_EXCEPTION(
00052     iq.get() == NULL, std::invalid_argument
00053     ,"AlgorithmState::set_iter_quant(...) : The iteration quantity witht the name = \'" << iq_name
00054     << "\' being inserted has iq.get() == NULL!" );
00055   iq_id_type new_id = iq_.size();
00056   std::pair<iq_name_to_id_t::iterator,bool>
00057     r = iq_name_to_id_.insert(iq_name_to_id_t::value_type(iq_name,new_id));
00058   TEST_FOR_EXCEPTION(
00059     !r.second // an insert did not take place, key = iq_name already existed.
00060     ,AlreadyExists
00061     ,"AlgorithmState::set_iter_quant(...) : An iteration quantity with the name \""
00062     << iq_name << "\" already exists with the iq_id = " << (*r.first).second );
00063   iq_.push_back(iq);
00064   return new_id;
00065 }
00066 
00067 void AlgorithmState::erase_iter_quant(const std::string& iq_name) {
00068   iq_name_to_id_t::iterator itr = find_and_assert(iq_name);
00069   const iq_id_type iq_id = (*itr).second;
00070   iq_[iq_id] = Teuchos::null;  // set the pointer to null
00071   iq_name_to_id_.erase( itr );
00072 }
00073 
00074 IterQuantity& AlgorithmState::iter_quant(iq_id_type iq_id) {
00075   bool exists = true;
00076   try {
00077     IQ_ptr &_iq = iq_.at(iq_id);
00078     if( _iq.get() )
00079       return *_iq;
00080     else
00081       exists = false;
00082   }
00083   catch(const std::out_of_range& excpt) { // Thrown by MS VC++ 6.0
00084     exists = false;
00085   }
00086   catch(const std::range_error& excpt) {  // Thrown by libstdc++ v3 in g++ 2.95.2
00087     exists = false;
00088   }
00089   TEST_FOR_EXCEPTION(
00090     !exists, DoesNotExist
00091     ,"AlgorithmState::iter_quant(iq_id) : Error, the iteration quantity iq_id = "
00092     << iq_id << " does not exist.  "
00093     << ( iq_id < iq_.size()
00094        ? "This iteration quantity was set and then erased."
00095        : "This iteration quantity was never set by the client." ) );
00096   return *iq_.at(0);  // Will never be executed.
00097 }
00098 
00099 const IterQuantity& AlgorithmState::iter_quant(iq_id_type iq_id) const {
00100   return const_cast<AlgorithmState*>(this)->iter_quant(iq_id);
00101 }
00102 
00103 void AlgorithmState::next_iteration(bool incr_k) {
00104   if(incr_k) this->incr_k();
00105   for(iq_t::iterator itr = iq_.begin(); itr != iq_.end(); ++itr)
00106     if(itr->get()) (*itr)->next_iteration();
00107 }
00108 
00109 void AlgorithmState::dump_iter_quant(std::ostream& out) const {
00110   using std::setw;
00111   using std::endl;
00112 
00113   // Find the maximum length of an iteration quantity name
00114   int name_w_max = 0;
00115   {for( iq_name_to_id_t::const_iterator itr = iq_name_to_id_.begin();
00116       itr !=  iq_name_to_id_.end(); ++itr )
00117   {
00118     name_w_max = my_max( (int)name_w_max, (int)(*itr).first.length() );
00119   }}  
00120 
00121   const int name_w = name_w_max + 4, id_w = 6;
00122   const char gap[] = "    ";
00123 
00124   out   << "\n\n"
00125       << std::left    << setw(name_w) << "iq_name"
00126       << std::right   << setw(id_w)   << "iq_id"
00127       << gap          << std::left    << "concrete type of iq / concrete type of object\n";
00128 
00129   out   << std::left    << setw(name_w) << "-----"
00130       << std::right   << setw(id_w)   << "------"
00131       << gap          << std::left    << "---------------------------------------------\n";
00132 
00133   {for( iq_name_to_id_t::const_iterator itr = iq_name_to_id_.begin();
00134       itr !=  iq_name_to_id_.end(); ++itr )
00135   {
00136     out << std::left    << setw(name_w) << (*itr).first
00137       << std::right   << setw(id_w)   << (*itr).second
00138       << gap          << std::left    << typeName(*iq_[(*itr).second]) << endl
00139       << std::left    << setw(name_w) << ""
00140       << std::right   << setw(id_w)   << ""
00141       << gap          << std::left;
00142     iq_[(*itr).second]->print_concrete_type(out);
00143     out << endl;
00144   }}
00145 }
00146 
00147 // private
00148 
00149 AlgorithmState::iq_name_to_id_t::iterator AlgorithmState::find_and_assert(
00150   const std::string& iq_name)
00151 {
00152   iq_name_to_id_t::iterator itr = iq_name_to_id_.find(iq_name);
00153   if(itr == iq_name_to_id_.end())
00154     TEST_FOR_EXCEPTION(
00155       true, DoesNotExist
00156       ,"AlgorithmState::find_and_assert(iq_name) : The iteration "
00157       "quantity with the name \"" << iq_name << "\" does not exist" );
00158   return itr;
00159 }
00160 
00161 AlgorithmState::iq_name_to_id_t::const_iterator AlgorithmState::find_and_assert(
00162   const std::string& iq_name) const
00163 {
00164   iq_name_to_id_t::const_iterator itr = iq_name_to_id_.find(iq_name);
00165   if(itr == iq_name_to_id_.end())
00166     TEST_FOR_EXCEPTION(
00167       true, DoesNotExist
00168       ,"AlgorithmState::find_and_assert(iq_name) : The iteration "
00169       "quantity with the name \"" << iq_name << "\" does not exist" );
00170   return itr;
00171 }
00172 
00173 } // end namespace IterationPack

Generated on Tue Oct 20 12:51:46 2009 for MOOCHO (Single Doxygen Collection) by doxygen 1.4.7