Debian Codegraph @

packages/nox/test/epetra/NOX_Operators/test.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" // Required headers
12 #include "NOX_Epetra.H" // Epetra Interface headers
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 #include "Laplace2D.H"
32
33
34 int main(int argc, char *argv[])
35 {
36 // Initialize MPI
37 #ifdef HAVE_MPI
38 MPI_Init(&argc,&argv);
39 #endif
40
41 // Create a communicator for Epetra objects
42 #ifdef HAVE_MPI
43 Epetra_MpiComm Comm( MPI_COMM_WORLD );
44 #else
45 Epetra_SerialComm Comm;
46 #endif
47
48 bool verbose = false;
49 bool success = false;
50 try {
51 if (argc > 1)
52 if (argv[1][0]=='-' && argv[1][1]=='v')
53 verbose = true;
54
55 // Get the process ID and the total number of processors
56 int MyPID = Comm.MyPID();
57 #ifdef HAVE_MPI
58 int NumProc = Comm.NumProc();
59 #endif
60
61 // define the parameters of the nonlinear PDE problem
62 int nx = 5;
63 int ny = 6;
64 double lambda = 1.0;
65
66 PDEProblem Problem(nx,ny,lambda,&Comm);
67
68 // starting solution, here a zero vector
69 Epetra_Vector InitialGuess(Problem.GetMatrix()->Map());
70 InitialGuess.PutScalar(0.0);
71
72 // random vector upon which to apply each operator being tested
73 Epetra_Vector directionVec(Problem.GetMatrix()->Map());
74 directionVec.Random();
75
76 // Set up the problem interface
77 Teuchos::RCP<SimpleProblemInterface> interface =
78 Teuchos::rcp(new SimpleProblemInterface(&Problem) );
79
80 // Set up theolver options parameter list
81 Teuchos::RCP<Teuchos::ParameterList> noxParamsPtr = Teuchos::rcp(new Teuchos::ParameterList);
82 Teuchos::ParameterList & noxParams = *(noxParamsPtr.get());
83
84 // Set the nonlinear solver method
85 noxParams.set("Nonlinear Solver", "Line Search Based");
86
87 // Set up the printing utilities
88 // Only print output if the "-v" flag is set on the command line
89 Teuchos::ParameterList& printParams = noxParams.sublist("Printing");
90 printParams.set("MyPID", MyPID);
91 printParams.set("Output Precision", 5);
92 printParams.set("Output Processor", 0);
93 if( verbose )
94 printParams.set("Output Information",
95 NOX::Utils::OuterIteration +
96 NOX::Utils::OuterIterationStatusTest +
97 NOX::Utils::InnerIteration +
98 NOX::Utils::Parameters +
99 NOX::Utils::Details +
100 NOX::Utils::Warning +
101 NOX::Utils::TestDetails);
102 else
103 printParams.set("Output Information", NOX::Utils::Error +
104 NOX::Utils::TestDetails);
105
106 NOX::Utils printing(printParams);
107
108
109 // Identify the test problem
110 if (printing.isPrintType(NOX::Utils::TestDetails))
111 printing.out() << "Starting epetra/NOX_Operators/NOX_Operators.exe" << std::endl;
112
113 // Identify processor information
114 #ifdef HAVE_MPI
115 if (printing.isPrintType(NOX::Utils::TestDetails)) {
116 printing.out() << "Parallel Run" << std::endl;
117 printing.out() << "Number of processors = " << NumProc << std::endl;
118 printing.out() << "Print Process = " << MyPID << std::endl;
119 }
120 Comm.Barrier();
121 if (printing.isPrintType(NOX::Utils::TestDetails))
122 printing.out() << "Process " << MyPID << " is alive!" << std::endl;
123 Comm.Barrier();
124 #else
125 if (printing.isPrintType(NOX::Utils::TestDetails))
126 printing.out() << "Serial Run" << std::endl;
127 #endif
128
129 int status = 0;
130
131 Teuchos::RCP<NOX::Epetra::Interface::Required> iReq = interface;
132
133 // Need a NOX::Epetra::Vector for constructor
134 NOX::Epetra::Vector noxInitGuess(InitialGuess, NOX::DeepCopy);
135
136 // Analytic matrix
137 Teuchos::RCP<Epetra_CrsMatrix> A = Teuchos::rcp( Problem.GetMatrix(), false );
138
139 Epetra_Vector A_resultVec(Problem.GetMatrix()->Map());
140 interface->computeJacobian( InitialGuess, *A );
141 A->Apply( directionVec, A_resultVec );
142
143 // FD operator
144 Teuchos::RCP<Epetra_CrsGraph> graph = Teuchos::rcp( const_cast<Epetra_CrsGraph*>(&A->Graph()), false );
145 Teuchos::RCP<NOX::Epetra::FiniteDifference> FD = Teuchos::rcp(
146 new NOX::Epetra::FiniteDifference(printParams, iReq, noxInitGuess, graph) );
147
148 Epetra_Vector FD_resultVec(Problem.GetMatrix()->Map());
149 FD->computeJacobian(InitialGuess, *FD);
150 FD->Apply( directionVec, FD_resultVec );
151
152 // Matrix-Free operator
153 Teuchos::RCP<NOX::Epetra::MatrixFree> MF = Teuchos::rcp(
154 new NOX::Epetra::MatrixFree(printParams, iReq, noxInitGuess) );
155
156 Epetra_Vector MF_resultVec(Problem.GetMatrix()->Map());
157 MF->computeJacobian(InitialGuess, *MF);
158 MF->Apply( directionVec, MF_resultVec );
159
160 // Matrix-Free operator (backward difference)
161 Teuchos::RCP<NOX::Epetra::MatrixFree> MFb = Teuchos::rcp(
162 new NOX::Epetra::MatrixFree(printParams, iReq, noxInitGuess) );
163 MFb->setDifferenceMethod(NOX::Epetra::MatrixFree::Backward);
164
165 Epetra_Vector MFb_resultVec(Problem.GetMatrix()->Map());
166 MFb->computeJacobian(InitialGuess, *MFb);
167 MFb->Apply( directionVec, MFb_resultVec );
168
169 // Matrix-Free operator (centered difference)
170 Teuchos::RCP<NOX::Epetra::MatrixFree> MFc = Teuchos::rcp(
171 new NOX::Epetra::MatrixFree(printParams, iReq, noxInitGuess) );
172 MFc->setDifferenceMethod(NOX::Epetra::MatrixFree::Centered);
173
174 Epetra_Vector MFc_resultVec(Problem.GetMatrix()->Map());
175 MFc->computeJacobian(InitialGuess, *MFc);
176 MFc->Apply( directionVec, MFc_resultVec );
177
178 // Need NOX::Epetra::Vectors for tests
179 NOX::Epetra::Vector noxAvec ( A_resultVec , NOX::DeepCopy );
180 NOX::Epetra::Vector noxFDvec( FD_resultVec, NOX::DeepCopy );
181 NOX::Epetra::Vector noxMFvec( MF_resultVec, NOX::DeepCopy );
182 NOX::Epetra::Vector noxMFbvec( MFb_resultVec, NOX::DeepCopy );
183 NOX::Epetra::Vector noxMFcvec( MFc_resultVec, NOX::DeepCopy );
184
185 // Create a TestCompare class
186 NOX::Epetra::TestCompare tester( printing.out(), printing);
187 double abstol = 1.e-4;
188 double reltol = 1.e-4 ;
189 //NOX::TestCompare::CompareType aComp = NOX::TestCompare::Absolute;
190
191 status += tester.testVector( noxFDvec, noxAvec, reltol, abstol,
192 "Finite-Difference Operator Apply Test" );
193 status += tester.testVector( noxMFvec, noxAvec, reltol, abstol,
194 "Matrix-Free Operator Apply Test" );
195 status += tester.testVector( noxMFbvec, noxAvec, reltol, abstol,
196 "Matrix-Free Operator (Backward difference) Apply Test" );
197 status += tester.testVector( noxMFcvec, noxAvec, reltol, abstol,
198 "Matrix-Free Operator (Centered difference) Apply Test" );
199
200 success = status==0;
201
202 // Summarize test results
203 if(success)
204 printing.out() << "Test passed!" << std::endl;
205 else
206 printing.out() << "Test failed!" << std::endl;
207 }
208 TEUCHOS_STANDARD_CATCH_STATEMENTS(verbose, std::cerr, success);
209
210 #ifdef HAVE_MPI
211 MPI_Finalize();
212 #endif
213
214 return ( success ? EXIT_SUCCESS : EXIT_FAILURE );
215 }
216 /*
217 end of file test.C
218 */