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

3
#include <list>
4 5 6

#include <amdis/linear_algebra/DOFVectorInterface.hpp>

7 8
#include <amdis/Output.hpp>

9 10
namespace AMDiS
{
11 12 13 14 15 16 17 18 19 20 21 22 23 24
  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;

  };


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

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

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

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

48 49 50 51 52 53 54 55 56
    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");
    }

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

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

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

86 87 88
  private:
    Grid* grid_ = nullptr;
    std::list<DOFVectorInterface*> data_;
89 90 91 92 93
    bool mightCoarsen_ = false;
    bool refined_ = false;
  };

} // end namespace AMDiS