MOOCHO Version of the Day
ExampleNLPFirstOrderMain.cpp
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 
00043 #include <assert.h>
00044 
00045 #include <fstream>
00046 #include <fstream>
00047 #include <iostream>
00048 #include <iomanip>
00049 
00050 #include "NLPInterfacePack_ExampleNLPFirstOrderRun.hpp"
00051 #include "AbstractLinAlgPack_exampleNLPDiagSetup.hpp"
00052 #include "AbstractLinAlgPack_VectorSpace.hpp"
00053 #include "Teuchos_GlobalMPISession.hpp"
00054 #include "Teuchos_Workspace.hpp"
00055 #include "Teuchos_oblackholestream.hpp"
00056 
00057 int main(int argc, char* argv[]) {
00058 
00059   using std::endl;
00060   using std::setw;
00061   namespace mmp = MemMngPack;
00062   using Teuchos::RCP;
00063   typedef AbstractLinAlgPack::size_type size_type;
00064   typedef AbstractLinAlgPack::value_type value_type;
00065   namespace NLPIP = NLPInterfacePack;
00066   namespace rsqp = MoochoPack;
00067   using rsqp::MoochoSolver;
00068   using AbstractLinAlgPack::VectorSpace;
00069   using Teuchos::GlobalMPISession;
00070 
00071   GlobalMPISession mpiSession(&argc, &argv);
00072 
00073   // Get an idea of what processors we have.
00074   const int proc_rank = GlobalMPISession::getRank();
00075 
00076   // Define program return values
00077   const int
00078     PROG_SUCCESS        =  0,
00079     PROG_NLP_TEST_ERR     = -1,
00080     PROG_EXCEPTION        = -2,
00081     PROG_MAX_ITER_EXEEDED   = -3,
00082     PROG_MAX_TIME_EXEEDED   = -4;
00083 
00084   int prog_return = PROG_SUCCESS;
00085 
00086   // Set the output stream
00087   std::ostream &out  = std::cout;
00088   std::ostream &eout = std::cerr;
00089   Teuchos::oblackholestream  blackhole;
00090 
00091   try {
00092   
00093     //
00094     // Initialize stuff
00095     //
00096 
00097     int n;
00098     value_type xo;
00099     bool has_bounds;
00100     bool dep_bounded;
00101     
00102     VectorSpace::space_ptr_t    vec_space;
00103     const int err = AbstractLinAlgPack::exampleNLPDiagSetup(
00104       argc, argv, MPI_COMM_WORLD, &vec_space, &n, &xo, &has_bounds, &dep_bounded);
00105     if(err) return err;
00106     
00107 
00108     // Create and test the NLP using this vector space object
00109     const MoochoSolver::ESolutionStatus
00110       solve_return = NLPIP::ExampleNLPFirstOrderRun(
00111         *vec_space, xo, has_bounds, dep_bounded
00112         ,proc_rank == 0 ? &out  : &blackhole  // console_out
00113         ,proc_rank == 0 ? &eout : &blackhole  // error_out
00114         ,proc_rank == 0 ? false : true        // throw_solve_exception
00115         ,proc_rank == 0 ? NULL  : &blackhole  // algo_out
00116         ,proc_rank == 0 ? NULL  : &blackhole  // summary_out
00117         ,proc_rank == 0 ? NULL  : &blackhole  // journal_out
00118         );
00119     
00120     switch(solve_return) {
00121       case MoochoSolver::SOLVE_RETURN_SOLVED:
00122         prog_return = PROG_SUCCESS;
00123         break;
00124       case MoochoSolver::SOLVE_RETURN_MAX_ITER:
00125         prog_return = PROG_MAX_ITER_EXEEDED;
00126         break;
00127       case MoochoSolver::SOLVE_RETURN_MAX_RUN_TIME:
00128         prog_return = PROG_MAX_TIME_EXEEDED;
00129         break;
00130       case MoochoSolver::SOLVE_RETURN_NLP_TEST_FAILED:
00131         prog_return = PROG_NLP_TEST_ERR;
00132         break;
00133       case MoochoSolver::SOLVE_RETURN_EXCEPTION:
00134         prog_return = PROG_EXCEPTION;
00135         break;
00136       default:
00137         TEUCHOS_TEST_FOR_EXCEPT(true);
00138     }
00139     
00140   } // end try
00141   catch(const std::exception& excpt) {
00142     eout << "\nCaught a std::exception on process " << proc_rank<< ": " << excpt.what() << endl;
00143     prog_return = PROG_EXCEPTION;
00144   }
00145   catch(...) {
00146     eout << "\nCaught an unknown exception on process " << proc_rank<< "\n";
00147     prog_return = PROG_EXCEPTION;
00148   }
00149 
00150   return prog_return;
00151 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Friends