function.hh 4.04 KB
Newer Older
Stenger, Florian's avatar
Stenger, Florian committed
1
2
3
4
5
6
7
8
9
10
11
12
#pragma once

#include <optional>
#include <type_traits>

#include <dune/common/std/type_traits.hh>

#include "localfunction.hh"
#include "types.hh"

namespace Dune
{
13
  // forward declarations
Stenger, Florian's avatar
Stenger, Florian committed
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
  template <class T, int N>
  class FieldVector;

  template <class T, int N, int M>
  class FieldMatrix;

  namespace Vtk
  {
    /// Wrapper class for functions allowing local evaluations.
    template <class GridView>
    class Function
    {
      template <class F>
      using LocalFunction = decltype(localFunction(std::declval<F>()));

      using Domain = typename GridView::template Codim<0>::Entity::Geometry::LocalCoordinate;

      template <class F>
      using Range = std::decay_t<std::result_of_t<F(Domain)>>;

    private:

      template <class T, int N>
      static auto sizeOfImpl (FieldVector<T,N> const&)
        -> std::integral_constant<int, N> { return {}; }

      template <class T, int N, int M>
      static auto sizeOfImpl (FieldMatrix<T,N,M> const&)
        -> std::integral_constant<int, N*M> { return {}; }

      static auto sizeOfImpl (...)
        -> std::integral_constant<int, 1> { return {}; }

      template <class T>
      static constexpr int sizeOf () { return decltype(sizeOfImpl(std::declval<T>()))::value; }

    public:
      /// Constructor VtkFunction from legacy VTKFunction
      /**
      * \param fct   The VTKFunction to wrap
      * \param type  The VTK datatype how to write the function values to the output [Vtk::FLOAT64]
      **/
      Function (std::shared_ptr<VTKFunction<GridView> const> const& fct,
                  std::optional<Vtk::DataTypes> type = {})
        : localFct_(fct)
        , name_(fct->name())
        , ncomps_(fct->ncomps())
        , type_(type ? *type : Vtk::FLOAT64)
      {}

      /// Construct VtkFunction from dune-functions GridFunction with Signature
      // NOTE: Stores the localFunction(fct) by value.
      /**
      * \param fct     A Grid(View)-function, providing a `localFunction(fct)`
      * \param name    The name to use component identification in the VTK file
      * \param ncomps  Number of components of the pointwise data. Is extracted
      *                from the range type of the GridFunction if not given.
      * \param type    The \ref Vtk::DataTypes used in the output. E.g. FLOAT32,
      *                or FLOAT64. Is extracted from the range type of the
      *                GridFunction if not given.
      **/
      template <class F,
        class = void_t<LocalFunction<F>> >
      Function (F&& fct, std::string name,
                  std::optional<int> ncomps = {},
                  std::optional<Vtk::DataTypes> type = {})
        : localFct_(localFunction(std::forward<F>(fct)))
        , name_(std::move(name))
      {
        using R = Range<LocalFunction<F>>;

        ncomps_ = ncomps ? *ncomps : sizeOf<R>();
        type_ = type ? *type : Vtk::Map::type<R>();
      }

      /// Constructor that forward the number of components and data type to the other constructor
      template <class F,
        class = void_t<LocalFunction<F>> >
      Function (F&& fct, Vtk::FieldInfo fieldInfo,
                  std::optional<Vtk::DataTypes> type = {})
        : Function(std::forward<F>(fct), fieldInfo.name(), fieldInfo.ncomps(), type)
      {}

      Function () = default;

      /// Create a LocalFunction
      friend Vtk::LocalFunction<GridView> localFunction (Function const& self)
      {
        return self.localFct_;
      }

      /// Return a name associated with the function
      std::string const& name () const
      {
        return name_;
      }

      /// Return the number of components of the Range
      int ncomps () const
      {
        return ncomps_ > 3 ? 9 : ncomps_ > 1 ? 3 : 1; // tensor, vector, scalar
      }

      /// Return the VTK Datatype associated with the functions range type
      Vtk::DataTypes type () const
      {
        return type_;
      }

    private:
      Vtk::LocalFunction<GridView> localFct_;
      std::string name_;
      int ncomps_ = 1;
      Vtk::DataTypes type_ = Vtk::FLOAT32;
    };

  } // end namespace Vtk
} // end namespace Dune