Debian Codegraph @

packages/panzer/disc-fe/test/la_factory/tEpetra_GlbEvalData.cpp

raw
1 // @HEADER
2 // *****************************************************************************
3 // Panzer: A partial differential equation assembly
4 // engine for strongly coupled complex multiphysics systems
5 //
6 // Copyright 2011 NTESS and the Panzer contributors.
7 // SPDX-License-Identifier: BSD-3-Clause
8 // *****************************************************************************
9 // @HEADER
10
11 #include "PanzerDiscFE_config.hpp"
12
13 #include <Teuchos_ConfigDefs.hpp>
14 #include <Teuchos_UnitTestHarness.hpp>
15 #include <Teuchos_RCP.hpp>
16 #include <Teuchos_TimeMonitor.hpp>
17
18 #include <string>
19 #include <iostream>
20
21
22 #include "Panzer_EpetraVector_ReadOnly_GlobalEvaluationData.hpp"
23 #include "Panzer_BlockedVector_ReadOnly_GlobalEvaluationData.hpp"
24
25 #include "Teuchos_DefaultMpiComm.hpp"
26
27 #ifdef HAVE_MPI
28 #include "Epetra_MpiComm.h"
29 #else
30 #include "Epetra_SerialComm.h"
31 #endif
32
33 #include "Epetra_Map.h"
34 #include "Epetra_MultiVector.h"
35 #include "Epetra_MpiComm.h"
36
37 #include "Thyra_VectorBase.hpp"
38 #include "Thyra_VectorSpaceBase.hpp"
39 #include "Thyra_SpmdVectorBase.hpp"
40 #include "Thyra_SpmdVectorSpaceBase.hpp"
41 #include "Thyra_DefaultSpmdVectorSpace.hpp"
42 #include "Thyra_DefaultProductVectorSpace.hpp"
43 #include "Thyra_DefaultProductVector.hpp"
44 #include "Thyra_ProductVectorBase.hpp"
45
46 using Teuchos::rcp;
47 using Teuchos::rcp_dynamic_cast;
48 using Teuchos::RCP;
49 using Teuchos::rcpFromRef;
50
51 namespace panzer {
52
53 TEUCHOS_UNIT_TEST(tEpetra_GlbEvalData, basic)
54 {
55 using Teuchos::RCP;
56 using Teuchos::rcp;
57
58 typedef Thyra::VectorBase<double> Thyra_Vector;
59 typedef Thyra::SpmdVectorBase<double> Thyra_SpmdVec;
60
61 Epetra_MpiComm comm(MPI_COMM_WORLD);
62
63 // This is required
64 TEST_ASSERT(comm.NumProc()==2);
65
66 std::vector<int> ghosted(5);
67 std::vector<int> owned(3);
68
69 if(comm.MyPID()==0) {
70 owned[0] = 0;
71 owned[1] = 1;
72 owned[2] = 2;
73
74 ghosted[0] = 0;
75 ghosted[1] = 1;
76 ghosted[2] = 2;
77 ghosted[3] = 3;
78 ghosted[4] = 4;
79 }
80 else {
81 owned[0] = 3;
82 owned[1] = 4;
83 owned[2] = 5;
84
85 ghosted[0] = 1;
86 ghosted[1] = 2;
87 ghosted[2] = 3;
88 ghosted[3] = 4;
89 ghosted[4] = 5;
90 }
91
92 RCP<const Epetra_Map> ownedMap = rcp(new Epetra_Map(-1,owned.size(),&owned[0],0,comm));
93 RCP<const Epetra_Map> ghostedMap = rcp(new Epetra_Map(-1,ghosted.size(),&ghosted[0],0,comm));
94 RCP<const Epetra_Import> importer = rcp(new Epetra_Import(*ghostedMap,*ownedMap));
95
96 EpetraVector_ReadOnly_GlobalEvaluationData ged;
97
98 TEST_ASSERT(!ged.isInitialized());
99
100 ged.initialize(importer,ghostedMap,ownedMap);
101
102 TEST_ASSERT(ged.isInitialized());
103
104 // test the ghosted vector sizing (we don't care what the entries are!)
105 {
106 RCP<Epetra_Vector> ghostedVecE = ged.getGhostedVector_Epetra();
107 RCP<Thyra_Vector> ghostedVecT = ged.getGhostedVector();
108
109 TEST_ASSERT(ghostedVecE!=Teuchos::null);
110 TEST_ASSERT(ghostedVecT!=Teuchos::null);
111
112 RCP<const Thyra::SpmdVectorSpaceBase<double> > ghostedSpace
113 = rcp_dynamic_cast<const Thyra::SpmdVectorSpaceBase<double> >(ghostedVecT->space());
114
115 TEST_EQUALITY(ghostedMap->NumMyElements(),ghostedVecE->MyLength());
116 TEST_EQUALITY(ghostedMap->NumGlobalElements(),ghostedVecE->GlobalLength());
117
118 TEST_EQUALITY(ghostedSpace->isLocallyReplicated(),false);
119 TEST_EQUALITY(ghostedSpace->localSubDim(),ghostedVecE->MyLength());
120 }
121
122 // test setting an owned vector
123 {
124 RCP<Epetra_Vector> ownedVec = rcp(new Epetra_Vector(*ownedMap));
125
126 if(comm.MyPID()==0) {
127 (*ownedVec)[0] = 3.14;
128 (*ownedVec)[1] = 1.82;
129 (*ownedVec)[2] = -.91;
130 }
131 else {
132 (*ownedVec)[0] = 2.72;
133 (*ownedVec)[1] = 6.23;
134 (*ownedVec)[2] = -.17;
135 }
136
137 // set the owned vector, assure that const can be used
138 ged.setOwnedVector_Epetra(ownedVec.getConst());
139 }
140
141 // test the owned vector sizing and thyra entries
142 {
143 RCP<const Thyra_Vector> ownedVecT = ged.getOwnedVector();
144
145 TEST_ASSERT(ownedVecT!=Teuchos::null);
146
147 RCP<const Thyra::SpmdVectorSpaceBase<double> > ownedSpace
148 = rcp_dynamic_cast<const Thyra::SpmdVectorSpaceBase<double> >(ownedVecT->space());
149
150 TEST_EQUALITY(ownedSpace->isLocallyReplicated(),false);
151
152 RCP<const Thyra_SpmdVec> spmdVec = rcp_dynamic_cast<const Thyra_SpmdVec>(ownedVecT);
153
154 Teuchos::ArrayRCP<const double> thyraVec;
155 spmdVec->getLocalData(Teuchos::ptrFromRef(thyraVec));
156
157 TEST_EQUALITY(thyraVec.size(),3);
158
159 if(comm.MyPID()==0) {
160 TEST_EQUALITY(thyraVec[0],3.14);
161 TEST_EQUALITY(thyraVec[1],1.82);
162 TEST_EQUALITY(thyraVec[2],-.91);
163 }
164 else {
165 TEST_EQUALITY(thyraVec[0],2.72);
166 TEST_EQUALITY(thyraVec[1],6.23);
167 TEST_EQUALITY(thyraVec[2],-.17);
168 }
169 }
170
171 // actually do something...
172 ged.initializeData();
173 ged.globalToGhost(0);
174
175 {
176 const Epetra_Vector & ghostedVecE = *ged.getGhostedVector_Epetra();
177 RCP<Thyra_Vector> ghostedVecT = ged.getGhostedVector();
178
179 RCP<const Thyra_SpmdVec> spmdVec = rcp_dynamic_cast<const Thyra_SpmdVec>(ghostedVecT);
180
181 Teuchos::ArrayRCP<const double> thyraVec;
182 spmdVec->getLocalData(Teuchos::ptrFromRef(thyraVec));
183
184 TEST_EQUALITY(thyraVec.size(),ghostedVecE.MyLength());
185
186 if(comm.MyPID()==0) {
187 TEST_EQUALITY(ghostedVecE[0],3.14);
188 TEST_EQUALITY(ghostedVecE[1],1.82);
189 TEST_EQUALITY(ghostedVecE[2],-.91);
190 TEST_EQUALITY(ghostedVecE[3],2.72);
191 TEST_EQUALITY(ghostedVecE[4],6.23);
192 }
193 else {
194 TEST_EQUALITY(ghostedVecE[0],1.82);
195 TEST_EQUALITY(ghostedVecE[1],-.91);
196 TEST_EQUALITY(ghostedVecE[2],2.72);
197 TEST_EQUALITY(ghostedVecE[3],6.23);
198 TEST_EQUALITY(ghostedVecE[4],-.17);
199 }
200
201 TEST_EQUALITY(ghostedVecE[0],thyraVec[0]);
202 TEST_EQUALITY(ghostedVecE[1],thyraVec[1]);
203 TEST_EQUALITY(ghostedVecE[2],thyraVec[2]);
204 TEST_EQUALITY(ghostedVecE[3],thyraVec[3]);
205 TEST_EQUALITY(ghostedVecE[4],thyraVec[4]);
206 }
207 }
208
209 TEUCHOS_UNIT_TEST(tEpetra_GlbEvalData, blocked)
210 {
211 using Teuchos::RCP;
212 using Teuchos::rcp;
213
214 typedef Thyra::SpmdVectorBase<double> Thyra_SpmdVec;
215
216 Teuchos::RCP<Teuchos::MpiComm<Thyra::Ordinal> > tComm = Teuchos::rcp(new Teuchos::MpiComm<Thyra::Ordinal>(MPI_COMM_WORLD));
217 Epetra_MpiComm comm(MPI_COMM_WORLD);
218
219 // This is required
220 TEST_ASSERT(comm.NumProc()==2);
221
222 std::vector<int> ghosted(5);
223 std::vector<int> owned(3);
224
225 if(comm.MyPID()==0) {
226 owned[0] = 0;
227 owned[1] = 1;
228 owned[2] = 2;
229
230 ghosted[0] = 0;
231 ghosted[1] = 1;
232 ghosted[2] = 2;
233 ghosted[3] = 3;
234 ghosted[4] = 4;
235 }
236 else {
237 owned[0] = 3;
238 owned[1] = 4;
239 owned[2] = 5;
240
241 ghosted[0] = 1;
242 ghosted[1] = 2;
243 ghosted[2] = 3;
244 ghosted[3] = 4;
245 ghosted[4] = 5;
246 }
247
248 RCP<const Epetra_Map> ownedMap = rcp(new Epetra_Map(-1,owned.size(),&owned[0],0,comm));
249 RCP<const Epetra_Map> ghostedMap = rcp(new Epetra_Map(-1,ghosted.size(),&ghosted[0],0,comm));
250 RCP<const Epetra_Import> importer = rcp(new Epetra_Import(*ghostedMap,*ownedMap));
251
252 RCP<EpetraVector_ReadOnly_GlobalEvaluationData> ged_a, ged_b;
253 ged_a = rcp(new EpetraVector_ReadOnly_GlobalEvaluationData(importer,ghostedMap,ownedMap));
254 ged_b = rcp(new EpetraVector_ReadOnly_GlobalEvaluationData(importer,ghostedMap,ownedMap));
255 std::vector<RCP<ReadOnlyVector_GlobalEvaluationData> > gedBlocks;
256 gedBlocks.push_back(ged_a);
257 gedBlocks.push_back(ged_b);
258
259 RCP<const Thyra::VectorSpaceBase<double> > ownedSpace_ab = Thyra::defaultSpmdVectorSpace<double>(tComm,3,-1);
260 RCP<const Thyra::VectorSpaceBase<double> > ghostedSpace_ab = ged_a->getGhostedVector()->space();
261
262 RCP<Thyra::DefaultProductVectorSpace<double> > ownedSpace = Thyra::productVectorSpace<double>(ownedSpace_ab,2);
263 RCP<Thyra::DefaultProductVectorSpace<double> > ghostedSpace = Thyra::productVectorSpace<double>(ghostedSpace_ab,2);
264
265 BlockedVector_ReadOnly_GlobalEvaluationData ged;
266 ged.initialize(ghostedSpace,ownedSpace,gedBlocks);
267
268 RCP<Thyra::VectorBase<double> > ownedVec = Thyra::createMember(*ownedSpace);
269
270 {
271 RCP<Thyra::ProductVectorBase<double> > vec = Thyra::castOrCreateNonconstProductVectorBase(ownedVec);
272
273 TEST_ASSERT(vec->productSpace()->numBlocks()==2);
274
275 if(comm.MyPID()==0) {
276 Teuchos::ArrayRCP<double> thyraVec;
277
278 rcp_dynamic_cast<Thyra_SpmdVec>(vec->getNonconstVectorBlock(0))->getNonconstLocalData(Teuchos::ptrFromRef(thyraVec));
279 thyraVec[0] = 3.14;
280 thyraVec[1] = 1.82;
281 thyraVec[2] = -.91;
282
283 rcp_dynamic_cast<Thyra_SpmdVec>(vec->getNonconstVectorBlock(1))->getNonconstLocalData(Teuchos::ptrFromRef(thyraVec));
284 thyraVec[0] = 3.14+9.0;
285 thyraVec[1] = 1.82+9.0;
286 thyraVec[2] = -.91+9.0;
287 }
288 else {
289 Teuchos::ArrayRCP<double> thyraVec;
290
291 rcp_dynamic_cast<Thyra_SpmdVec>(vec->getNonconstVectorBlock(0))->getNonconstLocalData(Teuchos::ptrFromRef(thyraVec));
292 thyraVec[0] = 2.72;
293 thyraVec[1] = 6.23;
294 thyraVec[2] = -.17;
295
296 rcp_dynamic_cast<Thyra_SpmdVec>(vec->getNonconstVectorBlock(1))->getNonconstLocalData(Teuchos::ptrFromRef(thyraVec));
297 thyraVec[0] = 2.72+7.0;
298 thyraVec[1] = 6.23+7.0;
299 thyraVec[2] = -.17+7.0;
300 }
301 }
302
303 ged.setOwnedVector(ownedVec);
304
305 ged.initializeData();
306 ged.globalToGhost(0);
307
308 {
309 RCP<Thyra::ProductVectorBase<double> > ghostedVec = Thyra::castOrCreateNonconstProductVectorBase(ged.getGhostedVector());
310
311 if(comm.MyPID()==0) {
312 Teuchos::ArrayRCP<const double> thyraVec;
313 rcp_dynamic_cast<const Thyra_SpmdVec>(ghostedVec->getVectorBlock(0))->getLocalData(Teuchos::ptrFromRef(thyraVec));
314
315 TEST_EQUALITY(thyraVec[0],3.14);
316 TEST_EQUALITY(thyraVec[1],1.82);
317 TEST_EQUALITY(thyraVec[2],-.91);
318 TEST_EQUALITY(thyraVec[3],2.72);
319 TEST_EQUALITY(thyraVec[4],6.23);
320
321 rcp_dynamic_cast<const Thyra_SpmdVec>(ghostedVec->getVectorBlock(1))->getLocalData(Teuchos::ptrFromRef(thyraVec));
322
323 TEST_EQUALITY(thyraVec[0],3.14+9.0);
324 TEST_EQUALITY(thyraVec[1],1.82+9.0);
325 TEST_EQUALITY(thyraVec[2],-.91+9.0);
326 TEST_EQUALITY(thyraVec[3],2.72+7.0);
327 TEST_EQUALITY(thyraVec[4],6.23+7.0);
328 }
329 else {
330 Teuchos::ArrayRCP<const double> thyraVec;
331 rcp_dynamic_cast<const Thyra_SpmdVec>(ghostedVec->getVectorBlock(0))->getLocalData(Teuchos::ptrFromRef(thyraVec));
332
333 TEST_EQUALITY(thyraVec[0],1.82);
334 TEST_EQUALITY(thyraVec[1],-.91);
335 TEST_EQUALITY(thyraVec[2],2.72);
336 TEST_EQUALITY(thyraVec[3],6.23);
337 TEST_EQUALITY(thyraVec[4],-.17);
338
339 rcp_dynamic_cast<const Thyra_SpmdVec>(ghostedVec->getVectorBlock(1))->getLocalData(Teuchos::ptrFromRef(thyraVec));
340
341 TEST_EQUALITY(thyraVec[0],1.82+9.0);
342 TEST_EQUALITY(thyraVec[1],-.91+9.0);
343 TEST_EQUALITY(thyraVec[2],2.72+7.0);
344 TEST_EQUALITY(thyraVec[3],6.23+7.0);
345 TEST_EQUALITY(thyraVec[4],-.17+7.0);
346 }
347 }
348 }
349
350 TEUCHOS_UNIT_TEST(tEpetra_GlbEvalData, filtered_dofs)
351 {
352 using Teuchos::RCP;
353 using Teuchos::rcp;
354
355 Epetra_MpiComm comm(MPI_COMM_WORLD);
356
357 // This is required
358 TEST_ASSERT(comm.NumProc()==2);
359
360 std::vector<int> ghosted(4);
361 std::vector<int> owned(2);
362
363 // This is a line with 6 notes (numbered 0-5). The boundaries
364 // are removed at 0 and 5.
365 if(comm.MyPID()==0) {
366 owned[0] = 1;
367 owned[1] = 2;
368
369 ghosted[0] = 0;
370 ghosted[1] = 1;
371 ghosted[2] = 2;
372 ghosted[3] = 3;
373 }
374 else {
375 owned[0] = 3;
376 owned[1] = 4;
377
378 ghosted[0] = 2;
379 ghosted[1] = 3;
380 ghosted[2] = 4;
381 ghosted[3] = 5;
382 }
383
384 RCP<const Epetra_Map> ownedMap = rcp(new Epetra_Map(-1,owned.size(),&owned[0],0,comm));
385 RCP<const Epetra_Map> ghostedMap = rcp(new Epetra_Map(-1,ghosted.size(),&ghosted[0],0,comm));
386 RCP<const Epetra_Import> importer = rcp(new Epetra_Import(*ghostedMap,*ownedMap));
387
388 EpetraVector_ReadOnly_GlobalEvaluationData ged;
389
390 std::vector<int> constIndex(1);
391
392 // setup filtered values
393 constIndex[0] = 0;
394 ged.useConstantValues(constIndex,2.0);
395
396 constIndex[0] = 5;
397 ged.useConstantValues(constIndex,3.0);
398
399 ged.initialize(importer,ghostedMap,ownedMap);
400
401 TEST_THROW(ged.useConstantValues(constIndex,4.0),std::logic_error);
402
403 {
404 RCP<Epetra_Vector> ownedVec = rcp(new Epetra_Vector(*ownedMap));
405
406 if(comm.MyPID()==0) {
407 (*ownedVec)[0] = 3.14;
408 (*ownedVec)[1] = 1.82;
409 }
410 else {
411 (*ownedVec)[0] = 2.72;
412 (*ownedVec)[1] = 6.23;
413 }
414
415 // set the owned vector, assure that const can be used
416 ged.setOwnedVector_Epetra(ownedVec.getConst());
417 }
418
419 // actually do something...
420 ged.initializeData();
421 ged.globalToGhost(0);
422
423 // check values making sure that the constants are there
424 {
425 const Epetra_Vector & ghostedVecE = *ged.getGhostedVector_Epetra();
426
427 if(comm.MyPID()==0) {
428 TEST_EQUALITY(ghostedVecE[0],2.0); // <= Replaced constant value
429 TEST_EQUALITY(ghostedVecE[1],3.14);
430 TEST_EQUALITY(ghostedVecE[2],1.82);
431 TEST_EQUALITY(ghostedVecE[3],2.72);
432 }
433 else {
434 TEST_EQUALITY(ghostedVecE[0],1.82);
435 TEST_EQUALITY(ghostedVecE[1],2.72);
436 TEST_EQUALITY(ghostedVecE[2],6.23);
437 TEST_EQUALITY(ghostedVecE[3],3.0); // <= Replaced constant value
438 }
439 }
440 }
441
442 } // end namespace panzer