MOOCHO (Single Doxygen Collection) Version of the Day
NLPThyraEpetraAdvDiffReactOptMain.cpp
Go to the documentation of this file.
00001 /*
00002 // @HEADER
00003 // ***********************************************************************
00004 // 
00005 // Moocho: Multi-functional Object-Oriented arCHitecture for Optimization
00006 //                  Copyright (2003) Sandia Corporation
00007 // 
00008 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
00009 // license for use of this work by or on behalf of the U.S. Government.
00010 // 
00011 // Redistribution and use in source and binary forms, with or without
00012 // modification, are permitted provided that the following conditions are
00013 // met:
00014 //
00015 // 1. Redistributions of source code must retain the above copyright
00016 // notice, this list of conditions and the following disclaimer.
00017 //
00018 // 2. Redistributions in binary form must reproduce the above copyright
00019 // notice, this list of conditions and the following disclaimer in the
00020 // documentation and/or other materials provided with the distribution.
00021 //
00022 // 3. Neither the name of the Corporation nor the names of the
00023 // contributors may be used to endorse or promote products derived from
00024 // this software without specific prior written permission.
00025 //
00026 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
00027 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00028 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
00029 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
00030 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00031 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00032 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00033 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00034 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00035 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00036 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00037 //
00038 // Questions? Contact Roscoe A. Bartlett (rabartl@sandia.gov) 
00039 // 
00040 // ***********************************************************************
00041 // @HEADER
00042 */
00043 
00044 #include "GLpApp_AdvDiffReactOptModelCreator.hpp"
00045 #include "MoochoPack_MoochoThyraSolver.hpp"
00046 #include "Thyra_EpetraModelEvaluator.hpp"
00047 #include "Thyra_DefaultSpmdMultiVectorFileIO.hpp"
00048 #include "Stratimikos_DefaultLinearSolverBuilder.hpp"
00049 #include "Teuchos_GlobalMPISession.hpp"
00050 #include "Teuchos_CommandLineProcessor.hpp"
00051 #include "Teuchos_StandardCatchMacros.hpp"
00052 #include "Teuchos_VerboseObject.hpp"
00053 #include "Teuchos_XMLParameterListHelpers.hpp"
00054 #ifdef HAVE_MPI
00055 #  include "Epetra_MpiComm.h"
00056 #else
00057 #  include "Epetra_SerialComm.h"
00058 #endif
00059 
00060 namespace {
00061 typedef AbstractLinAlgPack::value_type  Scalar;
00062 } // namespace
00063 
00064 
00065 int main( int argc, char* argv[] )
00066 {
00067   using Teuchos::rcp;
00068   using Teuchos::RCP;
00069   using Teuchos::OSTab;
00070   using MoochoPack::MoochoSolver;
00071   using MoochoPack::MoochoThyraSolver;
00072   using Teuchos::CommandLineProcessor;
00073 
00074   Teuchos::GlobalMPISession mpiSession(&argc,&argv);
00075 
00076   const int numProcs = mpiSession.getNProc();
00077 
00078   Teuchos::Time timer("");
00079   
00080   bool dummySuccess = true;
00081 
00082   Teuchos::RCP<Teuchos::FancyOStream>
00083     out = Teuchos::VerboseObjectBase::getDefaultOStream();
00084 
00085   try {
00086   
00087     // Create the solver object
00088     GLpApp::AdvDiffReactOptModelCreator     advDiffReacModelCreator;
00089     Stratimikos::DefaultLinearSolverBuilder   lowsfCreator;
00090     MoochoThyraSolver                       solver;
00091 
00092     //
00093     // Get options from the command line
00094     //
00095 
00096     std::string         matchingVecFile     = "";
00097 
00098     bool                showMoochoThyraParams = false;
00099     bool                showMoochoThyraParamsWithDoc = true;
00100     bool                showMoochoThyraParamsXML = false;
00101 
00102     CommandLineProcessor  clp;
00103     clp.throwExceptions(false);
00104     clp.addOutputSetupOptions(true);
00105 
00106     advDiffReacModelCreator.setupCLP(&clp);
00107     lowsfCreator.setupCLP(&clp);
00108     solver.setupCLP(&clp);
00109 
00110     clp.setOption(
00111       "q-vec-file", &matchingVecFile
00112       ,"Base file name to read the objective state matching "
00113       "vector q (i.e. ||x-q||_M in the objective)."
00114       );
00115     
00116     clp.setOption(
00117       "only-print-moocho-thyra-solver-params", "no-print-moocho-thyra-solver-params"
00118       ,&showMoochoThyraParams
00119       ,"Only print the parameters accepted by MoochoPack::MoochoThyraSolver and stop."
00120       );
00121     clp.setOption(
00122       "show-doc", "hide-doc", &showMoochoThyraParamsWithDoc
00123       ,"Show MoochoPack::MocohoThyraSolver parameters with documentation or not."
00124       );
00125     clp.setOption(
00126       "xml-format", "readable-format", &showMoochoThyraParamsXML
00127       ,"Show MoochoPack::MoochoThyraSolver parameters in XML or human-readable format."
00128       );
00129 
00130     CommandLineProcessor::EParseCommandLineReturn
00131       parse_return = clp.parse(argc,argv,&std::cerr);
00132 
00133     if( parse_return != CommandLineProcessor::PARSE_SUCCESSFUL )
00134       return parse_return;
00135 
00136     lowsfCreator.readParameters( !showMoochoThyraParams ? out.get() : NULL );
00137     solver.readParameters( !showMoochoThyraParams ? out.get() : NULL );
00138 
00139     if(showMoochoThyraParams) {
00140       typedef Teuchos::ParameterList::PrintOptions PLPrintOptions;
00141       if(showMoochoThyraParamsXML)
00142         Teuchos::writeParameterListToXmlOStream(
00143           *solver.getValidParameters()
00144           ,*out
00145           );
00146       else
00147         solver.getValidParameters()->print(
00148           *out,PLPrintOptions().indent(2).showTypes(true).showDoc(showMoochoThyraParamsWithDoc)
00149           );
00150       return 0;
00151     }
00152 
00153     //
00154     // Setup the output streams
00155     //
00156     
00157     Teuchos::RCP<Teuchos::FancyOStream>
00158       journalOut = Teuchos::rcp(
00159         new Teuchos::FancyOStream(
00160           solver.getSolver().generate_output_file("MoochoJournal")
00161           ,"  "
00162           )
00163         );
00164     journalOut->copyAllOutputOptions(*out);
00165 
00166     *out
00167       << "\n***"
00168       << "\n*** NLPThyraEpetraAdvDiffReactOptMain, Global numProcs = "<<numProcs
00169       << "\n***\n";
00170 
00171 #ifdef HAVE_MPI
00172     MPI_Comm mpiComm = MPI_COMM_WORLD;
00173 #endif
00174 
00175     Teuchos::RCP<Epetra_Comm> comm = Teuchos::null;
00176 #ifdef HAVE_MPI
00177     comm = Teuchos::rcp(new Epetra_MpiComm(mpiComm));
00178 #else
00179     comm = Teuchos::rcp(new Epetra_SerialComm());
00180 #endif
00181     
00182     //
00183     // Create the Thyra::ModelEvaluator object
00184     //
00185     
00186     *out << "\nCreate the GLpApp::AdvDiffReactOptModel wrapper object ...\n";
00187     
00188     Teuchos::RCP<GLpApp::AdvDiffReactOptModel>
00189       epetraModel = advDiffReacModelCreator.createModel(comm);
00190     epetraModel->setOStream(journalOut);
00191 
00192     *out << "\nCreate the Thyra::LinearOpWithSolveFactory object ...\n";
00193 
00194     Teuchos::RCP<Thyra::LinearOpWithSolveFactoryBase<Scalar> >
00195       lowsFactory = lowsfCreator.createLinearSolveStrategy("");
00196     // ToDo: Set the output stream before calling above!
00198     
00199     *out << "\nCreate the Thyra::EpetraModelEvaluator wrapper object ...\n";
00200     
00201     Teuchos::RCP<Thyra::EpetraModelEvaluator>
00202       epetraThyraModel = rcp(new Thyra::EpetraModelEvaluator()); // Sets default options!
00203     epetraThyraModel->setOStream(journalOut);
00204     epetraThyraModel->initialize(epetraModel,lowsFactory);
00205 
00206     *out
00207       << "\nnx = " << epetraThyraModel->get_x_space()->dim()
00208       << "\nnp = " << epetraThyraModel->get_p_space(0)->dim() << "\n";
00209 
00210     if(matchingVecFile != "") {
00211       *out << "\nReading the matching vector \'q\' from the file(s) with base name \""<<matchingVecFile<<"\" ...\n";
00212       Thyra::DefaultSpmdMultiVectorFileIO<Scalar> fileIO;
00213       epetraModel->set_q(
00214         Thyra::get_Epetra_Vector(
00215           *epetraModel->get_x_map()
00216           ,readVectorFromFile(fileIO,matchingVecFile,*epetraThyraModel->get_x_space())
00217           )
00218         );
00219     }
00220 
00221     //
00222     // Solve the NLP
00223     //
00224 
00225     // Set the journal file
00226     solver.getSolver().set_journal_out(journalOut);
00227     
00228     // Set the model
00229     solver.setModel(epetraThyraModel);
00230 
00231     // Read the initial guess if one exists
00232     solver.readInitialGuess(out.get());
00233 
00234     // Solve the NLP
00235     const MoochoSolver::ESolutionStatus solution_status = solver.solve();
00236 
00237     // Write the solution to file
00238     solver.writeFinalSolution(out.get());
00239     
00240     // Write the final parameters to file
00241     lowsfCreator.writeParamsFile(*lowsFactory);
00242     solver.writeParamsFile();
00243     
00244     //
00245     // Return the solution status (0 if successful)
00246     //
00247 
00248     if(solution_status == MoochoSolver::SOLVE_RETURN_SOLVED)
00249       *out << "\nEnd Result: TEST PASSED\n";
00250     else
00251       *out << "\nEnd Result: TEST FAILED\n";
00252     
00253     return solution_status;
00254 
00255   }
00256   TEUCHOS_STANDARD_CATCH_STATEMENTS(true, *out, dummySuccess)
00257 
00258   return MoochoSolver::SOLVE_RETURN_EXCEPTION;
00259 
00260 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines