1 #include <config.h>
2 
3 #include <iostream>
4 #include <sstream>
5 #include <string>
6 
7 #include <dune/common/parallel/mpihelper.hh>
8 
9 #include <dune/grid/cacheitgrid.hh>
10 #include <dune/grid/io/file/dgfparser/dgfwriter.hh>
11 
12 #include <dune/grid/test/gridcheck.hh>
13 
14 #include <dune/grid/test/checkgeometryinfather.hh>
15 #include <dune/grid/test/checkintersectionit.hh>
16 #include <dune/grid/test/checkcommunicate.hh>
17 #include <dune/grid/test/checkiterators.hh>
18 #include <dune/grid/test/checkpartition.hh>
19 
20 using namespace Dune;
21 
22 template <bool leafconform, class Grid>
checkCapabilities(const Grid & grid)23 void checkCapabilities(const Grid& grid)
24 {
25   static_assert( Dune::Capabilities::isLevelwiseConforming< Grid > :: v == ! leafconform,
26                  "isLevelwiseConforming is not set correctly");
27   static_assert( Dune::Capabilities::isLeafwiseConforming< Grid > :: v == leafconform,
28                  "isLevelwiseConforming is not set correctly");
29    static const bool hasEntity = Dune::Capabilities::hasEntity<Grid, 1> :: v == true;
30   static_assert( hasEntity,
31                  "hasEntity is not set correctly");
32   static_assert( Dune::Capabilities::hasBackupRestoreFacilities< Grid > :: v == true,
33                  "hasBackupRestoreFacilities is not set correctly");
34 
35    static const bool reallyParallel =
36 #if ALU3DGRID_PARALLEL && ALU2DGRID_PARALLEL
37     true ;
38 #elif ALU3DGRID_PARALLEL
39     Grid :: dimension == 3;
40 #else
41     false ;
42 #endif
43    static const bool reallyCanCommunicate =
44 #if ALU3DGRID_PARALLEL && ALU2DGRID_PARALLEL
45     true ;
46 #elif ALU3DGRID_PARALLEL
47     Grid :: dimension == 3;
48 #else
49     false ;
50 #endif
51    static const bool canCommunicate = Dune::Capabilities::canCommunicate< Grid, 1 > :: v
52      == reallyCanCommunicate;
53   static_assert( canCommunicate,
54                  "canCommunicate is not set correctly");
55 }
56 
57 template <class GridType>
makeNonConfGrid(GridType & grid,int level,int adapt)58 void makeNonConfGrid(GridType &grid,int level,int adapt) {
59   int myrank = grid.comm().rank();
60   grid.loadBalance();
61   grid.globalRefine(level);
62   grid.loadBalance();
63   for (int i=0;i<adapt;i++)
64   {
65     if (myrank==0)
66     {
67       typedef typename GridType :: template Codim<0> ::
68             template Partition<Interior_Partition> :: LeafIterator LeafIterator;
69 
70       LeafIterator endit = grid.template leafend<0,Interior_Partition>   ();
71       int nr = 0;
72       int size = grid.size(0);
73       for(LeafIterator it    = grid.template leafbegin<0,Interior_Partition> ();
74           it != endit ; ++it,nr++ )
75       {
76         grid.mark(1, *it );
77         if (nr>size*0.8) break;
78       }
79     }
80     grid.adapt();
81     grid.postAdapt();
82     grid.loadBalance();
83   }
84 }
85 
86 template <class GridView>
writeFile(const GridView & gridView)87 void writeFile( const GridView& gridView )
88 {
89   DGFWriter< GridView > writer( gridView );
90   writer.write( "dump.dgf" );
91 }
92 
93 template< class Grid >
doCheck(Grid & grid,const bool display=false)94 void doCheck ( Grid &grid, const bool display = false )
95 {
96   std::cerr << ">>> Checking grid..." << std::endl;
97   gridcheck( grid );
98   checkIterators( grid.leafGridView() );
99   checkPartitionType( grid.leafGridView() );
100   std::cerr << ">>> Checking intersections..." << std::endl;
101   checkIntersectionIterator( grid );
102   if( grid.maxLevel() > 0 )
103   {
104     std::cerr << ">>> Checking geometry in father..." << std::endl;
105     checkGeometryInFather( grid );
106   }
107 
108   std::cerr << ">>> Checking communication..." << std::endl;
109   checkCommunication( grid, -1, std::cout );
110 }
111 
112 template< class Grid >
checkSerial(Grid & grid,const int maxLevel=2,const bool display=false)113 void checkSerial ( Grid &grid, const int maxLevel = 2, const bool display = false )
114 {
115   doCheck( grid, display );
116 
117   for( int i = 0; i < maxLevel; ++i )
118   {
119     std::cerr << ">>> Refining grid globally..." << std::endl;
120     grid.globalRefine( 1 );
121     doCheck( grid, display );
122   }
123 
124   // check also non-conform grids
125   std::cerr << ">>> Trying to make grid non-conform..." << std::endl;
126   makeNonConfGrid( grid, 0, 1 );
127   doCheck( grid, display );
128 }
129 
130 template <class GridType>
checkParallel(GridType & grid,int gref,int mxl=3,const bool display=false)131 void checkParallel(GridType & grid, int gref, int mxl = 3, const bool display = false )
132 {
133 #if HAVE_MPI
134   makeNonConfGrid(grid,gref,mxl);
135 
136   // -1 stands for leaf check
137   checkCommunication(grid, -1, std::cout);
138 
139   for(int l=0; l<= mxl; ++l)
140     checkCommunication(grid, l , Dune::dvverb);
141 #endif
142 }
143 
144 
main(int argc,char ** argv)145 int main (int argc , char **argv)
146 {
147 
148   // this method calls MPI_Init, if MPI is enabled
149   MPIHelper & mpihelper = MPIHelper::instance(argc,argv);
150   int myrank = mpihelper.rank();
151   int mysize = mpihelper.size();
152 
153   try {
154     /* use grid-file appropriate for dimensions */
155 
156     if( argc < 2 )
157     {
158       std::cerr << "Usage: " << argv[0] << " <dgf file of hostgrid>" << std::endl;
159       return 1;
160     }
161 
162     const bool display = (argc > 2);
163 
164     typedef Dune::GridSelector :: GridType HostGridType ;
165     std::string filename( argv[1] );
166     GridPtr< HostGridType > hostGrid( filename );
167 
168     typedef CacheItGrid< HostGridType > CacheItGridType;
169     CacheItGridType grid( *hostGrid );
170 
171     //grid.loadBalance();
172 
173     {
174       std::cout << "Check serial grid" << std::endl;
175       checkSerial(grid,
176                      (mysize == 1) ? 1 : 0,
177                      (mysize == 1) ? display: false);
178     }
179 
180     // perform parallel check only when more then one proc
181     if(mysize > 1)
182     {
183       if (myrank == 0) std::cout << "Check conform grid" << std::endl;
184       checkParallel(grid,1,0, display );
185       if (myrank == 0) std::cout << "Check non-conform grid" << std::endl;
186       checkParallel(grid,0,2, display );
187     }
188 
189   }
190   catch (Dune::Exception &e)
191   {
192     std::cerr << e << std::endl;
193     return 1;
194   }
195   catch (...)
196   {
197     std::cerr << "Generic exception!" << std::endl;
198     return 2;
199   }
200 
201   return 0;
202 }
203