Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More Tensor simplifications with if constexpr. #16473

Merged
merged 1 commit into from
Jan 15, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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