GridTransfer.hpp 2.54 KB
Newer Older
1
2
#pragma once

3
#include <list>
4

5
#include <amdis/Output.hpp>
6
#include <amdis/linearalgebra/DOFVectorInterface.hpp>
7

8
9
namespace AMDiS
{
10
11
12
13
14
15
  /**
   * \addtogroup Adaption
   * @{
   **/

  /// \brief Interface for transfer between grid changes to be registered in a \ref GridTransferManager
16
17
18
19
20
21
22
23
24
25
26
27
28
  class GridTransferInterface
  {
  public:
    virtual ~GridTransferInterface() = default;

    virtual void attach(DOFVectorInterface*) = 0;
    virtual void detach(DOFVectorInterface*) = 0;
    virtual bool preAdapt() = 0;
    virtual bool adapt() = 0;
    virtual void postAdapt() = 0;
  };


29
  /// \brief Implementation of \ref GridTransferInterface for concrete Grid type.
30
31
  template <class Grid>
  class GridTransfer
32
      : public GridTransferInterface
33
34
35
36
  {
    using Self = GridTransfer;

  public:
37
38
39
40
41
42
43
44
45
    void bind(Grid& grid)
    {
      grid_ = &grid;
    }

    void unbind()
    {
      grid_ = nullptr;
    }
46
47

    /// Attach a data container to the grid transfer, that gets interpolated during grid change
48
    void attach(DOFVectorInterface* vec) override
49
50
51
52
    {
      data_.push_back(vec);
    }

53
    void detach(DOFVectorInterface* vec) override
54
55
56
57
58
59
60
61
    {
      auto it = std::find(data_.begin(), data_.end(), vec);
      if (it != data_.end())
        data_.erase(it);
      else
        warning("DOFVector to detach not found");
    }

62
    /// Prepare the grid and the data for the adaption
63
    bool preAdapt() override
64
    {
65
      assert(grid_ != nullptr);
66
      mightCoarsen_ = grid_->preAdapt(); // any element might be coarsened in adapt()
67
      for (auto* vec : this->data_)
68
69
70
71
72
        vec->preAdapt(mightCoarsen_);
      return mightCoarsen_;
    }

    /// do the grid adaption
73
    bool adapt() override
74
    {
75
      assert(grid_ != nullptr);
76
77
78
79
80
      refined_ = grid_->adapt(); // returns true if a least one entity was refined
      return refined_;
    }

    // Perform data adaption to the new grid
81
    void postAdapt() override
82
    {
83
      assert(grid_ != nullptr);
84
      if (mightCoarsen_ || refined_) {
85
        for (auto* vec : this->data_)
86
87
88
          vec->postAdapt(refined_);
      }
      grid_->postAdapt();
89
90
91
92
93
94
95
      changeIndex_++;
    }

    /// Returns the grid change index, see \ref changeIndex.
    unsigned long changeIndex() const
    {
      return changeIndex_;
96
97
    }

98
99
100
  private:
    Grid* grid_ = nullptr;
    std::list<DOFVectorInterface*> data_;
101
102
    bool mightCoarsen_ = false;
    bool refined_ = false;
103
104
105
106

    /// This index is incremented every time the grid is changed, e.g. by
    /// refinement or coarsening.
    unsigned long changeIndex_ = 0;
107
108
  };

109
110
  /// @}

111
} // end namespace AMDiS