GridTransfer.hpp 2.35 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 16 17 18 19 20 21 22 23
  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;

  };


24 25
  template <class Grid>
  class GridTransfer
26
      : public GridTransferInterface
27 28 29 30
  {
    using Self = GridTransfer;

  public:
31 32 33 34 35 36 37 38 39
    void bind(Grid& grid)
    {
      grid_ = &grid;
    }

    void unbind()
    {
      grid_ = nullptr;
    }
40 41

    /// Attach a data container to the grid transfer, that gets interpolated during grid change
42
    virtual void attach(DOFVectorInterface* vec) override
43 44 45 46
    {
      data_.push_back(vec);
    }

47 48 49 50 51 52 53 54 55
    virtual void detach(DOFVectorInterface* vec) override
    {
      auto it = std::find(data_.begin(), data_.end(), vec);
      if (it != data_.end())
        data_.erase(it);
      else
        warning("DOFVector to detach not found");
    }

56
    /// Prepare the grid and the data for the adaption
57
    virtual bool preAdapt() override
58
    {
59
      assert(grid_ != nullptr);
60
      mightCoarsen_ = grid_->preAdapt(); // any element might be coarsened in adapt()
61
      for (auto* vec : this->data_)
62 63 64 65 66
        vec->preAdapt(mightCoarsen_);
      return mightCoarsen_;
    }

    /// do the grid adaption
67
    virtual bool adapt() override
68
    {
69
      assert(grid_ != nullptr);
70 71 72 73 74
      refined_ = grid_->adapt(); // returns true if a least one entity was refined
      return refined_;
    }

    // Perform data adaption to the new grid
75
    virtual void postAdapt() override
76
    {
77
      assert(grid_ != nullptr);
78
      if (mightCoarsen_ || refined_) {
79
        for (auto* vec : this->data_)
80 81 82
          vec->postAdapt(refined_);
      }
      grid_->postAdapt();
83 84 85 86 87 88 89
      changeIndex_++;
    }

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

92 93 94
  private:
    Grid* grid_ = nullptr;
    std::list<DOFVectorInterface*> data_;
95 96
    bool mightCoarsen_ = false;
    bool refined_ = false;
97 98 99 100

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

} // end namespace AMDiS