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 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641
|
// Copyright (C) 2020-2021 Garth N. Wells and Matthew W. Scroggs
//
// This file is part of DOLFINx (https://www.fenicsproject.org)
//
// SPDX-License-Identifier: LGPL-3.0-or-later
#include "FiniteElement.h"
#include <algorithm>
#include <array>
#include <basix/finite-element.h>
#include <basix/interpolation.h>
#include <basix/polyset.h>
#include <dolfinx/common/log.h>
#include <functional>
#include <numeric>
#include <utility>
#include <vector>
using namespace dolfinx;
using namespace dolfinx::fem;
namespace
{
/// @brief Create a list of fem::FiniteElement from Basix elements and
/// other data.
/// @tparam T
/// @param elements
/// @return List of DOLFINx elements
template <std::floating_point T>
std::vector<std::shared_ptr<const FiniteElement<T>>>
_build_element_list(std::vector<BasixElementData<T>> elements)
{
std::vector<std::shared_ptr<const FiniteElement<T>>> _e;
std::ranges::transform(elements, std::back_inserter(_e),
[](auto& data)
{
auto& [e, bs, symm] = data;
return std::make_shared<fem::FiniteElement<T>>(e, bs,
symm);
});
return _e;
}
/// Recursively extract sub finite element
template <std::floating_point T>
std::shared_ptr<const FiniteElement<T>>
_extract_sub_element(const FiniteElement<T>& finite_element,
std::span<const int> component)
{
// Check that a sub system has been specified
if (component.empty())
{
throw std::runtime_error("Cannot extract subsystem of finite element. No "
"system was specified");
}
// Check if there are any sub systems
if (finite_element.num_sub_elements() == 0)
{
throw std::runtime_error("Cannot extract subsystem of finite element. "
"There are no subsystems.");
}
// Check the number of available sub systems
if (component[0] >= finite_element.num_sub_elements())
{
throw std::runtime_error("Cannot extract subsystem of finite element. "
"Requested subsystem out of range.");
}
// Get sub system
auto sub_element = finite_element.sub_elements()[component[0]];
assert(sub_element);
// Return sub system if sub sub system should not be extracted
if (component.size() == 1)
return sub_element;
// Otherwise, recursively extract the sub sub system
std::vector<int> sub_component(component.begin() + 1, component.end());
return _extract_sub_element(*sub_element, sub_component);
}
int _compute_block_size(std::optional<std::vector<std::size_t>> value_shape,
bool symmetric)
{
if (symmetric and value_shape)
{
if (value_shape->size() != 2
or (value_shape->front() != value_shape->back()))
{
throw std::runtime_error(
"Symmetric elements require square rank-2 value shape.");
}
return value_shape->front() * (value_shape->front() + 1) / 2;
}
else if (value_shape)
{
return std::accumulate(value_shape->begin(), value_shape->end(), 1,
std::multiplies{});
}
else
return 1;
}
} // namespace
//-----------------------------------------------------------------------------
template <std::floating_point T>
FiniteElement<T>::FiniteElement(
const basix::FiniteElement<T>& element,
const std::optional<std::vector<std::size_t>>& value_shape, bool symmetric)
: _value_shape(value_shape.value_or(element.value_shape())),
_bs(_compute_block_size(value_shape, symmetric)),
_cell_type(mesh::cell_type_from_basix_type(element.cell_type())),
_space_dim(_bs * element.dim()),
_reference_value_shape(element.value_shape()),
_element(std::make_unique<basix::FiniteElement<T>>(element)),
_symmetric(symmetric),
_needs_dof_permutations(
!element.dof_transformations_are_identity()
and element.dof_transformations_are_permutations()),
_needs_dof_transformations(
!element.dof_transformations_are_identity()
and !element.dof_transformations_are_permutations()),
_entity_dofs(element.entity_dofs()),
_entity_closure_dofs(element.entity_closure_dofs())
{
if (value_shape and !element.value_shape().empty())
{
throw std::runtime_error("Blocked finite elements can be constructed only "
"from scalar base elements.");
}
if (value_shape)
{
_sub_elements
= std::vector<std::shared_ptr<const FiniteElement<geometry_type>>>(
_bs, std::make_shared<FiniteElement<T>>(element));
}
else
_sub_elements = {};
std::string family;
switch (_element->family())
{
case basix::element::family::P:
family = "Lagrange";
break;
case basix::element::family::DPC:
family = "Discontinuous Lagrange";
break;
default:
family = "unknown";
break;
}
_signature = "Basix element " + family + " " + std::to_string(_bs);
}
//-----------------------------------------------------------------------------
template <std::floating_point T>
FiniteElement<T>::FiniteElement(std::vector<BasixElementData<T>> elements)
: FiniteElement(_build_element_list(std::move(elements)))
{
}
//-----------------------------------------------------------------------------
template <std::floating_point T>
FiniteElement<T>::FiniteElement(
const std::vector<std::shared_ptr<const FiniteElement<T>>>& elements)
: _value_shape(std::nullopt), _bs(1),
_cell_type(elements.front()->cell_type()), _space_dim(-1),
_sub_elements(elements), _reference_value_shape(std::nullopt),
_symmetric(false), _needs_dof_permutations(false),
_needs_dof_transformations(false)
{
_signature = "Mixed element (";
const std::vector<std::vector<std::vector<int>>>& ed
= elements.front()->entity_dofs();
_entity_dofs.resize(ed.size());
_entity_closure_dofs.resize(ed.size());
for (std::size_t i = 0; i < ed.size(); ++i)
{
_entity_dofs[i].resize(ed[i].size());
_entity_closure_dofs[i].resize(ed[i].size());
}
int dof_offset = 0;
for (auto& e : elements)
{
_signature += e->signature() + ", ";
if (e->needs_dof_permutations())
_needs_dof_permutations = true;
if (e->needs_dof_transformations())
_needs_dof_transformations = true;
const std::size_t sub_bs = e->block_size();
for (std::size_t i = 0; i < _entity_dofs.size(); ++i)
{
for (std::size_t j = 0; j < _entity_dofs[i].size(); ++j)
{
std::vector<int> sub_ed = e->entity_dofs()[i][j];
std::vector<int> sub_ecd = e->entity_closure_dofs()[i][j];
for (auto k : sub_ed)
{
for (std::size_t b = 0; b < sub_bs; ++b)
_entity_dofs[i][j].push_back(dof_offset + k * sub_bs + b);
}
for (auto k : sub_ecd)
{
for (std::size_t b = 0; b < sub_bs; ++b)
_entity_closure_dofs[i][j].push_back(dof_offset + k * sub_bs + b);
}
}
}
dof_offset += e->space_dimension();
}
_space_dim = dof_offset;
_signature += ")";
}
//-----------------------------------------------------------------------------
template <std::floating_point T>
FiniteElement<T>::FiniteElement(mesh::CellType cell_type,
std::span<const geometry_type> points,
std::array<std::size_t, 2> pshape,
std::vector<std::size_t> value_shape,
bool symmetric)
: _value_shape(value_shape),
_bs(_compute_block_size(value_shape, symmetric)), _cell_type(cell_type),
_signature("Quadrature element " + std::to_string(pshape[0]) + " "
+ std::to_string(_bs)),
_space_dim(pshape[0] * _bs), _sub_elements({}),
_reference_value_shape(std::vector<std::size_t>()), _element(nullptr),
_symmetric(symmetric), _needs_dof_permutations(false),
_needs_dof_transformations(false),
_entity_dofs(mesh::cell_dim(cell_type) + 1),
_entity_closure_dofs(mesh::cell_dim(cell_type) + 1),
_points(std::vector<T>(points.begin(), points.end()), pshape)
{
const int tdim = mesh::cell_dim(cell_type);
for (int d = 0; d <= tdim; ++d)
{
int num_entities = mesh::cell_num_entities(cell_type, d);
_entity_dofs[d].resize(num_entities);
_entity_closure_dofs[d].resize(num_entities);
}
for (std::size_t i = 0; i < pshape[0]; ++i)
{
_entity_dofs[tdim][0].push_back(i);
_entity_closure_dofs[tdim][0].push_back(i);
}
}
//-----------------------------------------------------------------------------
template <std::floating_point T>
bool FiniteElement<T>::operator==(const FiniteElement& e) const
{
if (!_element or !e._element)
{
throw std::runtime_error(
"Missing a Basix element. Cannot check for equivalence");
}
return *_element == *e._element;
}
//-----------------------------------------------------------------------------
template <std::floating_point T>
bool FiniteElement<T>::operator!=(const FiniteElement& e) const
{
return !(*this == e);
}
//-----------------------------------------------------------------------------
template <std::floating_point T>
mesh::CellType FiniteElement<T>::cell_type() const noexcept
{
return _cell_type;
}
//-----------------------------------------------------------------------------
template <std::floating_point T>
std::string FiniteElement<T>::signature() const noexcept
{
return _signature;
}
//-----------------------------------------------------------------------------
template <std::floating_point T>
int FiniteElement<T>::space_dimension() const noexcept
{
return _space_dim;
}
//-----------------------------------------------------------------------------
template <std::floating_point T>
int FiniteElement<T>::value_size() const
{
if (_value_shape)
{
return std::accumulate(_value_shape->begin(), _value_shape->end(), 1,
std::multiplies{});
}
else
throw std::runtime_error("Element does not have a value_shape.");
}
//-----------------------------------------------------------------------------
template <std::floating_point T>
std::span<const std::size_t> FiniteElement<T>::value_shape() const
{
if (_value_shape)
return *_value_shape;
else
throw std::runtime_error("Element does not have a value_shape.");
}
//-----------------------------------------------------------------------------
template <std::floating_point T>
int FiniteElement<T>::reference_value_size() const
{
if (_reference_value_shape)
{
return std::accumulate(_reference_value_shape->begin(),
_reference_value_shape->end(), 1, std::multiplies{});
}
else
throw std::runtime_error("Element does not have a reference_value_shape.");
}
//-----------------------------------------------------------------------------
template <std::floating_point T>
std::span<const std::size_t> FiniteElement<T>::reference_value_shape() const
{
if (_reference_value_shape)
return *_reference_value_shape;
else
throw std::runtime_error("Element does not have a reference_value_shape.");
}
//-----------------------------------------------------------------------------
template <std::floating_point T>
const std::vector<std::vector<std::vector<int>>>&
FiniteElement<T>::entity_dofs() const noexcept
{
return _entity_dofs;
}
//-----------------------------------------------------------------------------
template <std::floating_point T>
const std::vector<std::vector<std::vector<int>>>&
FiniteElement<T>::entity_closure_dofs() const noexcept
{
return _entity_closure_dofs;
}
//-----------------------------------------------------------------------------
template <std::floating_point T>
bool FiniteElement<T>::symmetric() const
{
return _symmetric;
}
//-----------------------------------------------------------------------------
template <std::floating_point T>
int FiniteElement<T>::block_size() const noexcept
{
return _bs;
}
//-----------------------------------------------------------------------------
template <std::floating_point T>
void FiniteElement<T>::tabulate(std::span<T> values, std::span<const T> X,
std::array<std::size_t, 2> shape,
int order) const
{
assert(_element);
_element->tabulate(order, X, shape, values);
}
//-----------------------------------------------------------------------------
template <std::floating_point T>
std::pair<std::vector<T>, std::array<std::size_t, 4>>
FiniteElement<T>::tabulate(std::span<const T> X,
std::array<std::size_t, 2> shape, int order) const
{
assert(_element);
return _element->tabulate(order, X, shape);
}
//-----------------------------------------------------------------------------
template <std::floating_point T>
int FiniteElement<T>::num_sub_elements() const noexcept
{
return _sub_elements.size();
}
//-----------------------------------------------------------------------------
template <std::floating_point T>
bool FiniteElement<T>::is_mixed() const noexcept
{
return !_reference_value_shape;
}
//-----------------------------------------------------------------------------
template <std::floating_point T>
const std::vector<std::shared_ptr<const FiniteElement<T>>>&
FiniteElement<T>::sub_elements() const noexcept
{
return _sub_elements;
}
//-----------------------------------------------------------------------------
template <std::floating_point T>
std::shared_ptr<const FiniteElement<T>>
FiniteElement<T>::extract_sub_element(const std::vector<int>& component) const
{
// Recursively extract sub element
auto sub_finite_element = _extract_sub_element(*this, component);
spdlog::debug("Extracted finite element for sub-system: {}",
sub_finite_element->signature().c_str());
return sub_finite_element;
}
//-----------------------------------------------------------------------------
template <std::floating_point T>
const basix::FiniteElement<T>& FiniteElement<T>::basix_element() const
{
if (_element)
return *_element;
else
{
throw std::runtime_error("No Basix element available. "
"Maybe this is a mixed element?");
}
}
//-----------------------------------------------------------------------------
template <std::floating_point T>
basix::maps::type FiniteElement<T>::map_type() const
{
if (_element)
return _element->map_type();
else
{
throw std::runtime_error("Cannot element map type - no Basix element "
"available. Maybe this is a mixed element?");
}
}
//-----------------------------------------------------------------------------
template <std::floating_point T>
bool FiniteElement<T>::map_ident() const noexcept
{
if (!_element
and _points.second.front()
> 0) // Quadratute elements must use identity map
{
return true;
}
assert(_element);
return _element->map_type() == basix::maps::type::identity;
}
//-----------------------------------------------------------------------------
template <std::floating_point T>
bool FiniteElement<T>::interpolation_ident() const noexcept
{
if (!_element and _points.second[0] > 0)
return true;
else
{
assert(_element);
return _element->interpolation_is_identity();
}
}
//-----------------------------------------------------------------------------
template <std::floating_point T>
std::pair<std::vector<T>, std::array<std::size_t, 2>>
FiniteElement<T>::interpolation_points() const
{
if (_points.second[0] > 0)
return _points;
else
{
if (!_element)
{
throw std::runtime_error(
"Cannot get interpolation points - no Basix element available. Maybe "
"this is a mixed element?");
}
return _element->points();
}
}
//-----------------------------------------------------------------------------
template <std::floating_point T>
std::pair<std::vector<T>, std::array<std::size_t, 2>>
FiniteElement<T>::interpolation_operator() const
{
if (!_element)
{
throw std::runtime_error("No underlying element for interpolation. "
"Cannot interpolate mixed elements directly.");
}
return _element->interpolation_matrix();
}
//-----------------------------------------------------------------------------
template <std::floating_point T>
std::pair<std::vector<T>, std::array<std::size_t, 2>>
FiniteElement<T>::create_interpolation_operator(const FiniteElement& from) const
{
assert(_element);
assert(from._element);
if (_element->map_type() != from._element->map_type())
{
throw std::runtime_error("Interpolation between elements with different "
"maps is not supported.");
}
if (_bs == 1 or from._bs == 1)
{
// If one of the elements has bs=1, Basix can figure out the size of
// the matrix
return basix::compute_interpolation_operator<T>(*from._element, *_element);
}
else if (_bs > 1 and from._bs == _bs)
{
// If bs != 1 for at least one element, then bs0 == bs1 for this
// case
const auto [data, dshape]
= basix::compute_interpolation_operator<T>(*from._element, *_element);
std::array<std::size_t, 2> shape = {dshape[0] * _bs, dshape[1] * _bs};
std::vector<T> out(shape[0] * shape[1]);
// NOTE: Alternatively this operation could be implemented during
// matvec with the original matrix.
for (std::size_t i = 0; i < dshape[0]; ++i)
for (std::size_t j = 0; j < dshape[1]; ++j)
for (int k = 0; k < _bs; ++k)
out[shape[1] * (i * _bs + k) + (j * _bs + k)]
= data[dshape[1] * i + j];
return {std::move(out), shape};
}
else
{
throw std::runtime_error(
"Interpolation for element combination is not supported.");
}
}
//-----------------------------------------------------------------------------
template <std::floating_point T>
bool FiniteElement<T>::needs_dof_transformations() const noexcept
{
return _needs_dof_transformations;
}
//-----------------------------------------------------------------------------
template <std::floating_point T>
bool FiniteElement<T>::needs_dof_permutations() const noexcept
{
return _needs_dof_permutations;
}
//-----------------------------------------------------------------------------
template <std::floating_point T>
void FiniteElement<T>::permute(std::span<std::int32_t> doflist,
std::uint32_t cell_permutation) const
{
_element->permute(doflist, cell_permutation);
}
//-----------------------------------------------------------------------------
template <std::floating_point T>
void FiniteElement<T>::permute_inv(std::span<std::int32_t> doflist,
std::uint32_t cell_permutation) const
{
_element->permute_inv(doflist, cell_permutation);
}
//-----------------------------------------------------------------------------
/// @cond
template <std::floating_point T>
std::function<void(std::span<std::int32_t>, std::uint32_t)>
FiniteElement<T>::dof_permutation_fn(bool inverse, bool scalar_element) const
/// @endcond
{
if (!needs_dof_permutations())
return [](std::span<std::int32_t>, std::uint32_t) {};
if (!_sub_elements.empty())
{
if (_bs == 1)
{
// Mixed element
std::vector<std::function<void(std::span<std::int32_t>, std::uint32_t)>>
sub_element_functions;
std::vector<int> dims;
for (std::size_t i = 0; i < _sub_elements.size(); ++i)
{
sub_element_functions.push_back(
_sub_elements[i]->dof_permutation_fn(inverse));
dims.push_back(_sub_elements[i]->space_dimension());
}
return [dims, sub_element_functions](std::span<std::int32_t> doflist,
std::uint32_t cell_permutation)
{
std::size_t start = 0;
for (std::size_t e = 0; e < sub_element_functions.size(); ++e)
{
sub_element_functions[e](doflist.subspan(start, dims[e]),
cell_permutation);
start += dims[e];
}
};
}
else if (!scalar_element)
{
// Blocked element
std::function<void(std::span<std::int32_t>, std::uint32_t)>
sub_element_function
= _sub_elements.front()->dof_permutation_fn(inverse);
int dim = _sub_elements.front()->space_dimension();
int bs = _bs;
return
[sub_element_function, bs, subdofs = std::vector<std::int32_t>(dim)](
std::span<std::int32_t> doflist,
std::uint32_t cell_permutation) mutable
{
for (int k = 0; k < bs; ++k)
{
for (std::size_t i = 0; i < subdofs.size(); ++i)
subdofs[i] = doflist[bs * i + k];
sub_element_function(subdofs, cell_permutation);
for (std::size_t i = 0; i < subdofs.size(); ++i)
doflist[bs * i + k] = subdofs[i];
}
};
}
}
if (inverse)
{
return
[this](std::span<std::int32_t> doflist, std::uint32_t cell_permutation)
{ permute_inv(doflist, cell_permutation); };
}
else
{
return
[this](std::span<std::int32_t> doflist, std::uint32_t cell_permutation)
{ permute(doflist, cell_permutation); };
}
}
//-----------------------------------------------------------------------------
template class fem::FiniteElement<float>;
template class fem::FiniteElement<double>;
//-----------------------------------------------------------------------------
|