| 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_TestError.H" // Test Suite headers |
| 14 |
#include "NOX_TestCompare.H" // Test Suite headers |
| 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 "Teuchos_StandardCatchMacros.hpp" |
| 25 |
|
| 26 |
int main(int argc, char *argv[]) { |
| 27 |
|
| 28 |
// Initialize MPI |
| 29 |
#ifdef HAVE_MPI |
| 30 |
MPI_Init(&argc,&argv); |
| 31 |
#endif |
| 32 |
|
| 33 |
// Create a communicator for Epetra objects |
| 34 |
#ifdef HAVE_MPI |
| 35 |
Epetra_MpiComm Comm( MPI_COMM_WORLD ); |
| 36 |
#else |
| 37 |
Epetra_SerialComm Comm; |
| 38 |
#endif |
| 39 |
|
| 40 |
bool verbose = false; |
| 41 |
bool success = false; |
| 42 |
try { |
| 43 |
int globalLength = 100; // This should suffice |
| 44 |
|
| 45 |
if (argc > 1) |
| 46 |
if (argv[1][0]=='-' && argv[1][1]=='v') |
| 47 |
verbose = true; |
| 48 |
|
| 49 |
// Get the process ID and the total number of processors |
| 50 |
int MyPID = Comm.MyPID(); |
| 51 |
|
| 52 |
// Set up the printing utilities |
| 53 |
Teuchos::RCP<Teuchos::ParameterList> noxParamsPtr = |
| 54 |
Teuchos::rcp(new Teuchos::ParameterList); |
| 55 |
Teuchos::ParameterList& noxParams = *(noxParamsPtr.get()); |
| 56 |
// Only print output if the "-v" flag is set on the command line |
| 57 |
Teuchos::ParameterList& printParams = noxParams.sublist("Printing"); |
| 58 |
printParams.set("MyPID", MyPID); |
| 59 |
printParams.set("Output Precision", 5); |
| 60 |
printParams.set("Output Processor", 0); |
| 61 |
if( verbose ) |
| 62 |
printParams.set("Output Information", |
| 63 |
NOX::Utils::OuterIteration + |
| 64 |
NOX::Utils::OuterIterationStatusTest + |
| 65 |
NOX::Utils::InnerIteration + |
| 66 |
NOX::Utils::Parameters + |
| 67 |
NOX::Utils::Details + |
| 68 |
NOX::Utils::Warning + |
| 69 |
NOX::Utils::TestDetails); |
| 70 |
else |
| 71 |
printParams.set("Output Information", NOX::Utils::Error); |
| 72 |
|
| 73 |
NOX::Utils printing(printParams); |
| 74 |
|
| 75 |
// Identify the test problem |
| 76 |
if (printing.isPrintType(NOX::Utils::TestDetails)) |
| 77 |
printing.out() << "Starting epetra/NOX_Vector/NOX_Vector.exe" << std::endl; |
| 78 |
|
| 79 |
// Create a TestCompare class |
| 80 |
NOX::TestCompare tester( printing.out(), printing); |
| 81 |
double tolerance = 1.e-12; |
| 82 |
NOX::TestCompare::CompareType aComp = NOX::TestCompare::Absolute; |
| 83 |
|
| 84 |
// Identify processor information |
| 85 |
#ifdef HAVE_MPI |
| 86 |
printing.out() << "Parallel Run" << std::endl; |
| 87 |
printing.out() << "Number of processors = " << Comm.NumProc() << std::endl; |
| 88 |
printing.out() << "Print Process = " << MyPID << std::endl; |
| 89 |
Comm.Barrier(); |
| 90 |
if (printing.isPrintType(NOX::Utils::TestDetails)) |
| 91 |
printing.out() << "Process " << MyPID << " is alive!" << std::endl; |
| 92 |
Comm.Barrier(); |
| 93 |
#else |
| 94 |
printing.out() << "Serial Run" << std::endl; |
| 95 |
#endif |
| 96 |
|
| 97 |
// Create a map describing data distribution |
| 98 |
Epetra_Map * standardMap = new Epetra_Map(globalLength, 0, Comm); |
| 99 |
|
| 100 |
// Return value |
| 101 |
int status = 0; |
| 102 |
|
| 103 |
// *** Start Testing Here!!! *** |
| 104 |
|
| 105 |
// First create the Epetra_Vector needed to construct our NOX vector |
| 106 |
Epetra_Vector * epetraVec = new Epetra_Vector(*standardMap, true); |
| 107 |
|
| 108 |
NOX::Epetra::Vector * noxVec1 = new NOX::Epetra::Vector(*epetraVec, NOX::DeepCopy); |
| 109 |
delete epetraVec; epetraVec = 0; |
| 110 |
|
| 111 |
NOX::Epetra::Vector * noxVec2 = new NOX::Epetra::Vector(*noxVec1); |
| 112 |
noxVec2->init(1.0); |
| 113 |
|
| 114 |
// Test our norms |
| 115 |
NOX::Abstract::Vector::NormType |
| 116 |
oneNorm = NOX::Abstract::Vector::OneNorm, |
| 117 |
twoNorm = NOX::Abstract::Vector::TwoNorm, |
| 118 |
infNorm = NOX::Abstract::Vector::MaxNorm; |
| 119 |
|
| 120 |
double expectedOneNorm = (double) globalLength, |
| 121 |
expectedTwoNorm = sqrt( (double) globalLength), |
| 122 |
expectedInfNorm = 1.0; |
| 123 |
|
| 124 |
status += tester.testValue( noxVec2->norm(oneNorm), expectedOneNorm, |
| 125 |
tolerance, "One-Norm Test", aComp); |
| 126 |
status += tester.testValue( noxVec2->norm(twoNorm), expectedTwoNorm, |
| 127 |
tolerance, "Two-Norm Test", aComp); |
| 128 |
status += tester.testValue( noxVec2->norm(infNorm), expectedInfNorm, |
| 129 |
tolerance, "Max-Norm Test", aComp); |
| 130 |
|
| 131 |
|
| 132 |
// Test random, reciprocal and dot methods |
| 133 |
noxVec1->random(); |
| 134 |
// Threshold values since we want to do a reciprocal |
| 135 |
int myLength = standardMap->NumMyElements(); |
| 136 |
for( int i = 0; i < myLength; ++i ) |
| 137 |
if( fabs(noxVec1->getEpetraVector()[i]) < 1.e-8 ) noxVec1->getEpetraVector()[i] = 1.e-8; |
| 138 |
|
| 139 |
noxVec2->reciprocal(*noxVec1); |
| 140 |
double product = noxVec1->innerProduct(*noxVec2); |
| 141 |
|
| 142 |
status += tester.testValue( product, expectedOneNorm, |
| 143 |
tolerance, "Random, Reciprocal and Dot Test", aComp); |
| 144 |
|
| 145 |
|
| 146 |
// Test abs and weighted-norm methods |
| 147 |
/* ---------------------------- |
| 148 |
NOT SUPPORTED AT THIS TIME |
| 149 |
---------------------------- |
| 150 |
noxVec2->abs(*noxVec2); |
| 151 |
double wNorm = noxVec1->norm(*noxVec2); |
| 152 |
status += tester.testValue( wNorm, noxVec1->norm(oneNorm), |
| 153 |
tolerance, "Abs and Weighted-Norm Test", aComp); |
| 154 |
*/ |
| 155 |
|
| 156 |
// Test operator= , abs, update and scale methods |
| 157 |
(*noxVec2) = (*noxVec1); |
| 158 |
noxVec2->abs(*noxVec2); |
| 159 |
double sumAll = noxVec1->norm(oneNorm); |
| 160 |
noxVec2->update( 1.0, *noxVec1, 1.0 ); |
| 161 |
noxVec2->scale(0.5); |
| 162 |
double sumPositive = noxVec2->norm(oneNorm); |
| 163 |
(*noxVec2) = (*noxVec1); |
| 164 |
noxVec2->abs(*noxVec2); |
| 165 |
noxVec2->update( 1.0, *noxVec1, -1.0 ); |
| 166 |
noxVec2->scale(0.5); |
| 167 |
double sumNegative = noxVec2->norm(oneNorm); |
| 168 |
|
| 169 |
status += tester.testValue( (sumPositive + sumNegative), sumAll, |
| 170 |
tolerance, "Abs, Operator= , Update and Scale Test", aComp); |
| 171 |
|
| 172 |
success = status==0; |
| 173 |
|
| 174 |
if (success) |
| 175 |
printing.out() << "Test passed!" << std::endl; |
| 176 |
else |
| 177 |
printing.out() << "Test failed!" << std::endl; |
| 178 |
|
| 179 |
delete noxVec2; |
| 180 |
delete noxVec1; |
| 181 |
delete standardMap; |
| 182 |
} |
| 183 |
TEUCHOS_STANDARD_CATCH_STATEMENTS(verbose, std::cerr, success); |
| 184 |
|
| 185 |
#ifdef HAVE_MPI |
| 186 |
MPI_Finalize(); |
| 187 |
#endif |
| 188 |
|
| 189 |
return ( success ? EXIT_SUCCESS : EXIT_FAILURE ); |
| 190 |
} |
| 191 |
/* |
| 192 |
end of file test.C |
| 193 |
*/ |