Debian Codegraph @

packages/nox/test/epetra/NOX_Operators/test_BroydenOp.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 headers
11 #include "NOX.H"
12 #include "NOX_Epetra.H"
13 #include "NOX_TestCompare.H" // Test Suite headers
14 #include "NOX_Epetra_DebugTools.H"
15
16 // Trilinos headers
17 #ifdef HAVE_MPI
18 #include "Epetra_MpiComm.h"
19 #else
20 #include "Epetra_SerialComm.h"
21 #endif
22 #include "Epetra_Map.h"
23 #include "Epetra_Vector.h"
24 #include "Epetra_RowMatrix.h"
25 #include "Epetra_CrsMatrix.h"
26 #include "Epetra_Map.h"
27 #include "Epetra_LinearProblem.h"
28 #include "AztecOO.h"
29 #include "Teuchos_StandardCatchMacros.hpp"
30
31
32 int main(int argc, char *argv[])
33 {
34 // Initialize MPI
35 Teuchos::GlobalMPISession session(&argc, &argv, NULL);
36
37 bool success = false;
38 bool verbose = false;
39 try {
40 // Create a communicator for Epetra objects
41 #ifdef HAVE_MPI
42 Epetra_MpiComm Comm( MPI_COMM_WORLD );
43 #else
44 Epetra_SerialComm Comm;
45 #endif
46
47 int * testInt = new int[100];
48 delete [] testInt;
49
50 if (argc > 1)
51 if (argv[1][0]=='-' && argv[1][1]=='v')
52 verbose = true;
53
54 // Get the process ID and the total number of processors
55 int MyPID = Comm.MyPID();
56 int NumProc = Comm.NumProc();
57
58 // Set up theolver options parameter list
59 Teuchos::RCP<Teuchos::ParameterList> noxParamsPtr = Teuchos::rcp(new Teuchos::ParameterList);
60 Teuchos::ParameterList & noxParams = *(noxParamsPtr.get());
61
62 // Set up the printing utilities
63 // Only print output if the "-v" flag is set on the command line
64 Teuchos::ParameterList& printParams = noxParams.sublist("Printing");
65 printParams.set("MyPID", MyPID);
66 printParams.set("Output Precision", 5);
67 printParams.set("Output Processor", 0);
68 if( verbose )
69 printParams.set("Output Information",
70 NOX::Utils::OuterIteration +
71 NOX::Utils::OuterIterationStatusTest +
72 NOX::Utils::InnerIteration +
73 NOX::Utils::Parameters +
74 NOX::Utils::Details +
75 NOX::Utils::Warning +
76 NOX::Utils::TestDetails);
77 else
78 printParams.set("Output Information", NOX::Utils::Error +
79 NOX::Utils::TestDetails);
80
81 Teuchos::RCP<NOX::Utils> printing = Teuchos::rcp( new NOX::Utils(printParams) );
82
83 // Identify the test problem
84 if (printing->isPrintType(NOX::Utils::TestDetails))
85 printing->out() << "Starting epetra/NOX_Operators/NOX_BroydenOp.exe" << std::endl;
86
87 // Identify processor information
88 #ifdef HAVE_MPI
89 if (printing->isPrintType(NOX::Utils::TestDetails))
90 {
91 printing->out() << "Parallel Run" << std::endl;
92 printing->out() << "Number of processors = " << NumProc << std::endl;
93 printing->out() << "Print Process = " << MyPID << std::endl;
94 }
95 Comm.Barrier();
96 if (printing->isPrintType(NOX::Utils::TestDetails))
97 printing->out() << "Process " << MyPID << " is alive!" << std::endl;
98 Comm.Barrier();
99 #else
100 if (printing->isPrintType(NOX::Utils::TestDetails))
101 printing->out() << "Serial Run" << std::endl;
102 #endif
103
104 int status = 0;
105
106 // Create a TestCompare class
107 NOX::Epetra::TestCompare tester( printing->out(), *printing);
108 double abstol = 1.e-4;
109 double reltol = 1.e-4 ;
110
111 // Test NOX::Epetra::BroydenOperator
112 int numGlobalElems = 3 * NumProc;
113 Epetra_Map broydenRowMap ( numGlobalElems, 0, Comm );
114 Epetra_Vector broydenWorkVec ( broydenRowMap );
115 Epetra_CrsGraph broydenWorkGraph( Copy, broydenRowMap, 0 );
116 std::vector<int> globalIndices(3);
117 for( int lcol = 0; lcol < 3; ++lcol )
118 globalIndices[lcol] = 3 * MyPID + lcol;
119
120 std::vector<int> myGlobalIndices(2);
121
122 // Row 1 structure
123 myGlobalIndices[0] = globalIndices[0];
124 myGlobalIndices[1] = globalIndices[2];
125 broydenWorkGraph.InsertGlobalIndices( globalIndices[0], 2, &myGlobalIndices[0] );
126 // Row 2 structure
127 myGlobalIndices[0] = globalIndices[0];
128 myGlobalIndices[1] = globalIndices[1];
129 broydenWorkGraph.InsertGlobalIndices( globalIndices[1], 2, &myGlobalIndices[0] );
130 // Row 3 structure
131 myGlobalIndices[0] = globalIndices[1];
132 myGlobalIndices[1] = globalIndices[2];
133 broydenWorkGraph.InsertGlobalIndices( globalIndices[2], 2, &myGlobalIndices[0] );
134
135 broydenWorkGraph.FillComplete();
136
137 Teuchos::RCP<Epetra_CrsMatrix> broydenWorkMatrix =
138 Teuchos::rcp( new Epetra_CrsMatrix( Copy, broydenWorkGraph ) );
139
140 // Create an identity matrix
141 broydenWorkVec.PutScalar(1.0);
142 broydenWorkMatrix->ReplaceDiagonalValues(broydenWorkVec);
143
144 NOX::Epetra::BroydenOperator broydenOp( noxParams, printing, broydenWorkVec, broydenWorkMatrix, true );
145
146 broydenWorkVec[0] = 1.0;
147 broydenWorkVec[1] = -1.0;
148 broydenWorkVec[2] = 2.0;
149 broydenOp.setStepVector( broydenWorkVec );
150
151 broydenWorkVec[0] = 2.0;
152 broydenWorkVec[1] = 1.0;
153 broydenWorkVec[2] = 3.0;
154 broydenOp.setYieldVector( broydenWorkVec );
155
156 broydenOp.computeSparseBroydenUpdate();
157
158 // Create the gold matrix for comparison
159 Teuchos::RCP<Epetra_CrsMatrix> goldMatrix = Teuchos::rcp( new Epetra_CrsMatrix( Copy, broydenWorkGraph ) );
160
161 int numCols ;
162 double * values ;
163
164 // Row 1 answers
165 goldMatrix->ExtractMyRowView( 0, numCols, values );
166 values[0] = 6.0 ;
167 values[1] = 2.0 ;
168 // Row 2 answers
169 goldMatrix->ExtractMyRowView( 1, numCols, values );
170 values[0] = 5.0 ;
171 values[1] = 0.0 ;
172 // Row 3 structure
173 goldMatrix->ExtractMyRowView( 2, numCols, values );
174 values[0] = -1.0 ;
175 values[1] = 7.0 ;
176
177 goldMatrix->Scale(0.2);
178
179 status += tester.testCrsMatrices( broydenOp.getBroydenMatrix(), *goldMatrix, reltol, abstol,
180 "Broyden Sparse Operator Update Test" );
181
182
183 // Now try a dense Broyden Update
184 Epetra_CrsGraph broydenWorkGraph2( Copy, broydenRowMap, 0 );
185
186 myGlobalIndices.resize(3);
187
188 // All Rowsstructure
189 myGlobalIndices[0] = globalIndices[0];
190 myGlobalIndices[1] = globalIndices[1];
191 myGlobalIndices[2] = globalIndices[2];
192 broydenWorkGraph2.InsertGlobalIndices( globalIndices[0], 3, &myGlobalIndices[0] );
193 broydenWorkGraph2.InsertGlobalIndices( globalIndices[1], 3, &myGlobalIndices[0] );
194 broydenWorkGraph2.InsertGlobalIndices( globalIndices[2], 3, &myGlobalIndices[0] );
195
196 broydenWorkGraph2.FillComplete();
197
198 Teuchos::RCP<Epetra_CrsMatrix> broydenWorkMatrix2 = Teuchos::rcp( new Epetra_CrsMatrix( Copy, broydenWorkGraph2 ) );
199
200 // Create an identity matrix
201 broydenWorkVec.PutScalar(1.0);
202 broydenWorkMatrix2->ReplaceDiagonalValues(broydenWorkVec);
203
204 NOX::Epetra::BroydenOperator broydenOp2( noxParams, printing, broydenWorkVec, broydenWorkMatrix2, true );
205
206 broydenWorkVec[0] = 1.0;
207 broydenWorkVec[1] = -1.0;
208 broydenWorkVec[2] = 2.0;
209 broydenOp2.setStepVector( broydenWorkVec );
210
211 broydenWorkVec[0] = 2.0;
212 broydenWorkVec[1] = 1.0;
213 broydenWorkVec[2] = 3.0;
214 broydenOp2.setYieldVector( broydenWorkVec );
215
216 broydenOp2.computeSparseBroydenUpdate();
217
218 // Create the gold matrix for comparison
219 Teuchos::RCP<Epetra_CrsMatrix> goldMatrix2 = Teuchos::rcp( new Epetra_CrsMatrix( Copy, broydenWorkGraph2 ) );
220
221 // Row 1 answers
222 goldMatrix2->ExtractMyRowView( 0, numCols, values );
223 values[0] = 7.0 ;
224 values[1] = -1.0 ;
225 values[2] = 2.0 ;
226 // Row 2 answers
227 goldMatrix2->ExtractMyRowView( 1, numCols, values );
228 values[0] = 2.0 ;
229 values[1] = 4.0 ;
230 values[2] = 4.0 ;
231 // Row 3 structure
232 goldMatrix2->ExtractMyRowView( 2, numCols, values );
233 values[0] = 1.0 ;
234 values[1] = -1.0 ;
235 values[2] = 8.0 ;
236
237 double scaleF = 1.0 / 6.0;
238 goldMatrix2->Scale( scaleF );
239
240 status += tester.testCrsMatrices( broydenOp2.getBroydenMatrix(), *goldMatrix2, reltol, abstol,
241 "Broyden Sparse Operator Update Test (Dense)" );
242
243 // Now test the ability to remove active entries in the Broyden update
244 Epetra_CrsGraph inactiveGraph( Copy, broydenRowMap, 0 );
245
246 // Row 1 structure
247 inactiveGraph.InsertGlobalIndices( globalIndices[0], 1, &myGlobalIndices[1] );
248 // Row 2 structure
249 inactiveGraph.InsertGlobalIndices( globalIndices[1], 1, &myGlobalIndices[2] );
250 // Row 3 structure
251 inactiveGraph.InsertGlobalIndices( globalIndices[2], 1, &myGlobalIndices[0] );
252
253 inactiveGraph.FillComplete();
254
255 // Inactivate entries in dense matrix to arrive again at the original sparse structure
256 broydenOp2.removeEntriesFromBroydenUpdate( inactiveGraph );
257
258 #ifdef HAVE_NOX_DEBUG
259 if( verbose )
260 broydenOp2.outputActiveEntries();
261 #endif
262
263 // Reset to the identity matrix
264 broydenOp2.resetBroydenMatrix( *broydenWorkMatrix2 );
265
266 // Step and Yield vectors are already set
267 broydenOp2.computeSparseBroydenUpdate();
268
269 status += tester.testCrsMatrices( broydenOp2.getBroydenMatrix(), *goldMatrix, reltol, abstol,
270 "Broyden Sparse Operator Update Test (Entry Removal)", false );
271
272 success = status==0;
273
274 // Summarize test results
275 if(success)
276 printing->out() << "Test passed!" << std::endl;
277 else
278 printing->out() << "Test failed!" << std::endl;
279 }
280 TEUCHOS_STANDARD_CATCH_STATEMENTS(verbose, std::cerr, success);
281
282 return ( success ? EXIT_SUCCESS : EXIT_FAILURE );
283 }
284 /*
285 end of file test.C
286 */