concat.hpp 3.6 KB
Newer Older
1
// Software License for MTL
2
3
//
// Copyright (c) 2007 The Trustees of Indiana University.
4
//               2008 Dresden University of Technology and the Trustees of Indiana University.
5
//               2010 SimuNova UG, www.simunova.com.
6
7
// All rights reserved.
// Authors: Peter Gottschling and Andrew Lumsdaine
8
//
9
// This file is part of the Matrix Template Library
10
//
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
// See also tools/license/license.mtl.txt in the distribution.

#ifndef ITL_PC_CONCAT_INCLUDE
#define ITL_PC_CONCAT_INCLUDE

#include <boost/mpl/if.hpp>
#include <boost/numeric/mtl/operation/resource.hpp>
#include <boost/numeric/mtl/interface/vpt.hpp>
#include <boost/numeric/itl/pc/solver.hpp>

namespace itl { namespace pc {

/// Class for concatenating \tparam PC1 and \tparam PC2
template <typename PC1, typename PC2, typename Matrix, bool Store1= true, bool Store2= true>
class concat
{
    typedef typename boost::mpl::if_c<Store1, PC1, const PC1&>::type pc1_type;
    typedef typename boost::mpl::if_c<Store2, PC2, const PC2&>::type pc2_type;

  public:
    /// Construct both preconditioners from matrix \p A
    explicit concat(const Matrix& A) : A(A), pc1(A), pc2(A)
    {
	BOOST_STATIC_ASSERT((Store1 && Store2));
    }

    /// Both preconditioners are already constructed and passed as arguments
    /** If pc1 or pc2 is only constructed temporarily in the constructor call,
	the according Store argument must be true; otherwise the preconditioner
	will be a stale reference.
	Conversely, if the preconditioner is already build outside the constructor call,
	the according Store argument should be false for not storing the preconditioner twice. **/
    concat(const Matrix& A, const PC1& pc1, const PC2& pc2) : A(A), pc1(pc1), pc2(pc2) {}

  private:
    template <typename VectorOut>
    VectorOut& create_r(const VectorOut& y) const
    {
	static VectorOut  r(resource(y));
	return r;
    }

    template <typename VectorOut>
    VectorOut& create_d(const VectorOut& y) const
    {
	static VectorOut  d(resource(y));
	return d;
    }

  public:
    /// Concatenated preconditioning: pc2 is applied regularly and pc1 afterwards by defect correction
    template <typename VectorIn, typename VectorOut>
    void solve(const VectorIn& x, VectorOut& y) const
    {
	mtl::vampir_trace<5058> tracer;
	y.checked_change_resource(x);
	pc2.solve(x, y);

	VectorOut &r= create_r(y), &d= create_d(y);
	r= x;
	r-= A * y;

	pc1.solve(r, d);
	y+= d;
    }


    /// Concatenated preconditioning: adjoint of pc1 is applied regularly and pc2's adjoint afterwards by defect correction
    template <typename VectorIn, typename VectorOut>
    void adjoint_solve(const VectorIn& x, VectorOut& y) const
    {
	mtl::vampir_trace<5059> tracer;
	y.checked_change_resource(x);
	pc1.adjoint_solve(x, y);

	VectorOut &r= create_r(y), &d= create_d(y);
	r= x;
	r-= adjoint(A) * y;

	pc2.adjoint_solve(r, d);
	y+= d;
    }

   private:
    const Matrix& A;
    pc1_type 	  pc1;
    pc2_type   	  pc2;
};

template <typename PC1, typename PC2, typename Matrix, bool Store1, bool Store2, typename Vector>
solver<concat<PC1, PC2, Matrix, Store1, Store2>, Vector, false>
inline solve(const concat<PC1, PC2, Matrix, Store1, Store2>& P, const Vector& x)
{
    return solver<concat<PC1, PC2, Matrix, Store1, Store2>, Vector, false>(P, x);
}

template <typename PC1, typename PC2, typename Matrix, bool Store1, bool Store2, typename Vector>
solver<concat<PC1, PC2, Matrix, Store1, Store2>, Vector, true>
inline adjoint_solve(const concat<PC1, PC2, Matrix, Store1, Store2>& P, const Vector& x)
{
    return solver<concat<PC1, PC2, Matrix, Store1, Store2>, Vector, true>(P, x);
}


}} // namespace itl::pc

#endif // ITL_PC_CONCAT_INCLUDE