AMDiS  0.3
The Adaptive Multi-Dimensional Simulation Toolbox
FileWriterCreator.hpp
1 #pragma once
2 
3 #include <memory>
4 #include <string>
5 
6 #include <dune/common/typeutilities.hh>
7 
8 #include <amdis/BoundaryManager.hpp>
9 #include <amdis/LinearAlgebra.hpp>
10 #include <amdis/Output.hpp>
11 #include <amdis/gridfunctions/DiscreteFunction.hpp>
12 #include <amdis/io/BackupWriter.hpp>
13 #include <amdis/io/DuneVtkWriter.hpp>
14 #include <amdis/io/FileWriterBase.hpp>
15 #include <amdis/io/GmshWriter.hpp>
16 #include <amdis/io/VTKWriter.hpp>
17 
18 namespace AMDiS
19 {
21 
24  template <class SystemVector>
26  {
27  using GlobalBasis = typename SystemVector::GlobalBasis;
28  using GridView = typename GlobalBasis::GridView;
29  using Grid = typename GridView::Grid;
30 
31  public:
33  FileWriterCreator(std::shared_ptr<SystemVector> systemVector,
34  std::shared_ptr<BoundaryManager<Grid>> boundaryManager = nullptr)
35  : systemVector_(std::move(systemVector))
36  , boundaryManager_(std::move(boundaryManager))
37  {}
38 
40 
45  template <class... Indices>
46  std::unique_ptr<FileWriterInterface>
47  create(std::string type, std::string prefix, Indices... ii) const
48  {
49  auto data = valueOf(*systemVector_, ii...);
50  return create_impl(std::move(type), std::move(prefix), data, Dune::PriorityTag<42>{});
51  }
52 
53  private:
54  template <class Data,
55  REQUIRES(not std::is_same_v<ValueCategory_t<typename Data::Range>,tag::unknown>),
56  REQUIRES(std::is_arithmetic_v<typename Dune::FieldTraits<typename Data::Range>::field_type>)>
57  std::unique_ptr<FileWriterInterface>
58  create_impl(std::string type, std::string prefix, Data const& data, Dune::PriorityTag<2>) const
59  {
60  GridView const& gridView = systemVector_->basis().gridView();
61 
62  // ParaView VTK format, writer from dune-grid
63  if (type == "vtk")
64  {
65  return std::make_unique<VTKWriter<GridView,Data>>(prefix, gridView, data);
66  }
67 #if HAVE_DUNE_VTK
68  // ParaView VTK format, writer from dune-vtk
69  else if (type == "dune-vtk")
70  {
71  return std::make_unique<DuneVtkWriter<GridView,Data>>(prefix, gridView, data);
72  }
73 #endif
74  // GMsh file format, writing just the grid and optionally boundary ids
75  else if (type == "gmsh")
76  {
77  if (!!boundaryManager_)
78  return std::make_unique<GmshWriter<GridView>>(prefix, gridView,
79  std::vector<int>{}, boundaryManager_->boundaryIds());
80  else
81  return std::make_unique<GmshWriter<GridView>>(prefix, gridView);
82  }
83  // Backup writer, writing the grid and the solution vector
84  else if (type == "backup")
85  {
86  return std::make_unique<BackupWriter<SystemVector>>(prefix, systemVector_);
87  }
88 
89  error_exit("Unknown filewriter type '{}' given for '{}'. Use one of "
90  "(vtk, gmsh, backup, [dune-vtk]), where the last one is only available "
91  "if the dune module dune-vtk is found.", type, prefix);
92  return {};
93  }
94 
95 
96  // The value-category is unknown, like a composite/hierarchic vector or any unknown type.
97  template <class Data,
98  REQUIRES(std::is_same_v<ValueCategory_t<typename Data::Range>,tag::unknown>),
99  REQUIRES(std::is_arithmetic_v<typename Dune::FieldTraits<typename Data::Range>::field_type>)>
100  std::unique_ptr<FileWriterInterface>
101  create_impl(std::string type, std::string prefix, Data const& /*data*/, Dune::PriorityTag<1>) const
102  {
103  // Backup writer, writing the grid and the solution vector
104  if (type == "backup")
105  {
106  return std::make_unique<BackupWriter<SystemVector>>(prefix, systemVector_);
107  }
108 
109  error_exit("Filewriter '{}' cannot be applied to this component in the tree. "
110  "Either use the writer 'backup' to write the whole tree, or add a treepath "
111  "to the output prefix '{}'.", type, prefix);
112  return {};
113  }
114 
115  // fallback implementation
116  template <class Data>
117  std::unique_ptr<FileWriterInterface>
118  create_impl(std::string, std::string, Data const&, Dune::PriorityTag<0>) const
119  {
120  error_exit("Filewriter cannot be used with unsupported Range and field_type");
121  return {};
122  }
123 
124  private:
125  std::shared_ptr<SystemVector> systemVector_;
126  std::shared_ptr<BoundaryManager<Grid>> boundaryManager_ = nullptr;
127  };
128 
129 } // end namespace AMDiS
std::index_sequence< I... > Indices
class that represents a sequence of indices
Definition: Index.hpp:40
void error_exit(std::string const &str, Args &&... args)
print a message and exit
Definition: Output.hpp:142
Definition: FieldMatVec.hpp:12
Contains all classes needed for solving linear and non linear equation systems.
Definition: AdaptBase.hpp:6
Creator class for filewriters depending on a given type name.
Definition: FileWriterCreator.hpp:25
auto valueOf(DiscreteFunction< C, GB, TP, R > &df, Indices... ii)
A Generator for the childs of a mutable DiscreteFunction.
Definition: DiscreteFunction.hpp:272
typename PreBasis::GridView GridView
The grid view that the FE space is defined on.
Definition: GlobalBasis.hpp:66
FileWriterCreator(std::shared_ptr< SystemVector > systemVector, std::shared_ptr< BoundaryManager< Grid >> boundaryManager=nullptr)
Constructor. Stores the pointer to the systemVector and to the (optional) boundaryManager.
Definition: FileWriterCreator.hpp:33
constexpr bool GlobalBasis
A Dune::Functions::GlobalBasis type.
Definition: Concepts.hpp:190
Manage boundary ids of boundary segments in a grid.
Definition: BoundaryManager.hpp:52
Definition: Tags.hpp:12
std::unique_ptr< FileWriterInterface > create(std::string type, std::string prefix, Indices... ii) const
Create a new FileWriter of type type
Definition: FileWriterCreator.hpp:47