SpECTRE  v2024.04.12
Tensor< X, Symm, IndexList< Indices... > > Class Template Reference

Represents an object with multiple components. More...

#include <Tensor.hpp>

Public Types

using storage_type = std::array< X, Tensor_detail::Structure< Symm, Indices... >::size()>
 The type of the sequence that holds the data.
 
using type = X
 The type that is stored by the Tensor.
 
using symmetry = Symm
 Typelist of the symmetry of the Tensor. More...
 
using index_list = tmpl::list< Indices... >
 Typelist of the TensorIndexType's that the Tensor has.
 
using structure = Tensor_detail::Structure< Symm, Indices... >
 The Tensor_detail::Structure for the particular tensor index structure. More...
 
template<typename ArgsList >
using TE = tenex::TensorAsExpression< Tensor< X, Symm, IndexList< Indices... > >, ArgsList >
 The type of the TensorExpression that would represent this Tensor in a tensor expression.
 
using value_type = typename storage_type::value_type
 
using reference = typename storage_type::reference
 
using const_reference = typename storage_type::const_reference
 
using iterator = typename storage_type::iterator
 
using const_iterator = typename storage_type::const_iterator
 
using pointer = typename storage_type::pointer
 
using const_pointer = typename storage_type::const_pointer
 
using reverse_iterator = typename storage_type::reverse_iterator
 
using const_reverse_iterator = typename storage_type::const_reverse_iterator
 

Public Member Functions

 Tensor (const Tensor &)=default
 
 Tensor (Tensor &&)=default
 
Tensoroperator= (const Tensor &)=default
 
Tensoroperator= (Tensor &&)=default
 
template<size_t NumberOfIndices = sizeof...(Indices), Requires<(NumberOfIndices<=1)> = nullptr>
 Tensor (storage_type data)
 Initialize a vector or scalar from an array. More...
 
template<typename... Args, Requires< not(std::disjunction_v< std::is_same< Tensor< X, Symm, IndexList< Indices... > >, std::decay_t< Args > >... > and sizeof...(Args)==1) and std::is_constructible_v< X, Args... > > = nullptr>
 Tensor (Args &&... args)
 Constructor that passes "args" to constructor of X and initializes each component to be the same.
 
iterator begin ()
 
const_iterator begin () const
 
const_iterator cbegin () const
 
iterator end ()
 
const_iterator end () const
 
const_iterator cend () const
 
reverse_iterator rbegin ()
 
const_reverse_iterator rbegin () const
 
const_reverse_iterator crbegin () const
 
reverse_iterator rend ()
 
const_reverse_iterator rend () const
 
const_reverse_iterator crend () const
 
std::pair< std::vector< std::string >, std::vector< X > > get_vector_of_data () const
 Copy tensor data into an std::vector<X> along with the component names into a std::vector<std::string> More...
 
template<typename T >
constexpr reference get (const std::array< T, sizeof...(Indices)> &tensor_index)
 Get data entry using an array representing a tensor index. More...
 
template<typename T >
constexpr const_reference get (const std::array< T, sizeof...(Indices)> &tensor_index) const
 Get data entry using an array representing a tensor index. More...
 
template<typename... N>
constexpr reference get (N... n)
 Get data entry using a list of integers representing a tensor index. More...
 
template<typename... N>
constexpr const_reference get (N... n) const
 Get data entry using a list of integers representing a tensor index. More...
 
template<typename... TensorIndices>
constexpr auto operator() (TensorIndices...) const
 Retrieve a TensorExpression object with the index structure passed in.
 
constexpr reference operator[] (const size_t storage_index)
 Return i'th component of storage vector.
 
constexpr const_reference operator[] (const size_t storage_index) const
 Return i'th component of storage vector.
 

Static Public Member Functions

static constexpr size_t size ()
 Return the number of independent components of the Tensor. More...
 
static constexpr size_t rank ()
 Returns the rank of the Tensor. More...
 
template<typename... N>
static constexpr size_t get_storage_index (const N... args)
 Get the storage index of the tensor index. Should only be used when optimizing code in which computing the storage index is a bottleneck.
 
template<typename I >
static constexpr size_t get_storage_index (const std::array< I, sizeof...(Indices)> &tensor_index)
 Get the storage index of the tensor index. Should only be used when optimizing code in which computing the storage index is a bottleneck.
 
static constexpr size_t index_dim (const size_t i)
 Get dimensionality of i'th tensor index. More...
 
static constexpr std::array< int, sizeof...(Indices)> symmetries ()
 Return an array corresponding to the Symmetry of the Tensor.
 
static constexpr std::array< IndexType, sizeof...(Indices)> index_types ()
 Return array of the IndexType's (spatial or spacetime)
 
static constexpr std::array< size_t, sizeof...(Indices)> index_dims ()
 Return array of dimensionality of each index. More...
 
static constexpr std::array< UpLo, sizeof...(Indices)> index_valences ()
 Return array of the valence of each index (UpLo)
 
static constexpr auto index_frames ()
 Returns std::tuple of the Frame of each index.
 
template<typename T = int>
static std::string component_name (const std::array< T, rank()> &tensor_index=std::array< T, rank()>{}, const std::array< std::string, rank()> &axis_labels=make_array< rank()>(std::string("")))
 Given a tensor index, get the canonical label associated with the canonical TensorIndexType. More...
 
template<typename IndexType = int>
static std::string component_suffix (const std::array< IndexType, rank()> &tensor_index=std::array< IndexType, rank()>{}, const std::array< std::string, rank()> &axis_labels=make_array< rank()>(std::string("")))
 Suffix to append to the tensor name that indicates the component. More...
 
static std::string component_suffix (const size_t storage_index, const std::array< std::string, rank()> &axis_labels)
 Suffix to append to the tensor name that indicates the component. More...
 
static std::string component_suffix (size_t storage_index)
 Suffix to append to the tensor name that indicates the component. More...
 

Static Public Attributes

static constexpr size_t num_tensor_indices = sizeof...(Indices)
 The number of indices that the Tensor has.
 

Friends

template<int... N, typename... Args>
constexpr Tensor< Args... >::reference get (Tensor< Args... > &t)
 Retrieve the index N... by computing the storage index at compile time.
 
template<int... N, typename... Args>
constexpr Tensor< Args... >::const_reference get (const Tensor< Args... > &t)
 Retrieve the index N... by computing the storage index at compile time.
 
constexpr std::array< size_t, sizeof...(Indices)> get_tensor_index (const const_iterator &iter) const
 Given an iterator or storage index, get the canonical tensor index. For scalars this is defined to be std::array<int, 1>{{0}}.
 
static constexpr std::array< size_t, sizeof...(Indices)> get_tensor_index (const size_t storage_index)
 Given an iterator or storage index, get the canonical tensor index. For scalars this is defined to be std::array<int, 1>{{0}}.
 
constexpr size_t multiplicity (const const_iterator &iter) const
 Given an iterator or storage index, get the multiplicity of an index. More...
 
static constexpr size_t multiplicity (const size_t storage_index)
 Given an iterator or storage index, get the multiplicity of an index. More...
 

Detailed Description

template<typename X, typename Symm, template< typename... > class IndexList, typename... Indices>
class Tensor< X, Symm, IndexList< Indices... > >

Represents an object with multiple components.

Details

Tensor is a container that represents indexable geometric objects. Each index has its own dimension, valence, and frame and must be either spatial or spacetime. Note that the dimension passed to SpatialIndex and SpacetimeIndex is always the spatial dimension of the index. Tensors with symmetric indices are stored only once and must be of the same type. A list of available type aliases can be found in the tnsr namespace where the adopted conventions are:

  1. Upper case for contravariant or upper indices, lower case for covariant or lower indices.
  2. a, b, c, d are used for spacetime indices while i, j, k, l are used for spatial indices.
  3. Scalar is not inside the tnsr namespace but is used to represent a scalar with no indices.

Example

Tensor<double> scalar{};
Definition: ContractFirstNIndices.hpp:16
tnsr::A<double, 3, Frame::Grid> spacetime_vector3{};
index_list<SpacetimeIndex<1, UpLo::Lo, Frame::Grid>,
tensor_1{};
Tensor_detail::TensorIndexType< SpatialDim, Ul, Fr, IndexType::Spatial > SpatialIndex
A SpatialIndex holds information about the number of spatial dimensions, whether the index is covaria...
Definition: IndexType.hpp:206
Template Parameters
Xthe type held
Symmthe Symmetry of the indices
IndexLista typelist of TensorIndexType's

Member Typedef Documentation

◆ structure

template<typename X , typename Symm , template< typename... > class IndexList, typename... Indices>
using Tensor< X, Symm, IndexList< Indices... > >::structure = Tensor_detail::Structure<Symm, Indices...>

The Tensor_detail::Structure for the particular tensor index structure.

Each tensor index structure, e.g. \(T_{ab}\), \(T_a{}^b\) or \(T^{ab}\) has its own Tensor_detail::TensorStructure that holds information about how the data is stored, what the multiplicity of the stored indices are, the number of (independent) components, etc.

◆ symmetry

template<typename X , typename Symm , template< typename... > class IndexList, typename... Indices>
using Tensor< X, Symm, IndexList< Indices... > >::symmetry = Symm

Typelist of the symmetry of the Tensor.

Details

For a rank-3 tensor symmetric in the last two indices, \(T_{a(bc)}\), the Symmetry is <2, 1, 1>. For a non-symmetric rank-2 tensor the Symmetry is <2, 1>.

Constructor & Destructor Documentation

◆ Tensor()

template<typename X , typename Symm , template< typename... > class IndexList, typename... Indices>
template<size_t NumberOfIndices, Requires<(NumberOfIndices<=1)> >
Tensor< X, Symm, IndexList< Indices... > >::Tensor ( storage_type  data)
explicit

Initialize a vector or scalar from an array.

Example

{{{1., 2., 3.}, {4., 5., 6.}, {7., 8., 9.}}}};
Scalar<DataVector> scalar_data_vector{{{{1., 2., 3.}}}};
Parameters
datathe values of the individual components of the Vector

Member Function Documentation

◆ component_name()

template<typename X , typename Symm , template< typename... > class IndexList, typename... Indices>
template<typename T = int>
static std::string Tensor< X, Symm, IndexList< Indices... > >::component_name ( const std::array< T, rank()> &  tensor_index = std::array<T, rank()>{},
const std::array< std::string, rank()> &  axis_labels = make_array<rank()>(std::string("")) 
)
inlinestatic

Given a tensor index, get the canonical label associated with the canonical TensorIndexType.

Parameters
tensor_indexThe index of the tensor component to label
axis_labelsThe labels for the indices. Defaults to "t", "x", "y" and "z" for spacetime indices and "x", "y" and "z" for spatial indices. Note that a tensor can have indices of different types, so we specify labels for each index individually.

◆ component_suffix() [1/3]

template<typename X , typename Symm , template< typename... > class IndexList, typename... Indices>
static std::string Tensor< X, Symm, IndexList< Indices... > >::component_suffix ( const size_t  storage_index,
const std::array< std::string, rank()> &  axis_labels 
)
inlinestatic

Suffix to append to the tensor name that indicates the component.

The suffix is empty for scalars, otherwise it is an underscore followed by the Tensor::component_name of either the tensor_index or the canonical tensor index obtained from the storage_index. Use axis_labels to overwrite the default labels for each component (see Tensor::component_name).

An example use case for the suffix is to label tensor components in data files.

See also
Tensor::component_name

◆ component_suffix() [2/3]

template<typename X , typename Symm , template< typename... > class IndexList, typename... Indices>
template<typename IndexType = int>
static std::string Tensor< X, Symm, IndexList< Indices... > >::component_suffix ( const std::array< IndexType, rank()> &  tensor_index = std::array<IndexTyperank()>{},
const std::array< std::string, rank()> &  axis_labels = make_array<rank()>(std::string("")) 
)
inlinestatic

Suffix to append to the tensor name that indicates the component.

The suffix is empty for scalars, otherwise it is an underscore followed by the Tensor::component_name of either the tensor_index or the canonical tensor index obtained from the storage_index. Use axis_labels to overwrite the default labels for each component (see Tensor::component_name).

An example use case for the suffix is to label tensor components in data files.

See also
Tensor::component_name

◆ component_suffix() [3/3]

template<typename X , typename Symm , template< typename... > class IndexList, typename... Indices>
static std::string Tensor< X, Symm, IndexList< Indices... > >::component_suffix ( size_t  storage_index)
static

Suffix to append to the tensor name that indicates the component.

The suffix is empty for scalars, otherwise it is an underscore followed by the Tensor::component_name of either the tensor_index or the canonical tensor index obtained from the storage_index. Use axis_labels to overwrite the default labels for each component (see Tensor::component_name).

An example use case for the suffix is to label tensor components in data files.

See also
Tensor::component_name

◆ get() [1/4]

template<typename X , typename Symm , template< typename... > class IndexList, typename... Indices>
template<typename T >
constexpr reference Tensor< X, Symm, IndexList< Indices... > >::get ( const std::array< T, sizeof...(Indices)> &  tensor_index)
inlineconstexpr

Get data entry using an array representing a tensor index.

Details

Let \(T_{abc}\) be a Tensor. Then get({{0, 2, 1}}) returns the \(T_{0 2 1}\) component.

Parameters
tensor_indexthe index at which to get the data

◆ get() [2/4]

template<typename X , typename Symm , template< typename... > class IndexList, typename... Indices>
template<typename T >
constexpr const_reference Tensor< X, Symm, IndexList< Indices... > >::get ( const std::array< T, sizeof...(Indices)> &  tensor_index) const
inlineconstexpr

Get data entry using an array representing a tensor index.

Details

Let \(T_{abc}\) be a Tensor. Then get({{0, 2, 1}}) returns the \(T_{0 2 1}\) component.

Parameters
tensor_indexthe index at which to get the data

◆ get() [3/4]

template<typename X , typename Symm , template< typename... > class IndexList, typename... Indices>
template<typename... N>
constexpr reference Tensor< X, Symm, IndexList< Indices... > >::get ( N...  n)
inlineconstexpr

Get data entry using a list of integers representing a tensor index.

Details

Let \(T_{abc}\) be a Tensor. Then get(0, 2, 1) returns the \(T_{0 2 1}\) component.

Parameters
nthe index at which to get the data

◆ get() [4/4]

template<typename X , typename Symm , template< typename... > class IndexList, typename... Indices>
template<typename... N>
constexpr const_reference Tensor< X, Symm, IndexList< Indices... > >::get ( N...  n) const
inlineconstexpr

Get data entry using a list of integers representing a tensor index.

Details

Let \(T_{abc}\) be a Tensor. Then get(0, 2, 1) returns the \(T_{0 2 1}\) component.

Parameters
nthe index at which to get the data

◆ get_vector_of_data()

template<typename X , typename Symm , template< typename... > class IndexList, typename... Indices>
std::pair< std::vector< std::string >, std::vector< X > > Tensor< X, Symm, IndexList< Indices... > >::get_vector_of_data

Copy tensor data into an std::vector<X> along with the component names into a std::vector<std::string>

Requires: std::is_same<X, DataVector>::value is true

◆ index_dim()

template<typename X , typename Symm , template< typename... > class IndexList, typename... Indices>
static constexpr size_t Tensor< X, Symm, IndexList< Indices... > >::index_dim ( const size_t  i)
inlinestaticconstexpr

Get dimensionality of i'th tensor index.

index_list<SpacetimeIndex<2, UpLo::Up, Frame::Inertial>,
const T t{};
CHECK(index_dim<0>(t) == 3);
CHECK(index_dim<1>(t) == 1);
CHECK(index_dim<2>(t) == 2);
CHECK(T::index_dim(0) == 3);
CHECK(T::index_dim(1) == 1);
CHECK(T::index_dim(2) == 2);
CHECK(T::index_dims() == std::array<size_t, 3>{{3, 1, 2}});
constexpr size_t index_dim(const Tensor< Ts... > &)
Get dimensionality of i'th tensor index.
Definition: Tensor.hpp:574
See also
index_dim

◆ index_dims()

template<typename X , typename Symm , template< typename... > class IndexList, typename... Indices>
static constexpr std::array< size_t, sizeof...(Indices)> Tensor< X, Symm, IndexList< Indices... > >::index_dims ( )
inlinestaticconstexpr

Return array of dimensionality of each index.

index_list<SpacetimeIndex<2, UpLo::Up, Frame::Inertial>,
const T t{};
CHECK(index_dim<0>(t) == 3);
CHECK(index_dim<1>(t) == 1);
CHECK(index_dim<2>(t) == 2);
CHECK(T::index_dim(0) == 3);
CHECK(T::index_dim(1) == 1);
CHECK(T::index_dim(2) == 2);
CHECK(T::index_dims() == std::array<size_t, 3>{{3, 1, 2}});
See also
index_dim index_dim

◆ multiplicity() [1/2]

template<typename X , typename Symm , template< typename... > class IndexList, typename... Indices>
constexpr size_t Tensor< X, Symm, IndexList< Indices... > >::multiplicity ( const const_iterator &  iter) const
inlineconstexpr

Given an iterator or storage index, get the multiplicity of an index.

See also
TensorMetafunctions::compute_multiplicity

◆ multiplicity() [2/2]

template<typename X , typename Symm , template< typename... > class IndexList, typename... Indices>
static constexpr size_t Tensor< X, Symm, IndexList< Indices... > >::multiplicity ( const size_t  storage_index)
inlinestaticconstexpr

Given an iterator or storage index, get the multiplicity of an index.

See also
TensorMetafunctions::compute_multiplicity

◆ rank()

template<typename X , typename Symm , template< typename... > class IndexList, typename... Indices>
static constexpr size_t Tensor< X, Symm, IndexList< Indices... > >::rank ( )
inlinestaticconstexpr

Returns the rank of the Tensor.

Details

The rank of a tensor is the number of indices it has. For example, the tensor \(v^a\) is rank-1, the tensor \(\phi\) is rank-0, and the tensor \(T_{abc}\) is rank-3.

◆ size()

template<typename X , typename Symm , template< typename... > class IndexList, typename... Indices>
static constexpr size_t Tensor< X, Symm, IndexList< Indices... > >::size ( )
inlinestaticconstexpr

Return the number of independent components of the Tensor.

Details

Returns the number of independent components of the Tensor taking into account symmetries. For example, let \(T_{ab}\) be a n-dimensional rank-2 symmetric tensor, then the number of independent components is \(n(n+1)/2\).


The documentation for this class was generated from the following file: