MOOCHO (Single Doxygen Collection) Version of the Day
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 // Redistribution and use in source and binary forms, with or without
00011 // modification, are permitted provided that the following conditions are
00012 // met:
00013 //
00014 // 1. Redistributions of source code must retain the above copyright
00015 // notice, this list of conditions and the following disclaimer.
00016 //
00017 // 2. Redistributions in binary form must reproduce the above copyright
00018 // notice, this list of conditions and the following disclaimer in the
00019 // documentation and/or other materials provided with the distribution.
00020 //
00021 // 3. Neither the name of the Corporation nor the names of the
00022 // contributors may be used to endorse or promote products derived from
00023 // this software without specific prior written permission.
00024 //
00025 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
00026 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00027 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
00028 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
00029 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00030 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00031 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00032 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00033 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00034 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00035 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00036 //
00037 // Questions? Contact Roscoe A. Bartlett (rabartl@sandia.gov) 
00038 // 
00039 // ***********************************************************************
00040 // @HEADER
00041 
00042 #include <ostream>
00043 #include <iomanip>
00044 #include <typeinfo>
00045 
00046 #include "IterationPack_AlgorithmState.hpp"
00047 #include "Teuchos_Assert.hpp"
00048 
00049 namespace {
00050 namespace {
00051 template< class T >
00052 inline
00053 T my_max( const T& v1, const T& v2 ) { return v1 > v2 ? v1 : v2; }
00054 } // end namespace
00055 inline void output_spaces(std::ostream& out, int spaces)
00056 { for(int i = 0; i < spaces; ++i) out << ' '; }
00057 }
00058 
00059 namespace IterationPack {
00060 
00061 AlgorithmState::iq_id_type AlgorithmState::set_iter_quant(
00062   const std::string& iq_name, const IQ_ptr& iq)
00063 {
00064   TEUCHOS_TEST_FOR_EXCEPTION(
00065     iq.get() == NULL, std::invalid_argument
00066     ,"AlgorithmState::set_iter_quant(...) : The iteration quantity witht the name = \'" << iq_name
00067     << "\' being inserted has iq.get() == NULL!" );
00068   iq_id_type new_id = iq_.size();
00069   std::pair<iq_name_to_id_t::iterator,bool>
00070     r = iq_name_to_id_.insert(iq_name_to_id_t::value_type(iq_name,new_id));
00071   TEUCHOS_TEST_FOR_EXCEPTION(
00072     !r.second // an insert did not take place, key = iq_name already existed.
00073     ,AlreadyExists
00074     ,"AlgorithmState::set_iter_quant(...) : An iteration quantity with the name \""
00075     << iq_name << "\" already exists with the iq_id = " << (*r.first).second );
00076   iq_.push_back(iq);
00077   return new_id;
00078 }
00079 
00080 void AlgorithmState::erase_iter_quant(const std::string& iq_name) {
00081   iq_name_to_id_t::iterator itr = find_and_assert(iq_name);
00082   const iq_id_type iq_id = (*itr).second;
00083   iq_[iq_id] = Teuchos::null;  // set the pointer to null
00084   iq_name_to_id_.erase( itr );
00085 }
00086 
00087 IterQuantity& AlgorithmState::iter_quant(iq_id_type iq_id) {
00088   bool exists = true;
00089   try {
00090     IQ_ptr &_iq = iq_.at(iq_id);
00091     if( _iq.get() )
00092       return *_iq;
00093     else
00094       exists = false;
00095   }
00096   catch(const std::out_of_range& excpt) { // Thrown by MS VC++ 6.0
00097     exists = false;
00098   }
00099   catch(const std::range_error& excpt) {  // Thrown by libstdc++ v3 in g++ 2.95.2
00100     exists = false;
00101   }
00102   TEUCHOS_TEST_FOR_EXCEPTION(
00103     !exists, DoesNotExist
00104     ,"AlgorithmState::iter_quant(iq_id) : Error, the iteration quantity iq_id = "
00105     << iq_id << " does not exist.  "
00106     << ( iq_id < iq_.size()
00107        ? "This iteration quantity was set and then erased."
00108        : "This iteration quantity was never set by the client." ) );
00109   return *iq_.at(0);  // Will never be executed.
00110 }
00111 
00112 const IterQuantity& AlgorithmState::iter_quant(iq_id_type iq_id) const {
00113   return const_cast<AlgorithmState*>(this)->iter_quant(iq_id);
00114 }
00115 
00116 void AlgorithmState::next_iteration(bool incr_k) {
00117   if(incr_k) this->incr_k();
00118   for(iq_t::iterator itr = iq_.begin(); itr != iq_.end(); ++itr)
00119     if(itr->get()) (*itr)->next_iteration();
00120 }
00121 
00122 void AlgorithmState::dump_iter_quant(std::ostream& out) const {
00123   using std::setw;
00124   using std::endl;
00125 
00126   // Find the maximum length of an iteration quantity name
00127   int name_w_max = 0;
00128   {for( iq_name_to_id_t::const_iterator itr = iq_name_to_id_.begin();
00129       itr !=  iq_name_to_id_.end(); ++itr )
00130   {
00131     name_w_max = my_max( (int)name_w_max, (int)(*itr).first.length() );
00132   }}  
00133 
00134   const int name_w = name_w_max + 4, id_w = 6;
00135   const char gap[] = "    ";
00136 
00137   out   << "\n\n"
00138       << std::left    << setw(name_w) << "iq_name"
00139       << std::right   << setw(id_w)   << "iq_id"
00140       << gap          << std::left    << "concrete type of iq / concrete type of object\n";
00141 
00142   out   << std::left    << setw(name_w) << "-----"
00143       << std::right   << setw(id_w)   << "------"
00144       << gap          << std::left    << "---------------------------------------------\n";
00145 
00146   {for( iq_name_to_id_t::const_iterator itr = iq_name_to_id_.begin();
00147       itr !=  iq_name_to_id_.end(); ++itr )
00148   {
00149     out << std::left    << setw(name_w) << (*itr).first
00150       << std::right   << setw(id_w)   << (*itr).second
00151       << gap          << std::left    << typeName(*iq_[(*itr).second]) << endl
00152       << std::left    << setw(name_w) << ""
00153       << std::right   << setw(id_w)   << ""
00154       << gap          << std::left;
00155     iq_[(*itr).second]->print_concrete_type(out);
00156     out << endl;
00157   }}
00158 }
00159 
00160 // private
00161 
00162 AlgorithmState::iq_name_to_id_t::iterator AlgorithmState::find_and_assert(
00163   const std::string& iq_name)
00164 {
00165   iq_name_to_id_t::iterator itr = iq_name_to_id_.find(iq_name);
00166   if(itr == iq_name_to_id_.end())
00167     TEUCHOS_TEST_FOR_EXCEPTION(
00168       true, DoesNotExist
00169       ,"AlgorithmState::find_and_assert(iq_name) : The iteration "
00170       "quantity with the name \"" << iq_name << "\" does not exist" );
00171   return itr;
00172 }
00173 
00174 AlgorithmState::iq_name_to_id_t::const_iterator AlgorithmState::find_and_assert(
00175   const std::string& iq_name) const
00176 {
00177   iq_name_to_id_t::const_iterator itr = iq_name_to_id_.find(iq_name);
00178   if(itr == iq_name_to_id_.end())
00179     TEUCHOS_TEST_FOR_EXCEPTION(
00180       true, DoesNotExist
00181       ,"AlgorithmState::find_and_assert(iq_name) : The iteration "
00182       "quantity with the name \"" << iq_name << "\" does not exist" );
00183   return itr;
00184 }
00185 
00186 } // end namespace IterationPack
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines