types.hh 8.66 KB
Newer Older
Praetorius, Simon's avatar
Praetorius, Simon committed
1
2
3
4
5
6
7
#pragma once

#include <cstdint>
#include <map>
#include <string>
#include <vector>

8
#include <dune/common/ftraits.hh>
9
#include <dune/common/typelist.hh>
Praetorius, Simon's avatar
Praetorius, Simon committed
10
#include <dune/common/version.hh>
Praetorius, Simon's avatar
Praetorius, Simon committed
11
#include <dune/geometry/type.hh>
12
#include <dune/grid/io/file/vtk/common.hh>
13
#include <dune/vtk/utility/arguments.hh>
Praetorius, Simon's avatar
Praetorius, Simon committed
14
#include <dune/vtk/utility/errors.hh>
Praetorius, Simon's avatar
Praetorius, Simon committed
15

16
namespace Dune
Praetorius, Simon's avatar
Praetorius, Simon committed
17
18
19
{
  namespace Vtk
  {
20
    /// Type used for representing the output format
Praetorius, Simon's avatar
Praetorius, Simon committed
21
22
23
24
25
26
    enum FormatTypes {
      ASCII      = 1<<0,
      BINARY     = 1<<1,
      COMPRESSED = 1<<2,
      APPENDED = BINARY | COMPRESSED
    };
27
    std::string to_string (Vtk::FormatTypes);
Praetorius, Simon's avatar
Praetorius, Simon committed
28

29
    /// Map the dune-grid OutputType to FormatTypes
30
31
32
33
34
35
36
37
38
39
40
    Vtk::FormatTypes formatTypeOf(Dune::VTK::OutputType);


    /// Type used to determine whether to limit output components to e.g. 3 (vector), or 9 (tensor)
    enum class RangeTypes {
      UNSPECIFIED,  //< The output components are not restricted
      AUTO,         //< Detect the category automatically from number of components
      SCALAR,       //< Use exactly 1 component
      VECTOR,       //< Use exactly 3 components
      TENSOR        //< Use exactly 9 components
    };
41
    std::string to_string (Vtk::RangeTypes);
42
43
44
45
46
47

    // Map a dune-grid FieldInfo::Type to ValueTypes
    Vtk::RangeTypes rangeTypeOf (Dune::VTK::FieldInfo::Type);

    // Map a number of components to a corresponding value type
    Vtk::RangeTypes rangeTypeOf (int ncomps);
48
49


Praetorius, Simon's avatar
Praetorius, Simon committed
50
51
52
53
54
55
56
57
58
    enum DataTypes {
      UNKNOWN = 0,
      INT8, UINT8,
      INT16, UINT16,
      INT32, UINT32,
      INT64, UINT64,
      FLOAT32 = 32,
      FLOAT64 = 64
    };
59
    std::string to_string (Vtk::DataTypes);
Praetorius, Simon's avatar
Praetorius, Simon committed
60

61
62
    // Map a dune-grid Precision type to DataTypes
    Vtk::DataTypes dataTypeOf (Dune::VTK::Precision);
63

64
65
    // Map a string to DataTypes
    Vtk::DataTypes dataTypeOf (std::string);
Praetorius, Simon's avatar
Praetorius, Simon committed
66

67
68
69
    // Map the field_type of T to DataTypes
    template <class T>
    Vtk::DataTypes dataTypeOf ()
Praetorius, Simon's avatar
Praetorius, Simon committed
70
    {
71
      using F = typename FieldTraits<T>::field_type;
72
73
74
75
76
77
78
79
80
81
82
83
      if constexpr (std::is_same_v<F, std::int8_t>)   { return Vtk::DataTypes::INT8; }
      if constexpr (std::is_same_v<F, std::uint8_t>)  { return Vtk::DataTypes::UINT8; }
      if constexpr (std::is_same_v<F, std::int16_t>)  { return Vtk::DataTypes::INT16; }
      if constexpr (std::is_same_v<F, std::uint16_t>) { return Vtk::DataTypes::UINT16; }
      if constexpr (std::is_same_v<F, std::int32_t>)  { return Vtk::DataTypes::INT32; }
      if constexpr (std::is_same_v<F, std::uint32_t>) { return Vtk::DataTypes::UINT32; }
      if constexpr (std::is_same_v<F, std::int64_t>)  { return Vtk::DataTypes::INT64; }
      if constexpr (std::is_same_v<F, std::uint64_t>) { return Vtk::DataTypes::UINT64; }
      if constexpr (std::is_same_v<F, float>)         { return Vtk::DataTypes::FLOAT32; }
      if constexpr (std::is_same_v<F, double>)        { return Vtk::DataTypes::FLOAT64; }
      if constexpr (std::is_same_v<F, long double>)   { return Vtk::DataTypes::FLOAT64; }
      return Vtk::DataTypes::UNKNOWN;
84
    }
Praetorius, Simon's avatar
Praetorius, Simon committed
85

86
    template <class> struct NoConstraint : std::true_type {};
Praetorius, Simon's avatar
Praetorius, Simon committed
87

88
    /// Map a given enum DataType to a type passed to Caller as \ref MetaType
89
    template <template <class> class C = NoConstraint, class Caller>
90
91
92
    void mapDataTypes (Vtk::DataTypes t, Caller caller)
    {
      switch (t) {
93
94
95
96
97
98
99
100
101
102
        case INT8:    if constexpr(C<std::int8_t>::value)   caller(MetaType<std::int8_t>{});   break;
        case UINT8:   if constexpr(C<std::uint8_t>::value)  caller(MetaType<std::uint8_t>{});  break;
        case INT16:   if constexpr(C<std::int16_t>::value)  caller(MetaType<std::int16_t>{});  break;
        case UINT16:  if constexpr(C<std::uint16_t>::value) caller(MetaType<std::uint16_t>{}); break;
        case INT32:   if constexpr(C<std::int32_t>::value)  caller(MetaType<std::int32_t>{});  break;
        case UINT32:  if constexpr(C<std::uint32_t>::value) caller(MetaType<std::uint32_t>{}); break;
        case INT64:   if constexpr(C<std::int64_t>::value)  caller(MetaType<std::int64_t>{});  break;
        case UINT64:  if constexpr(C<std::uint64_t>::value) caller(MetaType<std::uint64_t>{}); break;
        case FLOAT32: if constexpr(C<float>::value)         caller(MetaType<float>{});         break;
        case FLOAT64: if constexpr(C<double>::value)        caller(MetaType<double>{});        break;
103
        default:
104
          VTK_ASSERT_MSG(false, "Unsupported type " + to_string(t));
105
106
107
108
109
          break;
      }
    }

    /// Map two DataTypes as type parameters to the Caller
110
111
112
    template <template <class> class Constraint1 = NoConstraint,
              template <class> class Constraint2 = NoConstraint,
              class Caller>
113
114
    void mapDataTypes (Vtk::DataTypes t1, Vtk::DataTypes t2, Caller caller)
    {
115
116
      mapDataTypes<Constraint1>(t1, [&](auto type1) {
        mapDataTypes<Constraint2>(t2, [&](auto type2) {
117
118
119
120
121
122
          caller(type1, type2);
        });
      });
    }

    /// Map three DataTypes as type parameters to the Caller
123
124
125
126
    template <template <class> class Constraint1 = NoConstraint,
              template <class> class Constraint2 = NoConstraint,
              template <class> class Constraint3 = NoConstraint,
              class Caller>
127
128
    void mapDataTypes (Vtk::DataTypes t1, Vtk::DataTypes t2, Vtk::DataTypes t3, Caller caller)
    {
129
130
131
      mapDataTypes<Constraint1>(t1, [&](auto type1) {
        mapDataTypes<Constraint2>(t2, [&](auto type2) {
          mapDataTypes<Constraint3>(t3, [&](auto type3) {
132
133
134
135
136
137
138
            caller(type1, type2, type3);
          });
        });
      });
    }


139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
    enum CompressorTypes {
      NONE = 0,
      ZLIB,
      LZ4,
      LZMA
    };
    std::string to_string (CompressorTypes);


    enum CellParametrization {
      LINEAR,
      QUADRATIC,
      LAGRANGE
    };


    enum CellTypes : std::uint8_t {
      // Linear VTK cell types
      VERTEX         = 1,
      /* POLY_VERTEX    = 2, // not supported */
      LINE           = 3,
      /* POLY_LINE      = 4, // not supported */
      TRIANGLE       = 5,
      /* TRIANGLE_STRIP = 6, // not supported */
      POLYGON        = 7,
      /* PIXEL          = 8, // not supported */
      QUAD           = 9,
      TETRA          = 10,
      /* VOXEL          = 11, // not supported */
      HEXAHEDRON     = 12,
      WEDGE          = 13,
      PYRAMID        = 14,
      // Quadratic VTK cell types
      QUADRATIC_EDGE       = 21,
      QUADRATIC_TRIANGLE   = 22,
      QUADRATIC_QUAD       = 23,
      QUADRATIC_TETRA      = 24,
      QUADRATIC_HEXAHEDRON = 25,
      // Arbitrary order Lagrange elements
      LAGRANGE_CURVE = 68,
      LAGRANGE_TRIANGLE = 69,
      LAGRANGE_QUADRILATERAL = 70,
      LAGRANGE_TETRAHEDRON = 71,
      LAGRANGE_HEXAHEDRON = 72,
      LAGRANGE_WEDGE = 73,
      LAGRANGE_PYRAMID = 74,
    };
    GeometryType to_geometry (std::uint8_t);


Praetorius, Simon's avatar
Praetorius, Simon committed
189
190
191
192
    /// Mapping of Dune geometry types to VTK cell types
    class CellType
    {
    public:
193
      CellType (GeometryType const& t, CellParametrization = LINEAR);
Praetorius, Simon's avatar
Praetorius, Simon committed
194

195
      /// Return VTK Cell type
Praetorius, Simon's avatar
Praetorius, Simon committed
196
197
198
199
200
      std::uint8_t type () const
      {
        return type_;
      }

201
      /// Return a permutation of Dune elemenr vertices to conform to VTK element numbering
202
      int permutation (int idx) const
Praetorius, Simon's avatar
Praetorius, Simon committed
203
204
205
206
      {
        return permutation_[idx];
      }

207
208
209
210
211
      bool noPermutation () const
      {
        return noPermutation_;
      }

Praetorius, Simon's avatar
Praetorius, Simon committed
212
213
214
    private:
      std::uint8_t type_;
      std::vector<int> permutation_;
Praetorius, Simon's avatar
Praetorius, Simon committed
215
      bool noPermutation_ = true;
Praetorius, Simon's avatar
Praetorius, Simon committed
216
217
    };

218
219
220
221

    class FieldInfo
    {
    public:
222
223
      template <class... Args>
      explicit FieldInfo (std::string name, Args... args)
224
        : name_(std::move(name))
225
226
227
228
229
230
231
        , ncomps_(getArg<int,unsigned int,long,unsigned long>(args..., 1))
        , rangeType_(getArg<Vtk::RangeTypes>(args..., Vtk::RangeTypes::AUTO))
        , dataType_(getArg<Vtk::DataTypes>(args..., Vtk::DataTypes::FLOAT32))
      {
        if (rangeType_ == Vtk::RangeTypes::AUTO)
          rangeType_ = rangeTypeOf(ncomps_);
      }
232
233
234
235

      // Construct from dune-grid FieldInfo
      FieldInfo (Dune::VTK::FieldInfo info)
        : FieldInfo(info.name(), info.size(), rangeTypeOf(info.type()), dataTypeOf(info.precision()))
236
237
238
239
240
241
242
243
244
      {}

      /// The name of the data field
      std::string const& name () const
      {
        return name_;
      }

      /// The number of components in the data field.
245
      int size () const
246
247
248
249
      {
        return ncomps_;
      }

250
251
252
253
254
255
      /// Return the category of the stored range
      Vtk::RangeTypes rangeType () const
      {
        return rangeType_;
      }

256
257
258
259
260
261
      /// Return the data tpe of the data field.
      Vtk::DataTypes dataType () const
      {
        return dataType_;
      }

262
263
264
    private:
      std::string name_;
      int ncomps_;
265
      Vtk::RangeTypes rangeType_;
266
      Vtk::DataTypes dataType_;
267
268
    };

Praetorius, Simon's avatar
Praetorius, Simon committed
269
  } // end namespace Vtk
270
} // end namespace Dune