ParallelGlobalBasis.hpp 6.06 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#pragma once


#include <algorithm>
#include <list>
#include <memory>
#include <type_traits>
#include <utility>

#include <dune/common/concept.hh>
#include <dune/common/reservedvector.hh>
#include <dune/common/shared_ptr.hh>
#include <dune/common/typeutilities.hh>
#include <dune/common/version.hh>

#include <dune/functions/common/type_traits.hh>
#include <dune/functions/functionspacebases/concepts.hh>
18
#include <dune/functions/functionspacebases/defaultglobalbasis.hh>
19
20
21
22
23
24
25
26
27
#include <dune/functions/functionspacebases/flatmultiindex.hh>

#include <dune/grid/common/adaptcallback.hh>

#include <dune/typetree/treepath.hh>

#include <amdis/AdaptiveGrid.hpp>
#include <amdis/Observer.hpp>
#include <amdis/Output.hpp>
28
29
#include <amdis/common/Concepts.hpp>
#include <amdis/common/TypeTraits.hpp>
30
#include <amdis/functions/FlatPreBasis.hpp>
31
#include <amdis/functions/LocalView.hpp>
32
33
#include <amdis/linearalgebra/Traits.hpp>
#include <amdis/typetree/MultiIndex.hpp>
34
35
36
37
38
39
40

#if DUNE_VERSION_LT(DUNE_FUNCTIONS,2,7)
#include <dune/functions/functionspacebases/defaultlocalindexset.hh>
#endif

namespace AMDiS
{
Müller, Felix's avatar
Müller, Felix committed
41
  template <class PreBasisFactory>
42
  using MultiIndex_t = std::conditional_t<
Müller, Felix's avatar
Müller, Felix committed
43
44
45
46
47
    (remove_cvref_t<PreBasisFactory>::requiredMultiIndexSize == 1),
    Dune::Functions::FlatMultiIndex<std::size_t>,
    Dune::ReservedVector<std::size_t, remove_cvref_t<PreBasisFactory>::requiredMultiIndexSize>>;


48
49
50
51
52
53
54
55
56
57
  /**
   * \brief Parallel global basis defined on a (sequential) pre-basis
   *
   * This class is an expansion to Dune::Functions::DefaultGlobalBasis<PB>. It adds a communicator
   * to use the basis in parallel as well as automatic update functionality.
   *
   * \tparam PB  Pre-basis providing the implementation details
   */
  template <class PB>
  class ParallelGlobalBasis
58
59
      : public Dune::Functions::DefaultGlobalBasis<PB>
      , public Notifier<event::adapt>
Praetorius, Simon's avatar
Praetorius, Simon committed
60
      , private Observer<event::adapt>
61
62
  {
    using Self = ParallelGlobalBasis<PB>;
63
    using Super = Dune::Functions::DefaultGlobalBasis<PB>;
64
65
66
67
68
69
70

  public:
    /// Pre-basis providing the implementation details
    using PreBasis = PB;

    /// The grid view that the FE space is defined on
    using GridView = typename PreBasis::GridView;
Praetorius, Simon's avatar
Praetorius, Simon committed
71
    using Grid = typename GridView::Grid;
72
73

    /// Type of the local view on the restriction of the basis to a single element
74
    using LocalView = AMDiS::LocalView<Self>;
75
76

    /// Type of the communicator
77
78
79
    using Comm = std::conditional_t<Traits::IsFlatIndex<typename Super::MultiIndex>::value,
      typename BackendTraits<Self>::Comm,
      SequentialCommunication>;
80

Praetorius, Simon's avatar
Praetorius, Simon committed
81
    struct DummyImpl {};
82
83
    using ADH = Dune::AdaptDataHandle<Grid, DummyImpl>;

Praetorius, Simon's avatar
Praetorius, Simon committed
84
85
86
87

  public:

    /// Construct this global basis with given name and grid, and constructing a preBasis.
88
    /**
89
     * \param name     Name associated with this basis for initfile parameter attachment.
Praetorius, Simon's avatar
Praetorius, Simon committed
90
91
     * \param grid     The Grid providing the GridView for this basis
     * \param args...  Argument list for PreBasis
92
93
94
     *
     * This will forward all arguments to the constructor of PreBasis
     */
Praetorius, Simon's avatar
Praetorius, Simon committed
95
96
97
    template <class... Args,
      Dune::Functions::enableIfConstructible<PreBasis, Args...> = 0>
    ParallelGlobalBasis(std::string const& name, Grid const& grid, Args&&... args)
98
99
      : Super(FWD(args)...)
      , Observer<event::adapt>(grid)
Praetorius, Simon's avatar
Praetorius, Simon committed
100
      , comm_(CommunicationCreator<Comm>::create(static_cast<Super const&>(*this), name + "->solver"))
101
    {}
102

Müller, Felix's avatar
Müller, Felix committed
103
104
    /// Construct this global basis with a preBasisFactory
    template <class PBF>
105
    ParallelGlobalBasis(std::string const& name, GridView const& gridView, PBF const& preBasisFactory)
Müller, Felix's avatar
Müller, Felix committed
106
      : ParallelGlobalBasis(name, gridView.grid(),
107
          flatPreBasis(preBasisFactory.template makePreBasis<MultiIndex_t<PBF>>(gridView)))
108
109
    {}

Müller, Felix's avatar
Müller, Felix committed
110
111
112
113
114
    /// Construct this global basis with empty name
    template <class Arg, class... Args,
      REQUIRES(!std::is_same_v<std::string, remove_cvref_t<Arg>>)>
    ParallelGlobalBasis(Arg&& arg, Args&&... args)
      : ParallelGlobalBasis(std::string(""), FWD(arg), FWD(args)...)
Praetorius, Simon's avatar
Praetorius, Simon committed
115
116
    {}

Praetorius, Simon's avatar
Praetorius, Simon committed
117
118
119
120
121
    /// Copy constructor
    ParallelGlobalBasis(ParallelGlobalBasis const&) = delete;

    /// Move constructor
    ParallelGlobalBasis(ParallelGlobalBasis&&) = default;
Praetorius, Simon's avatar
Praetorius, Simon committed
122
123

  public:
124

Praetorius, Simon's avatar
Praetorius, Simon committed
125
    /// \brief Update the stored grid view
126
127
128
129
130
131
    /**
     * This will update the indexing information of the global basis as well as the communicator.
     * It is called automatically if the grid has changed.
     */
    void update(GridView const& gv)
    {
132
133
134
      Super::preBasis().update(gv);
      Super::preBasis().initializeIndices();
      comm_.update(*this);
135
136
137
138
139
140
141
142
143
144
145
146
147
148
    }

    /// Return local view for basis
    LocalView localView() const
    {
      return LocalView(*this);
    }

    /// Return *this because we are not embedded in a larger basis
    ParallelGlobalBasis const& rootBasis() const
    {
      return *this;
    }

Praetorius, Simon's avatar
Praetorius, Simon committed
149
150
151
152
153
    /// \brief Return the communicator.
    /**
     * This provides the means to communicate data associated to the basis with
     * other processes.
     **/
154
155
    Comm const& communicator() const  { return comm_; }
    Comm&       communicator()        { return comm_; }
156

Praetorius, Simon's avatar
Praetorius, Simon committed
157
    ADH globalRefineCallback() const
158
159
160
161
162
163
    {
      // TODO(FM): Implement
      error_exit("Not implemented: ParallelGlobalBasis::globalRefineCallback()");
      return ADH{};
    }

Praetorius, Simon's avatar
Praetorius, Simon committed
164
165
166
167
168
  protected:
    /// Updates the underlying basis when event::adapt is triggered by the observed grid
    void updateImpl(event::adapt e) override
    {
      if (e.value) {
169
170
        update(Super::gridView());
        Notifier<event::adapt>::notify(e);
Praetorius, Simon's avatar
Praetorius, Simon committed
171
172
173
      }
    }

Praetorius, Simon's avatar
Praetorius, Simon committed
174
175
    using Observer<event::adapt>::update;

176
  protected:
177
    Comm comm_;
178
179
  };

Praetorius, Simon's avatar
Praetorius, Simon committed
180

Müller, Felix's avatar
Müller, Felix committed
181
  // Deduction guides
Praetorius, Simon's avatar
Praetorius, Simon committed
182
  template <class GV, class PBF>
183
184
185
  ParallelGlobalBasis(std::string const& name, GV const& gridView, PBF const& preBasisFactory)
    -> ParallelGlobalBasis<decltype(flatPreBasis(
          preBasisFactory.template makePreBasis<MultiIndex_t<PBF>>(gridView)))>;
186

Praetorius, Simon's avatar
Praetorius, Simon committed
187
  template <class GV, class PBF>
188
189
190
  ParallelGlobalBasis(GV const& gridView, PBF const& preBasisFactory)
    -> ParallelGlobalBasis<decltype(flatPreBasis(
          preBasisFactory.template makePreBasis<MultiIndex_t<PBF>>(gridView)))>;
191
192

} // end namespace AMDiS