Debian Codegraph @

packages/nox/test/epetra/Thyra/Thyra_Rosenbrock_SafeguardedDirectionLineSearch.cpp

raw
1 // @HEADER
2 // *****************************************************************************
3 // NOX: An Object-Oriented Nonlinear Solver Package
4 //
5 // Copyright 2002 NTESS and the NOX contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9
10 #include <Teuchos_ConfigDefs.hpp>
11 #include <Teuchos_UnitTestHarness.hpp>
12 #include <Teuchos_TimeMonitor.hpp>
13
14 // NOX Objects
15 #include "NOX.H"
16 #include "NOX_Thyra.H"
17
18 // Trilinos Objects
19 #ifdef HAVE_MPI
20 #include "Epetra_MpiComm.h"
21 #else
22 #include "Epetra_SerialComm.h"
23 #endif
24 #include "Epetra_Map.h"
25 #include "Epetra_Vector.h"
26 #include "Epetra_RowMatrix.h"
27 #include "Epetra_CrsMatrix.h"
28 #include "Epetra_Map.h"
29 #include "Epetra_LinearProblem.h"
30 #include "AztecOO.h"
31
32 #include "Teuchos_ParameterList.hpp"
33 #include "Teuchos_Assert.hpp"
34
35 #include "Stratimikos_DefaultLinearSolverBuilder.hpp"
36 #include "Thyra_LinearOpWithSolveFactoryHelpers.hpp"
37 #include "Thyra_ScaledModelEvaluator.hpp"
38 #include "ModelEvaluatorRosenbrock.hpp"
39 #include "Thyra_SpmdVectorBase.hpp"
40 #include "Thyra_DefaultSpmdVectorSpace.hpp"
41
42 #include "NOX_PrePostOperator_RowSumScaling.H"
43 #include "Thyra_VectorBase.hpp"
44
45 #include "NOX_TOpEleWiseMinSwap.hpp"
46
47 using namespace std;
48
49 TEUCHOS_UNIT_TEST(rosenbrock, rtop_ele_wise_min_swap)
50 {
51 // Create a communicator for Epetra objects
52 #ifdef HAVE_MPI
53 Epetra_MpiComm Comm( MPI_COMM_WORLD );
54 #else
55 Epetra_SerialComm Comm;
56 #endif
57
58 TEST_ASSERT(Comm.NumProc() == 1);
59
60 Teuchos::RCP<RosenbrockModelEvaluator> thyraModel =
61 Teuchos::rcp(new RosenbrockModelEvaluator(Teuchos::rcp(&Comm,false)));
62
63 Teuchos::RCP< ::Thyra::VectorBase<double> > updateLimits = ::Thyra::createMember<double>(thyraModel->get_x_space());
64 ::Thyra::put_scalar(Teuchos::ScalarTraits<double>::rmax(),updateLimits.ptr());
65 ::Thyra::set_ele(1,0.5,updateLimits.ptr());
66 Teuchos::RCP< ::Thyra::VectorBase<double> > dir = ::Thyra::createMember<double>(thyraModel->get_x_space());
67 ::Thyra::set_ele(0,10.0,dir.ptr());
68 ::Thyra::set_ele(1,-8.0,dir.ptr());
69
70 ::Thyra::ele_wise_min_swap(*updateLimits,dir.ptr());
71
72 double i0 = ::Thyra::get_ele(*dir,0);
73 double i1 = ::Thyra::get_ele(*dir,1);
74 double tol = Teuchos::ScalarTraits<double>::eps() * 100.0;
75 TEST_FLOATING_EQUALITY(i0,10.0,tol);
76 TEST_FLOATING_EQUALITY(i1,-0.5,tol);
77 }
78
79 TEUCHOS_UNIT_TEST(rosenbrock, user_direction_change_ls)
80 {
81 int status = 0;
82
83 // Create a communicator for Epetra objects
84 #ifdef HAVE_MPI
85 Epetra_MpiComm Comm( MPI_COMM_WORLD );
86 #else
87 Epetra_SerialComm Comm;
88 #endif
89
90 TEST_ASSERT(Comm.NumProc() == 1);
91
92 ::Stratimikos::DefaultLinearSolverBuilder builder;
93
94 Teuchos::RCP<Teuchos::ParameterList> p =
95 Teuchos::rcp(new Teuchos::ParameterList);
96 {
97 p->set("Linear Solver Type", "AztecOO");
98 //p->set("Preconditioner Type", "Ifpack");
99 p->set("Preconditioner Type", "None");
100 Teuchos::ParameterList& az = p->sublist("Linear Solver Types").sublist("AztecOO");
101 az.sublist("Forward Solve").sublist("AztecOO Settings").set("Output Frequency", 1);
102 az.sublist("VerboseObject").set("Verbosity Level", "high");
103 Teuchos::ParameterList& ip = p->sublist("Preconditioner Types").sublist("Ifpack");
104 ip.sublist("VerboseObject").set("Verbosity Level", "high");
105 }
106
107 builder.setParameterList(p);
108
109 Teuchos::RCP< ::Thyra::LinearOpWithSolveFactoryBase<double> >
110 lowsFactory = builder.createLinearSolveStrategy("");
111
112 Teuchos::RCP<RosenbrockModelEvaluator> thyraModel =
113 Teuchos::rcp(new RosenbrockModelEvaluator(Teuchos::rcp(&Comm,false)));
114
115 thyraModel->set_W_factory(lowsFactory);
116
117 // Create nox parameter list
118 Teuchos::RCP<Teuchos::ParameterList> nl_params =
119 Teuchos::rcp(new Teuchos::ParameterList);
120 nl_params->set("Nonlinear Solver", "Line Search Based");
121 nl_params->sublist("Line Search").set("Method", "Safeguarded Direction");
122 Teuchos::RCP< ::Thyra::VectorBase<double> > updateLimits = ::Thyra::createMember<double>(thyraModel->get_x_space());
123 ::Thyra::put_scalar(Teuchos::ScalarTraits<double>::rmax(),updateLimits.ptr());
124 ::Thyra::set_ele(1,0.25,updateLimits.ptr());
125 Teuchos::RCP<NOX::Abstract::Vector> noxUpdateLimits = Teuchos::rcp(new NOX::Thyra::Vector(updateLimits));
126 nl_params->sublist("Line Search").sublist("Safeguarded Direction").set("Update Limit Vector",noxUpdateLimits);
127
128 Teuchos::ParameterList& printParams = nl_params->sublist("Printing");
129 printParams.set("Output Information",
130 NOX::Utils::OuterIteration +
131 NOX::Utils::OuterIterationStatusTest +
132 NOX::Utils::InnerIteration +
133 NOX::Utils::LinearSolverDetails +
134 NOX::Utils::Parameters +
135 NOX::Utils::Details +
136 NOX::Utils::Warning +
137 NOX::Utils::Debug +
138 NOX::Utils::TestDetails +
139 NOX::Utils::Error);
140
141 nl_params->sublist("Solver Options").set("Status Test Check Type", "Complete");
142
143 // Enable row sum scaling
144 nl_params->sublist("Thyra Group Options").set("Function Scaling", "Row Sum");
145
146 // Create Status Tests
147 {
148 Teuchos::ParameterList& st = nl_params->sublist("Status Tests");
149 st.set("Test Type", "Combo");
150 st.set("Combo Type", "OR");
151 st.set("Number of Tests", 3);
152
153 {
154 Teuchos::ParameterList& conv = st.sublist("Test 0");
155 conv.set("Test Type", "Combo");
156 conv.set("Combo Type", "AND");
157 conv.set("Number of Tests", 2);
158
159 Teuchos::ParameterList& normF_rel = conv.sublist("Test 0");
160 normF_rel.set("Test Type", "NormF");
161 normF_rel.set("Tolerance", 1.0e-8);
162
163 Teuchos::ParameterList& normWRMS = conv.sublist("Test 1");
164 normWRMS.set("Test Type", "NormWRMS");
165 normWRMS.set("Absolute Tolerance", 1.0e-8);
166 normWRMS.set("Relative Tolerance", 1.0e-5);
167 normWRMS.set("Tolerance", 1.0);
168 normWRMS.set("BDF Multiplier", 1.0);
169 normWRMS.set("Alpha", 1.0);
170 normWRMS.set("Beta", 0.5);
171 normWRMS.set("Disable Implicit Weighting", true);
172 }
173
174 {
175 Teuchos::ParameterList& fv = st.sublist("Test 1");
176 fv.set("Test Type", "FiniteValue");
177 fv.set("Vector Type", "F Vector");
178 fv.set("Norm Type", "Two Norm");
179 }
180
181 {
182 Teuchos::ParameterList& maxiters = st.sublist("Test 2");
183 maxiters.set("Test Type", "MaxIters");
184 maxiters.set("Maximum Iterations", 20);
185 }
186
187 }
188
189 // Create a Thyra nonlinear solver
190 Teuchos::RCP< ::Thyra::NonlinearSolverBase<double> > solver =
191 Teuchos::rcp(new ::Thyra::NOXNonlinearSolver);
192
193 solver->setParameterList(nl_params);
194 solver->setModel(thyraModel);
195
196 Teuchos::RCP< ::Thyra::VectorBase<double> >
197 initial_guess = thyraModel->getNominalValues().get_x()->clone_v();
198 ::Thyra::set_ele(1,0.0,initial_guess.ptr()); // forces more nonlinear steps than undamped case
199
200 ::Thyra::SolveCriteria<double> solve_criteria;
201 ::Thyra::SolveStatus<double> solve_status;
202
203 solve_status = solver->solve(initial_guess.get(), &solve_criteria);
204
205
206 Teuchos::RCP< ::Thyra::NOXNonlinearSolver> thyra_nox_solver =
207 Teuchos::rcp_dynamic_cast< ::Thyra::NOXNonlinearSolver>(solver);
208 TEST_EQUALITY(thyra_nox_solver->getNOXSolver()->getNumIterations(), 7);
209
210 Teuchos::RCP<const Epetra_Vector> x_analytic = thyraModel->get_analytic_solution();
211
212 Teuchos::RCP<const NOX::Abstract::Vector> x = thyra_nox_solver->getNOXSolver()->getSolutionGroup().getXPtr();
213
214 Teuchos::RCP<const NOX::Thyra::Vector> nox_thyra_x =
215 Teuchos::rcp_dynamic_cast<const NOX::Thyra::Vector>(x,true);
216
217 Teuchos::RCP<const Thyra::SpmdVectorBase<double> > spmd_x =
218 Teuchos::rcp_dynamic_cast<const Thyra::SpmdVectorBase<double> >(nox_thyra_x->getThyraRCPVector(),true);
219
220 Teuchos::ArrayRCP<const double> local_values;
221 spmd_x->getLocalData(outArg(local_values));
222
223 double tol = 1.0e-8;
224 TEST_FLOATING_EQUALITY((*x_analytic)[0],local_values[0],tol);
225 TEST_FLOATING_EQUALITY((*x_analytic)[1],local_values[1],tol);
226
227 if (solve_status.solveStatus == ::Thyra::SOLVE_STATUS_CONVERGED)
228 std::cout << "Test passed!" << std::endl;
229
230 // std::cout << *p << std::endl;
231
232 Teuchos::TimeMonitor::summarize();
233
234 // Final return value (0 = successfull, non-zero = failure)
235 TEST_ASSERT(status == 0);
236 }