MOOCHO (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
NLPThyraEpetraModelEval4DOptMain.cpp
Go to the documentation of this file.
1 /*
2 // @HEADER
3 // ***********************************************************************
4 //
5 // Moocho: Multi-functional Object-Oriented arCHitecture for Optimization
6 // Copyright (2003) Sandia Corporation
7 //
8 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
9 // license for use of this work by or on behalf of the U.S. Government.
10 //
11 // Redistribution and use in source and binary forms, with or without
12 // modification, are permitted provided that the following conditions are
13 // met:
14 //
15 // 1. Redistributions of source code must retain the above copyright
16 // notice, this list of conditions and the following disclaimer.
17 //
18 // 2. Redistributions in binary form must reproduce the above copyright
19 // notice, this list of conditions and the following disclaimer in the
20 // documentation and/or other materials provided with the distribution.
21 //
22 // 3. Neither the name of the Corporation nor the names of the
23 // contributors may be used to endorse or promote products derived from
24 // this software without specific prior written permission.
25 //
26 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
27 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
30 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
31 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
32 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
33 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
36 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 //
38 // Questions? Contact Roscoe A. Bartlett (rabartl@sandia.gov)
39 //
40 // ***********************************************************************
41 // @HEADER
42 */
43 
45 #include "EpetraModelEval4DOpt.hpp"
47 #include "Stratimikos_DefaultLinearSolverBuilder.hpp"
48 #include "Thyra_EpetraModelEvaluator.hpp"
53 
54 int main( int argc, char* argv[] )
55 {
57  typedef AbstractLinAlgPack::value_type Scalar;
60 
61  bool dummySuccess = true;
62 
63  Teuchos::GlobalMPISession mpiSession(&argc,&argv);
64 
66  out = Teuchos::VerboseObjectBase::getDefaultOStream();
67 
68  try {
69 
70  Stratimikos::DefaultLinearSolverBuilder lowsfCreator;
71  MoochoThyraSolver solver;
72 
73  //
74  // Get options from the command line
75  //
76 
77  Scalar xt0 = 1.0;
78  Scalar xt1 = 1.0;
79  Scalar pt0 = 2.0;
80  Scalar pt1 = 0.0;
81  Scalar d = 10.0;
82  Scalar x00 = 1.0;
83  Scalar x01 = 1.0;
84  Scalar p00 = 2.0;
85  Scalar p01 = 0.0;
86  Scalar pL0 = -1e+50;
87  Scalar pL1 = -1e+50;
88  Scalar pU0 = +1e+50;
89  Scalar pU1 = +1e+50;
90 
91  Scalar xL0 = -1e+50;
92  Scalar xL1 = -1e+50;
93  Scalar xU0 = +1e+50;
94  Scalar xU1 = +1e+50;
95 
96  bool supportDerivs = true;
97 
98  std::string extraXmlFile = "";
99 
100  CommandLineProcessor clp(false); // Don't throw exceptions
101 
102  lowsfCreator.setupCLP(&clp);
103  solver.setupCLP(&clp);
104 
105  clp.setOption( "xt0", &xt0 );
106  clp.setOption( "xt1", &xt1 );
107  clp.setOption( "pt0", &pt0 );
108  clp.setOption( "pt1", &pt1 );
109  clp.setOption( "d", &d );
110  clp.setOption( "x00", &x00 );
111  clp.setOption( "x01", &x01 );
112  clp.setOption( "p00", &p00 );
113  clp.setOption( "p01", &p01 );
114  clp.setOption( "pL0", &pL0 );
115  clp.setOption( "pL1", &pL1 );
116  clp.setOption( "pU0", &pU0 );
117  clp.setOption( "pU1", &pU1 );
118  clp.setOption( "xL0", &xL0 );
119  clp.setOption( "xL1", &xL1 );
120  clp.setOption( "xU0", &xU0 );
121  clp.setOption( "xU1", &xU1 );
122  clp.setOption( "support-derivs", "no-support-derivs", &supportDerivs );
123  clp.setOption("extra-xml-file",&extraXmlFile,"File with extra XML text that will modify the initial XML read in");
124 
125  std::string line("");
126  if(extraXmlFile.length()) {
127  std::ifstream myfile(extraXmlFile.c_str());
128  if (myfile.is_open())
129  {
130  getline (myfile,line);
131  solver.extraParamsXmlStringOption(line);
132  std::cout << line << "\n";
133  myfile.close();
134  }
135  }
136 
137  CommandLineProcessor::EParseCommandLineReturn
138  parse_return = clp.parse(argc,argv,&std::cerr);
139 
140  if( parse_return != CommandLineProcessor::PARSE_SUCCESSFUL )
141  return parse_return;
142 
143  lowsfCreator.readParameters(out.get());
144  solver.readParameters(out.get());
145 
146  //
147  // Create the NLP
148  //
149 
150  // Create the EpetraExt::ModelEvaluator object
151 
153  epetraModel = Teuchos::rcp(new EpetraModelEval4DOpt(xt0,xt1,pt0,pt1,d,x00,x01,p00,p01));
154  epetraModel->setSupportDerivs(supportDerivs);
155  epetraModel->set_p_bounds(pL0,pL1,pU0,pU1);
156  epetraModel->set_x_bounds(xL0,xL1,xU0,xU1);
157 
158  // Create the Thyra::EpetraModelEvaluator object
159 
161  lowsFactory = lowsfCreator.createLinearSolveStrategy("");
162 
164  epetraThyraModel = Teuchos::rcp(new Thyra::EpetraModelEvaluator());
165 
166  epetraThyraModel->initialize(epetraModel,lowsFactory);
167 
168  //
169  // Solve the NLP
170  //
171 
172  // Set the model
173  solver.setModel(epetraThyraModel);
174 
175  // Read the initial guess if one exists
176  solver.readInitialGuess(out.get());
177 
178  // Solve the NLP
179  const MoochoSolver::ESolutionStatus solution_status = solver.solve();
180 
181  // Write the final solution if requested
182  solver.writeFinalSolution(out.get());
183 
184  // Write the parameters that where read
185  lowsfCreator.writeParamsFile(*lowsFactory);
186  solver.writeParamsFile();
187 
188  //
189  // Return the solution status (0 if sucessfull)
190  //
191 
192  return solution_status;
193 
194  }
195  TEUCHOS_STANDARD_CATCH_STATEMENTS(true,*out,dummySuccess)
196 
197  return MoochoSolver::SOLVE_RETURN_EXCEPTION;
198 
199 }
void set_x_bounds(double xL0, double xL1, double xU0, double xU1)
void set_p_bounds(double pL0, double pL1, double pU0, double pU1)
T * get() const
MOOCHO NLP Solver class for models represented through Thyra::ModelEvaluator.
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
#define TEUCHOS_STANDARD_CATCH_STATEMENTS(VERBOSE, ERR_STREAM, SUCCESS_FLAG)
std::ostream * out
Universal interface to a MOOCHO solver.
void setSupportDerivs(bool supportDerivs)
int main(int argc, char *argv[])