Discontinuous Galerkin Library
#include "dg/algorithm.h"
implicit.h
Go to the documentation of this file.
1#pragma once
2#include "pcg.h"
3
4namespace dg{
6namespace detail{
7template<class Implicit, class Solver>
8struct Adaptor
9{
10 Adaptor( Implicit& im, Solver& solver) : m_im(im), m_solver(solver){}
11 template<class ContainerType, class value_type>
12 void operator()( value_type t, const ContainerType& x, ContainerType& y)
13 {
14 m_im( t,x,y);
15 }
16 template<class ContainerType, class value_type>
17 void operator()( value_type alpha, value_type t, ContainerType& y, const ContainerType& yp)
18 {
19 m_solver.solve( alpha, m_im, t, y, yp);
20 }
21 private:
22 Implicit& m_im;
23 Solver& m_solver;
24};
25}
27
58template<class ContainerType>
60{
61 using container_type = ContainerType;
85 template<class Implicit>
86 DefaultSolver( Implicit& im, const ContainerType& copyable,
87 unsigned max_iter, value_type eps): m_max_iter(max_iter)
88 {
89 m_im = [&im = im]( value_type t, const ContainerType& y, ContainerType&
90 yp) mutable
91 {
92 im( t, y, yp);
93 };
94 // We'll have to do some gymnastics to store precond and weights
95 // We do so by wrapping pcg's solve method
96 // This should not be an example of how to write custom Solvers!
97 m_solve = [ &weights = im.weights(), &precond = im.precond(), pcg =
98 dg::PCG<ContainerType>( copyable, max_iter), eps = eps ]
99 ( const std::function<void( const ContainerType&,ContainerType&)>&
100 wrapper, ContainerType& y, const ContainerType& ys) mutable
101 {
102 return pcg.solve( wrapper, y, ys, precond, weights, eps);
103 };
104 }
106 template<class ...Params>
107 void construct( Params&& ...ps)
108 {
109 //construct and swap
110 *this = DefaultSolver( std::forward<Params>( ps)...);
111 }
114 void set_benchmark( bool benchmark){ m_benchmark = benchmark;}
115
116 void operator()( value_type alpha, value_type time, ContainerType& y, const
117 ContainerType& ys)
118 {
119#ifdef MPI_VERSION
120 int rank;
121 MPI_Comm_rank(MPI_COMM_WORLD, &rank);
122#endif//MPI
123 auto wrapper = [a = alpha, t = time, &i = m_im]( const auto& x, auto& y){
124 i( t, x, y);
125 dg::blas1::axpby( 1., x, -a, y);
126 };
127 Timer ti;
128 if(m_benchmark) ti.tic();
129 dg::blas1::copy( ys, y); // take rhs as initial guess
130 unsigned number = m_solve( wrapper, y, ys);
131 if( m_benchmark)
132 {
133 ti.toc();
134 DG_RANK0 std::cout << "# of pcg iterations time solver: "
135 <<number<<"/"<<m_max_iter<<" took "<<ti.diff()<<"s\n";
136 }
137 }
138 private:
139 std::function<void( value_type, const ContainerType&, ContainerType&)>
140 m_im;
141 std::function< unsigned ( const std::function<void( const
142 ContainerType&,ContainerType&)>&, ContainerType&,
143 const ContainerType&)> m_solve;
144 unsigned m_max_iter;
145 bool m_benchmark = true;
146};
147
148}//namespace dg
Preconditioned conjugate gradient method to solve .
Definition: pcg.h:57
void copy(const ContainerTypeIn &source, ContainerTypeOut &target)
Definition: blas1.h:164
void axpby(get_value_type< ContainerType > alpha, const ContainerType1 &x, get_value_type< ContainerType > beta, ContainerType &y)
Definition: blas1.h:231
@ y
y 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
typename TensorTraits< std::decay_t< Vector > >::value_type get_value_type
Definition: tensor_traits.h:38
This is the namespace for all functions and classes defined and used by the discontinuous Galerkin li...
PCG Solver class for solving .
Definition: implicit.h:60
DefaultSolver()
No memory allocation.
Definition: implicit.h:64
ContainerType container_type
Definition: implicit.h:61
DefaultSolver(Implicit &im, const ContainerType &copyable, unsigned max_iter, value_type eps)
Definition: implicit.h:86
void construct(Params &&...ps)
Perfect forward parameters to one of the constructors.
Definition: implicit.h:107
void set_benchmark(bool benchmark)
Set or unset performance timings during iterations.
Definition: implicit.h:114
get_value_type< ContainerType > value_type
Definition: implicit.h:62
void operator()(value_type alpha, value_type time, ContainerType &y, const ContainerType &ys)
Definition: implicit.h:116
Simple tool for performance measuring.
Definition: dg_doc.h:445
double diff() const
Return time in seconds elapsed between tic and toc.
void toc()
Stop timer.
void tic()
Start timer.
#define DG_RANK0
Definition: typedefs.h:147