Discontinuous Galerkin Library
#include "dg/algorithm.h"
Loading...
Searching...
No Matches
average.h
Go to the documentation of this file.
1#pragma once
2
3#include "prolongation.h"
4#ifdef MPI_VERSION
5#include "mpi_prolongation.h"
6#endif
7
11namespace dg{
12
39template<class IMatrix, class ContainerType>
40struct Average
41{
42 using container_type = ContainerType;
45 Average() = default;
46
56 template<class Topology, size_t Md>
57 Average( const Topology& g, std::array<unsigned, Md> axes)
58 {
59 m_prolongation = dg::create::prolongation( g, axes);
60 m_average = dg::create::projection( axes, g);
61
62 auto remains = dg::create::detail::complement( g, axes);
63
64 auto tmp = dg::evaluate( dg::one, g.grid(remains[0]));
65 for( unsigned u=1; u<remains.size(); u++)
66 tmp = dg::kronecker( dg::Product(), tmp,
67 dg::evaluate(dg::one, g.grid(remains[u]))); //note tmp comes first (important for comms in MPI)
69 m_area_inv = 1.;
70 for( unsigned u=0; u<Md; u++)
71 m_area_inv /= g.l(axes[u]);
72 }
73
75 template< class Topology, typename = std::enable_if_t<Topology::ndim() == 2>>
76 Average( const Topology& g, enum coo2d axes)
77 : Average( g, coo2axis(axes))
78 {
79 }
80
82 template< class Topology, typename = std::enable_if_t<Topology::ndim() == 3>>
83 Average( const Topology& g, enum coo3d axes)
84 : Average( g, coo2axis(axes))
85 {
86 }
87
89 template< class Topology, typename = std::enable_if_t<Topology::ndim() == 3>>
90 Average( const Topology& g, std::array<enum coo3d,2> axes)
91 : Average( g, {coo2axis(axes[0]), coo2axis(axes[1])})
92 {
93 }
94
113 void operator() (const ContainerType& src, ContainerType& res, bool extend
114 = true)
115 {
116 dg::blas2::symv( m_average, src, m_tmp);
117 dg::blas1::scal( m_tmp, m_area_inv);
118 if( extend )
119 dg::blas2::symv( m_prolongation, m_tmp, res);
120 else
121 res = m_tmp;
122 }
123
124 private:
125 std::array<unsigned, 1> coo2axis( enum coo2d direction)
126 {
127 if( direction == coo2d::x)
128 return {0};
129 return {1};
130 }
131 std::array<unsigned, 1> coo2axis( enum coo3d direction)
132 {
133 if( direction == coo3d::x)
134 return {0};
135 if( direction == coo3d::y)
136 return {1};
137 return {2};
138 }
139 ContainerType m_tmp;
140 IMatrix m_average, m_prolongation;
141 double m_area_inv;
142};
143
144}//namespace dg
DG_DEVICE T one(T x, Ts ...xs)
Definition functions.h:24
auto kronecker(Functor &&f, const ContainerType &x0, const ContainerTypes &... xs)
Memory allocating version of dg::blas1::kronecker
Definition blas1.h:857
ContainerType construct(const from_ContainerType &from, Params &&... ps)
Generic way to construct an object of ContainerType given a from_ContainerType object and optional ad...
Definition blas1.h:792
void scal(ContainerType &x, value_type alpha)
Definition blas1.h:263
void symv(MatrixType &&M, const ContainerType1 &x, ContainerType2 &y)
Definition blas2.h:325
coo3d
3d coordinates
Definition enums.h:179
direction
Direction of a discrete derivative.
Definition enums.h:97
coo2d
2d coordinates
Definition enums.h:173
@ y
y direction
@ x
x direction
@ x
x direction
typename TensorTraits< std::decay_t< Vector > >::value_type get_value_type
Definition tensor_traits.h:45
auto evaluate(Functor &&f, const Topology &g)
Evaluate a function on grid coordinates
Definition evaluation.h:74
dg::MIHMatrix_t< typename MPITopology::value_type > projection(const MPITopology &g_new, const MPITopology &g_old, std::string method="dg")
Create a projection between two grids.
Definition mpi_projection.h:272
Useful MPI typedefs and overloads of interpolation and projection.
dg::MIHMatrix_t< typename MPITopology::value_type > prolongation(const MPITopology &g_new, std::array< unsigned, Md > axes)
Prolongation matrix along given axes / Transpose of reduction.
Definition mpi_prolongation.h:18
This is the namespace for all functions and classes defined and used by the discontinuous Galerkin li...
Prolongation matrix creation functions.
Topological average computations in a Cartesian topology.
Definition average.h:41
Average()=default
No allocation.
dg::get_value_type< ContainerType > value_type
Definition average.h:43
Average(const Topology &g, std::array< enum coo3d, 2 > axes)
Average along given axes.
Definition average.h:90
void operator()(const ContainerType &src, ContainerType &res, bool extend=true)
Compute the average as configured in the constructor.
Definition average.h:113
Average(const Topology &g, enum coo3d axes)
Average along given axes.
Definition average.h:83
ContainerType container_type
Definition average.h:42
Average(const Topology &g, enum coo2d axes)
Average along given axes.
Definition average.h:76
Average(const Topology &g, std::array< unsigned, Md > axes)
Average along given axes.
Definition average.h:57
Definition subroutines.h:100