Skip to content

Commit 42f6c87

Browse files
authored
Merge pull request #3389 from lindsayad/tensor-traits
Add TensorTraits
2 parents 80eccb7 + cd3c5f9 commit 42f6c87

File tree

5 files changed

+201
-6
lines changed

5 files changed

+201
-6
lines changed

include/base/libmesh_common.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -697,6 +697,13 @@ inline Tnew libmesh_cast_int (Told oldvar)
697697
return cast_int<Tnew>(oldvar);
698698
}
699699

700+
/**
701+
* This is a helper variable template for cases when we want to use a default compile-time
702+
* error with constexpr-based if conditions. The templating delays the triggering
703+
* of the static assertion until the template is instantiated.
704+
*/
705+
template <class T>
706+
constexpr std::false_type always_false{};
700707

701708
// build a integer representation of version
702709
#define LIBMESH_VERSION_ID(major,minor,patch) (((major) << 16) | ((minor) << 8) | ((patch) & 0xFF))

include/numerics/tensor_tools.h

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,51 @@ struct MakeBaseNumber<
350350
typedef typename MakeBaseNumber<T>::type type;
351351
};
352352

353+
template <typename T, typename Enable = void>
354+
struct TensorTraits
355+
{
356+
static_assert(always_false<T>,
357+
"Instantiating the generic template of TensorTraits. You must specialize "
358+
"TensorTraits for your type.");
359+
static constexpr unsigned char rank = 0;
360+
};
361+
362+
template <typename T>
363+
struct TensorTraits<T, typename std::enable_if<ScalarTraits<T>::value>::type>
364+
{
365+
static constexpr unsigned char rank = 0;
366+
};
367+
368+
template <typename T>
369+
struct TensorTraits<TypeVector<T>>
370+
{
371+
static constexpr unsigned char rank = 1;
372+
};
373+
374+
template <typename T>
375+
struct TensorTraits<VectorValue<T>>
376+
{
377+
static constexpr unsigned char rank = 1;
378+
};
379+
380+
template <typename T>
381+
struct TensorTraits<TypeTensor<T>>
382+
{
383+
static constexpr unsigned char rank = 2;
384+
};
385+
386+
template <typename T>
387+
struct TensorTraits<TensorValue<T>>
388+
{
389+
static constexpr unsigned char rank = 2;
390+
};
391+
392+
template <typename T, unsigned int N>
393+
struct TensorTraits<TypeNTensor<N, T>>
394+
{
395+
static constexpr unsigned char rank = static_cast<unsigned char>(N);
396+
};
397+
353398
}//namespace TensorTools
354399

355400
}//namespace libMesh

tests/Makefile.am

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ unit_tests_sources = \
9999
numerics/diagonal_matrix_test.C \
100100
numerics/lumped_mass_matrix_test.C \
101101
numerics/eigen_sparse_matrix_test.C \
102+
numerics/tensor_traits_test.C \
102103
parallel/message_tag.C \
103104
parallel/packed_range_test.C \
104105
parallel/parallel_sort_test.C \

0 commit comments

Comments
 (0)