Extension: Matrix functions
#include "dg/matrix/matrix.h"
sqrt_cauchy.h
Go to the documentation of this file.
1#pragma once
2// #undef BOOST_MATH_MAX_SERIES_ITERATION_POLICY
3// #define BOOST_MATH_MAX_SERIES_ITERATION_POLICY 1000000000
4#include <boost/math/special_functions.hpp>
5
6#include "dg/algorithm.h"
7
9#ifndef M_PI
10#define M_PI 3.14159265358979323846
11#endif
12
13
17namespace dg {
18namespace mat {
19
35template<class Container>
37{
38 public:
39 using container_type = Container;
47 SqrtCauchyInt( const Container& copyable)
48 {
49 m_helper = m_temp = m_helper3 = copyable;
50 }
52 template<class ...Params>
53 void construct( Params&& ...ps)
54 {
55 //construct and swap
56 *this = SqrtCauchyInt( std::forward<Params>( ps)...);
57 }
58
60 const double& w() const{return m_w;}
61
65 template<class MatrixType>
66 auto make_denominator(MatrixType& A) const{
67 return [&A=A, &w = m_w] ( const auto& x, auto& y)
68 {
69 dg::blas2::symv(A, x, y); // A x
70 dg::blas1::axpby(w*w, x, 1., y); // ( w^2 + A) x
71 };
72 }
73
97 template<class MatrixType0, class MatrixType1, class ContainerType0,
98 class ContainerType1>
99 void operator()(MatrixType0&& A, MatrixType1&& wAinv, const ContainerType0&
100 b, ContainerType1& x, std::array<value_type,2> EVs, unsigned
101 steps, int exp = +1)
102 {
103 dg::blas1::copy(0., m_helper3);
104 value_type s=0.;
105 value_type c=0.;
106 value_type d=0.;
107 m_w=0.;
108 value_type t=0.;
109 value_type minEV = EVs[0], maxEV = EVs[1];
110 value_type sqrtminEV = std::sqrt(minEV);
111 const value_type k2 = minEV/maxEV;
112 const value_type sqrt1mk2 = std::sqrt(1.-k2);
113 const value_type Ks=boost::math::ellint_1(sqrt1mk2 );
114 const value_type fac = 2.* Ks*sqrtminEV/(M_PI*steps);
115 for (unsigned j=1; j<steps+1; j++)
116 {
117 t = (j-0.5)*Ks/steps; //imaginary part .. 1i missing
118 // approx 5e-6 s each evaluation of 3 boost fcts
119 c = 1./boost::math::jacobi_cn(sqrt1mk2, t);
120 s = boost::math::jacobi_sn(sqrt1mk2, t)*c;
121 d = boost::math::jacobi_dn(sqrt1mk2, t)*c;
122 m_w = sqrtminEV*s;
123 dg::blas1::axpby(c*d, b, 0.0 , m_helper); //m_helper = c d b
124 dg::blas2::symv( std::forward<MatrixType1>(wAinv), m_helper, m_temp);
125
126 dg::blas1::axpby(fac, m_temp, 1.0, m_helper3); // m_helper3 += fac (w^2 + A )^(-1) c d x
127 }
128 if( exp > 0)
129 dg::blas2::symv(A, m_helper3, x);
130 else
131 dg::blas1::copy( m_helper3, x);
132 }
133
134 private:
135 Container m_helper, m_temp, m_helper3;
136 value_type m_w;
137};
138
142template< class Container>
144{
145 public:
146 using container_type = Container;
167 template<class MatrixType>
169 MatrixType& A,
170 const Container& weights,
171 value_type epsCG,
172 unsigned iterCauchy,
173 std::array<value_type,2> EVs, int exp)
174 {
175 m_pcg.construct( weights, 10000);
176 Container m_temp = weights;
177 m_iterCauchy = iterCauchy;
178 m_cauchysqrtint.construct(weights);
179 m_EVs = EVs;
180 m_A = [&]( const Container& x, Container& y){
181 dg::blas2::symv( A, x, y);
182 };
183 m_op = m_cauchysqrtint.make_denominator(A);
184 m_wAinv = [&, eps = epsCG, w = weights]
185 ( const Container& x, Container& y){
186 m_pcg.solve( m_op, y, x, 1., w, eps);
187 };
188 m_exp = exp;
189 }
191 template<class ...Params>
192 void construct( Params&& ...ps)
193 {
194 //construct and swap
195 *this = DirectSqrtCauchy( std::forward<Params>( ps)...);
196 }
204 unsigned operator()(const Container& b, Container& x)
205 {
206 m_cauchysqrtint(m_A, m_wAinv, b, x, m_EVs, m_iterCauchy, m_exp);
207 return m_iterCauchy;
208 }
209 private:
210 unsigned m_iterCauchy;
211 std::function<void ( const Container&, Container&)> m_A, m_wAinv, m_op;
212 dg::PCG<Container> m_pcg;
213 dg::mat::SqrtCauchyInt<Container> m_cauchysqrtint;
214 std::array<value_type,2> m_EVs;
215 int m_exp;
216};
217
218
219} //namespace mat
220} //namespace dg
void construct(Params &&...ps)
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 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)
typename TensorTraits< std::decay_t< Vector > >::value_type get_value_type
Classes for Krylov space approximations of a Matrix-Vector product.
#define M_PI
M_PI is non-standard ... so MSVC complains.
Definition: sqrt_cauchy.h:10
Shortcut for solve directly via sqrt Cauchy combined with PCG inversions.
Definition: sqrt_cauchy.h:144
Container container_type
Definition: sqrt_cauchy.h:146
unsigned operator()(const Container &b, Container &x)
Compute via sqrt Cauchy integral solve.
Definition: sqrt_cauchy.h:204
DirectSqrtCauchy(MatrixType &A, const Container &weights, value_type epsCG, unsigned iterCauchy, std::array< value_type, 2 > EVs, int exp)
Construct DirectSqrtCauchy.
Definition: sqrt_cauchy.h:168
void construct(Params &&...ps)
Perfect forward parameters to one of the constructors.
Definition: sqrt_cauchy.h:192
DirectSqrtCauchy()
empty object ( no memory allocation)
Definition: sqrt_cauchy.h:149
dg::get_value_type< Container > value_type
Definition: sqrt_cauchy.h:147
Cauchy integral
Definition: sqrt_cauchy.h:37
void construct(Params &&...ps)
Perfect forward parameters to one of the constructors.
Definition: sqrt_cauchy.h:53
dg::get_value_type< Container > value_type
Definition: sqrt_cauchy.h:40
void operator()(MatrixType0 &&A, MatrixType1 &&wAinv, const ContainerType0 &b, ContainerType1 &x, std::array< value_type, 2 > EVs, unsigned steps, int exp=+1)
Cauchy integral
Definition: sqrt_cauchy.h:99
auto make_denominator(MatrixType &A) const
Definition: sqrt_cauchy.h:66
SqrtCauchyInt(const Container &copyable)
Construct Rhs operator.
Definition: sqrt_cauchy.h:47
SqrtCauchyInt()
Definition: sqrt_cauchy.h:41
const double & w() const
The in .
Definition: sqrt_cauchy.h:60
Container container_type
Definition: sqrt_cauchy.h:39