Reference documentation for deal.II version 9.0.0
intergrid_map.cc
1 // ---------------------------------------------------------------------
2 //
3 // Copyright (C) 1999 - 2017 by the deal.II authors
4 //
5 // This file is part of the deal.II library.
6 //
7 // The deal.II library is free software; you can use it, redistribute
8 // it, and/or modify it under the terms of the GNU Lesser General
9 // Public License as published by the Free Software Foundation; either
10 // version 2.1 of the License, or (at your option) any later version.
11 // The full text of the license can be found in the file LICENSE at
12 // the top level of the deal.II distribution.
13 //
14 // ---------------------------------------------------------------------
15 
16 #include <deal.II/base/memory_consumption.h>
17 #include <deal.II/base/smartpointer.h>
18 #include <deal.II/grid/tria.h>
19 #include <deal.II/dofs/dof_handler.h>
20 #include <deal.II/fe/fe.h>
21 #include <deal.II/grid/tria_accessor.h>
22 #include <deal.II/dofs/dof_accessor.h>
23 #include <deal.II/grid/tria_iterator.h>
24 #include <deal.II/grid/intergrid_map.h>
25 #include <deal.II/distributed/tria.h>
26 #include <deal.II/distributed/shared_tria.h>
27 
28 
29 DEAL_II_NAMESPACE_OPEN
30 
31 
32 template <class MeshType>
34  :
35  source_grid(nullptr, typeid(*this).name()),
36  destination_grid(nullptr, typeid(*this).name())
37 {}
38 
39 
40 
41 
42 template <class MeshType>
43 void InterGridMap<MeshType>::make_mapping (const MeshType &source_grid,
44  const MeshType &destination_grid)
45 {
46  // first delete all contents
47  clear ();
48 
49  // next store pointers to grids
50  this->source_grid = &source_grid;
51  this->destination_grid = &destination_grid;
52 
53  // then set up the meshes from
54  // scratch and fill them with end-iterators
55  const unsigned int n_levels = source_grid.get_triangulation().n_levels();
56  mapping.resize (n_levels);
57  for (unsigned int level=0; level<n_levels; ++level)
58  {
59  // first find out about the highest
60  // index used on this level. We could
61  // in principle ask the triangulation
62  // about this, but we would have to
63  // know the underlying data structure
64  // for this and we would like to
65  // avoid such knowledge here
66  unsigned int n_cells = 0;
67  cell_iterator cell = source_grid.begin(level),
68  endc = source_grid.end(level);
69  for (; cell!=endc; ++cell)
70  if (static_cast<unsigned int>(cell->index()) > n_cells)
71  n_cells = cell->index();
72 
73  // note: n_cells is now the largest
74  // zero-based index, but we need the
75  // number of cells, which is one larger
76  mapping[level].resize (n_cells+1, destination_grid.end());
77  };
78 
79  // now make up the mapping
80  // loop over all cells and set the user
81  // pointers as well as the contents of
82  // the two arrays. note that the function
83  // takes a *reference* to the int and
84  // this may change it
85  cell_iterator src_cell = source_grid.begin(0),
86  dst_cell = destination_grid.begin(0),
87  endc = source_grid.end(0);
88  for (; src_cell!=endc; ++src_cell, ++dst_cell)
89  set_mapping (src_cell, dst_cell);
90 
91  // little assertion that the two grids
92  // are indeed related:
93  Assert (dst_cell == destination_grid.end(0),
94  ExcIncompatibleGrids ());
95 }
96 
97 
98 
99 template <class MeshType>
100 void
102  const cell_iterator &dst_cell)
103 {
104  // first set the map for this cell
105  mapping[src_cell->level()][src_cell->index()] = dst_cell;
106 
107  // if both cells have children, we may
108  // recurse further into the hierarchy
109  if (src_cell->has_children() && dst_cell->has_children())
110  {
111  Assert(src_cell->n_children()==
114  Assert(dst_cell->n_children()==
117  Assert(src_cell->refinement_case()==dst_cell->refinement_case(),
119  for (unsigned int c=0; c<GeometryInfo<MeshType::dimension>::max_children_per_cell; ++c)
120  set_mapping (src_cell->child(c),
121  dst_cell->child(c));
122  }
123  else if (src_cell->has_children() &&
124  !dst_cell->has_children())
125  // src grid is more refined here.
126  // set entries for all children
127  // of this cell to the one
128  // dst_cell
129  for (unsigned int c=0; c<src_cell->n_children(); ++c)
130  set_entries_to_cell (src_cell->child(c),
131  dst_cell);
132  // else (no cell is refined or
133  // dst_cell is refined): no pointers
134  // to be set
135 }
136 
137 
138 
139 template <class MeshType>
140 void
142  const cell_iterator &dst_cell)
143 {
144  // first set the map for this cell
145  mapping[src_cell->level()][src_cell->index()] = dst_cell;
146 
147  // then do so for the children as well
148  // if there are any
149  if (src_cell->has_children())
150  for (unsigned int c=0; c<src_cell->n_children(); ++c)
151  set_entries_to_cell (src_cell->child(c),
152  dst_cell);
153 }
154 
155 
156 template <class MeshType>
159 {
160  Assert (source_cell.state() == IteratorState::valid,
161  ExcInvalidKey (source_cell));
162  Assert (source_cell->level() <= static_cast<int>(mapping.size()),
163  ExcInvalidKey (source_cell));
164  Assert (source_cell->index() <= static_cast<int>(mapping[source_cell->level()].size()),
165  ExcInvalidKey (source_cell));
166 
167  return mapping[source_cell->level()][source_cell->index()];
168 }
169 
170 
171 
172 template <class MeshType>
174 {
175  mapping.clear ();
176  source_grid = nullptr;
177  destination_grid = nullptr;
178 }
179 
180 
181 
182 template <class MeshType>
183 const MeshType &
185 {
186  return *source_grid;
187 }
188 
189 
190 
191 template <class MeshType>
192 const MeshType &
194 {
195  return *destination_grid;
196 }
197 
198 
199 
200 template <class MeshType>
201 std::size_t
203 {
204  return (MemoryConsumption::memory_consumption (mapping) +
206  MemoryConsumption::memory_consumption (destination_grid));
207 }
208 
209 
210 
211 // explicit instantiations
212 #include "intergrid_map.inst"
213 
214 DEAL_II_NAMESPACE_CLOSE
215 
MeshType::cell_iterator cell_iterator
cell_iterator operator[](const cell_iterator &source_cell) const
const MeshType & get_destination_grid() const
void set_entries_to_cell(const cell_iterator &src_cell, const cell_iterator &dst_cell)
const MeshType & get_source_grid() const
std::size_t memory_consumption() const
#define Assert(cond, exc)
Definition: exceptions.h:1142
void make_mapping(const MeshType &source_grid, const MeshType &destination_grid)
static ::ExceptionBase & ExcNotImplemented()
Iterator points to a valid object.
std::enable_if< std::is_fundamental< T >::value, std::size_t >::type memory_consumption(const T &t)
void set_mapping(const cell_iterator &src_cell, const cell_iterator &dst_cell)