Extension: Matrix functions
#include "dg/matrix/matrix.h"
sqrt_ode.h
Go to the documentation of this file.
1#pragma once
2
3#include "dg/algorithm.h"
4#include "tridiaginv.h"
5
9namespace dg {
10namespace mat {
11
37template<class value_type, class ExplicitRHS>
38auto make_directODESolve( ExplicitRHS&& ode,
39 std::string tableau, value_type epsTimerel, value_type epsTimeabs,
40 unsigned& number, value_type t0 = 0., value_type t1 = 1.)
41{
42 return [=, &num = number,
43 cap = std::tuple<ExplicitRHS>(std::forward<ExplicitRHS>(ode)),
44 rtol = epsTimerel, atol = epsTimeabs]
45 ( const auto& x, auto& b) mutable
46 {
47 value_type reject_limit = 2;
48 dg::Adaptive<dg::ERKStep<std::decay_t<decltype(b)>>> adapt( tableau, x);
49 dg::AdaptiveTimeloop<std::decay_t<decltype(b)>> loop( adapt,
50 std::get<0>(cap), dg::pid_control, dg::l2norm, rtol, atol,
51 reject_limit);
52 loop.integrate( t0, x, t1, b);
53 num = adapt.nsteps();
54 };
55}
56
67template< class Container>
69{
70 public:
71 using container_type = Container;
74
81 template<class MatrixType>
82 InvSqrtODE( MatrixType& A, const Container& copyable)
83 {
84 m_helper = copyable;
85 m_A = [&]( const Container& x, Container& y){
86 return dg::apply( A, x, y);
87 };
88 m_yp_ex.set_max(3, copyable);
89 }
91 template<class ...Params>
92 void construct( Params&& ...ps)
93 {
94 //construct and swap
95 *this = SqrtODE( std::forward<Params>( ps)...);
96 }
97
98 const value_type& time() const{ return m_time;}
99
100 auto make_operator() const{
101 return [&t = m_time, &A = m_A]( const Container& x, Container& y)
102 {
103 dg::blas2::symv(A, x, y);
104 dg::blas1::axpby( t, x, (1.-t), y);
105 };
106 }
107 template<class MatrixType>
108 void set_inverse_operator( const MatrixType& OpInv ) {
109 m_Ainv = OpInv;
110 }
120 void operator()(value_type t, const Container& y, Container& yp)
121 {
122 m_time = t;
123 dg::blas2::symv(m_A, y, m_helper);
124 dg::blas1::axpby(0.5, y, -0.5, m_helper);
125
126 m_yp_ex.extrapolate(t, yp);
127 dg::blas2::symv( m_Ainv, m_helper, yp);
128 m_yp_ex.update(t, yp);
129 }
130 private:
131 Container m_helper;
132 std::function<void(const Container&, Container&)> m_A, m_Ainv;
133 value_type m_time;
135};
136
137
152template< class Matrix, class Preconditioner, class Container>
153InvSqrtODE<Container> make_inv_sqrtodeCG( Matrix& A, const Preconditioner& P,
154 const Container& weights, dg::get_value_type<Container> epsCG)
155{
156 InvSqrtODE<Container> sqrtode( A, weights);
157 dg::PCG<Container> pcg( weights, 10000);
158 auto op = sqrtode.make_operator();
159 sqrtode.set_inverse_operator( [ = ]( const auto& x, auto& y) mutable
160 {
161 pcg.solve( op, y, x, P, weights, epsCG);
162 });
163 return sqrtode;
164}
165
166template< class Matrix, class Container>
167InvSqrtODE<Container> make_inv_sqrtodeTri( const Matrix& TH, const Container&
168 copyable)
169{
170 InvSqrtODE<Container> sqrtode( TH, copyable);
171 sqrtode.set_inverse_operator( [ &TH = TH, &t = sqrtode.time() ]
172 ( const auto& x, auto& y) mutable
173 {
174 dg::mat::compute_Tinv_y( TH, y, x, (1.-t), t);
175 });
176 return sqrtode;
177}
178
187template<class MatrixType>
188auto make_expode( MatrixType& A)
189{
190 return [&]( auto t, const auto& y, auto& yp) mutable
191 {
192 dg::blas2::symv( A, y, yp);
193 };
194}
195
206template<class MatrixType>
207auto make_besselI0ode( MatrixType& A)
208{
209 return [&m_A = A]( auto t, const auto& z, auto& zp) mutable
210 {
211 dg::blas2::symv(m_A, z[0], zp[0]);
212 dg::blas2::symv(m_A, zp[0], zp[1]);
213 dg::blas1::axpby(-1./t, z[1], 1.0, zp[1]);
214 dg::blas1::copy(z[0],zp[0]);
215
216 };
217}
218
219
220} //namespace mat
221} //namespace dg
unsigned solve(MatrixType0 &&A, ContainerType0 &x, const ContainerType1 &b, MatrixType1 &&P, const ContainerType2 &W, value_type eps=1e-12, value_type nrmb_correction=1, int test_frequency=1)
void apply(get_value_type< ContainerType1 > alpha, MatrixType &&M, const ContainerType1 &x, get_value_type< ContainerType1 > beta, ContainerType2 &y)
void copy(const ContainerTypeIn &source, ContainerTypeOut &target)
void axpby(get_value_type< ContainerType > alpha, const ContainerType1 &x, get_value_type< ContainerType > beta, ContainerType &y)
void symv(MatrixType &&M, const ContainerType1 &x, ContainerType2 &y)
MPI_Vector< thrust::host_vector< real_type > > weights(const aRealMPITopology2d< real_type > &g)
auto make_expode(MatrixType &A)
Right hand side of the exponential ODE.
Definition: sqrt_ode.h:188
auto make_besselI0ode(MatrixType &A)
Right hand side of the (zeroth order) modified Bessel function ODE, rewritten as a system of coupled ...
Definition: sqrt_ode.h:207
InvSqrtODE< Container > make_inv_sqrtodeCG(Matrix &A, const Preconditioner &P, const Container &weights, dg::get_value_type< Container > epsCG)
Right hand side of the square root ODE.
Definition: sqrt_ode.h:153
auto make_directODESolve(ExplicitRHS &&ode, std::string tableau, value_type epsTimerel, value_type epsTimeabs, unsigned &number, value_type t0=0., value_type t1=1.)
Operator that integrates an ODE from 0 to 1 with an adaptive ERK class as timestepper
Definition: sqrt_ode.h:38
static auto pid_control
static auto l2norm
typename TensorTraits< std::decay_t< Vector > >::value_type get_value_type
InvSqrtODE< Container > make_inv_sqrtodeTri(const Matrix &TH, const Container &copyable)
Definition: sqrt_ode.h:167
Classes for Krylov space approximations of a Matrix-Vector product.
void extrapolate(value_type t, ContainerType0 &new_x) const
void set_max(unsigned max, const ContainerType &copyable)
void update(value_type t_new, const ContainerType0 &new_entry)
void integrate(value_type t0, const ContainerType &u0, value_type t1, ContainerType &u1)
Right hand side of the square root ODE.
Definition: sqrt_ode.h:69
void construct(Params &&...ps)
Perfect forward parameters to one of the constructors.
Definition: sqrt_ode.h:92
Container container_type
Definition: sqrt_ode.h:71
void operator()(value_type t, const Container &y, Container &yp)
Compute rhs term.
Definition: sqrt_ode.h:120
InvSqrtODE()
Definition: sqrt_ode.h:73
dg::get_value_type< Container > value_type
Definition: sqrt_ode.h:72
InvSqrtODE(MatrixType &A, const Container &copyable)
Construct SqrtOde operator.
Definition: sqrt_ode.h:82
const value_type & time() const
Definition: sqrt_ode.h:98
auto make_operator() const
Definition: sqrt_ode.h:100
void set_inverse_operator(const MatrixType &OpInv)
Definition: sqrt_ode.h:108