Discontinuous Galerkin Library
#include "dg/algorithm.h"
Loading...
Searching...
No Matches
xspacelib.h
Go to the documentation of this file.
1#ifndef _DG_XSPACELIB_CUH_
2#define _DG_XSPACELIB_CUH_
3
4#include <thrust/host_vector.h>
5
6#include <cassert>
7
9#include "grid.h"
10#include "dlt.h"
11#include "operator.h"
12#include "operator_tensor.h"
13
14
20namespace dg{
21
37template< class T>
41{
42 //dimensions of the matrix
43 size_t num_rows = lhs.num_rows()*rhs.num_rows();
44 size_t num_cols = lhs.num_cols()*rhs.num_cols();
45 size_t num_entries = lhs.num_entries()* rhs.num_entries();
46 // allocate output matrix
47 thrust::host_vector<int> A_row_offsets(num_rows+1), A_column_indices( num_entries);
48 thrust::host_vector<T> A_values( num_entries);
49 //LHS x RHS
50 A_row_offsets[0] = 0;
51 int counter = 0;
52 for( unsigned i=0; i<lhs.num_rows(); i++)
53 for( unsigned j=0; j<rhs.num_rows(); j++)
54 {
55 int num_entries_in_row =
56 (lhs.row_offsets()[i+1] - lhs.row_offsets()[i])*
57 (rhs.row_offsets()[j+1] - rhs.row_offsets()[j]);
58 A_row_offsets[i*rhs.num_rows()+j+1] =
59 A_row_offsets[i*rhs.num_rows()+j] + num_entries_in_row;
60 for( int k=lhs.row_offsets()[i]; k<lhs.row_offsets()[i+1]; k++)
61 for( int l=rhs.row_offsets()[j]; l<rhs.row_offsets()[j+1]; l++)
62 {
63 A_column_indices[counter] =
64 lhs.column_indices()[k]*rhs.num_cols() + rhs.column_indices()[l];
65 A_values[counter] = lhs.values()[k]*rhs.values()[l];
66 counter++;
67 }
68 }
69 // allocate output matrix
70 return { num_rows, num_cols, A_row_offsets, A_column_indices, A_values};
71}
72
88template< class T>
92{
93 if( lhs.num_rows() != rhs.num_rows())
94 throw Error( Message(_ping_)<<"lhs and rhs must have same number of rows: "<<lhs.num_rows()<<" rhs "<<rhs.num_rows());
95
96 //dimensions of the matrix
97 size_t num_rows = lhs.num_rows();
98 size_t num_cols = lhs.num_cols()*rhs.num_cols();
99 size_t num_entries = 0;
100 for( unsigned i=0; i<lhs.num_rows(); i++)
101 {
102 int num_entries_in_row =
103 (lhs.row_offsets()[i+1] - lhs.row_offsets()[i])*
104 (rhs.row_offsets()[i+1] - rhs.row_offsets()[i]);
105 num_entries += num_entries_in_row;
106 }
107 // allocate output matrix
108 thrust::host_vector<int> A_row_offsets(num_rows+1), A_column_indices( num_entries);
109 thrust::host_vector<T> A_values( num_entries);
110 //LHS x RHS
111 A_row_offsets[0] = 0;
112 int counter = 0;
113 for( unsigned i=0; i<lhs.num_rows(); i++)
114 {
115 int num_entries_in_row =
116 (lhs.row_offsets()[i+1] - lhs.row_offsets()[i])*
117 (rhs.row_offsets()[i+1] - rhs.row_offsets()[i]);
118 A_row_offsets[i+1] = A_row_offsets[i] + num_entries_in_row;
119 for( int k=lhs.row_offsets()[i]; k<lhs.row_offsets()[i+1]; k++)
120 for( int l=rhs.row_offsets()[i]; l<rhs.row_offsets()[i+1]; l++)
121 {
122 A_column_indices[counter] =
123 lhs.column_indices()[k]*rhs.num_cols() + rhs.column_indices()[l];
124 A_values[counter] = lhs.values()[k]*rhs.values()[l];
125 counter++;
126 }
127 }
128 return { num_rows, num_cols, A_row_offsets, A_column_indices, A_values};
129}
130
131
132namespace create{
135
145template<class real_type>
147{
148 //create equidistant backward transformation
151 dg::SquareMatrix<real_type> backward1d = backwardeq*forward;
152
153 return (dg::IHMatrix_t<real_type>)dg::tensorproduct( g.N(), backward1d);
154}
155
157template<class real_type>
159{
160 //create equidistant backward transformation
161 auto transformX = backscatter( g.gx());
162 auto transformY = backscatter( g.gy());
163 return dg::tensorproduct( transformY, transformX);
164}
165
167template<class real_type>
169{
170 auto transformX = backscatter( g.gx());
171 auto transformY = backscatter( g.gy());
172 auto transformZ = backscatter( g.gz());
173 return dg::tensorproduct( transformZ, dg::tensorproduct(transformY, transformX));
174}
175
187template<class real_type>
189{
190 //create equidistant backward transformation
193 dg::SquareMatrix<real_type> forward1d = backward*dg::invert(backwardeq);
194
195 return (dg::IHMatrix_t<real_type>)dg::tensorproduct( g.N(), forward1d);
196}
198template<class real_type>
200{
201 //create equidistant backward transformation
202 auto transformX = inv_backscatter( g.gx());
203 auto transformY = inv_backscatter( g.gy());
204 return dg::tensorproduct( transformY, transformX);
205}
206
208template<class real_type>
210{
211 auto transformX = inv_backscatter( g.gx());
212 auto transformY = inv_backscatter( g.gy());
213 auto transformZ = inv_backscatter( g.gz());
214 return dg::tensorproduct( transformZ, dg::tensorproduct(transformY, transformX));
215}
216
218
219} //namespace create
220} //namespace dg
221#endif // _DG_XSPACELIB_CUH_
class intended for the use in throw statements
Definition exceptions.h:83
small class holding a stringstream
Definition exceptions.h:29
A square nxn matrix.
Definition operator.h:31
The discrete legendre trafo class.
#define _ping_
Definition exceptions.h:12
void rhs(double t, double, double &yp)
base topology classes
@ 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
dg::SquareMatrix< T > invert(const dg::SquareMatrix< T > &in)
Compute inverse of square matrix (alias for dg::create::inverse)
Definition operator.h:691
dg::SparseMatrix< int, T, thrust::host_vector > tensorproduct_cols(const dg::SparseMatrix< int, T, thrust::host_vector > &lhs, const dg::SparseMatrix< int, T, thrust::host_vector > &rhs)
Form the tensor (Kronecker) product between two matrices in the column index
Definition xspacelib.h:89
SquareMatrix< T > tensorproduct(const SquareMatrix< T > &op1, const SquareMatrix< T > &op2)
Form the tensor product between two operators.
Definition operator_tensor.h:25
dg::IHMatrix_t< real_type > backscatter(const RealGrid1d< real_type > &g)
Create a matrix that interpolates values to an equidistant grid ready for visualisation.
Definition xspacelib.h:146
dg::IHMatrix_t< real_type > inv_backscatter(const RealGrid1d< real_type > &g)
Create a matrix that transforms values from an equidistant grid back to a dg grid.
Definition xspacelib.h:188
double lhs(double x, double y)
This is the namespace for all functions and classes defined and used by the discontinuous Galerkin li...
Struct providing coefficients for Discrete Legendre Transformation (DLT) related operations.
Definition dlt.h:20
The simplest implementation of aRealTopology.
Definition grid.h:710
A CSR formatted sparse matrix.
Definition sparsematrix.h:96
An abstract base class for Nd-dimensional dG grids.
Definition grid.h:95
RealGrid< real_type, 1 > gy() const
Equivalent to grid(1)
Definition grid.h:360
RealGrid< real_type, 1 > gx() const
Equivalent to grid(0)
Definition grid.h:354
unsigned n(unsigned u=0) const
Get number of polynomial coefficients for axis u.
Definition grid.h:262
RealGrid< real_type, 1 > gz() const
Equivalent to grid(2)
Definition grid.h:366
unsigned N(unsigned u=0) const
Get number of cells for axis u.
Definition grid.h:265
Useful typedefs of commonly used types.