Discontinuous Galerkin Library
#include "dg/algorithm.h"
dg::SparseTensor< container > Struct Template Reference

Class for 2x2 and 3x3 matrices sharing elements. More...

Public Types

using container_type = container
 

Public Member Functions

 SparseTensor ()
 no value is set, Indices default to -1 More...
 
template<class Topology >
 SparseTensor (const Topology &grid)
 Construct the unit tensor. More...
 
 SparseTensor (const container &copyable)
 Construct the unit tensor. More...
 
template<class Topology >
void construct (const Topology &grid)
 Construct the unit tensor. More...
 
void construct (const container &copyable)
 Construct the unit tensor. More...
 
template<class OtherContainer >
 SparseTensor (const SparseTensor< OtherContainer > &src)
 Type conversion from other value types. More...
 
int idx (unsigned i, unsigned j) const
 read index into the values array at the given position More...
 
int & idx (unsigned i, unsigned j)
 write index into the values array at the given position More...
 
const container & value (size_t i, size_t j) const
 Read access the underlying container. More...
 
std::vector< container > & values ()
 Return write access to the values array. More...
 
const std::vector< container > & values () const
 Return read access to the values array. More...
 
SparseTensor transpose () const
 Return the transpose of the currrent tensor. More...
 

Detailed Description

template<class container>
struct dg::SparseTensor< container >

Class for 2x2 and 3x3 matrices sharing elements.

This class enables shared access to stored containers. It contains a (dense) 3x3 matrix of integers that is automatically allocated. The integers represent a gather index into a stored array of containers. In this way duplicate entries are stored only once, which helps to avoid unnecessary memory accesses. For example an orthogonal metric is represented as follows

\[ \begin{pmatrix} g^{xx} & 0 & 0\\ 0 & g^{yy} & 0 \\ 0 & 0 & g^{zz} \end{pmatrix} \quad\rightarrow\quad \text{idx} = \begin{pmatrix} 1 & 0 & 0 \\ 0 & 2 & 0 \\ 0 & 0 & 3 \end{pmatrix} \quad \text{values} = \begin{pmatrix} 0 & g^{xx} & g^{yy} & g^{zz} \end{pmatrix} \]

which in code can be assembled as

dg::SparseTensor<dg::HVec> metric; // allocate 3x3 index matrix
metric.idx(0,0) = 1, metric.idx(0,1) = 0, metric.idx(0,2) = 0;
metric.idx(1,0) = 0, metric.idx(1,1) = 2, metric.idx(2,2) = 0;
metric.idx(2,0) = 0, metric.idx(2,1) = 0, metric.idx(2,2) = 3;
std::vector<dg::HVec> values( 4);
values[1] = ... // construct gxx element
values[2] = ... // construct gyy element
values[3] = ... // construct gzz element
metric.values() = values;
// then we can for example use dg::tensor functions:
// the individual elements can be accessed via the access operator
dg::HVec gxx = metric(0,0);
static DG_DEVICE double zero(double x)
Definition: functions.h:29
thrust::host_vector< real_type > evaluate(UnaryOp f, const RealGrid1d< real_type > &g)
Evaluate a 1d function on grid coordinates.
Definition: evaluation.h:67
ContainerType determinant(const SparseTensor< ContainerType > &t)
Definition: multiply.h:349
thrust::host_vector< double > HVec
Host Vector.
Definition: typedefs.h:19
Class for 2x2 and 3x3 matrices sharing elements.
Definition: tensor.h:66
int idx(unsigned i, unsigned j) const
read index into the values array at the given position
Definition: tensor.h:134
std::vector< container > & values()
Return write access to the values array.
Definition: tensor.h:166
Template Parameters
containermust be default constructible and copyable.
See also
dg::tensor

Member Typedef Documentation

◆ container_type

template<class container >
using dg::SparseTensor< container >::container_type = container

Constructor & Destructor Documentation

◆ SparseTensor() [1/4]

template<class container >
dg::SparseTensor< container >::SparseTensor ( )
inline

no value is set, Indices default to -1

◆ SparseTensor() [2/4]

template<class container >
template<class Topology >
dg::SparseTensor< container >::SparseTensor ( const Topology &  grid)
inline

Construct the unit tensor.

Parameters
gridused to create explicit zeroes (Index 0) and ones (Index 1)

◆ SparseTensor() [3/4]

template<class container >
dg::SparseTensor< container >::SparseTensor ( const container &  copyable)
inline

Construct the unit tensor.

Parameters
copyableused to create explicit zeroes (Index 0) and ones (Index 1)

◆ SparseTensor() [4/4]

template<class container >
template<class OtherContainer >
dg::SparseTensor< container >::SparseTensor ( const SparseTensor< OtherContainer > &  src)
inline

Type conversion from other value types.

Template Parameters
OtherContainerdg::assign must be callable for container and OtherContainer
Parameters
srcthe source matrix to convert

Member Function Documentation

◆ construct() [1/2]

template<class container >
void dg::SparseTensor< container >::construct ( const container &  copyable)
inline

Construct the unit tensor.

Parameters
copyableused to create explicit zeroes (Index 0) and ones (Index 1)

◆ construct() [2/2]

template<class container >
template<class Topology >
void dg::SparseTensor< container >::construct ( const Topology &  grid)
inline

Construct the unit tensor.

Parameters
gridused to create explicit zeroes (Index 0) and ones (Index 1)

◆ idx() [1/2]

template<class container >
int & dg::SparseTensor< container >::idx ( unsigned  i,
unsigned  j 
)
inline

write index into the values array at the given position

use this and the values() member to assemble the tensor

Parameters
irow index 0<=i<3
jcol index 0<=j<3
Returns
write access to value index to be set

◆ idx() [2/2]

template<class container >
int dg::SparseTensor< container >::idx ( unsigned  i,
unsigned  j 
) const
inline

read index into the values array at the given position

Parameters
irow index 0<=i<3
jcol index 0<=j<3
Returns
index into values array

◆ transpose()

template<class container >
SparseTensor dg::SparseTensor< container >::transpose ( ) const
inline

Return the transpose of the currrent tensor.

Returns
swapped rows and columns

◆ value()

template<class container >
const container & dg::SparseTensor< container >::value ( size_t  i,
size_t  j 
) const
inline

Read access the underlying container.

Returns
values[idx(i,j)] is returned.
Parameters
irow index 0<=i<3
jcol index 0<=j<3
Note
If the indices (i,j) fall out of range or if the corresponding value index idx(i,j) falls out of range of the values array, the result is undefined

◆ values() [1/2]

template<class container >
std::vector< container > & dg::SparseTensor< container >::values ( )
inline

Return write access to the values array.

Returns
write access to values array

◆ values() [2/2]

template<class container >
const std::vector< container > & dg::SparseTensor< container >::values ( ) const
inline

Return read access to the values array.

Returns
read access to values array

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