Discontinuous Galerkin Library
#include "dg/algorithm.h"
evaluation.h
Go to the documentation of this file.
1#pragma once
2
3#include <cassert>
4#include <cmath>
5#include <thrust/host_vector.h>
6#include "dg/backend/config.h"
7#include "grid.h"
8#include "operator.h"
9
13namespace dg
14{
16namespace create
17{
26template<class real_type>
27thrust::host_vector<real_type> abscissas( const RealGrid1d<real_type>& g)
28{
29 thrust::host_vector<real_type> abs(g.size());
30 for( unsigned i=0; i<g.N(); i++)
31 for( unsigned j=0; j<g.n(); j++)
32 {
33 real_type xmiddle = DG_FMA( g.h(), (real_type)(i), g.x0());
34 real_type h2 = g.h()/2.;
35 real_type absj = 1.+g.dlt().abscissas()[j];
36 abs[i*g.n()+j] = DG_FMA( h2, absj, xmiddle);
37 }
38 return abs;
39}
40}//
42
45
46
66template< class UnaryOp,class real_type>
67thrust::host_vector<real_type> evaluate( UnaryOp f, const RealGrid1d<real_type>& g)
68{
69 thrust::host_vector<real_type> abs = create::abscissas( g);
70 for( unsigned i=0; i<g.size(); i++)
71 abs[i] = f( abs[i]);
72 return abs;
73};
75template<class real_type>
76thrust::host_vector<real_type> evaluate( real_type (f)(real_type), const RealGrid1d<real_type>& g)
77{
78 thrust::host_vector<real_type> v = evaluate<real_type (real_type)>( *f, g);
79 return v;
80};
82
83
103template< class BinaryOp, class real_type>
104thrust::host_vector<real_type> evaluate( const BinaryOp& f, const aRealTopology2d<real_type>& g)
105{
106 thrust::host_vector<real_type> absx = create::abscissas( g.gx());
107 thrust::host_vector<real_type> absy = create::abscissas( g.gy());
108
109 thrust::host_vector<real_type> v( g.size());
110 for( unsigned i=0; i<g.Ny(); i++)
111 for( unsigned k=0; k<g.ny(); k++)
112 for( unsigned j=0; j<g.Nx(); j++)
113 for( unsigned r=0; r<g.nx(); r++)
114 v[ ((i*g.ny()+k)*g.Nx() + j)*g.nx() + r] =
115 f( absx[j*g.nx()+r], absy[i*g.ny()+k]);
116 return v;
117};
119template<class real_type>
120thrust::host_vector<real_type> evaluate( real_type(f)(real_type, real_type), const aRealTopology2d<real_type>& g)
121{
122 return evaluate<real_type(real_type, real_type)>( *f, g);
123};
125
145template< class TernaryOp,class real_type>
146thrust::host_vector<real_type> evaluate( const TernaryOp& f, const aRealTopology3d<real_type>& g)
147{
148 thrust::host_vector<real_type> absx = create::abscissas( g.gx());
149 thrust::host_vector<real_type> absy = create::abscissas( g.gy());
150 thrust::host_vector<real_type> absz = create::abscissas( g.gz());
151
152 thrust::host_vector<real_type> v( g.size());
153 for( unsigned s=0; s<g.Nz(); s++)
154 for( unsigned ss=0; ss<g.nz(); ss++)
155 for( unsigned i=0; i<g.Ny(); i++)
156 for( unsigned ii=0; ii<g.ny(); ii++)
157 for( unsigned k=0; k<g.Nx(); k++)
158 for( unsigned kk=0; kk<g.nx(); kk++)
159 v[ ((((s*g.nz()+ss)*g.Ny()+i)*g.ny()+ii)*g.Nx() + k)*g.nx() + kk] =
160 f( absx[k*g.nx()+kk], absy[i*g.ny()+ii], absz[s*g.nz()+ss]);
161 return v;
162};
164template<class real_type>
165thrust::host_vector<real_type> evaluate( real_type(f)(real_type, real_type, real_type), const aRealTopology3d<real_type>& g)
166{
167 return evaluate<real_type(real_type, real_type, real_type)>( *f, g);
168};
171
183template<class real_type>
184thrust::host_vector<real_type> integrate( const thrust::host_vector<real_type>& in, const RealGrid1d<real_type>& g, dg::direction dir = dg::forward)
185{
186 double h = g.h();
187 unsigned n = g.n();
188 thrust::host_vector<real_type> to_out(g.size(), 0.);
189 thrust::host_vector<real_type> to_in(in);
190 if( dir == dg::backward ) //reverse input vector
191 {
192 for( unsigned i=0; i<in.size(); i++)
193 to_in[i] = in[ in.size()-1-i];
194 }
195
196
197 dg::Operator<real_type> forward = g.dlt().forward();
198 dg::Operator<real_type> backward = g.dlt().backward();
199 dg::Operator<real_type> ninj = create::ninj<real_type>( n );
200 Operator<real_type> t = create::pipj_inv<real_type>(n);
201 t *= h/2.;
203 real_type constant = 0.;
204
205 for( unsigned i=0; i<g.N(); i++)
206 {
207 for( unsigned k=0; k<n; k++)
208 {
209 for( unsigned l=0; l<n; l++)
210 to_out[ i*n + k] += ninj(k,l)*to_in[ i*n + l];
211 to_out[ i*n + k] += constant;
212 }
213 for( unsigned l=0; l<n; l++)
214 constant += h*forward(0,l)*to_in[i*n+l];
215 }
216 thrust::host_vector<real_type> out(to_out);
217 if( dir == dg::backward ) //reverse output
218 {
219 for( unsigned i=0; i<in.size(); i++)
220 out[i] = -to_out[ in.size()-1-i]; // minus from reversing!
221 }
222 return out;
223}
224
225
238template< class UnaryOp,class real_type>
239thrust::host_vector<real_type> integrate( UnaryOp f, const RealGrid1d<real_type>& g, dg::direction dir = dg::forward)
240{
241 thrust::host_vector<real_type> vector = evaluate( f, g);
242 return integrate<real_type>(vector, g, dir);
243}
245template<class real_type>
246thrust::host_vector<real_type> integrate( real_type (f)(real_type), const RealGrid1d<real_type>& g, dg::direction dir = dg::forward)
247{
248 thrust::host_vector<real_type> vector = evaluate( f, g);
249 return integrate<real_type>(vector, g, dir);
250};
252
254}//namespace dg
255
base topology classes
direction
Direction of a discrete derivative.
Definition: enums.h:97
@ backward
backward derivative (cell to the left and current cell)
Definition: enums.h:99
@ forward
forward derivative (cell to the right and current cell)
Definition: enums.h:98
thrust::host_vector< real_type > integrate(const thrust::host_vector< real_type > &in, const RealGrid1d< real_type > &g, dg::direction dir=dg::forward)
Indefinite integral of a function on a grid.
Definition: evaluation.h:184
thrust::host_vector< real_type > evaluate(UnaryOp f, const RealGrid1d< real_type > &g)
Evaluate a 1d function on grid coordinates.
Definition: evaluation.h:67
Operator< real_type > ninj(unsigned n)
Create the N-matrix.
Definition: operator.h:639
This is the namespace for all functions and classes defined and used by the discontinuous Galerkin li...
1D grid
Definition: grid.h:80
real_type h() const
cell size
Definition: grid.h:129
unsigned n() const
number of polynomial coefficients
Definition: grid.h:141
const DLT< real_type > & dlt() const
the discrete legendre transformation
Definition: grid.h:197
unsigned size() const
n()*N() (Total number of grid points)
Definition: grid.h:191
unsigned N() const
number of cells
Definition: grid.h:135
An abstract base class for two-dimensional grids.
Definition: grid.h:277
const RealGrid1d< real_type > & gy() const
The y-axis grid.
Definition: grid.h:379
unsigned ny() const
number of polynomial coefficients in y
Definition: grid.h:340
unsigned size() const
The total number of points.
Definition: grid.h:424
unsigned Nx() const
number of cells in x
Definition: grid.h:346
const RealGrid1d< real_type > & gx() const
The x-axis grid.
Definition: grid.h:377
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
unsigned size() const
The total number of points.
Definition: grid.h:670
const RealGrid1d< real_type > & gz() const
The z-axis grid.
Definition: grid.h:664
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
const RealGrid1d< real_type > & gx() const
The x-axis grid.
Definition: grid.h:660
const RealGrid1d< real_type > & gy() const
The y-axis grid.
Definition: grid.h:662
unsigned Ny() const
number of points in y
Definition: grid.h:628
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