Skip to content

Commit

Permalink
Merge pull request #16473 from bangerth/tensor-3
Browse files Browse the repository at this point in the history
More Tensor simplifications with if constexpr.
  • Loading branch information
tamiko committed Jan 15, 2024
2 parents 4c1077b + 32c6cc7 commit 19ab82c
Showing 1 changed file with 19 additions and 47 deletions.
66 changes: 19 additions & 47 deletions include/deal.II/base/tensor.h
Original file line number Diff line number Diff line change
Expand Up @@ -1757,45 +1757,6 @@ Tensor<rank_, dim, Number>::component_to_unrolled_index(



namespace internal
{
// unrolled_to_component_indices is instantiated from DataOut for dim==0
// and rank=2. Make sure we don't have compiler warnings.

template <int dim>
DEAL_II_HOST_DEVICE inline constexpr unsigned int
mod(const unsigned int x)
{
return x % dim;
}

template <>
DEAL_II_HOST_DEVICE inline unsigned int
mod<0>(const unsigned int x)
{
Assert(false, ExcInternalError());
return x;
}

template <int dim>
DEAL_II_HOST_DEVICE inline constexpr unsigned int
div(const unsigned int x)
{
return x / dim;
}

template <>
DEAL_II_HOST_DEVICE inline unsigned int
div<0>(const unsigned int x)
{
Assert(false, ExcInternalError());
return x;
}

} // namespace internal



template <int rank_, int dim, typename Number>
constexpr inline TableIndices<rank_>
Tensor<rank_, dim, Number>::unrolled_to_component_indices(const unsigned int i)
Expand All @@ -1805,17 +1766,28 @@ Tensor<rank_, dim, Number>::unrolled_to_component_indices(const unsigned int i)
AssertIndexRange(i, dummy);
(void)dummy;

TableIndices<rank_> indices;

unsigned int remainder = i;
for (int r = rank_ - 1; r >= 0; --r)
if constexpr (dim == 0)
{
indices[r] = internal::mod<dim>(remainder);
remainder = internal::div<dim>(remainder);
Assert(false,
ExcMessage(
"A tensor with dimension 0 does not store any elements. "
"There is no indexing that can address its elements."));
return {};
}
Assert(remainder == 0, ExcInternalError());
else
{
TableIndices<rank_> indices;

return indices;
unsigned int remainder = i;
for (int r = rank_ - 1; r >= 0; --r)
{
indices[r] = remainder % dim;
remainder = remainder / dim;
}
Assert(remainder == 0, ExcInternalError());

return indices;
}
}


Expand Down

0 comments on commit 19ab82c

Please sign in to comment.