Newer
Older

Vít Hanousek
committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/***************************************************************************
DistributedGridTest.cpp - description
-------------------
begin : Sep 6, 2017
copyright : (C) 2017 by Tomas Oberhuber et al.
email : tomas.oberhuber@fjfi.cvut.cz
***************************************************************************/
#ifdef HAVE_GTEST
#include <gtest/gtest.h>
#ifdef HAVE_MPI
#define USE_MPI
#include <TNL/Meshes/DistributedGrid.h>
#include <TNL/Meshes/DistributedGridSynchronizer.h>
#include <TNL/Functions/MeshFunction.h>
#include <mpi.h>
#include "Functions.h"
using namespace TNL;
using namespace TNL::Containers;
using namespace TNL::Meshes;
using namespace TNL::Functions;
using namespace TNL::Devices;
template<typename DofType>
void setDof_1D(DofType &dof, typename DofType::RealType value)
{
for(int i=0;i<dof.getSize();i++)
dof[i]=value;
}
template<typename DofType>
void check_Boundary_1D(int rank, int nproc, DofType dof, typename DofType::RealType expectedValue)
{
if(rank==0)//Left
{
EXPECT_EQ( dof[0], expectedValue) << "Left boundary test failed";
return;
}
if(rank==(nproc-1))//Right
{
EXPECT_EQ( dof[dof.getSize()-1], expectedValue) << "Right boundary test failed";
return;
}
};
template<typename DofType>
void check_Overlap_1D(int rank, int nproc, DofType dof, typename DofType::RealType expectedValue)
{
if(rank==0)//Left
{
EXPECT_EQ( dof[dof.getSize()-1], expectedValue) << "Left boundary node overlap test failed";
return;
}
if(rank==(nproc-1))
{
EXPECT_EQ( dof[0], expectedValue) << "Right boundary node overlap test failed";
return;
}
EXPECT_EQ( dof[0], expectedValue) << "left overlap test failed";
EXPECT_EQ( dof[dof.getSize()-1], expectedValue)<< "right overlap test failed";
};
template<typename DofType>
void check_Inner_1D(int rank, int nproc, DofType dof, typename DofType::RealType expectedValue)
{
for(int i=1;i<(dof.getSize()-2);i++) //buď je vlevo hranice, nebo overlap
EXPECT_EQ( dof[i], expectedValue) << " "<< i;
};
/*
* Light check of 1D distriover grid and its synchronization.
* Number of process is not limitated.
* Overlap is limitated to 1
* Only double is tested as dof Real type -- it may be changed, extend test
* Global size is hardcoded as 10 -- it can be changed, extend test
*/
typedef Grid<1,double,Host,int> MeshType;
typedef MeshFunction<MeshType> MeshFunctionType;
typedef Vector<double,Host,int> DofType;
typedef typename MeshType::Cell Cell;
typedef typename MeshType::IndexType IndexType;
typedef typename MeshType::PointType PointType;

Vít Hanousek
committed
typedef DistributedGrid<MeshType> DistributedGridType;

Vít Hanousek
committed
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
class DistributedGirdTest_1D : public ::testing::Test {
protected:
static DistributedGrid<MeshType> *distrgrid;
static DistributedGridSynchronizer<DistributedGrid<MeshType>,MeshFunctionType,1> *synchronizer;
static DofType *dof;
static SharedPointer<MeshType> gridptr;
static SharedPointer<MeshFunctionType> meshFunctionptr;
static MeshFunctionEvaluator< MeshFunctionType, ConstFunction<double,1> > constFunctionEvaluator;
static SharedPointer< ConstFunction<double,1>, Host > constFunctionPtr;
static MeshFunctionEvaluator< MeshFunctionType, LinearFunction<double,1> > linearFunctionEvaluator;
static SharedPointer< LinearFunction<double,1>, Host > linearFunctionPtr;
static int rank;
static int nproc;
// Per-test-case set-up.
// Called before the first test in this test case.
// Can be omitted if not needed.
static void SetUpTestCase() {
int size=10;
rank=MPI::COMM_WORLD.Get_rank();
nproc=MPI::COMM_WORLD.Get_size();
PointType globalOrigin;
PointType globalProportions;
MeshType globalGrid;
globalOrigin.x()=-0.5;
globalProportions.x()=size;
globalGrid.setDimensions(size);
globalGrid.setDomain(globalOrigin,globalProportions);

Vít Hanousek
committed
typename DistributedGridType::CoordinatesType overlap;
overlap.setValue(1);
distrgrid=new DistributedGrid<MeshType> (globalGrid,overlap);

Vít Hanousek
committed
distrgrid->SetupGrid(*gridptr);
dof=new DofType(gridptr->template getEntitiesCount< Cell >());
meshFunctionptr->bind(gridptr,*dof);

Vít Hanousek
committed
synchronizer=new DistributedGridSynchronizer<DistributedGrid<MeshType>,MeshFunctionType,1>(distrgrid);

Vít Hanousek
committed
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
constFunctionPtr->Number=rank;
}
// Per-test-case tear-down.
// Called after the last test in this test case.
// Can be omitted if not needed.
static void TearDownTestCase() {
delete dof;
delete synchronizer;
delete distrgrid;
}
};
DistributedGrid<MeshType> *DistributedGirdTest_1D::distrgrid=NULL;
DistributedGridSynchronizer<DistributedGrid<MeshType>,MeshFunctionType,1> *DistributedGirdTest_1D::synchronizer=NULL;
DofType *DistributedGirdTest_1D::dof=NULL;
SharedPointer<MeshType> DistributedGirdTest_1D::gridptr;
SharedPointer<MeshFunctionType> DistributedGirdTest_1D::meshFunctionptr;
MeshFunctionEvaluator< MeshFunctionType, ConstFunction<double,1> > DistributedGirdTest_1D::constFunctionEvaluator;
SharedPointer< ConstFunction<double,1>, Host > DistributedGirdTest_1D::constFunctionPtr;
MeshFunctionEvaluator< MeshFunctionType, LinearFunction<double,1> > DistributedGirdTest_1D::linearFunctionEvaluator;
SharedPointer< LinearFunction<double,1>, Host > DistributedGirdTest_1D::linearFunctionPtr;
int DistributedGirdTest_1D::rank;
int DistributedGirdTest_1D::nproc;
TEST_F(DistributedGirdTest_1D, evaluateAllEntities)
{
//Check Traversars
//All entities, witout overlap
setDof_1D(*dof,-1);
constFunctionEvaluator.evaluateAllEntities( meshFunctionptr , constFunctionPtr );
//Printer<MeshType,DofType>::print_dof(rank,*gridptr,*dof);

Vít Hanousek
committed
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
check_Boundary_1D(rank, nproc, *dof, rank);
check_Overlap_1D(rank, nproc, *dof, -1);
check_Inner_1D(rank, nproc, *dof, rank);
}
TEST_F(DistributedGirdTest_1D, evaluateBoundaryEntities)
{
//Boundary entities, witout overlap
setDof_1D(*dof,-1);
constFunctionEvaluator.evaluateBoundaryEntities( meshFunctionptr , constFunctionPtr );
check_Boundary_1D(rank, nproc, *dof, rank);
check_Overlap_1D(rank, nproc, *dof, -1);
check_Inner_1D(rank, nproc, *dof, -1);
}
TEST_F(DistributedGirdTest_1D, evaluateInteriorEntities)
{
//Inner entities, witout overlap
setDof_1D(*dof,-1);
constFunctionEvaluator.evaluateInteriorEntities( meshFunctionptr , constFunctionPtr );
check_Boundary_1D(rank, nproc, *dof, -1);
check_Overlap_1D(rank, nproc, *dof, -1);
check_Inner_1D(rank, nproc, *dof, rank);
}
TEST_F(DistributedGirdTest_1D, LinearFunctionTest)
{
//fill meshfunction with linear function (physical center of cell corresponds with its coordinates in grid)
setDof_1D(*dof,-1);
linearFunctionEvaluator.evaluateAllEntities(meshFunctionptr, linearFunctionPtr);

Vít Hanousek
committed
synchronizer->Synchronize(*meshFunctionptr);

Vít Hanousek
committed
auto entite= gridptr->template getEntity< Cell >(0);
entite.refresh();
EXPECT_EQ(meshFunctionptr->getValue(entite), (*linearFunctionPtr)(entite)) << "Linear function Overlap error on left Edge.";
auto entite2= gridptr->template getEntity< Cell >((*dof).getSize()-1);
entite2.refresh();
EXPECT_EQ(meshFunctionptr->getValue(entite), (*linearFunctionPtr)(entite)) << "Linear function Overlap error on right Edge.";
}
TEST_F(DistributedGirdTest_1D, SynchronizerNeighborTest)
{
setDof_1D(*dof,-1);
constFunctionEvaluator.evaluateAllEntities( meshFunctionptr , constFunctionPtr );

Vít Hanousek
committed
synchronizer->Synchronize(*meshFunctionptr);

Vít Hanousek
committed
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
if(rank!=0)
EXPECT_EQ((*dof)[0],rank-1)<< "Left Overlap was filled by wrong process.";
if(rank!=nproc-1)
EXPECT_EQ((*dof)[dof->getSize()-1],rank+1)<< "Right Overlap was filled by wrong process.";
}
#else
TEST(NoMPI, NoTest)
{
ASSERT_TRUE(true) << ":-(";
}
#endif
#endif
#if (defined(HAVE_GTEST) && defined(HAVE_MPI))
#include <sstream>
class MinimalistBuffredPrinter : public ::testing::EmptyTestEventListener {
private:
std::stringstream sout;
public:
// Called before a test starts.
virtual void OnTestStart(const ::testing::TestInfo& test_info) {
sout<< test_info.test_case_name() <<"." << test_info.name() << " Start." <<std::endl;
}
// Called after a failed assertion or a SUCCEED() invocation.
virtual void OnTestPartResult(
const ::testing::TestPartResult& test_part_result) {
sout << (test_part_result.failed() ? "====Failure=== " : "===Success=== ")
<< test_part_result.file_name() << " "
<< test_part_result.line_number() <<std::endl
<< test_part_result.summary() <<std::endl;
}
// Called after a test ends.
virtual void OnTestEnd(const ::testing::TestInfo& test_info)
{
int rank=MPI::COMM_WORLD.Get_rank();
sout<< test_info.test_case_name() <<"." << test_info.name() << " End." <<std::endl;
std::cout << rank << ":" << std::endl << sout.str()<< std::endl;
sout.str( std::string() );
sout.clear();
}
};
#endif
#include "../../src/UnitTests/GtestMissingError.h"
int main( int argc, char* argv[] )
{
#ifdef HAVE_GTEST
::testing::InitGoogleTest( &argc, argv );
#ifdef HAVE_MPI
::testing::TestEventListeners& listeners =
::testing::UnitTest::GetInstance()->listeners();
delete listeners.Release(listeners.default_result_printer());
listeners.Append(new MinimalistBuffredPrinter);
MPI::Init(argc,argv);
#endif
int result= RUN_ALL_TESTS();
#ifdef HAVE_MPI
MPI::Finalize();
#endif
return result;
#else
throw GtestMissingError();
#endif
}