MOOCHO (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
NLPThyraEpetraAdvDiffReactOptMain.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 
46 #include "Thyra_EpetraModelEvaluator.hpp"
47 #include "Thyra_DefaultSpmdMultiVectorFileIO.hpp"
48 #include "Stratimikos_DefaultLinearSolverBuilder.hpp"
54 #ifdef HAVE_MPI
55 # include "Epetra_MpiComm.h"
56 #else
57 # include "Epetra_SerialComm.h"
58 #endif
59 
60 namespace {
61 typedef AbstractLinAlgPack::value_type Scalar;
62 } // namespace
63 
64 
65 int main( int argc, char* argv[] )
66 {
67  using Teuchos::rcp;
68  using Teuchos::RCP;
69  using Teuchos::OSTab;
73 
74  Teuchos::GlobalMPISession mpiSession(&argc,&argv);
75 
76  const int numProcs = mpiSession.getNProc();
77 
78  Teuchos::Time timer("");
79 
80  bool dummySuccess = true;
81 
83  out = Teuchos::VerboseObjectBase::getDefaultOStream();
84 
85  try {
86 
87  // Create the solver object
88  GLpApp::AdvDiffReactOptModelCreator advDiffReacModelCreator;
89  Stratimikos::DefaultLinearSolverBuilder lowsfCreator;
90  MoochoThyraSolver solver;
91 
92  //
93  // Get options from the command line
94  //
95 
96  std::string matchingVecFile = "";
97 
98  bool showMoochoThyraParams = false;
99  bool showMoochoThyraParamsWithDoc = true;
100  bool showMoochoThyraParamsXML = false;
101 
102  CommandLineProcessor clp;
103  clp.throwExceptions(false);
104  clp.addOutputSetupOptions(true);
105 
106  advDiffReacModelCreator.setupCLP(&clp);
107  lowsfCreator.setupCLP(&clp);
108  solver.setupCLP(&clp);
109 
110  clp.setOption(
111  "q-vec-file", &matchingVecFile
112  ,"Base file name to read the objective state matching "
113  "vector q (i.e. ||x-q||_M in the objective)."
114  );
115 
116  clp.setOption(
117  "only-print-moocho-thyra-solver-params", "no-print-moocho-thyra-solver-params"
118  ,&showMoochoThyraParams
119  ,"Only print the parameters accepted by MoochoPack::MoochoThyraSolver and stop."
120  );
121  clp.setOption(
122  "show-doc", "hide-doc", &showMoochoThyraParamsWithDoc
123  ,"Show MoochoPack::MocohoThyraSolver parameters with documentation or not."
124  );
125  clp.setOption(
126  "xml-format", "readable-format", &showMoochoThyraParamsXML
127  ,"Show MoochoPack::MoochoThyraSolver parameters in XML or human-readable format."
128  );
129 
130  CommandLineProcessor::EParseCommandLineReturn
131  parse_return = clp.parse(argc,argv,&std::cerr);
132 
133  if( parse_return != CommandLineProcessor::PARSE_SUCCESSFUL )
134  return parse_return;
135 
136  lowsfCreator.readParameters( !showMoochoThyraParams ? out.get() : NULL );
137  solver.readParameters( !showMoochoThyraParams ? out.get() : NULL );
138 
139  if(showMoochoThyraParams) {
141  if(showMoochoThyraParamsXML)
142  Teuchos::writeParameterListToXmlOStream(
143  *solver.getValidParameters()
144  ,*out
145  );
146  else
147  solver.getValidParameters()->print(
148  *out,PLPrintOptions().indent(2).showTypes(true).showDoc(showMoochoThyraParamsWithDoc)
149  );
150  return 0;
151  }
152 
153  //
154  // Setup the output streams
155  //
156 
158  journalOut = Teuchos::rcp(
160  solver.getSolver().generate_output_file("MoochoJournal")
161  ," "
162  )
163  );
164  journalOut->copyAllOutputOptions(*out);
165 
166  *out
167  << "\n***"
168  << "\n*** NLPThyraEpetraAdvDiffReactOptMain, Global numProcs = "<<numProcs
169  << "\n***\n";
170 
171 #ifdef HAVE_MPI
172  MPI_Comm mpiComm = MPI_COMM_WORLD;
173 #endif
174 
176 #ifdef HAVE_MPI
177  comm = Teuchos::rcp(new Epetra_MpiComm(mpiComm));
178 #else
179  comm = Teuchos::rcp(new Epetra_SerialComm());
180 #endif
181 
182  //
183  // Create the Thyra::ModelEvaluator object
184  //
185 
186  *out << "\nCreate the GLpApp::AdvDiffReactOptModel wrapper object ...\n";
187 
189  epetraModel = advDiffReacModelCreator.createModel(comm);
190  epetraModel->setOStream(journalOut);
191 
192  *out << "\nCreate the Thyra::LinearOpWithSolveFactory object ...\n";
193 
195  lowsFactory = lowsfCreator.createLinearSolveStrategy("");
196  // ToDo: Set the output stream before calling above!
198 
199  *out << "\nCreate the Thyra::EpetraModelEvaluator wrapper object ...\n";
200 
202  epetraThyraModel = rcp(new Thyra::EpetraModelEvaluator()); // Sets default options!
203  epetraThyraModel->setOStream(journalOut);
204  epetraThyraModel->initialize(epetraModel,lowsFactory);
205 
206  *out
207  << "\nnx = " << epetraThyraModel->get_x_space()->dim()
208  << "\nnp = " << epetraThyraModel->get_p_space(0)->dim() << "\n";
209 
210  if(matchingVecFile != "") {
211  *out << "\nReading the matching vector \'q\' from the file(s) with base name \""<<matchingVecFile<<"\" ...\n";
212  Thyra::DefaultSpmdMultiVectorFileIO<Scalar> fileIO;
213  epetraModel->set_q(
214  Thyra::get_Epetra_Vector(
215  *epetraModel->get_x_map()
216  ,readVectorFromFile(fileIO,matchingVecFile,*epetraThyraModel->get_x_space())
217  )
218  );
219  }
220 
221  //
222  // Solve the NLP
223  //
224 
225  // Set the journal file
226  solver.getSolver().set_journal_out(journalOut);
227 
228  // Set the model
229  solver.setModel(epetraThyraModel);
230 
231  // Read the initial guess if one exists
232  solver.readInitialGuess(out.get());
233 
234  // Solve the NLP
235  const MoochoSolver::ESolutionStatus solution_status = solver.solve();
236 
237  // Write the solution to file
238  solver.writeFinalSolution(out.get());
239 
240  // Write the final parameters to file
241  lowsfCreator.writeParamsFile(*lowsFactory);
242  solver.writeParamsFile();
243 
244  //
245  // Return the solution status (0 if successful)
246  //
247 
248  if(solution_status == MoochoSolver::SOLVE_RETURN_SOLVED)
249  *out << "\nEnd Result: TEST PASSED\n";
250  else
251  *out << "\nEnd Result: TEST FAILED\n";
252 
253  return solution_status;
254 
255  }
256  TEUCHOS_STANDARD_CATCH_STATEMENTS(true, *out, dummySuccess)
257 
258  return MoochoSolver::SOLVE_RETURN_EXCEPTION;
259 
260 }
Teuchos::RCP< const Epetra_Map > get_x_map() const
T * get() const
MOOCHO NLP Solver class for models represented through Thyra::ModelEvaluator.
basic_OSTab< char > OSTab
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
int MPI_Comm
Definition: RTOp_mpi.h:67
#define TEUCHOS_STANDARD_CATCH_STATEMENTS(VERBOSE, ERR_STREAM, SUCCESS_FLAG)
std::ostream * out
int main(int argc, char *argv[])
void setupCLP(Teuchos::CommandLineProcessor *clp)
ParameterList::PrintOptions PLPrintOptions
Universal interface to a MOOCHO solver.
void set_q(Teuchos::RCP< const Epetra_Vector > const &q)
void copyAllOutputOptions(const basic_FancyOStream< CharT, Traits > &oStream)
#define MPI_COMM_WORLD
Definition: RTOp_mpi.h:68
Teuchos::RCP< AdvDiffReactOptModel > createModel(const Teuchos::RCP< const Epetra_Comm > &comm, std::ostream *out=NULL) const