HyperHDG
lexicographic.h
Go to the documentation of this file.
1 #ifndef TPCC_LEXICOGRAPHIC_H
2 #define TPCC_LEXICOGRAPHIC_H
3 
4 #include <tpcc/element.h>
5 
6 namespace TPCC
7 {
8 
10 {
14 };
42 template <int n, int k, boundaries bnd = both, typename Bint = unsigned int,
43  typename Sint = unsigned short, typename Tint = unsigned char>
45 {
49  std::array<Sint, n> dimensions;
53  std::array<Bint, binomial(n, k)> block_sizes;
54 
55  std::array<Bint, binomial(n, k)> block_sizes_bnd;
56 
57 public:
59  static constexpr Tint order() { return n; }
60 
62  static constexpr Tint cell_dimension() { return k; }
63 
65  typedef Bint global_index_t;
67  typedef Sint fiber_index_t;
69  typedef Tint dimension_index_t;
70 
73 
77  constexpr Lexicographic(const std::array<Sint, n>& d)
78  : dimensions(d)
79  {
80  Combinations<n, k> combinations;
81  for (Tint i = 0; i < binomial(n, k); ++i)
82  {
83  Bint p = 1;
84  auto combination = combinations[i];
85  for (Tint j = 0; j < k; ++j)
86  p *= dimensions[n - 1 - combination.in(j)];
87  for (Tint j = 0; j < n - k; ++j)
88  switch (bnd)
89  {
90  case both:
91  p *= dimensions[n - 1 - combination.out(j)] + 1;
92  break;
93  case none:
94  p *= dimensions[n - 1 - combination.out(j)] - 1;
95  break;
96  default: // perioridic
97  p *= dimensions[n - 1 - combination.out(j)];
98  break;
99  }
100  block_sizes_bnd[i] = p;
101  }
102  for (Tint i = 0; i < binomial(n, k); ++i)
103  {
104  Bint p = 1;
105  auto combination = combinations[i];
106  for (Tint j = 0; j < k; ++j)
107  p *= dimensions[n - 1 - combination.in(j)];
108  for (Tint j = 0; j < n - k; ++j)
109  p *= dimensions[n - 1 - combination.out(j)] + 1;
110  block_sizes[i] = p;
111  }
112  }
113 
117  constexpr Bint size() const
118  {
119  Bint sum = 0;
120  for (unsigned int i = 0; i < block_sizes.size(); ++i)
121  sum += block_sizes_bnd[i];
122  return sum;
123  }
124 
128  constexpr Bint block_size(Tint block) const { return block_sizes[block]; }
129 
131  constexpr Sint fiber_dimension(Tint i) const { return dimensions[i]; }
132 
136  value_type operator[](Bint index) const;
137 
138  Bint index_in_slice(const value_type& e) const
139  {
140  Bint result = 0, factor = 1;
141  for (Tint i = 0; i < k; ++i)
142  {
143  Sint fdim = dimensions[e.along_direction(i)];
144  result += e.along_coordinate(i) * factor;
145  factor *= fdim;
146  }
147  return result;
148  }
149 
153  Bint index(const value_type& e) const;
154 
155  template <boundaries bndT = bnd>
156  constexpr Lexicographic<n, k - 1, bndT, Bint, Sint, Tint> boundary() const
157  {
158  return Lexicographic<n, k - 1, bndT, Bint, Sint, Tint>{ dimensions };
159  }
160 };
161 
162 template <int n, int k, boundaries bnd, typename Bint, typename Sint, typename Tint>
164 {
165  unsigned int b = 0;
166  while (b < block_sizes.size())
167  {
168  if (index < block_sizes[b])
169  break;
170  index -= block_sizes[b];
171  ++b;
172  }
173 
174  if (b == block_sizes.size())
175  throw(b);
176 
177  Combinations<n, k> combinations;
178  auto combination = combinations[b];
179 
180  std::array<Sint, n> coordinates;
181  for (int i = 0; i < k; ++i)
182  {
183  Sint fdim = dimensions[n - 1 - combination.in(i)];
184  coordinates[n - 1 - combination.in(i)] = index % fdim;
185  index /= fdim;
186  }
187  for (int i = 0; i < n - k; ++i)
188  {
189  Sint fdim = 1 + dimensions[n - 1 - combination.out(i)];
190  coordinates[n - 1 - combination.out(i)] = index % fdim;
191  index /= fdim;
192  }
193  return Element<n, k, Sint, Tint>{ combination, coordinates };
194 }
195 
196 template <int n, int k, boundaries bnd, typename Bint, typename Sint, typename Tint>
198 {
199  Bint ci = e.direction_index();
200  Bint result = 0;
201  for (unsigned int i = 0; i < ci; ++i)
202  result += block_sizes_bnd[i];
203 
204  Bint factor = 1;
205  for (Tint i = 0; i < k; ++i)
206  {
207  Sint fdim = dimensions[e.along_direction(i)];
208  result += e.along_coordinate(i) * factor;
209  factor *= fdim;
210  }
211  for (unsigned int i = 0; i < n - k; ++i)
212  {
213  Tint fdim = 1 + dimensions[e.across_direction(i)];
214 
215  if (bnd == periodic)
216  fdim -= 1;
217  else if (bnd == none)
218  fdim -= 2;
219 
220  Bint cross_coord = e.across_coordinate(i);
221  // std::cout << "fdim " << e.across_direction(i) << " " << fdim << " " << e.across_coordinate(i)
222  // << std::endl;
223  assert((cross_coord != 0 && cross_coord != fdim + 1) || bnd != none);
224  if (cross_coord == fdim && bnd == periodic)
225  cross_coord = 0;
226  else if (bnd == none)
227  --cross_coord;
228 
229  result += cross_coord * factor;
230  factor *= fdim;
231  }
232  return result;
233 }
234 
235 } // namespace TPCC
236 
237 #endif
TPCC::Element
Tensor coordinates for a facet of dimension k in the complex of dimension n.
Definition: element.h:38
TPCC::Lexicographic::fiber_dimension
constexpr Sint fiber_dimension(Tint i) const
Dimension of the fiber with given index in the tensor product.
Definition: lexicographic.h:131
TPCC::Element::along_direction
constexpr Tint along_direction(Tint index) const
Mapping from local to global coordinate directions.
Definition: element.h:84
check_push_test.index
index
Definition: check_push_test.py:10
TPCC::both
@ both
Definition: lexicographic.h:11
TPCC::Lexicographic::Lexicographic
constexpr Lexicographic(const std::array< Sint, n > &d)
Constructor setting the dimensions of the complex.
Definition: lexicographic.h:77
TPCC::boundaries
boundaries
Definition: lexicographic.h:9
TPCC::Element::across_coordinate
constexpr Sint across_coordinate(Tint index) const
The position of the element orthogonal to its extension.
Definition: element.h:95
TPCC::Lexicographic::block_size
constexpr Bint block_size(Tint block) const
The number of elements in one direction.
Definition: lexicographic.h:128
TPCC::Lexicographic::fiber_index_t
Sint fiber_index_t
Index type for addressing in the fibers.
Definition: lexicographic.h:67
TPCC::none
@ none
Definition: lexicographic.h:12
TPCC::Lexicographic::operator[]
value_type operator[](Bint index) const
Descriptor for the element at given index.
Definition: lexicographic.h:163
TPCC::Lexicographic::index
Bint index(const value_type &e) const
Find index of a given element.
Definition: lexicographic.h:197
TPCC::Lexicographic::size
constexpr Bint size() const
The number of elements in this set.
Definition: lexicographic.h:117
TPCC::Lexicographic::dimension_index_t
Tint dimension_index_t
Index type for addressing dimensions and elements of the chain complex.
Definition: lexicographic.h:69
TPCC::Element::across_direction
constexpr Tint across_direction(Tint index) const
Mapping of orthogonal coordinate directions to global.
Definition: element.h:92
TPCC
Definition: combinations.h:8
TPCC::Lexicographic::block_sizes
std::array< Bint, binomial(n, k)> block_sizes
The number of objects facing the same directions.
Definition: lexicographic.h:53
TPCC::Lexicographic::cell_dimension
static constexpr Tint cell_dimension()
The dimension of the elements, index in the chain complex.
Definition: lexicographic.h:62
element.h
TPCC::Lexicographic
Lexicographic enumeration of the k-dimensional faces in a tensor product chain complex of dimension n...
Definition: lexicographic.h:44
TPCC::Lexicographic::order
static constexpr Tint order()
The tensor order of the chain complex.
Definition: lexicographic.h:59
TPCC::Lexicographic::boundary
constexpr Lexicographic< n, k - 1, bndT, Bint, Sint, Tint > boundary() const
Definition: lexicographic.h:156
TPCC::Lexicographic::global_index_t
Bint global_index_t
Index type for addressing in the tensor product.
Definition: lexicographic.h:65
TPCC::Lexicographic::block_sizes_bnd
std::array< Bint, binomial(n, k)> block_sizes_bnd
Definition: lexicographic.h:55
TPCC::Element::along_coordinate
constexpr Sint along_coordinate(Tint index) const
The coordinates in the k-dimensional element.
Definition: element.h:87
TPCC::Lexicographic::index_in_slice
Bint index_in_slice(const value_type &e) const
Definition: lexicographic.h:138
TPCC::Lexicographic::dimensions
std::array< Sint, n > dimensions
The dimension of the fibers in each direction.
Definition: lexicographic.h:49
TPCC::Lexicographic::value_type
Element< n, k, Sint, Tint > value_type
The type of elements of this set in the complex.
Definition: lexicographic.h:72
TPCC::periodic
@ periodic
Definition: lexicographic.h:13
TPCC::Combinations
Definition: combinations.h:195
TPCC::Element::direction_index
constexpr Tint direction_index() const
The index of the combination enumerating directions.
Definition: element.h:67
TPCC::binomial
constexpr T binomial(T n, T k)
Compute the binomial coefficient n over k.
Definition: combinations.h:17