Debian Codegraph @

packages/nox/test/epetra/NOX_Vector/vectorSpace.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_TestError.H" // Test Suite headers
14 #include "NOX_TestCompare.H" // Test Suite headers
15 #include "Teuchos_RCP.hpp"
16 #include "Teuchos_StandardCatchMacros.hpp"
17
18 // Trilinos headers
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
27 int main(int argc, char *argv[]) {
28
29 // Initialize MPI
30 #ifdef HAVE_MPI
31 MPI_Init(&argc,&argv);
32 #endif
33
34 // Create a communicator for Epetra objects
35 #ifdef HAVE_MPI
36 Epetra_MpiComm Comm( MPI_COMM_WORLD );
37 #else
38 Epetra_SerialComm Comm;
39 #endif
40
41 bool verbose = false;
42
43 if (argc > 1)
44 if (argv[1][0]=='-' && argv[1][1]=='v')
45 verbose = true;
46
47 bool success = false;
48 try {
49 int globalLength = 100; // This should suffice
50
51 // Get the process ID and the total number of processors
52 int MyPID = Comm.MyPID();
53
54 // Set up the printing utilities
55 Teuchos::RCP<Teuchos::ParameterList> noxParamsPtr =
56 Teuchos::rcp(new Teuchos::ParameterList);
57 Teuchos::ParameterList& noxParams = *(noxParamsPtr.get());
58 // Only print output if the "-v" flag is set on the command line
59 Teuchos::ParameterList& printParams = noxParams.sublist("Printing");
60 printParams.set("MyPID", MyPID);
61 printParams.set("Output Precision", 5);
62 printParams.set("Output Processor", 0);
63 if( verbose )
64 printParams.set("Output Information",
65 NOX::Utils::OuterIteration +
66 NOX::Utils::OuterIterationStatusTest +
67 NOX::Utils::InnerIteration +
68 NOX::Utils::Parameters +
69 NOX::Utils::Details +
70 NOX::Utils::Warning +
71 NOX::Utils::TestDetails);
72 else
73 printParams.set("Output Information", NOX::Utils::Error);
74
75 NOX::Utils printing(printParams);
76
77 // Return value
78 int status = 0;
79
80 // *** Start Testing Here!!! ***
81
82 // Create a map describing data distribution
83 Teuchos::RCP<Epetra_Map> standardMap =
84 Teuchos::rcp(new Epetra_Map(globalLength, 0, Comm));
85
86 // Create the L2 vector space
87 Teuchos::RCP<NOX::Epetra::VectorSpace> vectorSpace1 =
88 Teuchos::rcp(new NOX::Epetra::VectorSpaceL2);
89
90 // Create the scaled L2 vector space
91 Teuchos::RCP<Epetra_Vector> scaleVec =
92 Teuchos::rcp(new Epetra_Vector(*standardMap,true));
93 Teuchos::RCP<NOX::Epetra::Scaling> scaling =
94 Teuchos::rcp(new NOX::Epetra::Scaling);
95 scaleVec->PutScalar(2.0);
96 scaling->addUserScaling(NOX::Epetra::Scaling::Left, scaleVec);
97 Teuchos::RCP<NOX::Epetra::VectorSpace> vectorSpace2 =
98 Teuchos::rcp(new NOX::Epetra::VectorSpaceScaledL2(scaling));
99
100 // Create a base Epetra_Vector
101 Teuchos::RCP<Epetra_Vector> epVec =
102 Teuchos::rcp(new Epetra_Vector(*standardMap, true));
103
104 // Create NOX::Epetra::Vectors
105 Teuchos::RCP<NOX::Epetra::Vector> vecL2 =
106 Teuchos::rcp(new NOX::Epetra::Vector(epVec,
107 NOX::Epetra::Vector::CreateView,
108 NOX::DeepCopy,
109 vectorSpace1));
110 Teuchos::RCP<NOX::Epetra::Vector> vecScaledL2 =
111 Teuchos::rcp(new NOX::Epetra::Vector(*epVec, NOX::DeepCopy, vectorSpace2));
112
113 // Test our norms
114 NOX::Abstract::Vector::NormType oneNorm = NOX::Abstract::Vector::OneNorm;
115 NOX::Abstract::Vector::NormType twoNorm = NOX::Abstract::Vector::TwoNorm;
116 NOX::Abstract::Vector::NormType infNorm = NOX::Abstract::Vector::MaxNorm;
117
118 double expectedInfNorm = 1.0;
119 double expectedOneNorm = static_cast<double>(globalLength);
120 double expectedTwoNorm = sqrt(static_cast<double>(globalLength));
121
122 double expectedScaledInfNorm = 0.5;
123 double expectedScaledOneNorm = static_cast<double>(globalLength) / 2.0;
124 double expectedScaledTwoNorm = 0.0;
125 for (int i = 0; i < globalLength; i++)
126 expectedScaledTwoNorm += 0.5 * 0.5;
127 expectedScaledTwoNorm = sqrt(expectedScaledTwoNorm);
128
129 // Create a TestCompare class
130 NOX::TestCompare tester(printing.out(), printing);
131 double tolerance = 1.e-12;
132 NOX::TestCompare::CompareType aComp = NOX::TestCompare::Absolute;
133
134 vecL2->init(1.0);
135 vecScaledL2->init(1.0);
136
137 status += tester.testValue(vecL2->norm(infNorm), expectedInfNorm,
138 tolerance, "Max-Norm Test", aComp);
139 status += tester.testValue(vecL2->norm(oneNorm), expectedOneNorm,
140 tolerance, "One-Norm Test", aComp);
141 status += tester.testValue(vecL2->norm(twoNorm), expectedTwoNorm,
142 tolerance, "Two-Norm Test", aComp);
143
144 status += tester.testValue(vecScaledL2->norm(infNorm), expectedScaledInfNorm,
145 tolerance, "Scaled Max-Norm Test", aComp);
146 status += tester.testValue(vecScaledL2->norm(oneNorm), expectedScaledOneNorm,
147 tolerance, "Scaled One-Norm Test", aComp);
148 status += tester.testValue(vecScaledL2->norm(twoNorm), expectedScaledTwoNorm,
149 tolerance, "Scaled Two-Norm Test", aComp);
150
151 // Test the inner products for L2
152 Teuchos::RCP<NOX::Abstract::Vector> tmpVector =
153 vecL2->clone();
154 vecL2->init(2.0);
155 tmpVector->init(1.0);
156 double expectedInnerProduct = static_cast<double>(globalLength) * 2.0;
157
158 status += tester.testValue(vecL2->innerProduct(*tmpVector),
159 expectedInnerProduct,
160 tolerance,
161 "Inner Product Test",
162 aComp);
163
164 // Test the inner products for Scaled L2
165 tmpVector = vecScaledL2->clone();
166 vecScaledL2->init(2.0);
167 tmpVector->init(1.0);
168 expectedInnerProduct = static_cast<double>(globalLength) * 0.5;
169
170 status += tester.testValue(vecScaledL2->innerProduct(*tmpVector),
171 expectedInnerProduct,
172 tolerance,
173 "Scaled Inner Product Test",
174 aComp);
175
176 success = status==0;
177
178 if (success)
179 printing.out() << "Test passed!" << std::endl;
180 else
181 printing.out() << "Test failed!" << std::endl;
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 }