NLPThyraEpetraAdvDiffReactOptMain.cpp

00001 #include "GLpApp_AdvDiffReactOptModelCreator.hpp"
00002 #include "MoochoPack_ThyraModelEvaluatorSolver.hpp"
00003 #include "Thyra_EpetraModelEvaluator.hpp"
00004 #include "Thyra_SpmdMultiVectorFileIO.hpp"
00005 #include "Thyra_DefaultRealLinearSolverBuilder.hpp"
00006 #include "Teuchos_GlobalMPISession.hpp"
00007 #include "Teuchos_CommandLineProcessor.hpp"
00008 #include "Teuchos_StandardCatchMacros.hpp"
00009 #include "Teuchos_VerboseObject.hpp"
00010 #ifdef HAVE_MPI
00011 #  include "Epetra_MpiComm.h"
00012 #else
00013 #  include "Epetra_SerialComm.h"
00014 #endif
00015 
00016 namespace {
00017 
00018 typedef AbstractLinAlgPack::value_type  Scalar;
00019 
00020 } // namespace
00021 
00022 int main( int argc, char* argv[] )
00023 {
00024   using Teuchos::rcp;
00025   using Teuchos::RefCountPtr;
00026   using Teuchos::OSTab;
00027   using MoochoPack::MoochoSolver;
00028   using MoochoPack::ThyraModelEvaluatorSolver;
00029   using Teuchos::CommandLineProcessor;
00030 
00031   Teuchos::GlobalMPISession mpiSession(&argc,&argv);
00032 
00033   const int procRank = mpiSession.getRank();
00034   const int numProcs = mpiSession.getNProc();
00035 
00036   Teuchos::Time timer("");
00037   
00038   bool dummySuccess = true;
00039 
00040   Teuchos::RefCountPtr<Teuchos::FancyOStream>
00041     out = Teuchos::VerboseObjectBase::getDefaultOStream();
00042 
00043   try {
00044   
00045     // Create the solver object
00046     GLpApp::AdvDiffReactOptModelCreator     advDiffReacModelCreator;
00047     Thyra::DefaultRealLinearSolverBuilder   lowsfCreator;
00048     ThyraModelEvaluatorSolver               solver;
00049     solver.insertStateElimCommandLineOptions(true);
00050     solver.insertFiniteDiffCommandLineOptions(true);
00051 
00052     //
00053     // Get options from the command line
00054     //
00055 
00056     std::string         matchingVecFile     = "";
00057     bool                dump_all            = false;
00058     int                 numProcsPerCluster  = -1;
00059 
00060     CommandLineProcessor  clp;
00061     clp.throwExceptions(false);
00062     clp.addOutputSetupOptions(true);
00063 
00064     advDiffReacModelCreator.setupCLP(&clp);
00065     lowsfCreator.setupCLP(&clp);
00066     solver.setupCLP(&clp);
00067 
00068     clp.setOption( "q-vec-file", &matchingVecFile, "Base file name to read the objective state matching vector q (i.e. ||x-q||_M in objective)." );
00069 
00070     CommandLineProcessor::EParseCommandLineReturn
00071       parse_return = clp.parse(argc,argv,&std::cerr);
00072 
00073     if( parse_return != CommandLineProcessor::PARSE_SUCCESSFUL )
00074       return parse_return;
00075 
00076     lowsfCreator.readParameters(out.get());
00077 
00078     //
00079     // Setup the output streams
00080     //
00081     
00082     std::string journalOutName;
00083     if( numProcs > 1 && out->getOutputToRootOnly() < 0 ) {
00084       std::ostringstream oss;
00085       oss << "MoochoJournal."<<std::setfill('0')<<std::setw(4)<<std::right<<procRank<<".out";
00086       journalOutName = oss.str();
00087     }
00088     else {
00089       journalOutName = "MoochoJournal.out";
00090     }
00091     
00092     Teuchos::RefCountPtr<Teuchos::FancyOStream>
00093       journalOut = Teuchos::rcp(
00094         new Teuchos::FancyOStream(
00095           Teuchos::rcp(new std::ofstream(journalOutName.c_str()))
00096           ,"  "
00097           )
00098         );
00099     journalOut->copyAllOutputOptions(*out);
00100 
00101     *out
00102       << "\n***"
00103       << "\n*** NLPThyraEpetraAdvDiffReactOptMain, Global numProcs = "<<numProcs
00104       << "\n***\n";
00105 
00106 #ifdef HAVE_MPI
00107     MPI_Comm mpiComm = MPI_COMM_WORLD;
00108 #endif
00109 
00110     Teuchos::RefCountPtr<Epetra_Comm> comm = Teuchos::null;
00111 #ifdef HAVE_MPI
00112     comm = Teuchos::rcp(new Epetra_MpiComm(mpiComm));
00113 #else
00114     comm = Teuchos::rcp(new Epetra_SerialComm());
00115 #endif
00116     
00117     //
00118     // Create the Thyra::ModelEvaluator object
00119     //
00120     
00121     *out << "\nCreate the GLpApp::AdvDiffReactOptModel wrapper object ...\n";
00122     
00123     Teuchos::RefCountPtr<GLpApp::AdvDiffReactOptModel>
00124       epetraModel = advDiffReacModelCreator.createModel(comm);
00125     epetraModel->setOStream(journalOut);
00126     if(dump_all) epetraModel->setVerbLevel(Teuchos::VERB_EXTREME);
00127 
00128     *out << "\nCreate the Thyra::LinearOpWithSolveFactory object ...\n";
00129 
00130     Teuchos::RefCountPtr<Thyra::LinearOpWithSolveFactoryBase<Scalar> >
00131       lowsFactory = lowsfCreator.createLinearSolveStrategy("");
00132     // ToDo: Set the output stream before calling above!
00134     
00135     *out << "\nCreate the Thyra::EpetraModelEvaluator wrapper object ...\n";
00136     
00137     Teuchos::RefCountPtr<Thyra::EpetraModelEvaluator>
00138       epetraThyraModel = rcp(new Thyra::EpetraModelEvaluator()); // Sets default options!
00139     epetraThyraModel->setOStream(journalOut);
00140     epetraThyraModel->initialize(epetraModel,lowsFactory);
00141 
00142     *out
00143       << "\nnx = " << epetraThyraModel->get_x_space()->dim()
00144       << "\nnp = " << epetraThyraModel->get_p_space(0)->dim() << "\n";
00145 
00146     if(matchingVecFile != "") {
00147       *out << "\nReading the matching vector \'q\' from the file(s) with base name \""<<matchingVecFile<<"\" ...\n";
00148       Thyra::SpmdMultiVectorFileIO<Scalar> fileIO;
00149       epetraModel->set_q(
00150         Thyra::get_Epetra_Vector(
00151           *epetraModel->get_x_map(),fileIO.readVectorFromFile(matchingVecFile,epetraThyraModel->get_x_space())
00152           )
00153         );
00154     }
00155 
00156     //
00157     // Solve the NLP
00158     //
00159 
00160     // Set the journal file
00161     solver.getSolver().set_journal_out(journalOut);
00162     
00163     // Set the model
00164     solver.setModel(epetraThyraModel);
00165 
00166     // Read the initial guess if one exists
00167     solver.readInitialGuess(out.get());
00168 
00169     // Solve the NLP
00170     const MoochoSolver::ESolutionStatus solution_status = solver.solve();
00171 
00172     // Write the solution to file
00173     solver.writeFinalSolution(out.get());
00174     
00175     // Write the final parameters to file
00176     lowsfCreator.writeParamsFile(*lowsFactory);
00177     
00178     //
00179     // Return the solution status (0 if successful)
00180     //
00181 
00182     return solution_status;
00183 
00184   }
00185   TEUCHOS_STANDARD_CATCH_STATEMENTS(true,*out,dummySuccess)
00186 
00187   return MoochoSolver::SOLVE_RETURN_EXCEPTION;
00188 }

Generated on Thu Sep 18 12:36:38 2008 for MOOCHO by doxygen 1.3.9.1