MOOCHO (Single Doxygen Collection) Version of the Day
NLPThyraEpetraMultiPointModelEval4DOptMain.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 "NLPInterfacePack_NLPFirstOrderThyraModelEvaluator.hpp"
00045 #ifdef HAVE_MPI
00046 #include "EpetraExt_MultiPointModelEvaluator.h"
00047 #include "EpetraExt_MultiMpiComm.h"
00048 #endif
00049 #include "EpetraMultiPointModelEval4DOpt.hpp"
00050 #include "MoochoPack_MoochoThyraSolver.hpp"
00051 #include "Stratimikos_DefaultLinearSolverBuilder.hpp"
00052 #include "Thyra_EpetraModelEvaluator.hpp"
00053 #include "Teuchos_RCP.hpp"
00054 #include "Teuchos_GlobalMPISession.hpp"
00055 #include "Teuchos_VerboseObject.hpp"
00056 #include "Teuchos_CommandLineProcessor.hpp"
00057 #include "Teuchos_StandardCatchMacros.hpp"
00058 
00059 int main( int argc, char* argv[] )
00060 #ifdef HAVE_MPI
00061 {
00062   using Teuchos::CommandLineProcessor;
00063   typedef AbstractLinAlgPack::value_type  Scalar;
00064   using MoochoPack::MoochoSolver;
00065   using MoochoPack::MoochoThyraSolver;
00066 
00067   bool dummySuccess = true;
00068 
00069   Teuchos::GlobalMPISession mpiSession(&argc,&argv);
00070 
00071   Teuchos::RCP<Teuchos::FancyOStream>
00072     out = Teuchos::VerboseObjectBase::getDefaultOStream();
00073 
00074   try {
00075 
00076     Stratimikos::DefaultLinearSolverBuilder lowsfCreator;
00077     MoochoThyraSolver                     solver;
00078   
00079     //
00080     // Get options from the command line
00081     //
00082     
00083     Scalar       xt0         = 1.0;
00084     Scalar       xt1         = 1.0;
00085     Scalar       pt0         = 2.0;
00086     Scalar       pt1         = 0.0;
00087     Scalar       d           = 10.0;
00088     Scalar       x00         = 1.0;
00089     Scalar       x01         = 1.0;
00090     Scalar       p00         = 2.0;
00091     Scalar       p01         = 0.0;
00092     Scalar       pL0         = -1e+50;
00093     Scalar       pL1         = -1e+50;
00094     Scalar       pU0         = +1e+50;
00095     Scalar       pU1         = +1e+50;
00096     Scalar       q0          = 0.0;
00097 
00098     Scalar       xL0         = -1e+50;
00099     Scalar       xL1         = -1e+50;
00100     Scalar       xU0         = +1e+50;
00101     Scalar       xU1         = +1e+50;
00102 
00103     CommandLineProcessor  clp(false); // Don't throw exceptions
00104 
00105     lowsfCreator.setupCLP(&clp);
00106     solver.setupCLP(&clp);
00107 
00108     clp.setOption( "xt0", &xt0 );
00109     clp.setOption( "xt1", &xt1 );
00110     clp.setOption( "pt0", &pt0 );
00111     clp.setOption( "pt1", &pt1 );
00112     clp.setOption( "d", &d );
00113     clp.setOption( "x00", &x00 );
00114     clp.setOption( "x01", &x01 );
00115     clp.setOption( "p00", &p00 );
00116     clp.setOption( "q0", &q0 );
00117     clp.setOption( "p01", &p01 );
00118     clp.setOption( "pL0", &pL0 );
00119     clp.setOption( "pL1", &pL1 );
00120     clp.setOption( "pU0", &pU0 );
00121     clp.setOption( "pU1", &pU1 );
00122     clp.setOption( "xL0", &xL0 );
00123     clp.setOption( "xL1", &xL1 );
00124     clp.setOption( "xU0", &xU0 );
00125     clp.setOption( "xU1", &xU1 );
00126  
00127     CommandLineProcessor::EParseCommandLineReturn
00128       parse_return = clp.parse(argc,argv,&std::cerr);
00129 
00130     if( parse_return != CommandLineProcessor::PARSE_SUCCESSFUL )
00131       return parse_return;
00132 
00133     lowsfCreator.readParameters(out.get());
00134     solver.readParameters(out.get());
00135 
00136     //
00137     // Create the NLP
00138     //
00139     
00140     // Construct global and split (individual point) communicators
00141     int numPoints = 20;
00142     Teuchos::RCP<EpetraExt::MultiMpiComm> globalComm = 
00143       Teuchos::rcp(new EpetraExt::MultiMpiComm(MPI_COMM_WORLD, 1,  numPoints));
00144     int myPoints = globalComm->NumTimeStepsOnDomain();
00145     int myFirstPoint = globalComm->FirstTimeStepOnDomain();
00146 
00147     Teuchos::RCP<Epetra_MpiComm> epetra_comm =
00148       Teuchos::rcp_dynamic_cast<Epetra_MpiComm>(
00149         Teuchos::rcp(&globalComm->SubDomainComm(),false)
00150         );
00151 
00152     // Create the single-point EpetraExt::ModelEvaluator object
00153     Teuchos::RCP<EpetraMultiPointModelEval4DOpt>
00154       epetraModel = Teuchos::rcp(new EpetraMultiPointModelEval4DOpt(epetra_comm,xt0,xt1,pt0,pt1,d,x00,x01,p00,p01, q0));
00155     epetraModel->set_p_bounds(pL0,pL1,pU0,pU1);
00156     epetraModel->set_x_bounds(xL0,xL1,xU0,xU1);
00157 
00158     // Fill a vector of pointers to initial guesses for each point
00159     std::vector<Epetra_Vector*> multi_x_init(myPoints);
00160     Epetra_Vector init_vec(*(epetraModel->get_x_init().get()));
00161     for (int i=0; i<myPoints; i++) { multi_x_init[i] = &init_vec;}
00162 
00163     // Fill a vector of pointers to parameter vectors that define the multiple points
00164     Teuchos::RCP<std::vector< Teuchos::RCP<Epetra_Vector> > >  q_vec
00165         = Teuchos::rcp(new std::vector< Teuchos::RCP<Epetra_Vector> >(myPoints));
00166     for (int i=0; i<myPoints; i++) {
00167        q_vec->operator[](i) = Teuchos::rcp(new Epetra_Vector( *(epetraModel->get_p_map(1))));
00168        q_vec->operator[](i)->operator[](0) = 0.0 + 0.1*(i+myFirstPoint);
00169     }
00170 
00171     // Create the EpetraExt::MultiPointModelEvaluator object
00172     Teuchos::RCP<EpetraExt::MultiPointModelEvaluator>
00173       multiPointModel = Teuchos::rcp(new EpetraExt::MultiPointModelEvaluator(
00174                              epetraModel, globalComm, multi_x_init, q_vec));
00175 
00176     // Create the Thyra::EpetraModelEvaluator object
00177 
00178     Teuchos::RCP<Thyra::LinearOpWithSolveFactoryBase<double> >
00179       lowsFactory = lowsfCreator.createLinearSolveStrategy("");
00180 
00181     Teuchos::RCP<Thyra::EpetraModelEvaluator>
00182       epetraThyraModel = rcp(new Thyra::EpetraModelEvaluator());
00183     
00184     epetraThyraModel->initialize(multiPointModel,lowsFactory);
00185     
00186     //
00187     // Solve the NLP
00188     //
00189     
00190     // Set the model
00191     solver.setModel(epetraThyraModel);
00192 
00193     // Read the initial guess if one exists
00194     solver.readInitialGuess(out.get());
00195 
00196     // Solve the NLP
00197     const MoochoSolver::ESolutionStatus solution_status = solver.solve();
00198 
00199     // Write the parameters that where read
00200     lowsfCreator.writeParamsFile(*lowsFactory);
00201     solver.writeParamsFile();
00202     
00203     //
00204     // Return the solution status (0 if sucessfull)
00205     //
00206 
00207     return solution_status;
00208 
00209   }
00210   TEUCHOS_STANDARD_CATCH_STATEMENTS(true,*out,dummySuccess)
00211 
00212   return MoochoSolver::SOLVE_RETURN_EXCEPTION;
00213 
00214 }
00215 #else
00216 {
00217   cout << "NLPThyraEpetraModelEval4DOpt does nothing when HAVE_MPI is not defined" << endl;
00218   return 0;
00219 }
00220 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines