Debian Codegraph @

packages/nox/test/epetra/Thyra/Thyra_RowSumScaling.C

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 // NOX Objects
11 #include "NOX.H"
12 #include "NOX_Thyra.H"
13
14 // Trilinos Objects
15 #ifdef HAVE_MPI
16 #include "Epetra_MpiComm.h"
17 #else
18 #include "Epetra_SerialComm.h"
19 #endif
20 #include "Epetra_Map.h"
21 #include "Epetra_Vector.h"
22 #include "Epetra_RowMatrix.h"
23 #include "Epetra_CrsMatrix.h"
24 #include "Epetra_Map.h"
25 #include "Epetra_LinearProblem.h"
26 #include "AztecOO.h"
27
28 #include "Teuchos_ParameterList.hpp"
29 #include "Teuchos_RCP.hpp"
30 #include "Teuchos_GlobalMPISession.hpp"
31 #include "Teuchos_CommandLineProcessor.hpp"
32 #include "Teuchos_Assert.hpp"
33 #include "Teuchos_TimeMonitor.hpp"
34 #include "Teuchos_StandardCatchMacros.hpp"
35
36 #include "Stratimikos_DefaultLinearSolverBuilder.hpp"
37 #include "Thyra_LinearOpWithSolveFactoryHelpers.hpp"
38 #include "Thyra_ScaledModelEvaluator.hpp"
39 #include "ModelEvaluator2DSim.hpp"
40
41 #include "NOX_PrePostOperator_RowSumScaling.H"
42 #include "Thyra_VectorBase.hpp"
43
44 using namespace std;
45
46 int main(int argc, char *argv[])
47 {
48 Teuchos::GlobalMPISession mpiSession(&argc, &argv);
49
50 bool success = true;
51 bool verbose = true;
52 try {
53 // Parse the command line
54 using Teuchos::CommandLineProcessor;
55 CommandLineProcessor clp;
56 clp.throwExceptions(false);
57 clp.addOutputSetupOptions(true);
58 bool verbose = false;
59 clp.setOption( "v", "disable-verbosity", &verbose, "Enable verbosity" );
60
61 CommandLineProcessor::EParseCommandLineReturn
62 parse_return = clp.parse(argc,argv,&std::cerr);
63
64 if( parse_return != CommandLineProcessor::PARSE_SUCCESSFUL )
65 return parse_return;
66
67 if (verbose)
68 std::cout << "Verbosity Activated" << std::endl;
69 else
70 std::cout << "Verbosity Disabled" << std::endl;
71
72 // Create a communicator for Epetra objects
73 #ifdef HAVE_MPI
74 Epetra_MpiComm Comm( MPI_COMM_WORLD );
75 #else
76 Epetra_SerialComm Comm;
77 #endif
78
79 // Check we have only one processor since this problem doesn't work
80 // for more than one proc
81 if (Comm.NumProc() > 1) {
82 std::cerr << "Error! Problem can only be run with at most 1 processor!"
83 << std::endl;
84 return -1;
85 }
86
87 // Create the model evaluator object
88 double d = 10.0;
89 double p0 = 2.0;
90 double p1 = 0.0;
91 double x00 = 0.0;
92 double x01 = 1.0;
93 Teuchos::RCP<ModelEvaluator2DSim<double> > thyraModel =
94 Teuchos::rcp(new ModelEvaluator2DSim<double>(Teuchos::rcp(&Comm,false),
95 d,p0,p1,x00,x01));
96
97 ::Stratimikos::DefaultLinearSolverBuilder builder;
98
99 Teuchos::RCP<Teuchos::ParameterList> p =
100 Teuchos::rcp(new Teuchos::ParameterList);
101 {
102 p->set("Linear Solver Type", "AztecOO");
103 //p->set("Preconditioner Type", "Ifpack");
104 p->set("Preconditioner Type", "None");
105 Teuchos::ParameterList& az = p->sublist("Linear Solver Types").sublist("AztecOO");
106 az.sublist("Forward Solve").sublist("AztecOO Settings").set("Output Frequency", 1);
107 az.sublist("VerboseObject").set("Verbosity Level", "high");
108 Teuchos::ParameterList& ip = p->sublist("Preconditioner Types").sublist("Ifpack");
109 ip.sublist("VerboseObject").set("Verbosity Level", "high");
110 }
111
112 builder.setParameterList(p);
113
114 Teuchos::RCP< ::Thyra::LinearOpWithSolveFactoryBase<double> >
115 lowsFactory = builder.createLinearSolveStrategy("");
116
117 thyraModel->set_W_factory(lowsFactory);
118
119 // Create nox parameter list
120 Teuchos::RCP<Teuchos::ParameterList> nl_params =
121 Teuchos::rcp(new Teuchos::ParameterList);
122 nl_params->set("Nonlinear Solver", "Line Search Based");
123 nl_params->sublist("Line Search").set("Method", "Polynomial");
124
125 Teuchos::ParameterList& printParams = nl_params->sublist("Printing");
126 printParams.set("Output Information",
127 NOX::Utils::OuterIteration +
128 NOX::Utils::OuterIterationStatusTest +
129 NOX::Utils::InnerIteration +
130 NOX::Utils::LinearSolverDetails +
131 NOX::Utils::Parameters +
132 NOX::Utils::Details +
133 NOX::Utils::Warning +
134 NOX::Utils::Debug +
135 NOX::Utils::TestDetails +
136 NOX::Utils::Error);
137
138 nl_params->sublist("Solver Options").set("Status Test Check Type", "Complete");
139
140 // Enable row sum scaling
141 nl_params->sublist("Thyra Group Options").set("Function Scaling", "Row Sum");
142
143 // Create Status Tests
144 {
145 Teuchos::ParameterList& st = nl_params->sublist("Status Tests");
146 st.set("Test Type", "Combo");
147 st.set("Combo Type", "OR");
148 st.set("Number of Tests", 3);
149
150 {
151 Teuchos::ParameterList& conv = st.sublist("Test 0");
152 conv.set("Test Type", "Combo");
153 conv.set("Combo Type", "AND");
154 conv.set("Number of Tests", 2);
155
156 Teuchos::ParameterList& normF_rel = conv.sublist("Test 0");
157 normF_rel.set("Test Type", "RelativeNormF");
158 normF_rel.set("Tolerance", 1.0e-4);
159
160 Teuchos::ParameterList& normWRMS = conv.sublist("Test 1");
161 normWRMS.set("Test Type", "NormWRMS");
162 normWRMS.set("Absolute Tolerance", 1.0e-8);
163 normWRMS.set("Relative Tolerance", 1.0e-5);
164 normWRMS.set("Tolerance", 1.0);
165 normWRMS.set("BDF Multiplier", 1.0);
166 normWRMS.set("Alpha", 1.0);
167 normWRMS.set("Beta", 0.5);
168 normWRMS.set("Disable Implicit Weighting", true);
169 }
170
171 {
172 Teuchos::ParameterList& fv = st.sublist("Test 1");
173 fv.set("Test Type", "FiniteValue");
174 fv.set("Vector Type", "F Vector");
175 fv.set("Norm Type", "Two Norm");
176 }
177
178 {
179 Teuchos::ParameterList& maxiters = st.sublist("Test 2");
180 maxiters.set("Test Type", "MaxIters");
181 maxiters.set("Maximum Iterations", 20);
182 }
183
184 }
185
186 // Create a Thyra nonlinear solver
187 Teuchos::RCP< ::Thyra::NonlinearSolverBase<double> > solver =
188 Teuchos::rcp(new ::Thyra::NOXNonlinearSolver);
189
190 solver->setParameterList(nl_params);
191 solver->setModel(thyraModel);
192
193 Teuchos::RCP< ::Thyra::VectorBase<double> >
194 initial_guess = thyraModel->getNominalValues().get_x()->clone_v();
195
196 ::Thyra::SolveCriteria<double> solve_criteria;
197 ::Thyra::SolveStatus<double> solve_status;
198
199 solve_status = solver->solve(initial_guess.get(), &solve_criteria);
200
201 // Test total num iterations
202 {
203 // Problem converges in 7 nonlinear iterations with NO scaling
204 // Problem converges in 6 nonlinear iterations with RS scaling (bad test problem - too easy)
205 Teuchos::RCP< ::Thyra::NOXNonlinearSolver> thyra_nox_solver =
206 Teuchos::rcp_dynamic_cast< ::Thyra::NOXNonlinearSolver>(solver);
207 TEUCHOS_ASSERT(thyra_nox_solver->getNOXSolver()->getNumIterations() == 6);
208 }
209
210 if (solve_status.solveStatus == ::Thyra::SOLVE_STATUS_CONVERGED) {
211 std::cout << "Test passed!" << std::endl;
212 success = true;
213 }
214 else {
215 success = false;
216 }
217
218 std::cout << *p << std::endl;
219
220 Teuchos::TimeMonitor::summarize();
221 }
222 TEUCHOS_STANDARD_CATCH_STATEMENTS(verbose, std::cerr, success);
223
224 return ( success ? EXIT_SUCCESS : EXIT_FAILURE );
225 }