Thyra Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ForwardSolveEpetraModelEval2DSimMain.cpp
Go to the documentation of this file.
1 // @HEADER
2 // *****************************************************************************
3 // Thyra: Interfaces and Support for Abstract Numerical Algorithms
4 //
5 // Copyright 2004 NTESS and the Thyra contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9 
10 #include "EpetraModelEval2DSim.hpp"
11 #include "EpetraModelEval4DOpt.hpp"
13 #include "Thyra_DefaultModelEvaluatorWithSolveFactory.hpp"
14 #include "Stratimikos_DefaultLinearSolverBuilder.hpp"
15 #include "Thyra_DampenedNewtonNonlinearSolver.hpp"
20 
21 
22 namespace {
23 
24 
26 createScalingVec(const double &scale, const Epetra_Map &map)
27 {
28  Teuchos::RCP<Epetra_Vector> scalingVec = Teuchos::rcp(new Epetra_Vector(map));
29  scalingVec->PutScalar(scale);
30  return scalingVec;
31 }
32 
33 
34 void scaleEpetraModelEvaluator( const double &s_x, const double &s_f,
36  )
37 {
38  if (s_x != 1.0) {
40  createScalingVec(s_x, *model->getEpetraModel()->get_x_map())
41  );
42  }
43  if (s_f != 1.0) {
45  createScalingVec(s_f, *model->getEpetraModel()->get_f_map())
46  );
47  }
48 }
49 
50 
51 } // namespace
52 
53 
54 int main( int argc, char* argv[] )
55 {
56 
57  using Teuchos::RCP;
58  using Teuchos::rcp;
60  using Teuchos::outArg;
61  typedef RCP<Thyra::VectorBase<double> > VectorPtr;
62 
63  bool success = true;
64 
65  try {
66 
67  //
68  // Get options from the command line
69  //
70 
71  CommandLineProcessor clp;
72  clp.throwExceptions(false);
73  clp.addOutputSetupOptions(true);
74 
75  clp.setDocString(
76  "This example program solves a simple 2 x 2 set of nonlinear equations using a simple\n"
77  "dampened Newton method.\n\n"
78 
79  "The equations that are solved are:\n\n"
80 
81  " f[0] = x[0] + x[1]*x[1] - p[0];\n"
82  " f[1] = d * ( x[0]*x[0] - x[1] - p[1] );\n\n"
83 
84  "The Jacobian for these equations is nonsingular for every point except x=(-0.5,0.5)\n"
85  "and x=(0.5,-0.5) You can cause the Jacobian to be singular at the solution by setting\n"
86  "p[0]=x[0]+x[1]*x[1] and p[1] = x[0]*x[0]-x[1] for these values of x.\n\n"
87 
88  "The equations are solved using a simple dampended Newton method that uses a Armijo\n"
89  "line search which is implemented in the general class Thyra::DampenedNewtonNonlinearsolver\n"
90  "You can get different levels of detail about the Newton method by adjustingthe command-line\n"
91  "option \"verb-level\" (see above)\n"
92  );
93 
94  double d = 10.0;
95  clp.setOption( "d", &d, "Model constant d" );
96  double p0 = 2.0;
97  clp.setOption( "p0", &p0, "Model constant p[0]" );
98  double p1 = 0.0;
99  clp.setOption( "p1", &p1, "Model constant p[1]" );
100  double x00 = 0.0;
101  clp.setOption( "x00", &x00, "Initial guess for x[0]" );
102  double x01 = 1.0;
103  clp.setOption( "x01", &x01, "Initial guess for x[1]" );
105  setVerbosityLevelOption( "verb-level", &verbLevel, "Verbosity level.", &clp );
106  double tol = 1e-10;
107  clp.setOption( "tol", &tol, "Nonlinear solve tolerance" );
108  int maxIters = 100;
109  clp.setOption( "max-iters", &maxIters, "Maximum number of nonlinear iterations" );
110  bool use4DOpt = false;
111  clp.setOption( "use-4D-opt", "use-2D-sim", &use4DOpt,
112  "Determines if the EpetraModelEval4DOpt or EpetraModelEval2DSim subclasses are used" );
113  bool externalFactory = false;
114  clp.setOption( "external-lowsf", "internal-lowsf", &externalFactory,
115  "Determines of the Thyra::LinearOpWithSolveFactory is used externally or internally to the Thyra::EpetraModelEvaluator object" );
116  bool showSetInvalidArg = false;
117  clp.setOption( "show-set-invalid-arg", "no-show-set-invalid-arg", &showSetInvalidArg,
118  "Determines if an attempt is made to set an invalid/unsupported ModelEvaluator input argument" );
119  bool showGetInvalidArg = false;
120  clp.setOption( "show-get-invalid-arg", "no-show-get-invalid-arg", &showGetInvalidArg,
121  "Determines if an attempt is made to get an invalid/unsupported ModelEvaluator output argument (2DSim only)" );
122  double s_x = 1.0;
123  clp.setOption( "x-scale", &s_x, "State variables scaling." );
124  double s_f = 1.0;
125  clp.setOption( "f-scale", &s_f, "State function scaling." );
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  RCP<Teuchos::FancyOStream>
134  out = Teuchos::VerboseObjectBase::getDefaultOStream();
135 
136  *out << "\nCreating the nonlinear equations object ...\n";
137 
138  RCP<EpetraExt::ModelEvaluator> epetraModel;
139  if(use4DOpt) {
140  epetraModel = rcp(new EpetraModelEval4DOpt(0.0,0.0,p0,p1,d,x00,x01,p0,p1));
141  }
142  else {
143  epetraModel = rcp(new EpetraModelEval2DSim(d,p0,p1,x00,x01,showGetInvalidArg));
144  }
145 
146  *out << "\nCreating linear solver strategy ...\n";
147 
148  Stratimikos::DefaultLinearSolverBuilder linearSolverBuilder;
149  linearSolverBuilder.setParameterList(Teuchos::parameterList());
150  RCP<Thyra::LinearOpWithSolveFactoryBase<double> >
151  lowsFactory = linearSolverBuilder.createLinearSolveStrategy("Amesos");
152  // Above, we are just using the stratimkikos class
153  // DefaultLinearSolverBuilder to create a default Amesos solver
154  // (typically KLU) with a default set of options. By setting a parameter
155  // list on linearSolverBuilder, you build from a number of solvers.
156 
157  RCP<Thyra::EpetraModelEvaluator>
158  epetraThyraModel = rcp(new Thyra::EpetraModelEvaluator());
159 
160  RCP<Thyra::ModelEvaluator<double> > thyraModel;
161  if(externalFactory) {
162  epetraThyraModel->initialize(epetraModel, Teuchos::null);
163  thyraModel = rcp(
164  new Thyra::DefaultModelEvaluatorWithSolveFactory<double>(
165  epetraThyraModel, lowsFactory
166  )
167  );
168  }
169  else {
170  epetraThyraModel->initialize(epetraModel, lowsFactory);
171  thyraModel = epetraThyraModel;
172  }
173 
174  scaleEpetraModelEvaluator( s_x, s_f, epetraThyraModel.ptr() );
175 
176  if( showSetInvalidArg ) {
177  *out << "\nAttempting to set an invalid input argument that throws an exception ...\n\n";
178  Thyra::ModelEvaluatorBase::InArgs<double> inArgs = thyraModel->createInArgs();
179  inArgs.set_x_dot(createMember(thyraModel->get_x_space()));
180  }
181 
182  *out << "\nCreating the nonlinear solver and solving the equations ...\n\n";
183 
184  Thyra::DampenedNewtonNonlinearSolver<double> newtonSolver; // Set defaults
185  newtonSolver.setVerbLevel(verbLevel);
186 
187  VectorPtr x = createMember(thyraModel->get_x_space());
188  V_V( &*x, *thyraModel->getNominalValues().get_x() );
189 
190  Thyra::SolveCriteria<double> solveCriteria; // Sets defaults
191  solveCriteria.solveMeasureType.set(Thyra::SOLVE_MEASURE_NORM_RESIDUAL,Thyra::SOLVE_MEASURE_NORM_RHS);
192  solveCriteria.requestedTol = tol;
193  solveCriteria.extraParameters = Teuchos::parameterList("Nonlinear Solve");
194  solveCriteria.extraParameters->set("Max Iters",int(maxIters));
195 
196  newtonSolver.setModel(thyraModel);
197  Thyra::SolveStatus<double>
198  solveStatus = Thyra::solve( newtonSolver, &*x, &solveCriteria );
199 
200  *out << "\nNonlinear solver return status:\n";
201  {
202  Teuchos::OSTab tab(out);
203  *out << solveStatus;
204  }
205  *out << "\nFinal solution for x=\n" << *x;
206 
207  }
208  TEUCHOS_STANDARD_CATCH_STATEMENTS(true,std::cerr,success)
209 
210  return success ? 0 : 1;
211 }
RCP< const EpetraExt::ModelEvaluator > getEpetraModel() const
void setStateVariableScalingVec(const RCP< const Epetra_Vector > &stateVariableScalingVec)
Set the state variable scaling vector s_x (see above).
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
void setStateFunctionScalingVec(const RCP< const Epetra_Vector > &stateFunctionScalingVec)
Set the state function scaling vector s_f (see above).
#define TEUCHOS_STANDARD_CATCH_STATEMENTS(VERBOSE, ERR_STREAM, SUCCESS_FLAG)
int main(int argc, char *argv[])
void throwExceptions(const bool &throwExceptions)
Concrete Adapter subclass that takes an EpetraExt::ModelEvaluator object and wraps it as a Thyra::Mod...