10 #include "EpetraModelEval2DSim.hpp"
11 #include "EpetraModelEval4DOpt.hpp"
13 #include "Thyra_DefaultModelEvaluatorWithSolveFactory.hpp"
14 #include "Stratimikos_DefaultLinearSolverBuilder.hpp"
15 #include "Thyra_DampenedNewtonNonlinearSolver.hpp"
26 createScalingVec(
const double &scale,
const Epetra_Map &map)
29 scalingVec->PutScalar(scale);
34 void scaleEpetraModelEvaluator(
const double &s_x,
const double &s_f,
54 int main(
int argc,
char* argv[] )
60 using Teuchos::outArg;
61 typedef RCP<Thyra::VectorBase<double> > VectorPtr;
71 CommandLineProcessor clp;
73 clp.addOutputSetupOptions(
true);
76 "This example program solves a simple 2 x 2 set of nonlinear equations using a simple\n"
77 "dampened Newton method.\n\n"
79 "The equations that are solved are:\n\n"
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"
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"
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"
95 clp.setOption(
"d", &d,
"Model constant d" );
97 clp.setOption(
"p0", &p0,
"Model constant p[0]" );
99 clp.setOption(
"p1", &p1,
"Model constant p[1]" );
101 clp.setOption(
"x00", &x00,
"Initial guess for x[0]" );
103 clp.setOption(
"x01", &x01,
"Initial guess for x[1]" );
105 setVerbosityLevelOption(
"verb-level", &verbLevel,
"Verbosity level.", &clp );
107 clp.setOption(
"tol", &tol,
"Nonlinear solve tolerance" );
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)" );
123 clp.setOption(
"x-scale", &s_x,
"State variables scaling." );
125 clp.setOption(
"f-scale", &s_f,
"State function scaling." );
127 CommandLineProcessor::EParseCommandLineReturn
128 parse_return = clp.parse(argc,argv,&std::cerr);
130 if( parse_return != CommandLineProcessor::PARSE_SUCCESSFUL )
133 RCP<Teuchos::FancyOStream>
134 out = Teuchos::VerboseObjectBase::getDefaultOStream();
136 *out <<
"\nCreating the nonlinear equations object ...\n";
138 RCP<EpetraExt::ModelEvaluator> epetraModel;
140 epetraModel =
rcp(
new EpetraModelEval4DOpt(0.0,0.0,p0,p1,d,x00,x01,p0,p1));
143 epetraModel =
rcp(
new EpetraModelEval2DSim(d,p0,p1,x00,x01,showGetInvalidArg));
146 *out <<
"\nCreating linear solver strategy ...\n";
148 Stratimikos::DefaultLinearSolverBuilder linearSolverBuilder;
149 linearSolverBuilder.setParameterList(Teuchos::parameterList());
150 RCP<Thyra::LinearOpWithSolveFactoryBase<double> >
151 lowsFactory = linearSolverBuilder.createLinearSolveStrategy(
"Amesos");
157 RCP<Thyra::EpetraModelEvaluator>
160 RCP<Thyra::ModelEvaluator<double> > thyraModel;
161 if(externalFactory) {
164 new Thyra::DefaultModelEvaluatorWithSolveFactory<double>(
165 epetraThyraModel, lowsFactory
170 epetraThyraModel->initialize(epetraModel, lowsFactory);
171 thyraModel = epetraThyraModel;
174 scaleEpetraModelEvaluator( s_x, s_f, epetraThyraModel.ptr() );
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()));
182 *out <<
"\nCreating the nonlinear solver and solving the equations ...\n\n";
184 Thyra::DampenedNewtonNonlinearSolver<double> newtonSolver;
185 newtonSolver.setVerbLevel(verbLevel);
187 VectorPtr x = createMember(thyraModel->get_x_space());
188 V_V( &*x, *thyraModel->getNominalValues().get_x() );
190 Thyra::SolveCriteria<double> solveCriteria;
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));
196 newtonSolver.setModel(thyraModel);
197 Thyra::SolveStatus<double>
198 solveStatus = Thyra::solve( newtonSolver, &*x, &solveCriteria );
200 *out <<
"\nNonlinear solver return status:\n";
205 *out <<
"\nFinal solution for x=\n" << *x;
210 return success ? 0 : 1;
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...