MOOCHO  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
ExampleNLPFirstOrderMain.cpp
1 // @HEADER
2 // ***********************************************************************
3 //
4 // Moocho: Multi-functional Object-Oriented arCHitecture for Optimization
5 // Copyright (2003) Sandia Corporation
6 //
7 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
8 // license for use of this work by or on behalf of the U.S. Government.
9 //
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions are
12 // met:
13 //
14 // 1. Redistributions of source code must retain the above copyright
15 // notice, this list of conditions and the following disclaimer.
16 //
17 // 2. Redistributions in binary form must reproduce the above copyright
18 // notice, this list of conditions and the following disclaimer in the
19 // documentation and/or other materials provided with the distribution.
20 //
21 // 3. Neither the name of the Corporation nor the names of the
22 // contributors may be used to endorse or promote products derived from
23 // this software without specific prior written permission.
24 //
25 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
26 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
29 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 //
37 // Questions? Contact Roscoe A. Bartlett (rabartl@sandia.gov)
38 //
39 // ***********************************************************************
40 // @HEADER
41 //
42 
43 #include <assert.h>
44 
45 #include <fstream>
46 #include <fstream>
47 #include <iostream>
48 #include <iomanip>
49 
50 #include "NLPInterfacePack_ExampleNLPFirstOrderRun.hpp"
51 #include "AbstractLinAlgPack_exampleNLPDiagSetup.hpp"
52 #include "AbstractLinAlgPack_VectorSpace.hpp"
53 #include "Teuchos_GlobalMPISession.hpp"
54 #include "Teuchos_Workspace.hpp"
55 #include "Teuchos_oblackholestream.hpp"
56 
57 int main(int argc, char* argv[]) {
58 
59  using std::endl;
60  using std::setw;
61  namespace mmp = MemMngPack;
62  using Teuchos::RCP;
64  typedef AbstractLinAlgPack::value_type value_type;
65  namespace NLPIP = NLPInterfacePack;
66  namespace rsqp = MoochoPack;
67  using rsqp::MoochoSolver;
70 
71  GlobalMPISession mpiSession(&argc, &argv);
72 
73  // Get an idea of what processors we have.
74  const int proc_rank = GlobalMPISession::getRank();
75 
76  // Define program return values
77  const int
78  PROG_SUCCESS = 0,
79  PROG_NLP_TEST_ERR = -1,
80  PROG_EXCEPTION = -2,
81  PROG_MAX_ITER_EXEEDED = -3,
82  PROG_MAX_TIME_EXEEDED = -4;
83 
84  int prog_return = PROG_SUCCESS;
85 
86  // Set the output stream
87  std::ostream &out = std::cout;
88  std::ostream &eout = std::cerr;
89  Teuchos::oblackholestream blackhole;
90 
91  try {
92 
93  //
94  // Initialize stuff
95  //
96 
97  int n;
98  value_type xo;
99  bool has_bounds;
100  bool dep_bounded;
101 
102  VectorSpace::space_ptr_t vec_space;
103  const int err = AbstractLinAlgPack::exampleNLPDiagSetup(
104  argc, argv, MPI_COMM_WORLD, &vec_space, &n, &xo, &has_bounds, &dep_bounded);
105  if(err) return err;
106 
107 
108  // Create and test the NLP using this vector space object
109  const MoochoSolver::ESolutionStatus
110  solve_return = NLPIP::ExampleNLPFirstOrderRun(
111  *vec_space, xo, has_bounds, dep_bounded
112  ,proc_rank == 0 ? &out : &blackhole // console_out
113  ,proc_rank == 0 ? &eout : &blackhole // error_out
114  ,proc_rank == 0 ? false : true // throw_solve_exception
115  ,proc_rank == 0 ? NULL : &blackhole // algo_out
116  ,proc_rank == 0 ? NULL : &blackhole // summary_out
117  ,proc_rank == 0 ? NULL : &blackhole // journal_out
118  );
119 
120  switch(solve_return) {
121  case MoochoSolver::SOLVE_RETURN_SOLVED:
122  prog_return = PROG_SUCCESS;
123  break;
124  case MoochoSolver::SOLVE_RETURN_MAX_ITER:
125  prog_return = PROG_MAX_ITER_EXEEDED;
126  break;
127  case MoochoSolver::SOLVE_RETURN_MAX_RUN_TIME:
128  prog_return = PROG_MAX_TIME_EXEEDED;
129  break;
130  case MoochoSolver::SOLVE_RETURN_NLP_TEST_FAILED:
131  prog_return = PROG_NLP_TEST_ERR;
132  break;
133  case MoochoSolver::SOLVE_RETURN_EXCEPTION:
134  prog_return = PROG_EXCEPTION;
135  break;
136  default:
138  }
139 
140  } // end try
141  catch(const std::exception& excpt) {
142  eout << "\nCaught a std::exception on process " << proc_rank<< ": " << excpt.what() << endl;
143  prog_return = PROG_EXCEPTION;
144  }
145  catch(...) {
146  eout << "\nCaught an unknown exception on process " << proc_rank<< "\n";
147  prog_return = PROG_EXCEPTION;
148  }
149 
150  return prog_return;
151 }
MoochoPack::MoochoSolver::ESolutionStatus ExampleNLPFirstOrderRun(const VectorSpace &vec_space, value_type xo, bool has_bounds, bool dep_bounded, std::ostream *console_out, std::ostream *error_out, bool throw_solve_exception=false, std::ostream *algo_out=NULL, std::ostream *summary_out=NULL, std::ostream *journal_out=NULL)
Function accepts a VectorSpace object and then uses it to define an example NLP and run MoochoPack::M...
size_t size_type
#define TEUCHOS_TEST_FOR_EXCEPT(throw_exception_test)