Extension: Matrix functions
#include "dg/matrix/matrix.h"
polarization.h
Go to the documentation of this file.
1#pragma once
2#include "dg/algorithm.h"
3
4#include "lanczos.h"
5#include "matrixsqrt.h"
6#include "matrixfunction.h"
7#include "tensorelliptic.h"
8
9namespace dg {
10namespace mat {
11
34template <class Geometry, class Matrix, class Container>
36{
37 public:
57 PolCharge(value_type alpha, std::vector<value_type> eps_gamma,
58 const Geometry& g, direction dir = forward, value_type jfactor=1.,
59 std::string mode = "df", bool commute = false):
60 PolCharge( alpha, eps_gamma, g, g.bcx(), g.bcy(), dir, jfactor, mode,
61 commute)
62 { }
81 PolCharge( value_type alpha, std::vector<value_type> eps_gamma,
82 const Geometry& g, bc bcx, bc bcy, direction dir = forward,
83 value_type jfactor=1., std::string mode = "df", bool commute = false)
84 {
85 m_alpha = alpha;
86 m_eps_gamma = eps_gamma;
87 m_mode = mode;
88 m_commute = commute;
89 m_temp2 = dg::evaluate(dg::zero, g);
90 m_temp = m_temp2;
91 m_temp2_ex.set_max(1, m_temp2);
92 m_temp_ex.set_max(1, m_temp);
93 if (m_mode == "df")
94 {
95 m_ell.construct(g, bcx, bcy, dir, jfactor );
96 m_multi_g.construct(g, 3);
97 for( unsigned u=0; u<3; u++)
98 {
99 m_multi_gamma.push_back( {m_alpha, {m_multi_g.grid(u), bcx, bcy,
100 dir, jfactor}});
101 }
102 }
103 if (m_mode == "ff")
104 {
105 m_ell.construct(g, bcx, bcy, dir, jfactor );
106 m_multi_gamma.resize(1);
107 m_multi_gamma.resize(1);
108 m_multi_gamma[0].construct( m_alpha, dg::Elliptic<Geometry,
109 Matrix, Container>{g, bcx, bcy, dir, jfactor});
110
111 m_inv_sqrt.construct( m_multi_gamma[0], -1,
112 m_multi_gamma[0].weights(), m_eps_gamma[0]);
113 }
114 if (m_mode == "ffO4")
115 {
116 m_tensorell.construct(g, bcx, bcy, dir, jfactor);
117 m_multi_g.construct(g, 3);
118 for( unsigned u=0; u<3; u++)
119 {
120 m_multi_gamma.push_back({ m_alpha, {m_multi_g.grid(u), bcx, bcy,
121 dir, jfactor}});
122 }
123 }
124 }
125
127 template<class ...Params>
128 void construct( Params&& ...ps)
129 {
130 //construct and swap
131 *this = PolCharge( std::forward<Params>( ps)...);
132 }
139 template<class ContainerType0>
140 void set_chi( const ContainerType0& sigma)
141 {
142 if (m_mode == "ff")
143 m_ell.set_chi(sigma);
144 if (m_mode == "ffO4")
145 {
146 m_tensorell.set_chi(sigma);
147 }
148 }
155 template<class ContainerType0>
157 {
158 if (m_mode == "ff")
159 m_ell.set_chi(tau);
160 if (m_mode == "ffO4")
161 {
162 m_tensorell.set_chi(tau);
163 }
164 }
171 template<class ContainerType0>
172 void set_iota( const ContainerType0& sigma)
173 {
174 if (m_mode == "ffO4")
175 {
176 m_tensorell.set_iota(sigma);
177 }
178 }
183 void set_commute( bool commute) {m_commute = commute;}
188 bool get_commute() const {return m_commute;}
195 const Container& weights()const {
196 if (m_mode == "ffO4")
197 return m_tensorell.weights();
198 else return m_ell.weights();
199 }
208 const Container& precond()const {
209 if (m_mode == "ffO4")
210 return m_tensorell.precond();
211 else return m_ell.precond();
212 }
213
214 template<class ContainerType0, class ContainerType1>
215 void variation( const ContainerType0& phi, ContainerType1& varphi)
216 {
217 if (m_mode == "ff")
218 m_ell.variation(phi, varphi);
219 if (m_mode == "ffO4")
220 m_tensorell.variation(phi, varphi);
221 }
230 template<class ContainerType0, class ContainerType1>
231 void operator()( const ContainerType0& x, ContainerType1& y){
232 symv( 1, x, 0, y);
233 }
234
243 template<class ContainerType0, class ContainerType1>
244 void symv( const ContainerType0& x, ContainerType1& y){
245 symv( 1, x, 0, y);
246 }
257 template<class ContainerType0, class ContainerType1>
258 void symv( value_type alpha, const ContainerType0& x, value_type beta, ContainerType1& y)
259 {
260
261 if (m_alpha == 0)
262 {
263 dg::blas1::scal( y, beta);
264 return;
265 }
266 if (m_mode == "df")
267 {
268 if (m_commute == false)
269 {
270 m_temp2_ex.extrapolate(m_temp2);
271 std::vector<unsigned> number = m_multi_g.solve(
272 m_multi_gamma, m_temp2, x, m_eps_gamma);
273 m_temp2_ex.update(m_temp2);
274
275 m_ell.symv(alpha, m_temp2, beta, y);
276 }
277 else
278 {
279 m_ell.symv(1.0, x, 0.0, m_temp);
280
281 m_temp2_ex.extrapolate(m_temp2);
282 std::vector<unsigned> number = m_multi_g.solve(
283 m_multi_gamma, m_temp2, m_temp, m_eps_gamma);
284 m_temp2_ex.update(m_temp2);
285
286 dg::blas1::axpby(alpha, m_temp2, beta, y);
287
288 }
289
290 }
291 if (m_mode == "ff" ) //assuming constant FLR effects
292 {
293 if (m_commute == false)
294 {
295 unsigned number = 0 ;
296 dg::apply( m_inv_sqrt, x, m_temp2);
297 //std::cout << "#number of sqrt iterations: " << number << " "<<m_eps_gamma[0]<< std::endl;
298
299 m_ell.symv(1.0, m_temp2, 0.0, m_temp);
300
301 dg::apply( m_inv_sqrt, m_temp, m_temp2);
302 //std::cout << "#number of sqrt iterations: " << number << std::endl;
303 number++;//avoid compiler warning
304
305 dg::blas1::axpby(alpha, m_temp2, beta, y);
306 }
307 else
308 {
309 //TODO not implemented so far (relevant thermal models)
310 }
311 }
312 if (m_mode == "ffO4")
313 {
314 if (m_commute == false)
315 {
316 m_temp2_ex.extrapolate(m_temp2);
317 std::vector<unsigned> number = m_multi_g.solve(
318 m_multi_gamma, m_temp2, x, m_eps_gamma);
319 m_temp2_ex.update(m_temp2);
320
321 m_tensorell.symv(1.0, m_temp2, 0.0, m_temp);
322
323 m_temp_ex.extrapolate(m_temp2);
324 number = m_multi_g.solve( m_multi_gamma, m_temp2,
325 m_temp, m_eps_gamma);
326 m_temp_ex.update(m_temp2);
327
328 dg::blas1::axpby(alpha, m_temp2, beta, y);
329 }
330 if (m_commute == true)
331 {
332 //TODO not implemented so far (relevant thermal models)
333 }
334 }
335 }
336
337 private:
340
341 std::vector< dg::Helmholtz<Geometry, Matrix, Container> > m_multi_gamma;
344 Container m_temp, m_temp2;
345 value_type m_alpha;
346 std::vector<value_type> m_eps_gamma;
347 std::string m_mode;
348 dg::Extrapolation<Container> m_temp2_ex, m_temp_ex;
349 bool m_commute;
350
351
352};
353
354} //namespace mat
355
357template< class G, class M, class V>
358struct TensorTraits< mat::PolCharge<G, M, V> >
359{
361 using tensor_category = SelfMadeMatrixTag;
362};
364} //namespace dg
Various arbitary wavelength polarization charge operators of delta-f (df) and full-f (ff)
Definition: polarization.h:36
PolCharge(value_type alpha, std::vector< value_type > eps_gamma, const Geometry &g, bc bcx, bc bcy, direction dir=forward, value_type jfactor=1., std::string mode="df", bool commute=false)
Construct from boundary conditions.
Definition: polarization.h:81
PolCharge()
empty object ( no memory allocation)
Definition: polarization.h:40
const Container & weights() const
Return the vector making the matrix symmetric.
Definition: polarization.h:195
get_value_type< Container > value_type
Definition: polarization.h:38
bool get_commute() const
Get the current state of commute.
Definition: polarization.h:188
void set_chi(const SparseTensor< ContainerType0 > &tau)
Change in the elliptic or tensor elliptic operator.
Definition: polarization.h:156
void variation(const ContainerType0 &phi, ContainerType1 &varphi)
Definition: polarization.h:215
void symv(value_type alpha, const ContainerType0 &x, value_type beta, ContainerType1 &y)
Compute elliptic term and add to output.
Definition: polarization.h:258
void symv(const ContainerType0 &x, ContainerType1 &y)
Compute elliptic term and store in output.
Definition: polarization.h:244
void operator()(const ContainerType0 &x, ContainerType1 &y)
Compute elliptic term and store in output.
Definition: polarization.h:231
void set_iota(const ContainerType0 &sigma)
Change in the tensor elliptic operator.
Definition: polarization.h:172
void construct(Params &&...ps)
Perfect forward parameters to one of the constructors.
Definition: polarization.h:128
void set_chi(const ContainerType0 &sigma)
Change in the elliptic or tensor elliptic operator.
Definition: polarization.h:140
const Container & precond() const
Return the default preconditioner to use in conjugate gradient.
Definition: polarization.h:208
void set_commute(bool commute)
Set the commute.
Definition: polarization.h:183
PolCharge(value_type alpha, std::vector< value_type > eps_gamma, const Geometry &g, direction dir=forward, value_type jfactor=1., std::string mode="df", bool commute=false)
Construct from Grid.
Definition: polarization.h:57
void apply(get_value_type< ContainerType1 > alpha, MatrixType &&M, const ContainerType1 &x, get_value_type< ContainerType1 > beta, ContainerType2 &y)
static DG_DEVICE double zero(double x)
void axpby(get_value_type< ContainerType > alpha, const ContainerType1 &x, get_value_type< ContainerType > beta, ContainerType &y)
void scal(ContainerType &x, get_value_type< ContainerType > alpha)
direction
thrust::host_vector< real_type > evaluate(UnaryOp f, const RealGrid1d< real_type > &g)
typename TensorTraits< std::decay_t< Vector > >::value_type get_value_type
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)
std::vector< unsigned > solve(std::vector< MatrixType > &ops, ContainerType0 &x, const ContainerType1 &b, value_type eps)
void construct(Params &&...ps)
const Geometry & grid(unsigned stage) const
NotATensorTag tensor_category
void construct(Params &&...ps)
Perfect forward parameters to one of the constructors.
Definition: matrixsqrt.h:62
Matrix class that represents the arbitrary polarization operator.
Definition: tensorelliptic.h:25
contains special differential operators