Discontinuous Galerkin Library
#include "dg/algorithm.h"
average.h
Go to the documentation of this file.
1#pragma once
2
3#include "grid.h"
4#include "weights.h"
5#include "dg/blas1.h"
7#include "dg/backend/view.h"
8
12namespace dg{
14template<class container>
15void simple_average( unsigned nx, unsigned ny, const container& in0, const container& in1, container& out)
16{
17 const double* in0_ptr = thrust::raw_pointer_cast( in0.data());
18 const double* in1_ptr = thrust::raw_pointer_cast( in1.data());
19 double* out_ptr = thrust::raw_pointer_cast( out.data());
20 dg::View<const container> in0_view( in0_ptr, nx), in1_view( in1_ptr, nx);
21 dg::View<container> out_view( out_ptr, nx);
22 dg::blas1::pointwiseDot( in0_view, in1_view, out_view);
23 for( unsigned i=1; i<ny; i++)
24 {
25 in0_view.construct( in0_ptr+i*nx, nx);
26 in1_view.construct( in1_ptr+i*nx, nx);
27 dg::blas1::pointwiseDot( 1., in0_view, in1_view, 1, out_view);
28 }
29}
31
51template< class ContainerType>
52struct Average
53{
54 using container_type = ContainerType;
66 Average( const aTopology2d& g, enum coo2d direction, std::string mode = "exact") : m_mode(mode)
67 {
68 m_nx = g.Nx()*g.nx(), m_ny = g.Ny()*g.ny();
69 m_w=dg::construct<ContainerType>(dg::create::weights(g, direction));
70 m_temp = m_w;
71 m_transpose = false;
72 unsigned size1d = 0;
73 if( direction == coo2d::x)
74 {
75 dg::blas1::scal( m_temp, 1./g.lx());
76 dg::blas1::scal( m_w, 1./g.lx());
77 size1d = m_ny;
78 if( "simple" == mode)
79 dg::transpose( m_nx, m_ny, m_temp, m_w);
80 }
81 else
82 {
83 m_transpose = true;
84 dg::blas1::scal( m_temp, 1./g.ly());
85 dg::blas1::scal( m_w, 1./g.ly());
86 if( "exact" == mode)
87 dg::transpose( m_nx, m_ny, m_temp, m_w);
88 size1d = m_nx;
89 }
90 thrust::host_vector<double> t1d( size1d);
91 m_temp1d = dg::construct<ContainerType>( t1d);
92 if( !("exact"==mode || "simple" == mode))
93 throw dg::Error( dg::Message( _ping_) << "Mode must either be exact or simple!");
94
95 }
96
98 Average( const aTopology3d& g, enum coo3d direction, std::string mode = "exact"): m_mode(mode)
99 {
100 m_w = dg::construct<ContainerType>(dg::create::weights(g, direction));
101 m_temp = m_w;
102 m_transpose = false;
103 unsigned nx = g.nx()*g.Nx(), ny = g.ny()*g.Ny(), nz = g.nz()*g.Nz();
104 if( direction == coo3d::x) {
105 dg::blas1::scal( m_temp, 1./g.lx());
106 dg::blas1::scal( m_w, 1./g.lx());
107 m_nx = nx, m_ny = ny*nz;
108 if( "simple" == mode)
109 dg::transpose( m_nx, m_ny, m_temp, m_w);
110 }
111 else if( direction == coo3d::z) {
112 m_transpose = true;
113 dg::blas1::scal( m_temp, 1./g.lz());
114 dg::blas1::scal( m_w, 1./g.lz());
115 m_nx = nx*ny, m_ny = nz;
116 if( "exact" == mode)
117 dg::transpose( m_nx, m_ny, m_temp, m_w);
118 }
119 else if( direction == coo3d::xy) {
120 dg::blas1::scal( m_temp, 1./g.lx()/g.ly());
121 dg::blas1::scal( m_w, 1./g.lx()/g.ly());
122 m_nx = nx*ny, m_ny = nz;
123 if( "simple" == mode)
124 dg::transpose( m_nx, m_ny, m_temp, m_w);
125 }
126 else if( direction == coo3d::yz) {
127 m_transpose = true;
128 dg::blas1::scal( m_temp, 1./g.ly()/g.lz());
129 dg::blas1::scal( m_w, 1./g.ly()/g.lz());
130 m_nx = nx, m_ny = ny*nz;
131 if( "exact" == mode)
132 dg::transpose( m_nx, m_ny, m_temp, m_w);
133 }
134 else
135 std::cerr << "Warning: this direction is not implemented\n";
136 if(!m_transpose)
137 m_temp1d = dg::construct<ContainerType>(
138 thrust::host_vector<double>( m_ny,0.));
139 else
140 m_temp1d = dg::construct<ContainerType>(
141 thrust::host_vector<double>( m_nx,0.));
142 if( !("exact"==mode || "simple" == mode))
143 throw dg::Error( dg::Message( _ping_) << "Mode must either be exact or simple!");
144 }
156 void operator() (const ContainerType& src, ContainerType& res, bool extend = true)
157 {
158 if( !m_transpose)
159 {
160 //temp1d has size m_ny
161 if( "exact" == m_mode)
162 dg::average( m_nx, m_ny, src, m_w, m_temp1d);
163 else
164 {
165 dg::transpose( m_nx, m_ny, src, m_temp);
166 dg::simple_average( m_ny, m_nx, m_temp, m_w, m_temp1d);
167 }
168 if( extend )
169 dg::extend_column( m_nx, m_ny, m_temp1d, res);
170 else
171 res = m_temp1d;
172 }
173 else
174 {
175 //temp1d has size m_nx
176 if( "exact" == m_mode)
177 {
178 dg::transpose( m_nx, m_ny, src, m_temp);
179 dg::average( m_ny, m_nx, m_temp, m_w, m_temp1d);
180 }
181 else
182 dg::simple_average( m_nx, m_ny, src, m_w, m_temp1d);
183 if( extend )
184 dg::extend_line( m_nx, m_ny, m_temp1d, res);
185 else
186 res = m_temp1d;
187 }
188 }
189
190 private:
191 unsigned m_nx, m_ny;
192 ContainerType m_w, m_temp, m_temp1d;
193 bool m_transpose;
194 std::string m_mode;
195};
196
197
198}//namespace dg
class intended for the use in throw statements
Definition: exceptions.h:83
small class holding a stringstream
Definition: exceptions.h:29
#define _ping_
Definition: exceptions.h:12
base topology classes
void scal(ContainerType &x, get_value_type< ContainerType > alpha)
Definition: blas1.h:185
void pointwiseDot(get_value_type< ContainerType > alpha, const ContainerType1 &x1, const ContainerType2 &x2, get_value_type< ContainerType > beta, ContainerType &y)
Definition: blas1.h:336
coo3d
3d contra- and covariant coordinates
Definition: enums.h:177
direction
Direction of a discrete derivative.
Definition: enums.h:97
coo2d
2d coordinates
Definition: enums.h:171
@ yz
yz plane
@ xy
xy plane
@ x
x direction
@ z
z direction
@ x
x direction
MPI_Vector< thrust::host_vector< real_type > > weights(const aRealMPITopology2d< real_type > &g)
Nodal weight coefficients.
Definition: mpi_weights.h:22
void extend_column(unsigned nx, unsigned ny, const ContainerType &in, ContainerType &out)
Copy a line into columns of output vector.
Definition: average_dispatch.h:67
void extend_line(unsigned nx, unsigned ny, const ContainerType &in, ContainerType &out)
Copy a line into rows of output vector.
Definition: average_dispatch.h:47
void transpose(unsigned nx, unsigned ny, const ContainerType &in, ContainerType &out)
Transpose vector.
Definition: average_dispatch.h:26
This is the namespace for all functions and classes defined and used by the discontinuous Galerkin li...
Topological average computations in a Cartesian topology.
Definition: average.h:53
void operator()(const ContainerType &src, ContainerType &res, bool extend=true)
Compute the average as configured in the constructor.
Definition: average.h:156
Average(const aTopology2d &g, enum coo2d direction, std::string mode="exact")
Prepare internal workspace.
Definition: average.h:66
ContainerType container_type
Definition: average.h:54
Average(const aTopology3d &g, enum coo3d direction, std::string mode="exact")
Prepare internal workspace.
Definition: average.h:98
A vector view class, usable in dg functions.
Definition: view.h:43
real_type ly() const
length of y
Definition: grid.h:318
unsigned ny() const
number of polynomial coefficients in y
Definition: grid.h:340
real_type lx() const
length of x
Definition: grid.h:312
unsigned Nx() const
number of cells in x
Definition: grid.h:346
unsigned nx() const
number of polynomial coefficients in x
Definition: grid.h:338
unsigned Ny() const
number of cells in y
Definition: grid.h:352
An abstract base class for three-dimensional grids.
Definition: grid.h:523
real_type lx() const
length in x
Definition: grid.h:573
unsigned nz() const
number of polynomial coefficients in z
Definition: grid.h:616
unsigned Nx() const
number of points in x
Definition: grid.h:622
unsigned ny() const
number of polynomial coefficients in y
Definition: grid.h:614
real_type ly() const
length in y
Definition: grid.h:579
unsigned Ny() const
number of points in y
Definition: grid.h:628
real_type lz() const
length in z
Definition: grid.h:585
unsigned Nz() const
number of points in z
Definition: grid.h:634
unsigned nx() const
number of polynomial coefficients in x
Definition: grid.h:612
Creation functions for integration weights and their inverse.