Discontinuous Galerkin Library
#include "dg/algorithm.h"
Loading...
Searching...
No Matches
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
 
template<class Topology >
 SparseTensor (const Topology &grid)
 Construct the unit tensor.
 
 SparseTensor (const container &copyable)
 Construct the unit tensor.
 
template<class Topology >
void construct (const Topology &grid)
 Construct the unit tensor.
 
void construct (const container &copyable)
 Construct the unit tensor.
 
template<class OtherContainer >
 SparseTensor (const SparseTensor< OtherContainer > &src)
 Type conversion from other value types.
 
int idx (unsigned i, unsigned j) const
 read index into the values array at the given position
 
int & idx (unsigned i, unsigned j)
 write index into the values array at the given position
 
const container & value (size_t i, size_t j) const
 Read access the underlying container.
 
std::vector< container > & values ()
 Return write access to the values array.
 
const std::vector< container > & values () const
 Return read access to the values array.
 
SparseTensor transpose () const
 Return the transpose of the currrent tensor.
 

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(1,2) = 0;
metric.idx(2,0) = 0, metric.idx(2,1) = 0, metric.idx(2,2) = 3;
std::vector<dg::HVec> values( 4);
values[0] = dg::HVec( 100, 0); // the zero element
values[1] = dg::HVec( 100, 20.); // construct gxx element
values[2] = dg::HVec( 100, 30.); // construct gyy element
values[3] = dg::HVec( 100, 1.); // construct gzz element
metric.values() = values;
// then we can for example use dg::tensor functions:
CHECK( det == dg::HVec( 100, 20*30));
// the individual elements can be accessed via the access operator
dg::HVec gxx = metric.value(0,0);
CHECK( gxx == dg::HVec( 100, 20));
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: