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
42template<class IMatrix, class ContainerType>
43struct Average
44{
45 using container_type = ContainerType;
48 Average() = default;
49
59 template<class Topology, size_t Md>
60 Average( const Topology& g, std::array<unsigned, Md> axes)
61 {
62 m_prolongation = dg::create::prolongation( g, axes);
63 m_average = dg::create::projection( axes, g);
64
65 auto remains = dg::create::detail::complement( g, axes);
66
67 auto tmp = dg::evaluate( dg::one, g.grid(remains[0]));
68 for( unsigned u=1; u<remains.size(); u++)
69 tmp = dg::kronecker( dg::Product(), tmp,
70 dg::evaluate(dg::one, g.grid(remains[u]))); //note tmp comes first (important for comms in MPI)
72 m_area_inv = 1.;
73 for( unsigned u=0; u<Md; u++)
74 m_area_inv /= g.l(axes[u]);
75 }
76
78 template< class Topology, typename = std::enable_if_t<Topology::ndim() == 2>>
79 Average( const Topology& g, enum coo2d axes)
80 : Average( g, coo2axis(axes))
81 {
82 }
83
85 template< class Topology, typename = std::enable_if_t<Topology::ndim() == 3>>
86 Average( const Topology& g, enum coo3d axes)
87 : Average( g, coo2axis(axes))
88 {
89 }
90
92 template< class Topology, typename = std::enable_if_t<Topology::ndim() == 3>>
93 Average( const Topology& g, std::array<enum coo3d,2> axes)
94 : Average( g, {coo2axis(axes[0]), coo2axis(axes[1])})
95 {
96 }
97
117 void operator() (const ContainerType& src, ContainerType& res, bool extend
118 = true)
119 {
120 // Optimization note: Notice tmp is smaller than src so
121 // blas1 functions do not matter so much performance wise
122 // Remove possible NaN in tmp (because symv does not do it)
123 // (This must be done to remove potential NaN from previous computations,
124 // otherwise the class can be "infected" with NaN)
125 dg::blas1::copy( 0., m_tmp);
126 // Cusparse makes no guarantee on NaN behaviour
127 // m_average in MPI has the "allreduce" distribution!
128 dg::blas2::symv( m_average, src, m_tmp);
129 dg::blas1::scal( m_tmp, m_area_inv);
130 if( extend )
131 {
132 // Remove possible NaN in res
133 dg::blas1::copy( 0., res);
134 dg::blas2::symv( m_prolongation, m_tmp, res);
135 }
136 else
137 {
138 res = m_tmp;
139 }
140 }
141
142 private:
143 std::array<unsigned, 1> coo2axis( enum coo2d direction)
144 {
145 if( direction == coo2d::x)
146 return {0};
147 return {1};
148 }
149 std::array<unsigned, 1> coo2axis( enum coo3d direction)
150 {
151 if( direction == coo3d::x)
152 return {0};
153 if( direction == coo3d::y)
154 return {1};
155 return {2};
156 }
157 ContainerType m_tmp;
158 IMatrix m_average, m_prolongation;
159 double m_area_inv;
160};
161
162}//namespace dg
DG_DEVICE T one(T, Ts ...)
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
void copy(const ContainerTypeIn &source, ContainerTypeOut &target)
Definition blas1.h:243
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:331
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:288
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:44
Average()=default
No allocation.
dg::get_value_type< ContainerType > value_type
Definition average.h:46
Average(const Topology &g, std::array< enum coo3d, 2 > axes)
Average along given axes.
Definition average.h:93
void operator()(const ContainerType &src, ContainerType &res, bool extend=true)
Compute the average as configured in the constructor.
Definition average.h:117
Average(const Topology &g, enum coo3d axes)
Average along given axes.
Definition average.h:86
ContainerType container_type
Definition average.h:45
Average(const Topology &g, enum coo2d axes)
Average along given axes.
Definition average.h:79
Average(const Topology &g, std::array< unsigned, Md > axes)
Average along given axes.
Definition average.h:60
Definition subroutines.h:100