MOOCHO Version of the Day
NLPThyraEpetraAdvDiffReactOptMain.cpp
00001 #include "GLpApp_AdvDiffReactOptModelCreator.hpp"
00002 #include "MoochoPack_MoochoThyraSolver.hpp"
00003 #include "Thyra_EpetraModelEvaluator.hpp"
00004 #include "Thyra_DefaultSpmdMultiVectorFileIO.hpp"
00005 #include "Stratimikos_DefaultLinearSolverBuilder.hpp"
00006 #include "Teuchos_GlobalMPISession.hpp"
00007 #include "Teuchos_CommandLineProcessor.hpp"
00008 #include "Teuchos_StandardCatchMacros.hpp"
00009 #include "Teuchos_VerboseObject.hpp"
00010 #include "Teuchos_XMLParameterListHelpers.hpp"
00011 #ifdef HAVE_MPI
00012 #  include "Epetra_MpiComm.h"
00013 #else
00014 #  include "Epetra_SerialComm.h"
00015 #endif
00016 
00017 namespace {
00018 typedef AbstractLinAlgPack::value_type  Scalar;
00019 } // namespace
00020 
00021 
00022 int main( int argc, char* argv[] )
00023 {
00024   using Teuchos::rcp;
00025   using Teuchos::RCP;
00026   using Teuchos::OSTab;
00027   using MoochoPack::MoochoSolver;
00028   using MoochoPack::MoochoThyraSolver;
00029   using Teuchos::CommandLineProcessor;
00030 
00031   Teuchos::GlobalMPISession mpiSession(&argc,&argv);
00032 
00033   const int numProcs = mpiSession.getNProc();
00034 
00035   Teuchos::Time timer("");
00036   
00037   bool dummySuccess = true;
00038 
00039   Teuchos::RCP<Teuchos::FancyOStream>
00040     out = Teuchos::VerboseObjectBase::getDefaultOStream();
00041 
00042   try {
00043   
00044     // Create the solver object
00045     GLpApp::AdvDiffReactOptModelCreator     advDiffReacModelCreator;
00046     Stratimikos::DefaultLinearSolverBuilder   lowsfCreator;
00047     MoochoThyraSolver                       solver;
00048 
00049     //
00050     // Get options from the command line
00051     //
00052 
00053     std::string         matchingVecFile     = "";
00054 
00055     bool                showMoochoThyraParams = false;
00056     bool                showMoochoThyraParamsWithDoc = true;
00057     bool                showMoochoThyraParamsXML = false;
00058 
00059     CommandLineProcessor  clp;
00060     clp.throwExceptions(false);
00061     clp.addOutputSetupOptions(true);
00062 
00063     advDiffReacModelCreator.setupCLP(&clp);
00064     lowsfCreator.setupCLP(&clp);
00065     solver.setupCLP(&clp);
00066 
00067     clp.setOption(
00068       "q-vec-file", &matchingVecFile
00069       ,"Base file name to read the objective state matching "
00070       "vector q (i.e. ||x-q||_M in the objective)."
00071       );
00072     
00073     clp.setOption(
00074       "only-print-moocho-thyra-solver-params", "no-print-moocho-thyra-solver-params"
00075       ,&showMoochoThyraParams
00076       ,"Only print the parameters accepted by MoochoPack::MoochoThyraSolver and stop."
00077       );
00078     clp.setOption(
00079       "show-doc", "hide-doc", &showMoochoThyraParamsWithDoc
00080       ,"Show MoochoPack::MocohoThyraSolver parameters with documentation or not."
00081       );
00082     clp.setOption(
00083       "xml-format", "readable-format", &showMoochoThyraParamsXML
00084       ,"Show MoochoPack::MoochoThyraSolver parameters in XML or human-readable format."
00085       );
00086 
00087     CommandLineProcessor::EParseCommandLineReturn
00088       parse_return = clp.parse(argc,argv,&std::cerr);
00089 
00090     if( parse_return != CommandLineProcessor::PARSE_SUCCESSFUL )
00091       return parse_return;
00092 
00093     lowsfCreator.readParameters( !showMoochoThyraParams ? out.get() : NULL );
00094     solver.readParameters( !showMoochoThyraParams ? out.get() : NULL );
00095 
00096     if(showMoochoThyraParams) {
00097       typedef Teuchos::ParameterList::PrintOptions PLPrintOptions;
00098       if(showMoochoThyraParamsXML)
00099         Teuchos::writeParameterListToXmlOStream(
00100           *solver.getValidParameters()
00101           ,*out
00102           );
00103       else
00104         solver.getValidParameters()->print(
00105           *out,PLPrintOptions().indent(2).showTypes(true).showDoc(showMoochoThyraParamsWithDoc)
00106           );
00107       return 0;
00108     }
00109 
00110     //
00111     // Setup the output streams
00112     //
00113     
00114     Teuchos::RCP<Teuchos::FancyOStream>
00115       journalOut = Teuchos::rcp(
00116         new Teuchos::FancyOStream(
00117           solver.getSolver().generate_output_file("MoochoJournal")
00118           ,"  "
00119           )
00120         );
00121     journalOut->copyAllOutputOptions(*out);
00122 
00123     *out
00124       << "\n***"
00125       << "\n*** NLPThyraEpetraAdvDiffReactOptMain, Global numProcs = "<<numProcs
00126       << "\n***\n";
00127 
00128 #ifdef HAVE_MPI
00129     MPI_Comm mpiComm = MPI_COMM_WORLD;
00130 #endif
00131 
00132     Teuchos::RCP<Epetra_Comm> comm = Teuchos::null;
00133 #ifdef HAVE_MPI
00134     comm = Teuchos::rcp(new Epetra_MpiComm(mpiComm));
00135 #else
00136     comm = Teuchos::rcp(new Epetra_SerialComm());
00137 #endif
00138     
00139     //
00140     // Create the Thyra::ModelEvaluator object
00141     //
00142     
00143     *out << "\nCreate the GLpApp::AdvDiffReactOptModel wrapper object ...\n";
00144     
00145     Teuchos::RCP<GLpApp::AdvDiffReactOptModel>
00146       epetraModel = advDiffReacModelCreator.createModel(comm);
00147     epetraModel->setOStream(journalOut);
00148 
00149     *out << "\nCreate the Thyra::LinearOpWithSolveFactory object ...\n";
00150 
00151     Teuchos::RCP<Thyra::LinearOpWithSolveFactoryBase<Scalar> >
00152       lowsFactory = lowsfCreator.createLinearSolveStrategy("");
00153     // ToDo: Set the output stream before calling above!
00155     
00156     *out << "\nCreate the Thyra::EpetraModelEvaluator wrapper object ...\n";
00157     
00158     Teuchos::RCP<Thyra::EpetraModelEvaluator>
00159       epetraThyraModel = rcp(new Thyra::EpetraModelEvaluator()); // Sets default options!
00160     epetraThyraModel->setOStream(journalOut);
00161     epetraThyraModel->initialize(epetraModel,lowsFactory);
00162 
00163     *out
00164       << "\nnx = " << epetraThyraModel->get_x_space()->dim()
00165       << "\nnp = " << epetraThyraModel->get_p_space(0)->dim() << "\n";
00166 
00167     if(matchingVecFile != "") {
00168       *out << "\nReading the matching vector \'q\' from the file(s) with base name \""<<matchingVecFile<<"\" ...\n";
00169       Thyra::DefaultSpmdMultiVectorFileIO<Scalar> fileIO;
00170       epetraModel->set_q(
00171         Thyra::get_Epetra_Vector(
00172           *epetraModel->get_x_map()
00173           ,readVectorFromFile(fileIO,matchingVecFile,*epetraThyraModel->get_x_space())
00174           )
00175         );
00176     }
00177 
00178     //
00179     // Solve the NLP
00180     //
00181 
00182     // Set the journal file
00183     solver.getSolver().set_journal_out(journalOut);
00184     
00185     // Set the model
00186     solver.setModel(epetraThyraModel);
00187 
00188     // Read the initial guess if one exists
00189     solver.readInitialGuess(out.get());
00190 
00191     // Solve the NLP
00192     const MoochoSolver::ESolutionStatus solution_status = solver.solve();
00193 
00194     // Write the solution to file
00195     solver.writeFinalSolution(out.get());
00196     
00197     // Write the final parameters to file
00198     lowsfCreator.writeParamsFile(*lowsFactory);
00199     solver.writeParamsFile();
00200     
00201     //
00202     // Return the solution status (0 if successful)
00203     //
00204 
00205     if(solution_status == MoochoSolver::SOLVE_RETURN_SOLVED)
00206       *out << "\nEnd Result: TEST PASSED\n";
00207     else
00208       *out << "\nEnd Result: TEST FAILED\n";
00209     
00210     return solution_status;
00211 
00212   }
00213   TEUCHOS_STANDARD_CATCH_STATEMENTS(true, *out, dummySuccess)
00214 
00215   return MoochoSolver::SOLVE_RETURN_EXCEPTION;
00216 
00217 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Friends