FileWriterCreator.hpp 4.82 KB
Newer Older
Praetorius, Simon's avatar
Praetorius, Simon committed
1
2
3
4
5
#pragma once

#include <memory>
#include <string>

Praetorius, Simon's avatar
Praetorius, Simon committed
6
7
#include <dune/common/typeutilities.hh>

Praetorius, Simon's avatar
Praetorius, Simon committed
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <amdis/BoundaryManager.hpp>
#include <amdis/LinearAlgebra.hpp>
#include <amdis/Output.hpp>
#include <amdis/gridfunctions/DiscreteFunction.hpp>
#include <amdis/io/BackupWriter.hpp>
#include <amdis/io/DuneVtkWriter.hpp>
#include <amdis/io/FileWriterBase.hpp>
#include <amdis/io/GmshWriter.hpp>
#include <amdis/io/VTKWriter.hpp>

namespace AMDiS
{
  /// \brief Creator class for filewriters depending on a given type name
  /**
   * \tparam SystemVector  Type of the DOFVector to write with the filewriter
   **/
  template <class SystemVector>
  class FileWriterCreator
  {
    using GlobalBasis  = typename SystemVector::GlobalBasis;
    using GridView     = typename GlobalBasis::GridView;
Praetorius, Simon's avatar
Praetorius, Simon committed
29
    using Grid         = typename GridView::Grid;
Praetorius, Simon's avatar
Praetorius, Simon committed
30
31
32
33
34
35
36
37
38
39
40
41
42

  public:
    /// Constructor. Stores the pointer to the systemVector and to the (optional) boundaryManager.
    FileWriterCreator(std::shared_ptr<SystemVector> systemVector,
                      std::shared_ptr<BoundaryManager<Grid>> boundaryManager = nullptr)
      : systemVector_(std::move(systemVector))
      , boundaryManager_(std::move(boundaryManager))
    {}

    /// Create a new FileWriter of type `type`
    /**
     * \param type      String representing the type of the writer. One of {vtk, dune-vtk, gmsh, backup}
     * \param prefix    The initfile prefix tp configure the filewriter
43
     * \param ii...     Indices of the treepath to the component of the systemVector to be handled by the fileWriter.
Praetorius, Simon's avatar
Praetorius, Simon committed
44
     **/
45
    template <class... Indices>
Praetorius, Simon's avatar
Praetorius, Simon committed
46
    std::unique_ptr<FileWriterInterface>
47
    create(std::string type, std::string prefix, Indices... ii) const
Praetorius, Simon's avatar
Praetorius, Simon committed
48
    {
49
      auto data = discreteFunction(*systemVector_, ii...);
Praetorius, Simon's avatar
Praetorius, Simon committed
50
      return create_impl(std::move(type), std::move(prefix), data, Dune::PriorityTag<42>{});
Praetorius, Simon's avatar
Praetorius, Simon committed
51
52
53
    }

  private:
Praetorius, Simon's avatar
Praetorius, Simon committed
54
    template <class Data,
55
56
      REQUIRES(not std::is_same_v<ValueCategory_t<typename Data::Range>,tag::unknown>),
      REQUIRES(std::is_arithmetic_v<typename Dune::FieldTraits<typename Data::Range>::field_type>)>
Praetorius, Simon's avatar
Praetorius, Simon committed
57
    std::unique_ptr<FileWriterInterface>
Praetorius, Simon's avatar
Praetorius, Simon committed
58
    create_impl(std::string type, std::string prefix, Data const& data, Dune::PriorityTag<2>) const
Praetorius, Simon's avatar
Praetorius, Simon committed
59
    {
60
61
      GridView const& gridView = systemVector_->basis()->gridView();

Praetorius, Simon's avatar
Praetorius, Simon committed
62
63
64
      // ParaView VTK format, writer from dune-grid
      if (type == "vtk")
      {
65
        return std::make_unique<VTKWriter<GridView,Data>>(prefix, gridView, data);
Praetorius, Simon's avatar
Praetorius, Simon committed
66
67
68
69
70
      }
#if HAVE_DUNE_VTK
      // ParaView VTK format, writer from dune-vtk
      else if (type == "dune-vtk")
      {
71
        return std::make_unique<DuneVtkWriter<GridView,Data>>(prefix, gridView, data);
Praetorius, Simon's avatar
Praetorius, Simon committed
72
73
74
75
76
77
      }
#endif
      // GMsh file format, writing just the grid and optionally boundary ids
      else if (type == "gmsh")
      {
        if (!!boundaryManager_)
78
          return std::make_unique<GmshWriter<GridView>>(prefix, gridView,
Praetorius, Simon's avatar
Praetorius, Simon committed
79
80
            std::vector<int>{}, boundaryManager_->boundaryIds());
        else
81
          return std::make_unique<GmshWriter<GridView>>(prefix, gridView);
Praetorius, Simon's avatar
Praetorius, Simon committed
82
83
84
85
86
87
88
89
90
91
92
93
94
      }
      // Backup writer, writing the grid and the solution vector
      else if (type == "backup")
      {
        return std::make_unique<BackupWriter<SystemVector>>(prefix, systemVector_);
      }

      error_exit("Unknown filewriter type '{}' given for '{}'. Use one of "
                 "(vtk, gmsh, backup, [dune-vtk]), where the last one is only available "
                 "if the dune module dune-vtk is found.", type, prefix);
      return {};
    }

Praetorius, Simon's avatar
Praetorius, Simon committed
95

Praetorius, Simon's avatar
Praetorius, Simon committed
96
    // The value-category is unknown, like a composite/hierarchic vector or any unknown type.
Praetorius, Simon's avatar
Praetorius, Simon committed
97
    template <class Data,
98
99
      REQUIRES(std::is_same_v<ValueCategory_t<typename Data::Range>,tag::unknown>),
      REQUIRES(std::is_arithmetic_v<typename Dune::FieldTraits<typename Data::Range>::field_type>)>
Praetorius, Simon's avatar
Praetorius, Simon committed
100
    std::unique_ptr<FileWriterInterface>
Praetorius, Simon's avatar
Praetorius, Simon committed
101
    create_impl(std::string type, std::string prefix, Data const& /*data*/, Dune::PriorityTag<1>) const
Praetorius, Simon's avatar
Praetorius, Simon committed
102
103
104
105
106
107
108
109
110
111
112
113
114
    {
      // Backup writer, writing the grid and the solution vector
      if (type == "backup")
      {
        return std::make_unique<BackupWriter<SystemVector>>(prefix, systemVector_);
      }

      error_exit("Filewriter '{}' cannot be applied to this component in the tree. "
                 "Either use the writer 'backup' to write the whole tree, or add a treepath "
                 "to the output prefix '{}'.", type, prefix);
      return {};
    }

Praetorius, Simon's avatar
Praetorius, Simon committed
115
116
117
118
119
120
121
122
123
    // fallback implementation
    template <class Data>
    std::unique_ptr<FileWriterInterface>
    create_impl(std::string, std::string, Data const&, Dune::PriorityTag<0>) const
    {
      error_exit("Filewriter cannot be used with unsupported Range and field_type");
      return {};
    }

Praetorius, Simon's avatar
Praetorius, Simon committed
124
125
126
127
128
129
  private:
    std::shared_ptr<SystemVector> systemVector_;
    std::shared_ptr<BoundaryManager<Grid>> boundaryManager_ = nullptr;
  };

} // end namespace AMDiS