MOOCHO (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
NLPThyraEpetraMultiPointModelEval4DOptMain.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 
45 #ifdef HAVE_MPI
47 #include "EpetraExt_MultiMpiComm.h"
48 #endif
51 #include "Stratimikos_DefaultLinearSolverBuilder.hpp"
52 #include "Thyra_EpetraModelEvaluator.hpp"
53 #include "Teuchos_RCP.hpp"
58 
59 int main( int argc, char* argv[] )
60 #ifdef HAVE_MPI
61 {
63  typedef AbstractLinAlgPack::value_type Scalar;
66 
67  bool dummySuccess = true;
68 
69  Teuchos::GlobalMPISession mpiSession(&argc,&argv);
70 
72  out = Teuchos::VerboseObjectBase::getDefaultOStream();
73 
74  try {
75 
76  Stratimikos::DefaultLinearSolverBuilder lowsfCreator;
77  MoochoThyraSolver solver;
78 
79  //
80  // Get options from the command line
81  //
82 
83  Scalar xt0 = 1.0;
84  Scalar xt1 = 1.0;
85  Scalar pt0 = 2.0;
86  Scalar pt1 = 0.0;
87  Scalar d = 10.0;
88  Scalar x00 = 1.0;
89  Scalar x01 = 1.0;
90  Scalar p00 = 2.0;
91  Scalar p01 = 0.0;
92  Scalar pL0 = -1e+50;
93  Scalar pL1 = -1e+50;
94  Scalar pU0 = +1e+50;
95  Scalar pU1 = +1e+50;
96  Scalar q0 = 0.0;
97 
98  Scalar xL0 = -1e+50;
99  Scalar xL1 = -1e+50;
100  Scalar xU0 = +1e+50;
101  Scalar xU1 = +1e+50;
102 
103  CommandLineProcessor clp(false); // Don't throw exceptions
104 
105  lowsfCreator.setupCLP(&clp);
106  solver.setupCLP(&clp);
107 
108  clp.setOption( "xt0", &xt0 );
109  clp.setOption( "xt1", &xt1 );
110  clp.setOption( "pt0", &pt0 );
111  clp.setOption( "pt1", &pt1 );
112  clp.setOption( "d", &d );
113  clp.setOption( "x00", &x00 );
114  clp.setOption( "x01", &x01 );
115  clp.setOption( "p00", &p00 );
116  clp.setOption( "q0", &q0 );
117  clp.setOption( "p01", &p01 );
118  clp.setOption( "pL0", &pL0 );
119  clp.setOption( "pL1", &pL1 );
120  clp.setOption( "pU0", &pU0 );
121  clp.setOption( "pU1", &pU1 );
122  clp.setOption( "xL0", &xL0 );
123  clp.setOption( "xL1", &xL1 );
124  clp.setOption( "xU0", &xU0 );
125  clp.setOption( "xU1", &xU1 );
126 
127  CommandLineProcessor::EParseCommandLineReturn
128  parse_return = clp.parse(argc,argv,&std::cerr);
129 
130  if( parse_return != CommandLineProcessor::PARSE_SUCCESSFUL )
131  return parse_return;
132 
133  lowsfCreator.readParameters(out.get());
134  solver.readParameters(out.get());
135 
136  //
137  // Create the NLP
138  //
139 
140  // Construct global and split (individual point) communicators
141  int numPoints = 20;
144  int myPoints = globalComm->NumTimeStepsOnDomain();
145  int myFirstPoint = globalComm->FirstTimeStepOnDomain();
146 
147  Teuchos::RCP<Epetra_MpiComm> epetra_comm =
148  Teuchos::rcp_dynamic_cast<Epetra_MpiComm>(
149  Teuchos::rcp(&globalComm->SubDomainComm(),false)
150  );
151 
152  // Create the single-point EpetraExt::ModelEvaluator object
154  epetraModel = Teuchos::rcp(new EpetraMultiPointModelEval4DOpt(epetra_comm,xt0,xt1,pt0,pt1,d,x00,x01,p00,p01, q0));
155  epetraModel->set_p_bounds(pL0,pL1,pU0,pU1);
156  epetraModel->set_x_bounds(xL0,xL1,xU0,xU1);
157 
158  // Fill a vector of pointers to initial guesses for each point
159  std::vector<Epetra_Vector*> multi_x_init(myPoints);
160  Epetra_Vector init_vec(*(epetraModel->get_x_init().get()));
161  for (int i=0; i<myPoints; i++) { multi_x_init[i] = &init_vec;}
162 
163  // Fill a vector of pointers to parameter vectors that define the multiple points
165  = Teuchos::rcp(new std::vector< Teuchos::RCP<Epetra_Vector> >(myPoints));
166  for (int i=0; i<myPoints; i++) {
167  q_vec->operator[](i) = Teuchos::rcp(new Epetra_Vector( *(epetraModel->get_p_map(1))));
168  q_vec->operator[](i)->operator[](0) = 0.0 + 0.1*(i+myFirstPoint);
169  }
170 
171  // Create the EpetraExt::MultiPointModelEvaluator object
174  epetraModel, globalComm, multi_x_init, q_vec));
175 
176  // Create the Thyra::EpetraModelEvaluator object
177 
179  lowsFactory = lowsfCreator.createLinearSolveStrategy("");
180 
182  epetraThyraModel = rcp(new Thyra::EpetraModelEvaluator());
183 
184  epetraThyraModel->initialize(multiPointModel,lowsFactory);
185 
186  //
187  // Solve the NLP
188  //
189 
190  // Set the model
191  solver.setModel(epetraThyraModel);
192 
193  // Read the initial guess if one exists
194  solver.readInitialGuess(out.get());
195 
196  // Solve the NLP
197  const MoochoSolver::ESolutionStatus solution_status = solver.solve();
198 
199  // Write the parameters that where read
200  lowsfCreator.writeParamsFile(*lowsFactory);
201  solver.writeParamsFile();
202 
203  //
204  // Return the solution status (0 if sucessfull)
205  //
206 
207  return solution_status;
208 
209  }
210  TEUCHOS_STANDARD_CATCH_STATEMENTS(true,*out,dummySuccess)
211 
212  return MoochoSolver::SOLVE_RETURN_EXCEPTION;
213 
214 }
215 #else
216 {
217  cout << "NLPThyraEpetraModelEval4DOpt does nothing when HAVE_MPI is not defined" << endl;
218  return 0;
219 }
220 #endif
virtual int FirstTimeStepOnDomain() const
virtual Epetra_Comm & SubDomainComm() const
virtual int NumTimeStepsOnDomain() const
T * get() const
Teuchos::RCP< const Epetra_Map > get_p_map(int l) const
MOOCHO NLP Solver class for models represented through Thyra::ModelEvaluator.
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
#define TEUCHOS_STANDARD_CATCH_STATEMENTS(VERBOSE, ERR_STREAM, SUCCESS_FLAG)
std::ostream * out
void set_p_bounds(double pL0, double pL1, double pU0, double pU1)
Universal interface to a MOOCHO solver.
int main(int argc, char *argv[])
void set_x_bounds(double xL0, double xL1, double xU0, double xU1)
#define MPI_COMM_WORLD
Definition: RTOp_mpi.h:68
Teuchos::RCP< const Epetra_Vector > get_x_init() const