28template<
class Geometry,
class Matrix,
class Container>
36 NestedGrids(): m_stages(0), m_grids(0), m_inter(0), m_project(0){}
45 template<
class ...ContainerParams>
53 m_grids[0].reset(
grid);
56 for(
unsigned u=1; u<
stages; u++)
58 m_grids[u] = m_grids[u-1];
59 m_grids[u]->multiplyCellNumbers(0.5, 0.5);
64 m_project.resize(
stages-1);
65 for(
unsigned u=0; u<
stages-1; u++)
70 2, 2), std::forward<ContainerParams>(ps)...);
72 1, 2, 2), std::forward<ContainerParams>(ps)...);
74 for(
unsigned u=0; u<m_stages; u++)
76 *m_grids[u]), std::forward<ContainerParams>(ps)...);
77 m_w = m_r = m_b = m_x;
82 template<
class ...Params>
94 template<
class ContainerType0>
95 void project(
const ContainerType0& src, std::vector<ContainerType0>& out)
const
98 for(
unsigned u=0; u<m_stages-1; u++)
107 template<
class ContainerType0>
108 std::vector<ContainerType0>
project(
const ContainerType0& src)
const
111 std::vector<Container> out( m_x);
127 const Geometry&
grid(
unsigned stage)
const {
128 return *(m_grids[stage]);
135 return m_inter[stage];
141 return m_project[stage];
144 const Container&
x(
unsigned stage)
const{
return m_x[stage];}
146 const Container&
r(
unsigned stage)
const{
return m_r[stage];}
148 const Container&
b(
unsigned stage)
const{
return m_b[stage];}
150 const Container&
w(
unsigned stage)
const{
return m_w[stage];}
154 std::vector< dg::ClonePtr< Geometry> > m_grids;
155 std::vector< MultiMatrix<Matrix, Container> > m_inter;
156 std::vector< MultiMatrix<Matrix, Container> > m_project;
157 std::vector< Container> m_x, m_r, m_b, m_w;
197template<
class MatrixType0,
class ContainerType0,
class ContainerType1,
class MatrixType1,
class NestedGr
ids>
199 std::vector<MatrixType0>& ops, ContainerType0& x,
const ContainerType1& b,
200 std::vector<MatrixType1>& inverse_ops,
NestedGrids& nested_grids)
208 for(
unsigned u=0; u<nested.
stages()-1; u++)
219 for(
unsigned u=nested.
stages()-1; u>0; u--)
222 dg::apply( inverse_ops[u], nested.
b(u), nested.
x(u));
276template<
class NestedGr
ids,
class MatrixType0,
class MatrixType1,
class MatrixType2>
278 std::vector<MatrixType0>& ops,
279 std::vector<MatrixType1>& inverse_ops_down,
280 std::vector<MatrixType2>& inverse_ops_up,
281 NestedGrids& nested_grids,
unsigned gamma,
unsigned p)
297 dg::apply( inverse_ops_down[p], nested.
b(p), nested.
x(p));
312 if( p+1 == nested.
stages()-1)
315 dg::apply( inverse_ops_up[p+1], nested.
b(p+1), nested.
x(p+1));
324 for(
unsigned u=0; u<gamma; u++)
336 dg::apply(inverse_ops_up[p], nested.
b(p), nested.
x(p));
365template<
class MatrixType0,
class MatrixType1,
class MatrixType2,
class NestedGr
ids,
class ContainerType0,
class ContainerType1>
367 std::vector<MatrixType0>& ops, ContainerType0& x,
const ContainerType1& b,
368 std::vector<MatrixType1>& inverse_ops_down,
369 std::vector<MatrixType2>& inverse_ops_up,
370 NestedGrids& nested_grids,
unsigned gamma,
unsigned mu)
379 for(
unsigned u=0; u<nested.
stages()-1; u++)
390 unsigned s = nested.
stages()-1;
392 dg::apply( inverse_ops_up[s], nested.
b(s), nested.
x(s));
401 for(
int p=nested.
stages()-2; p>=1; p--)
403 for(
unsigned u=0; u<
mu; u++)
404 multigrid_cycle( ops, inverse_ops_down, inverse_ops_up, nested, gamma, p);
410 for(
unsigned u=0; u<
mu; u++)
411 multigrid_cycle( ops, inverse_ops_down, inverse_ops_up, nested, gamma, 0);
439template<
class NestedGrids,
class MatrixType0,
class MatrixType1,
class MatrixType2,
440 class ContainerType0,
class ContainerType1,
class ContainerType2>
442 std::vector<MatrixType0>& ops,
443 ContainerType0& x,
const ContainerType1& b,
444 std::vector<MatrixType1>& inverse_ops_down,
445 std::vector<MatrixType2>& inverse_ops_up,
447 const ContainerType2& weights,
double eps,
unsigned gamma)
454 full_multigrid( ops,
x, b, inverse_ops_down, inverse_ops_up, nested_grids, gamma, 1);
462 double error = sqrt(
blas2::dot(weights,nested_grids.
r(0)) );
464 while ( error > eps*(nrmb + 1))
470 full_multigrid( ops,
x, b, inverse_ops_down, inverse_ops_up, nested_grids, gamma, 1);
478 error = sqrt(
blas2::dot(weights,nested_grids.
r(0)) );
507template<
class Geometry,
class Matrix,
class Container, ComplexMode complex_mode = dg::complex_hermitian>
524 template<
class ...ContainerParams>
526 ContainerParams&& ... ps):
530 for (
unsigned u = 0; u <
stages; u++)
531 m_pcg[u].construct(m_nested.x(u), m_nested.grid(u).size());
540 template<
class ...Params>
548 template<
class ContainerType0>
549 void project(
const ContainerType0& src, std::vector<ContainerType0>& out)
const
551 m_nested.project( src, out);
555 template<
class ContainerType0>
556 std::vector<ContainerType0>
project(
const ContainerType0& src)
const
558 return m_nested.project( src);
561 unsigned stages()
const{
return m_nested.stages();}
567 const Geometry&
grid(
unsigned stage)
const {
568 return m_nested.grid(stage);
574 unsigned max_iter()
const{
return m_pcg[0].get_max();}
591 void set_benchmark(
bool benchmark, std::string message =
"Nested Iterations"){
592 m_benchmark = benchmark;
625 template<
class MatrixType,
class ContainerType0,
class ContainerType1>
626 std::vector<unsigned>
solve( std::vector<MatrixType>& ops, ContainerType0& x,
const ContainerType1& b,
value_type eps)
628 std::vector<value_type> v_eps( m_stages, eps);
629 for(
unsigned u=m_stages-1; u>0; u--)
631 return solve( ops,
x, b, v_eps);
634 template<
class MatrixType,
class ContainerType0,
class ContainerType1>
635 std::vector<unsigned>
solve( std::vector<MatrixType>& ops, ContainerType0& x,
const ContainerType1& b, std::vector<value_type> eps)
639 MPI_Comm_rank(MPI_COMM_WORLD, &rank);
641 std::vector<unsigned> number(m_stages);
642 std::vector<std::function<void(
const ContainerType1&, ContainerType0&)> >
643 multi_inv_pol(m_stages);
644 for(
unsigned u=0; u<m_stages; u++)
646 multi_inv_pol[u] = [&, u, &pcg = m_pcg[u], &
pol = ops[u]](
647 const auto&
y,
auto&
x)
652 number[u] = pcg.solve(
pol,
x,
y,
pol.precond(),
653 pol.weights(), eps[u], 1, 1);
655 number[u] = pcg.solve(
pol,
x,
y,
pol.precond(),
656 pol.weights(), eps[u], 1, 10);
659 DG_RANK0 std::cout <<
"# `"<<m_message<<
"` stage: " << u <<
", iter: " << number[u] <<
", took "<<t.
diff()<<
"s\n";
669 std::vector< PCG<Container, complex_mode> > m_pcg;
671 bool m_benchmark =
true;
672 std::string m_message =
"Nested Iterations";
class intended for the use in throw statements
Definition exceptions.h:83
void append_line(const Message &message)
Appends a newline and a message verbatim to the what string.
Definition exceptions.h:108
small class holding a stringstream
Definition exceptions.h:29
Error classes or the dg library.
#define _ping_
Definition exceptions.h:12
A matrix type for fast interpolations/projections.
DG_DEVICE T zero(T, Ts ...)
This enum can be used in dg::evaluate.
Definition functions.h:19
void copy(const ContainerTypeIn &source, ContainerTypeOut &target)
Definition blas1.h:243
void axpby(value_type alpha, const ContainerType1 &x, value_type1 beta, ContainerType &y)
Definition blas1.h:306
ContainerType construct(const from_ContainerType &from, Params &&... ps)
Generic way to construct an object of ContainerType given a from_ContainerType object and optional ad...
Definition blas1.h:792
void gemv(get_value_type< ContainerType1 > alpha, MatrixType &&M, const ContainerType1 &x, get_value_type< ContainerType1 > beta, ContainerType2 &y)
Alias for blas2::symv ;.
Definition blas2.h:345
auto dot(const ContainerType1 &x, const MatrixType &m, const ContainerType2 &y)
; Binary reproducible general dot product
Definition blas2.h:94
void apply(get_value_type< ContainerType1 > alpha, MatrixType &&M, const ContainerType1 &x, get_value_type< ContainerType1 > beta, ContainerType2 &y)
Alias for dg::blas2::symv ;.
Definition blas2.h:496
void symv(MatrixType &&M, const ContainerType1 &x, ContainerType2 &y)
Definition blas2.h:331
@ forward
forward derivative (cell to the right and current cell)
Definition enums.h:98
auto evaluate(Functor &&f, const Topology &g)
Evaluate a function on grid coordinates
Definition evaluation.h:74
EllSparseBlockMat< real_type, thrust::host_vector > fast_interpolation(unsigned coord, const aRealTopology< real_type, Nd > &t, unsigned multiplyn, unsigned multiplyNx)
Create interpolation matrix for integer multipliers.
Definition fast_interpolation.h:302
EllSparseBlockMat< real_type, thrust::host_vector > fast_projection(unsigned coord, const aRealTopology< real_type, Nd > &t, unsigned dividen, unsigned divideNx)
Create projecton matrix for integer dividers.
Definition fast_interpolation.h:314
void multigrid_cycle(std::vector< MatrixType0 > &ops, std::vector< MatrixType1 > &inverse_ops_down, std::vector< MatrixType2 > &inverse_ops_up, NestedGrids &nested_grids, unsigned gamma, unsigned p)
EXPERIMENTAL Full approximation multigrid cycle.
Definition multigrid.h:277
void fmg_solve(std::vector< MatrixType0 > &ops, ContainerType0 &x, const ContainerType1 &b, std::vector< MatrixType1 > &inverse_ops_down, std::vector< MatrixType2 > &inverse_ops_up, NestedGrids &nested_grids, const ContainerType2 &weights, double eps, unsigned gamma)
EXPERIMENTAL Full multigrid cycles.
Definition multigrid.h:441
void full_multigrid(std::vector< MatrixType0 > &ops, ContainerType0 &x, const ContainerType1 &b, std::vector< MatrixType1 > &inverse_ops_down, std::vector< MatrixType2 > &inverse_ops_up, NestedGrids &nested_grids, unsigned gamma, unsigned mu)
EXPERIMENTAL One Full multigrid cycle.
Definition multigrid.h:366
void nested_iterations(std::vector< MatrixType0 > &ops, ContainerType0 &x, const ContainerType1 &b, std::vector< MatrixType1 > &inverse_ops, NestedGrids &nested_grids)
Full approximation nested iterations.
Definition multigrid.h:198
Interpolation matrix creation functions.
Useful MPI typedefs and overloads of interpolation and projection.
This is the namespace for all functions and classes defined and used by the discontinuous Galerkin li...
Ell Sparse Block Matrix format.
Definition sparseblockmat.h:46
Struct that applies given matrices one after the other.
Definition fast_interpolation.h:37
Solve.
Definition multigrid.h:509
typename Geometry::value_type value_type
Definition multigrid.h:513
void set_max_iter(unsigned new_max)
Set the maximum number of iterations allowed at stage 0.
Definition multigrid.h:583
void project(const ContainerType0 &src, std::vector< ContainerType0 > &out) const
Project vector to all involved grids.
Definition multigrid.h:549
std::vector< unsigned > solve(std::vector< MatrixType > &ops, ContainerType0 &x, const ContainerType1 &b, std::vector< value_type > eps)
Nested iterations.
Definition multigrid.h:635
unsigned max_iter() const
Definition multigrid.h:574
Container container_type
Definition multigrid.h:512
unsigned num_stages() const
Definition multigrid.h:563
std::vector< ContainerType0 > project(const ContainerType0 &src) const
Project vector to all involved grids (allocate memory version)
Definition multigrid.h:556
void construct(Params &&...ps)
Perfect forward parameters to one of the constructors.
Definition multigrid.h:541
Geometry geometry_type
Definition multigrid.h:510
MultigridCG2d()=default
Allocate nothing, Call construct method before usage.
const Geometry & grid(unsigned stage) const
return the grid at given stage
Definition multigrid.h:567
const Container & copyable() const
Return an object of same size as the object used for construction on the finest grid.
Definition multigrid.h:598
std::vector< unsigned > solve(std::vector< MatrixType > &ops, ContainerType0 &x, const ContainerType1 &b, value_type eps)
Nested iterations.
Definition multigrid.h:626
unsigned stages() const
Definition multigrid.h:561
MultigridCG2d(const Geometry &grid, const unsigned stages, ContainerParams &&... ps)
Construct the grids and the interpolation/projection operators.
Definition multigrid.h:525
void set_benchmark(bool benchmark, std::string message="Nested Iterations")
Set or unset performance timings during iterations.
Definition multigrid.h:591
Hold nested grids and provide dg fast interpolation and projection matrices.
Definition multigrid.h:30
const Container & w(unsigned stage) const
Definition multigrid.h:150
const Container & copyable() const
Return an object of same size as the object used for construction on the finest grid.
Definition multigrid.h:118
typename Geometry::value_type value_type
Definition multigrid.h:34
Container & b(unsigned stage)
Definition multigrid.h:147
const Container & b(unsigned stage) const
Definition multigrid.h:148
const MultiMatrix< Matrix, Container > & interpolation(unsigned stage) const
return the interpolation matrix at given stage
Definition multigrid.h:133
Container container_type
Definition multigrid.h:33
void project(const ContainerType0 &src, std::vector< ContainerType0 > &out) const
Project vector to all involved grids.
Definition multigrid.h:95
const MultiMatrix< Matrix, Container > & projection(unsigned stage) const
return the projection matrix at given stage
Definition multigrid.h:139
void construct(Params &&...ps)
Perfect forward parameters to one of the constructors.
Definition multigrid.h:83
std::vector< ContainerType0 > project(const ContainerType0 &src) const
Project vector to all involved grids (allocate memory version)
Definition multigrid.h:108
NestedGrids()
Allocate nothing, Call construct method before usage.
Definition multigrid.h:36
Container & r(unsigned stage)
Definition multigrid.h:145
const Container & x(unsigned stage) const
Definition multigrid.h:144
unsigned stages() const
Definition multigrid.h:121
Geometry geometry_type
Definition multigrid.h:31
const Container & r(unsigned stage) const
Definition multigrid.h:146
Container & w(unsigned stage)
Definition multigrid.h:149
const Geometry & grid(unsigned stage) const
return the grid at given stage
Definition multigrid.h:127
unsigned num_stages() const
Definition multigrid.h:123
NestedGrids(const Geometry &grid, const unsigned stages, ContainerParams &&...ps)
Construct the grids and the interpolation/projection operators.
Definition multigrid.h:46
Container & x(unsigned stage)
Definition multigrid.h:143
Simple tool for performance measuring.
Definition dg_doc.h:352
double diff() const
Return time in seconds elapsed between tic and toc.
double pol(double x, double y)
double mu(double s, unsigned i, unsigned n)
#define DG_RANK0
Definition typedefs.h:152