DUNE-FEM (unstable)

petsc.hh
1#ifndef DUNE_FEM_SPACE_MAPPER_PETSC_HH
2#define DUNE_FEM_SPACE_MAPPER_PETSC_HH
3
4#include <cassert>
5
6#include <memory>
7#include <tuple>
8#include <utility>
9
10#include <dune/fem/space/mapper/ghost.hh>
11#include <dune/fem/space/mapper/parallel.hh>
12#include <dune/fem/storage/singletonlist.hh>
13
14namespace Dune
15{
16
17 namespace Fem
18 {
19
20 // PetscMappers
21 // ------------
22
23#if HAVE_PETSC
24 template< class DiscreteFunctionSpace >
25 class PetscMappers
26 {
27 typedef PetscMappers< DiscreteFunctionSpace > PetscMappersType;
28
29 public:
30 typedef DiscreteFunctionSpace DiscreteFunctionSpaceType;
31
32 typedef typename DiscreteFunctionSpaceType::BlockMapperType BlockMapperType;
33 typedef typename DiscreteFunctionSpaceType::GridPartType GridPartType;
34
35 typedef GhostDofMapper< GridPartType, BlockMapperType, PetscInt > GhostMapperType;
36 typedef ParallelDofMapper< GridPartType, GhostMapperType, PetscInt > ParallelMapperType;
37
38 private:
39 template< class Mapper >
40 struct MapperFactory
41 {
42 static std::pair< Mapper, int > *createObject ( std::pair< GridPartType *, typename Mapper::BaseMapperType * > key, const InterfaceType commif, int sequence )
43 {
44 return new std::pair< Mapper, int >( std::piecewise_construct, std::tie( *key.first, *key.second, commif ), std::make_tuple( sequence ) );
45 }
46
47 static void deleteObject ( std::pair< Mapper, int > *object ) { delete object; }
48 };
49
50 template< class Mapper >
51 using MapperProvider = SingletonList< std::pair< GridPartType *, typename Mapper::BaseMapperType * >, std::pair< Mapper, int >, MapperFactory< Mapper > >;
52
53 typedef MapperProvider< GhostMapperType > GhostMapperProviderType;
54 typedef MapperProvider< ParallelMapperType > ParallelMapperProviderType;
55
56 public:
58 PetscMappers ( const DiscreteFunctionSpaceType &space )
59 : space_( space )
60 {
61 const int sequence = space_.sequence();
62
63 ghostMapper_.reset( &GhostMapperProviderType::getObject( std::make_pair( &space_.gridPart(), &space_.blockMapper() ), communicationInterface(), sequence-1 ) );
64 checkCommunicationInterface( *ghostMapper_, communicationInterface() );
65
66 // update anyway here because of comm interface for Lagrange spaces
67 update( *ghostMapper_, sequence, communicationInterface() );
68
69 parallelMapper_.reset( &ParallelMapperProviderType::getObject( std::make_pair( &space_.gridPart(), &ghostMapper_->first ), communicationInterface(), sequence-1 ) );
70 checkCommunicationInterface( *parallelMapper_, communicationInterface() );
71
72 // update anyway here because of comm interface for Lagrange spaces
73 update( *parallelMapper_, sequence, communicationInterface() );
74 }
75
77 PetscMappers ( const PetscMappers &other )
78 : space_( other.space_ )
79 {
80 const int sequence = space_.sequence();
81
82 ghostMapper_.reset( &GhostMapperProviderType::getObject( std::make_pair( &space_.gridPart(), &space_.blockMapper() ), communicationInterface(), sequence ) );
83 checkCommunicationInterface( *ghostMapper_, communicationInterface() );
84
85 parallelMapper_.reset( &ParallelMapperProviderType::getObject( std::make_pair( &space_.gridPart(), &ghostMapper_->first ), communicationInterface(), sequence ) );
86 checkCommunicationInterface( *parallelMapper_, communicationInterface() );
87 }
88
89 const DiscreteFunctionSpaceType &space () const { return space_; }
90
91 const GhostMapperType &ghostMapper () const { assert( ghostMapper_ ); return ghostMapper_->first; }
92 const ParallelMapperType &parallelMapper () const { assert( parallelMapper_ ); return parallelMapper_->first; }
93
94 PetscInt ghostIndex ( const typename BlockMapperType::GlobalKeyType &index ) const
95 {
96 assert( static_cast< std::size_t >( index ) < size() );
97 return ghostMapper().mapping()[ index ];
98 }
99
100 PetscInt parallelIndex ( const typename BlockMapperType::GlobalKeyType &index ) const
101 {
102 return parallelMapper().mapping()[ ghostIndex( index ) ];
103 }
104
105 std::size_t size () const { return ghostMapper().mapping().size(); }
106
107 void update ()
108 {
109 const int sequence = space().sequence();
110
111 assert( ghostMapper_ );
112 update( *ghostMapper_, sequence, communicationInterface() );
113
114 assert( parallelMapper_ );
115 update( *parallelMapper_, sequence, communicationInterface() );
116 }
117
118 private:
119 InterfaceType communicationInterface() const
120 {
121 // TODO: For Lagrange spaces the comm interface needs to be InteriorBorder_InteriorBorder_Interface
122 // otherwise the petsc solvers don't work.
123 // This is only a temporary fix and need further investigation.
124 return space().continuous() ? InteriorBorder_InteriorBorder_Interface : space().communicationInterface();
125 }
126
127 template< class Mapper >
128 static void checkCommunicationInterface( std::pair< Mapper, int > &mapper, const InterfaceType commInterface )
129 {
130 if( commInterface != mapper.first.communicationInterface() )
131 {
132 DUNE_THROW(InvalidStateException,"PetscMappers: communication interfaces do not match!");
133 }
134 }
135
136 template< class Mapper >
137 static void update ( std::pair< Mapper, int > &mapper, int sequence, const InterfaceType commInterface )
138 {
139 checkCommunicationInterface( mapper, commInterface );
140 if( mapper.second != sequence )
141 {
142 mapper.first.update();
143 mapper.second = sequence;
144 }
145 }
146
147 const DiscreteFunctionSpaceType &space_;
148 std::unique_ptr< std::pair< GhostMapperType, int >, typename GhostMapperProviderType::Deleter > ghostMapper_;
149 std::unique_ptr< std::pair< ParallelMapperType, int >, typename ParallelMapperProviderType::Deleter > parallelMapper_;
150 };
151#endif // #if HAVE_PETSC
152
153 } // namespace Fem
154
155} // namespace Dune
156
157#endif // #ifndef DUNE_FEM_SPACE_MAPPER_PETSC_HH
discrete function space
mapper allocating one DoF per subentity of a given codimension
Definition: codimensionmapper.hh:357
#define DUNE_THROW(E, m)
Definition: exceptions.hh:218
InterfaceType
Parameter to be used for the communication functions.
Definition: gridenums.hh:86
@ InteriorBorder_InteriorBorder_Interface
send/receive interior and border entities
Definition: gridenums.hh:87
Dune namespace.
Definition: alignedallocator.hh:13
constexpr std::integral_constant< std::size_t, sizeof...(II)> size(std::integer_sequence< T, II... >)
Return the size of the sequence.
Definition: integersequence.hh:75
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden  |  generated with Hugo v0.111.3 (Nov 13, 23:29, 2024)