dune-fem 2.8.0
Loading...
Searching...
No Matches
solver/communication/hierarchical.hh
Go to the documentation of this file.
1#ifndef DUNE_FEM_SOLVER_COMMUNICATION_HIERARCHICAL_HH
2#define DUNE_FEM_SOLVER_COMMUNICATION_HIERARCHICAL_HH
3
4#include <memory>
5#include <type_traits>
6#include <utility>
7
8#include <dune/common/ftraits.hh>
9
10#include <dune/istl/bvector.hh>
11#include <dune/istl/multitypeblockvector.hh>
12#include <dune/istl/solvercategory.hh>
13
17
18namespace Dune
19{
20
21 namespace Fem
22 {
23
24 namespace ISTL
25 {
26
27 // HierarchicalCommunicationVector
28 // -------------------------------
29
30 template< class DiscreteFunctionSpace, class DofContainer >
32 {
34
35 public:
36 struct DofVector
37 {
38 typedef typename Dune::Fem::Impl::BlockIndicesFor< DofContainer >::Type BlockIndices;
39 static constexpr std::size_t blockSize = Dune::Hybrid::size( BlockIndices() );
40
43
44 explicit DofVector ( DofContainer &data ) : data_( data ) {}
45
46 ConstDofBlockType operator[] ( std::size_t i ) const { return ConstDofBlockType( data_, i ); }
47 DofBlockType operator[] ( std::size_t i ) { return DofBlockType( data_, i ); }
48
49 private:
50 DofContainer &data_;
51 };
52
54
55 typedef typename DiscreteFunctionSpaceType::LocalBlockIndices BlockIndices;
56
57 static constexpr std::size_t blockSize = Hybrid::size( BlockIndices() );
58
59 typedef typename DofContainer::field_type DofType;
60
61 template< class Operation >
63 {
64 typedef typename DiscreteFunctionSpaceType::template CommDataHandle< ThisType, Operation >::Type Type;
65 };
66
67 HierarchicalCommunicationVector ( const DiscreteFunctionSpace &dfSpace, DofContainer &dofContainer )
68 : dfSpace_( dfSpace ), dofVector_( dofContainer )
69 {}
70
71 template< class Operation >
72 typename CommDataHandle< Operation >::Type dataHandle ( const Operation &operation )
73 {
74 return space().createDataHandle( *this, operation );
75 }
76
77 const DofVector &dofVector () const { return dofVector_; }
78 DofVector &dofVector () { return dofVector_; }
79
80 const DiscreteFunctionSpaceType &space () const { return dfSpace_; }
81
82 private:
83 const DiscreteFunctionSpace &dfSpace_;
84 DofVector dofVector_;
85 };
86
87
88
89 // HierarchicalCommunication
90 // -------------------------
91
92 template< class DiscreteFunctionSpace >
94 {
96
97 typedef typename DiscreteFunctionSpace::AuxiliaryDofsType AuxiliaryDofsType;
98
99 public:
101
102 explicit HierarchicalCommunication ( const DiscreteFunctionSpaceType &dfSpace, Dune::SolverCategory::Category solverCategory = Dune::SolverCategory::sequential )
103 : dfSpace_( dfSpace ), solverCategory_( solverCategory )
104 {}
105
106 const typename DiscreteFunctionSpace::GridPartType::CollectiveCommunicationType &communicator () const { return dfSpace_.gridPart().comm(); }
107
108 template< class T >
109 void copyOwnerToAll ( const T &x, T &y ) const
110 {
111 y = x;
112 project( y );
114 dfSpace_.communicator().exchange( z, DFCommunicationOperation::Add() );
115 }
116
117 template< class T >
118 void project ( T &x ) const
119 {
120 project( dfSpace_.auxiliaryDofs(), x );
121 }
122
123 template< class T, class F >
124 void dot ( const T &x, const T &y, F &scp ) const
125 {
126 dot( dfSpace_.auxiliaryDofs(), x, y, scp );
127 scp = communicator().sum( scp );
128 }
129
130 template< class T >
131 typename Dune::FieldTraits< typename T::field_type >::real_type norm ( const T &x ) const
132 {
133 using std::sqrt;
134 typename Dune::FieldTraits< typename T::field_type >::real_type norm2( 0 );
135 dot( x, x, norm2 );
136 return sqrt( norm2 );
137 }
138
139 Dune::SolverCategory::Category getSolverCategory () const { return solverCategory_; }
140
141 private:
142 template< class... V >
143 static void project ( const AuxiliaryDofsType &auxiliaryDofs, MultiTypeBlockVector< V... > &x )
144 {
145 Dune::Hybrid::forEach( std::index_sequence_for< V... >(), [ &auxiliaryDofs, &x ] ( auto &&i ) { ThisType::project( auxiliaryDofs, x[ i ] ); } );
146 }
147
148 template< class B, class A >
149 static void project ( const AuxiliaryDofsType &auxiliaryDofs, BlockVector< B, A > &x )
150 {
151 typedef typename B::field_type field_type;
152 for( int i : auxiliaryDofs )
153 x[ i ] = field_type( 0 );
154 }
155
156 template< class... V, class F >
157 static void dot ( const AuxiliaryDofsType &auxiliaryDofs, const MultiTypeBlockVector< V... > &x, const MultiTypeBlockVector< V... > &y, F &scp )
158 {
159 Dune::Hybrid::forEach( std::index_sequence_for< V... >(), [ &auxiliaryDofs, &x, &y, &scp ] ( auto &&i ) { ThisType::dot( auxiliaryDofs, x[ i ], y[ i ], scp ); } );
160 }
161
162 template< class B, class A, class F >
163 static void dot ( const AuxiliaryDofsType &auxiliaryDofs, const BlockVector< B, A > &x, const BlockVector< B, A > &y, F &scp )
164 {
165 const int numAuxiliarys = auxiliaryDofs.size();
166 for( int auxiliary = 0, i = 0; auxiliary < numAuxiliarys; ++auxiliary, ++i )
167 {
168 const int nextAuxiliary = auxiliaryDofs[ auxiliary ];
169 for( ; i < nextAuxiliary; ++i )
170 scp += x[ i ] * y[ i ];
171 }
172 }
173
174 const DiscreteFunctionSpaceType &dfSpace_;
175 Dune::SolverCategory::Category solverCategory_;
176 };
177
178
179
180 // buildCommunication
181 // ------------------
182
183 template< class DiscreteFunctionSpace >
185 Dune::SolverCategory::Category solverCategory,
186 std::shared_ptr< HierarchicalCommunication< DiscreteFunctionSpace > > &communication )
187 {
188 communication.reset( new HierarchicalCommunication< DiscreteFunctionSpace >( dfSpace, solverCategory ) );
189 }
190
191
192
193 // SupportsAMG for HierarchicalCommunication
194 // -----------------------------------------
195
196 template< class T >
197 struct SupportsAMG;
198
199 template< class DiscreteFunctionSpace >
201 : public std::false_type
202 {};
203
204 } // namespace ISTL
205
206 } // namespace Fem
207
208} // namespace Dune
209
210#endif // #ifndef DUNE_FEM_SOLVER_COMMUNICATION_HIERARCHICAL_HH
double sqrt(const Dune::Fem::Double &v)
Definition: double.hh:977
Definition: bindguard.hh:11
static double sqrt(const Double &v)
Definition: double.hh:886
static void forEach(IndexRange< T, sz > range, F &&f)
Definition: hybrid.hh:129
void buildCommunication(const DiscreteFunctionSpace &dfSpace, Dune::SolverCategory::Category solverCategory, std::shared_ptr< FemCommunication< DiscreteFunctionSpace > > &communication)
Definition: fem.hh:143
Definition: hierarchical/dofvector.hh:60
Definition: fem.hh:156
Definition: solver/communication/hierarchical.hh:32
HierarchicalCommunicationVector(const DiscreteFunctionSpace &dfSpace, DofContainer &dofContainer)
Definition: solver/communication/hierarchical.hh:67
DofContainer::field_type DofType
Definition: solver/communication/hierarchical.hh:59
DofVector & dofVector()
Definition: solver/communication/hierarchical.hh:78
DiscreteFunctionSpace DiscreteFunctionSpaceType
Definition: solver/communication/hierarchical.hh:53
const DiscreteFunctionSpaceType & space() const
Definition: solver/communication/hierarchical.hh:80
const DofVector & dofVector() const
Definition: solver/communication/hierarchical.hh:77
DiscreteFunctionSpaceType::LocalBlockIndices BlockIndices
Definition: solver/communication/hierarchical.hh:55
CommDataHandle< Operation >::Type dataHandle(const Operation &operation)
Definition: solver/communication/hierarchical.hh:72
static constexpr std::size_t blockSize
Definition: solver/communication/hierarchical.hh:57
Definition: solver/communication/hierarchical.hh:37
Dune::Fem::Impl::BlockIndicesFor< DofContainer >::Type BlockIndices
Definition: solver/communication/hierarchical.hh:38
DofVector(DofContainer &data)
Definition: solver/communication/hierarchical.hh:44
static constexpr std::size_t blockSize
Definition: solver/communication/hierarchical.hh:39
HierarchicalDofBlock< DofContainer > DofBlockType
Definition: solver/communication/hierarchical.hh:42
HierarchicalDofBlock< const DofContainer > ConstDofBlockType
Definition: solver/communication/hierarchical.hh:41
ConstDofBlockType operator[](std::size_t i) const
Definition: solver/communication/hierarchical.hh:46
Definition: solver/communication/hierarchical.hh:63
DiscreteFunctionSpaceType::template CommDataHandle< ThisType, Operation >::Type Type
Definition: solver/communication/hierarchical.hh:64
Definition: solver/communication/hierarchical.hh:94
void copyOwnerToAll(const T &x, T &y) const
Definition: solver/communication/hierarchical.hh:109
Dune::FieldTraits< typenameT::field_type >::real_type norm(const T &x) const
Definition: solver/communication/hierarchical.hh:131
DiscreteFunctionSpace DiscreteFunctionSpaceType
Definition: solver/communication/hierarchical.hh:100
void dot(const T &x, const T &y, F &scp) const
Definition: solver/communication/hierarchical.hh:124
const DiscreteFunctionSpace::GridPartType::CollectiveCommunicationType & communicator() const
Definition: solver/communication/hierarchical.hh:106
Dune::SolverCategory::Category getSolverCategory() const
Definition: solver/communication/hierarchical.hh:139
HierarchicalCommunication(const DiscreteFunctionSpaceType &dfSpace, Dune::SolverCategory::Category solverCategory=Dune::SolverCategory::sequential)
Definition: solver/communication/hierarchical.hh:102
void project(T &x) const
Definition: solver/communication/hierarchical.hh:118
sum up data
Definition: commoperations.hh:144
discrete function space