File: testiteratorranges.cc

package info (click to toggle)
dune-grid 2.11.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 8,472 kB
  • sloc: cpp: 60,883; python: 1,438; perl: 191; makefile: 12; sh: 3
file content (193 lines) | stat: -rw-r--r-- 4,930 bytes parent folder | download | duplicates (2)
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
94
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
135
136
137
138
139
140
141
142
143
144
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
// SPDX-FileCopyrightText: Copyright © DUNE Project contributors, see file LICENSE.md in module root
// SPDX-License-Identifier: LicenseRef-GPL-2.0-only-with-DUNE-exception
// -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
// vi: set et ts=4 sw=2 sts=2:

#include <iostream>

#include <dune/common/parallel/mpihelper.hh>
#include <dune/grid/yaspgrid.hh>

#define VERIFY(t,msg) do { if (!((t))) DUNE_THROW(Dune::Exception, "Check " #t " failed (" msg ")"); } while (false)

template<typename GV, typename R>
void checkRange(GV gv, int codim, R range, bool verify_less = false)
{
  int count = 0;
  int indices = 0;
  for (auto& e : range)
    {
      VERIFY(codim == e.codimension,"wrong codimension");
      ++count;
      indices += gv.indexSet().index(e);
    }
  if (verify_less)
    VERIFY(count <= gv.size(codim),"wrong number of elements for codim " << codim << " ");
  else
    VERIFY(count == gv.size(codim),"wrong number of elements for codim " << codim << " ");
}


// The grid *must* be refined at least once!
template<typename Grid, typename OS>
void check_ranges(const Grid& grid, OS&& os) {

  const int dim = Grid::dimension;

  auto gv = grid.leafGridView();

  os << "Checking entity ranges with default PartitionSet... ";

  // check elements
  checkRange(gv,0,elements(gv));
  // check facets
  checkRange(gv,1,facets(gv));
  // check edges
  checkRange(gv,dim-1,edges(gv));
  // check vertices
  checkRange(gv,dim,vertices(gv));
  // check elements using codim
  checkRange(gv,0,entities(gv,Dune::Codim<0>()));
  // check facets using codim
  checkRange(gv,1,entities(gv,Dune::Codim<1>()));
  // check edges using dim
  checkRange(gv,dim-1,entities(gv,Dune::Dim<1>()));
  // check vertices using dim
  checkRange(gv,dim,entities(gv,Dune::Dim<0>()));

  os << "OK" << std::endl;
  os << "Checking entity ranges with non-default PartitionSet... ";

  // check versions with PartitionSet parameter
  checkRange(gv,0,elements(gv,Dune::Partitions::interiorBorder),true);
  checkRange(gv,1,facets(gv,Dune::Partitions::all),true);
  checkRange(gv,dim-1,edges(gv,Dune::Partitions::interior + Dune::Partitions::border),true);
  checkRange(gv,dim,vertices(gv,Dune::Partitions::interiorBorder + Dune::Partitions::overlap + Dune::Partitions::front),false);
  checkRange(gv,1,entities(gv,Dune::Codim<1>(),Dune::Partitions::interiorBorder),true);
  checkRange(gv,dim-1,entities(gv,Dune::Dim<1>(),Dune::Partitions::all - Dune::Partitions::ghost),true);

  os << "OK" << std::endl;
  os << "Checking hierarchic entity range... ";

  // check hierarchic entity range
  {
    int count = 0;
    int indices = 0;

    // we need a LevelGridView higher up in the hierarchy for this
    auto gv = grid.levelGridView(grid.maxLevel()-1);

    for (auto& e : descendantElements(*elements(gv).begin(),grid.maxLevel()))
      {
        ++count;
        indices += grid.levelGridView(e.level()).indexSet().index(e);
      }

    VERIFY(count == 1 << dim,"wrong number of descendant elements");
  }

  os << "OK" << std::endl;
  os << "Checking intersection range... ";

  // check intersections
  {
    int count = 0;
    int indices = 0;

    for (auto& is : intersections(gv,*elements(gv).begin()))
      {
        ++count;
        indices += is.indexInInside();
      }

    VERIFY(count == 2 * dim,"wrong number of intersections");
  }

  os << "OK" << std::endl;
}


// little helper to only print output on rank 0

template<typename S, typename C>
struct Rank0Stream
{

  Rank0Stream(S& s, C c)
    : _s(s)
    , _c(c)
  {}

  template<typename T>
  Rank0Stream& operator<<(const T& t)
  {
    if (_c.rank() == 0)
      _s << t;
    return *this;
  }

  // needed for manipulators (std::endl etc.)
  Rank0Stream& operator<<(std::ostream& (*f)(std::ostream&))
  {
    if (_c.rank() == 0)
      f(_s);
    return *this;
  }

  S& _s;
  C _c;

};

template<typename S, typename C>
Rank0Stream<S,C> rank0Stream(S& s, C c)
{
  return {s,c};
}

template<typename OS>
void check_yasp_3d(OS&& os)
{

  os << "Running tests on 3D YaspGrid..." << std::endl;

  const int dim = 3;

  Dune::FieldVector<double,dim> Len(1.0);
  std::array<int,dim> s;
  std::fill(s.begin(), s.end(), 8);
  std::bitset<dim> p;
  int overlap = 1;

  Dune::YaspGrid<dim> grid(Len,s,p,overlap);

#if HAVE_MPI
  os << "Parallel run on " << grid.comm().size() << " processors" << std::endl;
#else
  os << "Sequential run" << std::endl;
#endif

  // refine once so that we can check the hierarchic iterator range
  grid.globalRefine(1);

  check_ranges(grid,os);
}


int main(int argc , char **argv) {
  try {
    // Initialize MPI, if present

    Dune::MPIHelper::instance(argc, argv);
    check_yasp_3d(rank0Stream(std::cout,Dune::MPIHelper::getCommunication()));

  } catch (Dune::Exception &e) {
    std::cerr << e << std::endl;
    return 1;
  } catch (...) {
    std::cerr << "Generic exception!" << std::endl;
    return 2;
  }

  return 0;
}