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 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 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 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343
|
// Copyright (C) 2020 Matthew Scroggs
//
// This file is part of DOLFINx (https://www.fenicsproject.org)
//
// SPDX-License-Identifier: LGPL-3.0-or-later
#include "permutationcomputation.h"
#include "Topology.h"
#include "cell_types.h"
#include <algorithm>
#include <bitset>
#include <dolfinx/common/IndexMap.h>
#include <dolfinx/common/Timer.h>
#include <dolfinx/common/log.h>
#include <dolfinx/graph/AdjacencyList.h>
namespace
{
constexpr int _BITSETSIZE = 32;
} // namespace
using namespace dolfinx;
namespace
{
std::pair<std::int8_t, std::int8_t>
compute_triangle_rot_reflect(const std::vector<std::int32_t>& e_vertices,
const std::vector<std::int64_t>& vertices)
{
// Number of rotations
std::uint8_t min_v
= std::distance(e_vertices.begin(), std::ranges::min_element(e_vertices));
// pre is the (local) number of the next vertex clockwise from the lowest
// numbered vertex
const int pre = e_vertices[(min_v + 2) % 3];
// post is the (local) number of the next vertex anticlockwise from the
// lowest numbered vertex
const int post = e_vertices[(min_v + 1) % 3];
std::uint8_t g_min_v
= std::distance(vertices.begin(), std::ranges::min_element(vertices));
// g_pre is the (global) number of the next vertex clockwise from the lowest
// numbered vertex
const int g_pre = vertices[(g_min_v + 2) % 3];
// g_post is the (global) number of the next vertex anticlockwise from the
// lowest numbered vertex
const int g_post = vertices[(g_min_v + 1) % 3];
std::uint8_t rots = 0;
if (g_post > g_pre)
rots = (g_min_v + 3 - min_v) % 3;
else
rots = (min_v + 3 - g_min_v) % 3;
return {(post > pre) == (g_post < g_pre), rots};
}
//-----------------------------------------------------------------------------
std::pair<std::int8_t, std::int8_t>
compute_quad_rot_reflect(const std::vector<std::int32_t>& e_vertices,
const std::vector<std::int64_t>& vertices)
{
// Find minimum local cell vertex on facet
std::uint8_t min_v
= std::distance(e_vertices.begin(), std::ranges::min_element(e_vertices));
// Table of next and previous vertices
// 0 - 2
// | |
// 1 - 3
const std::array<std::int8_t, 4> prev = {2, 0, 3, 1};
// pre is the (local) number of the next vertex clockwise from the
// lowest numbered vertex
std::int32_t pre = e_vertices[prev[min_v]];
// post is the (local) number of the next vertex anticlockwise
// from the lowest numbered vertex
std::int32_t post = e_vertices[prev[3 - min_v]];
// If min_v is 2 or 3, swap:
// 0 - 2 0 - 3
// | | | |
// 1 - 3 1 - 2
// Because of the dolfinx ordering (left), in order to compute the number of
// anti-clockwise rotations required correctly, min_v is altered to give the
// ordering on the right.
if (min_v == 2 or min_v == 3)
min_v = 5 - min_v;
// Find minimum global vertex in facet
std::uint8_t g_min_v
= std::distance(vertices.begin(), std::ranges::min_element(vertices));
// rots is the number of rotations to get the lowest numbered
// vertex to the origin
// g_pre is the (global) number of the next vertex clockwise from the
// lowest numbered vertex
std::int64_t g_pre = vertices[prev[g_min_v]];
// g_post is the (global) number of the next vertex anticlockwise
// from the lowest numbered vertex
std::int64_t g_post = vertices[prev[3 - g_min_v]];
if (g_min_v == 2 or g_min_v == 3)
g_min_v = 5 - g_min_v;
std::uint8_t rots = 0;
if (g_post > g_pre)
rots = (g_min_v - min_v + 4) % 4;
else
rots = (min_v - g_min_v + 4) % 4;
return {(post > pre) == (g_post < g_pre), rots};
}
//-----------------------------------------------------------------------------
template <int BITSETSIZE>
std::vector<std::bitset<BITSETSIZE>>
compute_triangle_quad_face_permutations(const mesh::Topology& topology,
int cell_index)
{
const std::vector<mesh::CellType>& cell_types = topology.entity_types(3);
mesh::CellType cell_type = cell_types.at(cell_index);
// Get face types of the cell and mesh
const std::vector<mesh::CellType>& mesh_face_types = topology.entity_types(2);
std::vector<mesh::CellType> cell_face_types(
mesh::cell_num_entities(cell_type, 2));
for (std::size_t i = 0; i < cell_face_types.size(); ++i)
cell_face_types[i] = mesh::cell_facet_type(cell_type, i);
// Connectivity for each face type
std::vector<std::shared_ptr<const graph::AdjacencyList<std::int32_t>>> c_to_f;
std::vector<std::shared_ptr<const graph::AdjacencyList<std::int32_t>>> f_to_v;
// Create mapping for each face type to cell-local face index
int tdim = topology.dim();
std::vector<std::vector<int>> face_type_indices(mesh_face_types.size());
for (std::size_t i = 0; i < mesh_face_types.size(); ++i)
{
for (std::size_t j = 0; j < cell_face_types.size(); ++j)
{
if (mesh_face_types[i] == cell_face_types[j])
face_type_indices[i].push_back(j);
}
c_to_f.push_back(topology.connectivity({tdim, cell_index}, {2, int(i)}));
f_to_v.push_back(topology.connectivity({2, int(i)}, {0, 0}));
}
auto c_to_v = topology.connectivity({tdim, cell_index}, {0, 0});
assert(c_to_v);
const std::int32_t num_cells = c_to_v->num_nodes();
std::vector<std::bitset<BITSETSIZE>> face_perm(num_cells, 0);
std::vector<std::int64_t> cell_vertices, vertices;
std::vector<std::int32_t> e_vertices;
auto im = topology.index_map(0);
for (std::size_t t = 0; t < face_type_indices.size(); ++t)
{
spdlog::info("Computing permutations for face type {}", t);
if (!face_type_indices[t].empty())
{
auto compute_refl_rots = (mesh_face_types[t] == mesh::CellType::triangle)
? compute_triangle_rot_reflect
: compute_quad_rot_reflect;
for (int c = 0; c < num_cells; ++c)
{
cell_vertices.resize(c_to_v->links(c).size());
im->local_to_global(c_to_v->links(c), cell_vertices);
auto cell_faces = c_to_f[t]->links(c);
for (std::size_t i = 0; i < cell_faces.size(); ++i)
{
// Get the face
const int face = cell_faces[i];
e_vertices.resize(f_to_v[t]->num_links(face));
vertices.resize(f_to_v[t]->num_links(face));
im->local_to_global(f_to_v[t]->links(face), vertices);
// Orient that triangle or quadrilateral so the lowest numbered
// vertex is the origin, and the next vertex anticlockwise from
// the lowest has a lower number than the next vertex clockwise.
// Find the index of the lowest numbered vertex.
// Find iterators pointing to cell vertex given a vertex on facet
for (std::size_t j = 0; j < vertices.size(); ++j)
{
auto it = std::find(cell_vertices.begin(), cell_vertices.end(),
vertices[j]);
// Get the actual local vertex indices
e_vertices[j] = std::distance(cell_vertices.begin(), it);
}
// Compute reflections and rotations for this face type
auto [refl, rots] = compute_refl_rots(e_vertices, vertices);
// Store bits for this face
int fi = face_type_indices[t][i];
face_perm[c][3 * fi] = refl;
face_perm[c][3 * fi + 1] = rots % 2;
face_perm[c][3 * fi + 2] = rots / 2;
}
}
}
}
return face_perm;
}
//-----------------------------------------------------------------------------
template <int BITSETSIZE>
std::vector<std::bitset<BITSETSIZE>>
compute_edge_reflections(const mesh::Topology& topology)
{
mesh::CellType cell_type = topology.cell_type();
const int tdim = topology.dim();
const int edges_per_cell = cell_num_entities(cell_type, 1);
const std::int32_t num_cells = topology.connectivity(tdim, 0)->num_nodes();
auto c_to_v = topology.connectivity(tdim, 0);
assert(c_to_v);
auto c_to_e = topology.connectivity(tdim, 1);
assert(c_to_e);
auto e_to_v = topology.connectivity(1, 0);
assert(e_to_v);
auto im = topology.index_map(0);
assert(im);
std::vector<std::bitset<BITSETSIZE>> edge_perm(num_cells, 0);
std::vector<std::int64_t> cell_vertices, vertices;
for (int c = 0; c < c_to_v->num_nodes(); ++c)
{
cell_vertices.resize(c_to_v->num_links(c));
im->local_to_global(c_to_v->links(c), cell_vertices);
auto cell_edges = c_to_e->links(c);
for (int i = 0; i < edges_per_cell; ++i)
{
vertices.resize(e_to_v->links(cell_edges[i]).size());
im->local_to_global(e_to_v->links(cell_edges[i]), vertices);
// If the entity is an interval, it should be oriented pointing
// from the lowest numbered vertex to the highest numbered vertex.
// Find iterators pointing to cell vertex given a vertex on facet
auto it0
= std::find(cell_vertices.begin(), cell_vertices.end(), vertices[0]);
auto it1
= std::find(cell_vertices.begin(), cell_vertices.end(), vertices[1]);
// The number of reflections. Comparing iterators directly instead
// of values they point to is sufficient here.
edge_perm[c][i] = (it1 < it0) == (vertices[1] > vertices[0]);
}
}
return edge_perm;
}
//-----------------------------------------------------------------------------
template <int BITSETSIZE>
std::vector<std::bitset<BITSETSIZE>>
compute_face_permutations(const mesh::Topology& topology)
{
if (topology.entity_types(3).size() > 1)
{
throw std::runtime_error(
"Cannot compute permutations for mixed topology mesh.");
}
[[maybe_unused]] const int tdim = topology.dim();
assert(tdim > 2);
if (!topology.index_map(2))
throw std::runtime_error("Faces have not been computed.");
// Compute face permutations for first cell type in the topology
return compute_triangle_quad_face_permutations<BITSETSIZE>(topology, 0);
}
//-----------------------------------------------------------------------------
} // namespace
//-----------------------------------------------------------------------------
std::pair<std::vector<std::uint8_t>, std::vector<std::uint32_t>>
mesh::compute_entity_permutations(const mesh::Topology& topology)
{
common::Timer t_perm("Compute entity permutations");
const int tdim = topology.dim();
CellType cell_type = topology.cell_type();
const std::int32_t num_cells = topology.connectivity(tdim, 0)->num_nodes();
const int facets_per_cell = cell_num_entities(cell_type, tdim - 1);
std::vector<std::uint32_t> cell_permutation_info(num_cells, 0);
std::vector<std::uint8_t> facet_permutations(num_cells * facets_per_cell);
std::int32_t used_bits = 0;
if (tdim > 2)
{
spdlog::info("Compute face permutations");
const int faces_per_cell = cell_num_entities(cell_type, 2);
const auto face_perm = compute_face_permutations<_BITSETSIZE>(topology);
for (int c = 0; c < num_cells; ++c)
cell_permutation_info[c] = face_perm[c].to_ulong();
// Currently, 3 bits are used for each face. If faces with more than
// 4 sides are implemented, this will need to be increased.
used_bits += faces_per_cell * 3;
assert(tdim == 3);
for (int c = 0; c < num_cells; ++c)
{
for (int i = 0; i < facets_per_cell; ++i)
{
facet_permutations[c * facets_per_cell + i]
= (cell_permutation_info[c] >> (3 * i)) & 7;
}
}
}
if (tdim > 1)
{
spdlog::info("Compute edge permutations");
const int edges_per_cell = cell_num_entities(cell_type, 1);
const auto edge_perm = compute_edge_reflections<_BITSETSIZE>(topology);
for (int c = 0; c < num_cells; ++c)
cell_permutation_info[c] |= edge_perm[c].to_ulong() << used_bits;
used_bits += edges_per_cell;
if (tdim == 2)
{
for (int c = 0; c < num_cells; ++c)
{
for (int i = 0; i < facets_per_cell; ++i)
facet_permutations[c * facets_per_cell + i] = edge_perm[c][i];
}
}
}
assert(used_bits < _BITSETSIZE);
return {std::move(facet_permutations), std::move(cell_permutation_info)};
}
//-----------------------------------------------------------------------------
|