Debian Codegraph @

packages/nox/test/tpetra/Tpetra_VectorOpsTests.cpp

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 #include "Teuchos_UnitTestHarness.hpp"
10 #include "Teuchos_ScalarTraits.hpp"
11 #include "Teuchos_Comm.hpp"
12 #include "Teuchos_RCP.hpp"
13
14 #include "Tpetra_Core.hpp"
15 #include "Tpetra_Vector.hpp"
16 #include "NOX_Thyra_Vector.H"
17 #include "Thyra_TpetraThyraWrappers.hpp"
18
19 // Typedefs
20
21 typedef Tpetra::Vector<>::scalar_type Scalar;
22 typedef Tpetra::Vector<>::local_ordinal_type LO;
23 typedef Tpetra::Vector<>::global_ordinal_type GO;
24 typedef Tpetra::Vector<>::node_type Node;
25
26 typedef Teuchos::ScalarTraits<Scalar> ST;
27 typedef Tpetra::Map<LO,GO,Node> Map;
28 typedef Tpetra::Vector<Scalar,LO,GO,Node> TV;
29 typedef Thyra::VectorBase<Scalar> TVB;
30 typedef NOX::Thyra::Vector NTV;
31 typedef typename TV::mag_type mag_type;
32
33 #if defined HAVE_TPETRACORE_CUDA
34 #define NUM_LOCAL 1000000
35 #else
36 #define NUM_LOCAL 1000
37 #endif
38 const std::size_t numLocalElements = NUM_LOCAL;
39
40 //Routines for checking solution
41
42 bool checkVectors(const Teuchos::RCP<TV>& a,
43 const Teuchos::RCP<TV>& b,
44 const mag_type& expectedNorm,
45 Teuchos::FancyOStream& out)
46 {
47 bool success = true;
48 ST::magnitudeType tol = 1.0e-14;
49 b->update(-1.0*ST::one(), *a, ST::one());
50 TEUCHOS_TEST_EQUALITY(b->norm2() < tol, true, out, success);
51 TEUCHOS_TEST_FLOATING_EQUALITY(a->norm2(), expectedNorm, tol, out, success);
52 return success;
53 }
54
55 template <class T>
56 bool checkReduction(const T& a,
57 const T& b,
58 const T& expectedVal,
59 Teuchos::FancyOStream& out)
60 {
61 bool success = true;
62 typename Teuchos::ScalarTraits<T>::magnitudeType tol = 1.0e-14;
63 TEUCHOS_TEST_FLOATING_EQUALITY(expectedVal, a, tol, out, success);
64 TEUCHOS_TEST_FLOATING_EQUALITY(expectedVal, b, tol, out, success);
65 return success;
66 }
67
68 // Unit tests
69
70 TEUCHOS_UNIT_TEST(Tpetra_VectorOps, CopyConstructor)
71 {
72 Teuchos::RCP<const Teuchos::Comm<int> > comm = Tpetra::getDefaultComm();
73 const Tpetra::global_size_t numGlobalElements = comm->getSize()*numLocalElements;
74
75 // Create Tpetra vectors
76 Teuchos::RCP<const Map> map = Teuchos::rcp(new const Map(numGlobalElements, numLocalElements, 0, comm));
77 Teuchos::RCP<TV> x = Teuchos::rcp(new TV(map));
78
79 x->putScalar(ST::one());
80
81 // Create new NOX vector with copy of Thyra vector
82 Teuchos::RCP<TVB> x_thyra = Thyra::createVector(x);
83 Teuchos::RCP<NTV> y = Teuchos::rcp(new NTV(*x_thyra));
84
85 // Check for correct answer
86 typedef Thyra::TpetraOperatorVectorExtraction<Scalar, LO, GO, Node> TOVE;
87 mag_type ans = static_cast<mag_type>(ST::squareroot(numGlobalElements));
88 success = checkVectors(x, TOVE::getTpetraVector(y->getThyraRCPVector()), ans, out);
89 }
90
91 TEUCHOS_UNIT_TEST(Tpetra_VectorOps, OperatorEquals)
92 {
93 Teuchos::RCP<const Teuchos::Comm<int> > comm = Tpetra::getDefaultComm();
94 const Tpetra::global_size_t numGlobalElements = comm->getSize()*numLocalElements;
95
96 // Create Tpetra vectors
97 Teuchos::RCP<const Map> map = Teuchos::rcp(new const Map(numGlobalElements, numLocalElements, 0, comm));
98 Teuchos::RCP<TV> x = Teuchos::rcp(new TV(map));
99 Teuchos::RCP<TV> y = Teuchos::rcp(new TV(map));
100
101 x->putScalar(ST::one());
102 y->putScalar(ST::zero());
103
104 // Copy x into y through NOX interface
105 Teuchos::RCP<TVB> x_thyra = Thyra::createVector(x);
106 Teuchos::RCP<NTV> x_nox = Teuchos::rcp(new NTV(x_thyra));
107 Teuchos::RCP<TVB> y_thyra = Thyra::createVector(y);
108 Teuchos::RCP<NTV> y_nox = Teuchos::rcp(new NTV(y_thyra));
109
110 *y_nox = *x_nox;
111
112 // Check for correct answer
113 mag_type ans = static_cast<mag_type>(ST::squareroot(numGlobalElements));;
114 success = checkVectors(x, y, ans, out);
115 }
116
117 TEUCHOS_UNIT_TEST(Tpetra_VectorOps, Init)
118 {
119 Teuchos::RCP<const Teuchos::Comm<int> > comm = Tpetra::getDefaultComm();
120 const Tpetra::global_size_t numGlobalElements = comm->getSize()*numLocalElements;
121
122 // Create Tpetra vectors
123 Teuchos::RCP<const Map> map = Teuchos::rcp(new const Map(numGlobalElements, numLocalElements, 0, comm));
124 Teuchos::RCP<TV> x = Teuchos::rcp(new TV(map));
125 Teuchos::RCP<TV> y = Teuchos::rcp(new TV(map));
126
127 // Perform operation with Tpetra directly
128 x->putScalar(2.0*ST::one());
129
130 // Do the same thing through NOX/Thyra interface
131 Teuchos::RCP<TVB> y_thyra = Thyra::createVector(y);
132 Teuchos::RCP<NTV> y_nox = Teuchos::rcp(new NTV(y_thyra));
133
134 y_nox->init(2.0*ST::one());
135
136 // Check for correct answer
137 mag_type ans = static_cast<mag_type>(ST::squareroot(4.0*numGlobalElements));;
138 success = checkVectors(x, y, ans, out);
139 }
140
141 TEUCHOS_UNIT_TEST(Tpetra_VectorOps, Random)
142 {
143 Teuchos::RCP<const Teuchos::Comm<int> > comm = Tpetra::getDefaultComm();
144 const Tpetra::global_size_t numGlobalElements = comm->getSize()*numLocalElements;
145
146 // Create Tpetra vectors
147 Teuchos::RCP<const Map> map = Teuchos::rcp(new const Map(numGlobalElements, numLocalElements, 0, comm));
148 Teuchos::RCP<TV> x = Teuchos::rcp(new TV(map));
149 Teuchos::RCP<TV> y = Teuchos::rcp(new TV(map));
150
151 // Perform operation with Tpetra directly
152 x->randomize();
153
154 // Do the same thing through NOX/Thyra interface
155 Teuchos::RCP<TVB> y_thyra = Thyra::createVector(y);
156 Teuchos::RCP<NTV> y_nox = Teuchos::rcp(new NTV(y_thyra));
157
158 y_nox->random();
159
160 // Probaby can't expect these to get the same answer
161 }
162
163 TEUCHOS_UNIT_TEST(Tpetra_VectorOps, Abs)
164 {
165 Teuchos::RCP<const Teuchos::Comm<int> > comm = Tpetra::getDefaultComm();
166 const Tpetra::global_size_t numGlobalElements = comm->getSize()*numLocalElements;
167
168 // Create Tpetra vectors
169 Teuchos::RCP<const Map> map = Teuchos::rcp(new const Map(numGlobalElements, numLocalElements, 0, comm));
170 Teuchos::RCP<TV> x = Teuchos::rcp(new TV(map));
171 Teuchos::RCP<TV> y = Teuchos::rcp(new TV(map));
172
173 x->putScalar(-1.0*ST::one());
174 y->putScalar(-1.0*ST::one());
175
176 // Perform operation with Tpetra directly
177 x->abs(*x);
178
179 // Do the same thing through NOX/Thyra interface
180 Teuchos::RCP<TVB> y_thyra = Thyra::createVector(y);
181 Teuchos::RCP<NTV> y_nox = Teuchos::rcp(new NTV(y_thyra));
182
183 y_nox->abs(*y_nox);
184
185 // Check for correct answer
186 mag_type ans = static_cast<mag_type>(ST::squareroot(numGlobalElements));;
187 success = checkVectors(x, y, ans, out);
188 }
189
190 TEUCHOS_UNIT_TEST(Tpetra_VectorOps, Reciprocal)
191 {
192 Teuchos::RCP<const Teuchos::Comm<int> > comm = Tpetra::getDefaultComm();
193 const Tpetra::global_size_t numGlobalElements = comm->getSize()*numLocalElements;
194
195 // Create Tpetra vectors
196 Teuchos::RCP<const Map> map = Teuchos::rcp(new const Map(numGlobalElements, numLocalElements, 0, comm));
197 Teuchos::RCP<TV> x = Teuchos::rcp(new TV(map));
198 Teuchos::RCP<TV> y = Teuchos::rcp(new TV(map));
199
200 x->putScalar(2.0*ST::one());
201 y->putScalar(2.0*ST::one());
202
203 // Perform operation with Tpetra directly
204 x->reciprocal(*x);
205
206 // Do the same thing through NOX/Thyra interface
207 Teuchos::RCP<TVB> y_thyra = Thyra::createVector(y);
208 Teuchos::RCP<NTV> y_nox = Teuchos::rcp(new NTV(y_thyra));
209
210 y_nox->reciprocal(*y_nox);
211
212 // Check for correct answer
213 mag_type ans = static_cast<mag_type>(ST::squareroot(0.25*numGlobalElements));;
214 success = checkVectors(x, y, ans, out);
215 }
216
217 TEUCHOS_UNIT_TEST(Tpetra_VectorOps, Scale_Scalar)
218 {
219 Teuchos::RCP<const Teuchos::Comm<int> > comm = Tpetra::getDefaultComm();
220 const Tpetra::global_size_t numGlobalElements = comm->getSize()*numLocalElements;
221
222 // Create Tpetra vectors
223 Teuchos::RCP<const Map> map = Teuchos::rcp(new const Map(numGlobalElements, numLocalElements, 0, comm));
224 Teuchos::RCP<TV> x = Teuchos::rcp(new TV(map));
225 Teuchos::RCP<TV> y = Teuchos::rcp(new TV(map));
226
227 x->putScalar(ST::one());
228 y->putScalar(ST::one());
229
230 // Perform operation with Tpetra directly
231 x->scale(2.0*ST::one());
232
233 // Do the same thing through NOX/Thyra interface
234 Teuchos::RCP<TVB> y_thyra = Thyra::createVector(y);
235 Teuchos::RCP<NTV> y_nox = Teuchos::rcp(new NTV(y_thyra));
236
237 y_nox->scale(2.0*ST::one());
238
239 // Check for correct answer
240 mag_type ans = static_cast<mag_type>(ST::squareroot(4.0*numGlobalElements));;
241 success = checkVectors(x, y, ans, out);
242 }
243
244 TEUCHOS_UNIT_TEST(Tpetra_VectorOps, Scale_Vector)
245 {
246 Teuchos::RCP<const Teuchos::Comm<int> > comm = Tpetra::getDefaultComm();
247 const Tpetra::global_size_t numGlobalElements = comm->getSize()*numLocalElements;
248
249 // Create Tpetra vectors
250 Teuchos::RCP<const Map> map = Teuchos::rcp(new const Map(numGlobalElements, numLocalElements, 0, comm));
251 Teuchos::RCP<TV> x = Teuchos::rcp(new TV(map));
252 Teuchos::RCP<TV> y = Teuchos::rcp(new TV(map));
253 Teuchos::RCP<TV> z = Teuchos::rcp(new TV(map));
254
255 x->putScalar(ST::one());
256 y->putScalar(ST::one());
257 z->putScalar(2.0*ST::one());
258
259 // Perform operation with Tpetra directly
260 x->elementWiseMultiply(ST::one(), *z, *x, ST::zero());
261
262 // Do the same thing through NOX/Thyra interface
263 Teuchos::RCP<TVB> y_thyra = Thyra::createVector(y);
264 Teuchos::RCP<NTV> y_nox = Teuchos::rcp(new NTV(y_thyra));
265 Teuchos::RCP<TVB> z_thyra = Thyra::createVector(z);
266 Teuchos::RCP<NTV> z_nox = Teuchos::rcp(new NTV(z_thyra));
267
268 y_nox->scale(*z_nox);
269
270 // Check for correct answer
271 mag_type ans = static_cast<mag_type>(ST::squareroot(4.0*numGlobalElements));;
272 success = checkVectors(x, y, ans, out);
273 }
274
275 TEUCHOS_UNIT_TEST(Tpetra_VectorOps, Update_1)
276 {
277 Teuchos::RCP<const Teuchos::Comm<int> > comm = Tpetra::getDefaultComm();
278 const Tpetra::global_size_t numGlobalElements = comm->getSize()*numLocalElements;
279
280 // Create Tpetra vectors
281 Teuchos::RCP<const Map> map = Teuchos::rcp(new const Map(numGlobalElements, numLocalElements, 0, comm));
282 Teuchos::RCP<TV> x = Teuchos::rcp(new TV(map));
283 Teuchos::RCP<TV> y = Teuchos::rcp(new TV(map));
284 Teuchos::RCP<TV> z = Teuchos::rcp(new TV(map));
285
286 x->putScalar(ST::one());
287 y->putScalar(ST::one());
288 z->putScalar(ST::one());
289
290 // Perform operation with Tpetra directly
291 x->update(2.0*ST::one(), *z, ST::one());
292
293 // Do the same thing through NOX/Thyra interface
294 Teuchos::RCP<TVB> y_thyra = Thyra::createVector(y);
295 Teuchos::RCP<NTV> y_nox = Teuchos::rcp(new NTV(y_thyra));
296 Teuchos::RCP<TVB> z_thyra = Thyra::createVector(z);
297 Teuchos::RCP<NTV> z_nox = Teuchos::rcp(new NTV(z_thyra));
298
299 y_nox->update(2.0*ST::one(), *z_nox, ST::one());
300
301 // Check for correct answer
302 mag_type ans = static_cast<mag_type>(ST::squareroot(9.0*numGlobalElements));;
303 success = checkVectors(x, y, ans, out);
304 }
305
306 TEUCHOS_UNIT_TEST(Tpetra_VectorOps, Update_2)
307 {
308 Teuchos::RCP<const Teuchos::Comm<int> > comm = Tpetra::getDefaultComm();
309 const Tpetra::global_size_t numGlobalElements = comm->getSize()*numLocalElements;
310
311 // Create Tpetra vectors
312 Teuchos::RCP<const Map> map = Teuchos::rcp(new const Map(numGlobalElements, numLocalElements, 0, comm));
313 Teuchos::RCP<TV> w = Teuchos::rcp(new TV(map));
314 Teuchos::RCP<TV> x = Teuchos::rcp(new TV(map));
315 Teuchos::RCP<TV> y = Teuchos::rcp(new TV(map));
316 Teuchos::RCP<TV> z = Teuchos::rcp(new TV(map));
317
318 w->putScalar(ST::one());
319 x->putScalar(ST::one());
320 y->putScalar(ST::one());
321 z->putScalar(ST::one());
322
323 // Perform operation with Tpetra directly
324 x->update(2.0*ST::one(), *w, 2.0*ST::one(), *z, ST::one());
325
326 // Do the same thing through NOX/Thyra interface
327 Teuchos::RCP<TVB> w_thyra = Thyra::createVector(w);
328 Teuchos::RCP<NTV> w_nox = Teuchos::rcp(new NTV(w_thyra));
329 Teuchos::RCP<TVB> y_thyra = Thyra::createVector(y);
330 Teuchos::RCP<NTV> y_nox = Teuchos::rcp(new NTV(y_thyra));
331 Teuchos::RCP<TVB> z_thyra = Thyra::createVector(z);
332 Teuchos::RCP<NTV> z_nox = Teuchos::rcp(new NTV(z_thyra));
333
334 y_nox->update(2.0*ST::one(), *w_nox, 2.0*ST::one(), *z_nox, ST::one());
335
336 // Check for correct answer
337 mag_type ans = static_cast<mag_type>(ST::squareroot(25.0*numGlobalElements));;
338 success = checkVectors(x, y, ans, out);
339 }
340
341 TEUCHOS_UNIT_TEST(Tpetra_VectorOps, Norm_1)
342 {
343 Teuchos::RCP<const Teuchos::Comm<int> > comm = Tpetra::getDefaultComm();
344 const Tpetra::global_size_t numGlobalElements = comm->getSize()*numLocalElements;
345
346 // Create Tpetra vectors
347 Teuchos::RCP<const Map> map = Teuchos::rcp(new const Map(numGlobalElements, numLocalElements, 0, comm));
348 Teuchos::RCP<TV> x = Teuchos::rcp(new TV(map));
349
350 x->putScalar(ST::one());
351
352 // Perform operation with Tpetra directly
353 ST::magnitudeType norm_tpetra = x->norm1();
354
355 // Do the same thing through NOX/Thyra interface
356 Teuchos::RCP<TVB> x_thyra = Thyra::createVector(x);
357 Teuchos::RCP<NTV> x_nox = Teuchos::rcp(new NTV(x_thyra));
358
359 ST::magnitudeType norm_nox = x_nox->norm(NOX::Abstract::Vector::OneNorm);
360
361 // Check for correct answer
362 ST::magnitudeType ans = static_cast<mag_type>(numGlobalElements);;
363 success = checkReduction(norm_tpetra, norm_nox, ans, out);
364 }
365
366 TEUCHOS_UNIT_TEST(Tpetra_VectorOps, Norm_2)
367 {
368 Teuchos::RCP<const Teuchos::Comm<int> > comm = Tpetra::getDefaultComm();
369 const Tpetra::global_size_t numGlobalElements = comm->getSize()*numLocalElements;
370
371 // Create Tpetra vectors
372 Teuchos::RCP<const Map> map = Teuchos::rcp(new const Map(numGlobalElements, numLocalElements, 0, comm));
373 Teuchos::RCP<TV> x = Teuchos::rcp(new TV(map));
374
375 x->putScalar(ST::one());
376
377 // Perform operation with Tpetra directly
378 ST::magnitudeType norm_tpetra = x->norm2();
379
380 // Do the same thing through NOX/Thyra interface
381 Teuchos::RCP<TVB> x_thyra = Thyra::createVector(x);
382 Teuchos::RCP<NTV> x_nox = Teuchos::rcp(new NTV(x_thyra));
383
384 ST::magnitudeType norm_nox = x_nox->norm(NOX::Abstract::Vector::TwoNorm);
385
386 // Check for correct answer
387 ST::magnitudeType ans = static_cast<mag_type>(ST::squareroot(numGlobalElements));;
388 success = checkReduction(norm_tpetra, norm_nox, ans, out);
389 }
390
391 TEUCHOS_UNIT_TEST(Tpetra_VectorOps, Norm_Inf)
392 {
393 Teuchos::RCP<const Teuchos::Comm<int> > comm = Tpetra::getDefaultComm();
394 const Tpetra::global_size_t numGlobalElements = comm->getSize()*numLocalElements;
395
396 // Create Tpetra vectors
397 Teuchos::RCP<const Map> map = Teuchos::rcp(new const Map(numGlobalElements, numLocalElements, 0, comm));
398 Teuchos::RCP<TV> x = Teuchos::rcp(new TV(map));
399
400 x->putScalar(ST::one());
401
402 // Perform operation with Tpetra directly
403 ST::magnitudeType norm_tpetra = x->normInf();
404
405 // Do the same thing through NOX/Thyra interface
406 Teuchos::RCP<TVB> x_thyra = Thyra::createVector(x);
407 Teuchos::RCP<NTV> x_nox = Teuchos::rcp(new NTV(x_thyra));
408
409 ST::magnitudeType norm_nox = x_nox->norm(NOX::Abstract::Vector::MaxNorm);
410
411 // Check for correct answer
412 ST::magnitudeType ans = static_cast<mag_type>(1.0);;
413 success = checkReduction(norm_tpetra, norm_nox, ans, out);
414 }
415
416 TEUCHOS_UNIT_TEST(Tpetra_VectorOps, Norm_Weighted)
417 {
418 Teuchos::RCP<const Teuchos::Comm<int> > comm = Tpetra::getDefaultComm();
419 const Tpetra::global_size_t numGlobalElements = comm->getSize()*numLocalElements;
420
421 // Create Tpetra vectors
422 Teuchos::RCP<const Map> map = Teuchos::rcp(new const Map(numGlobalElements, numLocalElements, 0, comm));
423 Teuchos::RCP<TV> x = Teuchos::rcp(new TV(map));
424 Teuchos::RCP<TV> y = Teuchos::rcp(new TV(map));
425
426 x->putScalar(ST::one());
427 y->putScalar(2.0*ST::one());
428
429 // Tpetra weighted vector norm is deprecated, so need to do weighting manually
430 ST::magnitudeType norm_tpetra;
431 {
432 Teuchos::RCP<TV> copy = Teuchos::rcp(new TV(*x, Teuchos::Copy));
433 copy->elementWiseMultiply(ST::one(), *y, *copy, ST::zero());
434 norm_tpetra = ST::magnitude(ST::squareroot(copy->dot(*x)));
435 }
436
437 // Do the same thing through NOX/Thyra interface
438 Teuchos::RCP<TVB> x_thyra = Thyra::createVector(x);
439 Teuchos::RCP<NTV> x_nox = Teuchos::rcp(new NTV(x_thyra));
440 Teuchos::RCP<TVB> y_thyra = Thyra::createVector(y);
441 Teuchos::RCP<NTV> y_nox = Teuchos::rcp(new NTV(y_thyra));
442
443 ST::magnitudeType norm_nox = x_nox->norm(*y_nox);
444
445 // Check for correct answer
446 ST::magnitudeType ans = static_cast<mag_type>(ST::squareroot(2.0*numGlobalElements));;
447 success = checkReduction(norm_tpetra, norm_nox, ans, out);
448 }
449
450 TEUCHOS_UNIT_TEST(Tpetra_VectorOps, InnerProduct)
451 {
452 Teuchos::RCP<const Teuchos::Comm<int> > comm = Tpetra::getDefaultComm();
453 const Tpetra::global_size_t numGlobalElements = comm->getSize()*numLocalElements;
454
455 // Create Tpetra vectors
456 Teuchos::RCP<const Map> map = Teuchos::rcp(new const Map(numGlobalElements, numLocalElements, 0, comm));
457 Teuchos::RCP<TV> x = Teuchos::rcp(new TV(map));
458 Teuchos::RCP<TV> y = Teuchos::rcp(new TV(map));
459
460 x->putScalar(ST::one());
461 y->putScalar(2.0*ST::one());
462
463 // Perform operation with Tpetra directly
464 Scalar dot_tpetra = x->dot(*y);
465
466 // Do the same thing through NOX/Thyra interface
467 Teuchos::RCP<TVB> x_thyra = Thyra::createVector(x);
468 Teuchos::RCP<NTV> x_nox = Teuchos::rcp(new NTV(x_thyra));
469 Teuchos::RCP<TVB> y_thyra = Thyra::createVector(y);
470 Teuchos::RCP<NTV> y_nox = Teuchos::rcp(new NTV(y_thyra));
471
472 Scalar dot_nox = x_nox->innerProduct(*y_nox);
473
474 // Check for correct answer
475 Scalar ans = static_cast<mag_type>(2.0*numGlobalElements);;
476 success = checkReduction(dot_tpetra, dot_nox, ans, out);
477 }
478