types.hh 9.51 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
    enum class FormatTypes {
Praetorius, Simon's avatar
Praetorius, Simon committed
22
23
24
25
26
      ASCII      = 1<<0,
      BINARY     = 1<<1,
      COMPRESSED = 1<<2,
      APPENDED = BINARY | COMPRESSED
    };
27
    std::string to_string (Vtk::FormatTypes);
dedner's avatar
dedner committed
28
    inline auto formatTypesList = {FormatTypes::ASCII, FormatTypes::BINARY, FormatTypes::COMPRESSED, FormatTypes::APPENDED};
Praetorius, Simon's avatar
Praetorius, Simon committed
29

30
    /// Map the dune-grid OutputType to FormatTypes
31
32
33
34
35
36
37
38
39
40
41
    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
    };
42
    std::string to_string (Vtk::RangeTypes);
Praetorius, Simon's avatar
Praetorius, Simon committed
43
    inline auto rangeTypesList = {RangeTypes::UNSPECIFIED, RangeTypes::AUTO, RangeTypes::SCALAR, RangeTypes::VECTOR, RangeTypes::TENSOR};
44
45
46
47
48
49

    // 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);
50
51


Praetorius, Simon's avatar
Praetorius, Simon committed
52
    enum class DataTypes {
Praetorius, Simon's avatar
Praetorius, Simon committed
53
54
55
56
57
58
59
60
      UNKNOWN = 0,
      INT8, UINT8,
      INT16, UINT16,
      INT32, UINT32,
      INT64, UINT64,
      FLOAT32 = 32,
      FLOAT64 = 64
    };
61
    std::string to_string (Vtk::DataTypes);
dedner's avatar
dedner committed
62
63
64
65
66
67
68
69
    inline auto dataTypesLists = {
      DataTypes::UNKNOWN,
      DataTypes::INT8,    DataTypes::UINT8,
      DataTypes::INT16,   DataTypes::UINT16,
      DataTypes::INT32,   DataTypes::UINT32,
      DataTypes::INT64,   DataTypes::UINT64,
      DataTypes::FLOAT32, DataTypes::FLOAT64
    };
Praetorius, Simon's avatar
Praetorius, Simon committed
70

71
72
    // Map a dune-grid Precision type to DataTypes
    Vtk::DataTypes dataTypeOf (Dune::VTK::Precision);
73

74
75
    // Map a string to DataTypes
    Vtk::DataTypes dataTypeOf (std::string);
Praetorius, Simon's avatar
Praetorius, Simon committed
76

77
78
79
    // Map the field_type of T to DataTypes
    template <class T>
    Vtk::DataTypes dataTypeOf ()
Praetorius, Simon's avatar
Praetorius, Simon committed
80
    {
81
      using F = typename FieldTraits<T>::field_type;
82
83
84
85
86
87
88
89
90
91
92
93
      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;
94
    }
Praetorius, Simon's avatar
Praetorius, Simon committed
95

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

98
    /// Map a given enum DataType to a type passed to Caller as \ref MetaType
99
    template <template <class> class C = NoConstraint, class Caller>
100
101
102
    void mapDataTypes (Vtk::DataTypes t, Caller caller)
    {
      switch (t) {
Praetorius, Simon's avatar
Praetorius, Simon committed
103
104
105
106
107
108
109
110
111
112
        case DataTypes::INT8:    if constexpr(C<std::int8_t>::value)   caller(MetaType<std::int8_t>{});   break;
        case DataTypes::UINT8:   if constexpr(C<std::uint8_t>::value)  caller(MetaType<std::uint8_t>{});  break;
        case DataTypes::INT16:   if constexpr(C<std::int16_t>::value)  caller(MetaType<std::int16_t>{});  break;
        case DataTypes::UINT16:  if constexpr(C<std::uint16_t>::value) caller(MetaType<std::uint16_t>{}); break;
        case DataTypes::INT32:   if constexpr(C<std::int32_t>::value)  caller(MetaType<std::int32_t>{});  break;
        case DataTypes::UINT32:  if constexpr(C<std::uint32_t>::value) caller(MetaType<std::uint32_t>{}); break;
        case DataTypes::INT64:   if constexpr(C<std::int64_t>::value)  caller(MetaType<std::int64_t>{});  break;
        case DataTypes::UINT64:  if constexpr(C<std::uint64_t>::value) caller(MetaType<std::uint64_t>{}); break;
        case DataTypes::FLOAT32: if constexpr(C<float>::value)         caller(MetaType<float>{});         break;
        case DataTypes::FLOAT64: if constexpr(C<double>::value)        caller(MetaType<double>{});        break;
113
        default:
114
          VTK_ASSERT_MSG(false, "Unsupported type " + to_string(t));
115
116
117
118
119
          break;
      }
    }

    /// Map two DataTypes as type parameters to the Caller
120
121
122
    template <template <class> class Constraint1 = NoConstraint,
              template <class> class Constraint2 = NoConstraint,
              class Caller>
123
124
    void mapDataTypes (Vtk::DataTypes t1, Vtk::DataTypes t2, Caller caller)
    {
125
126
      mapDataTypes<Constraint1>(t1, [&](auto type1) {
        mapDataTypes<Constraint2>(t2, [&](auto type2) {
127
128
129
130
131
132
          caller(type1, type2);
        });
      });
    }

    /// Map three DataTypes as type parameters to the Caller
133
134
135
136
    template <template <class> class Constraint1 = NoConstraint,
              template <class> class Constraint2 = NoConstraint,
              template <class> class Constraint3 = NoConstraint,
              class Caller>
137
138
    void mapDataTypes (Vtk::DataTypes t1, Vtk::DataTypes t2, Vtk::DataTypes t3, Caller caller)
    {
139
140
141
      mapDataTypes<Constraint1>(t1, [&](auto type1) {
        mapDataTypes<Constraint2>(t2, [&](auto type2) {
          mapDataTypes<Constraint3>(t3, [&](auto type3) {
142
143
144
145
146
147
148
            caller(type1, type2, type3);
          });
        });
      });
    }


Praetorius, Simon's avatar
Praetorius, Simon committed
149
    enum class CompressorTypes {
150
151
152
153
154
155
156
157
      NONE = 0,
      ZLIB,
      LZ4,
      LZMA
    };
    std::string to_string (CompressorTypes);


Praetorius, Simon's avatar
Praetorius, Simon committed
158
    /// Mapping of Dune geometry types to VTK cell types
Praetorius, Simon's avatar
Praetorius, Simon committed
159
    struct CellType
Praetorius, Simon's avatar
Praetorius, Simon committed
160
    {
Praetorius, Simon's avatar
Praetorius, Simon committed
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
189
190
191
192
193
194
195
196
197
198
      enum Parametrization {
        LINEAR      = 1,
        QUADRATIC   = 2,
        LAGRANGE    = 3
      };

      enum Type : 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,
      };

Praetorius, Simon's avatar
Praetorius, Simon committed
199
    public:
Praetorius, Simon's avatar
Praetorius, Simon committed
200
      CellType (GeometryType const& t, Parametrization = LINEAR);
Praetorius, Simon's avatar
Praetorius, Simon committed
201

202
      /// Return VTK Cell type
Praetorius, Simon's avatar
Praetorius, Simon committed
203
204
205
206
207
      std::uint8_t type () const
      {
        return type_;
      }

208
      /// Return a permutation of Dune elemenr vertices to conform to VTK element numbering
209
      int permutation (int idx) const
Praetorius, Simon's avatar
Praetorius, Simon committed
210
211
212
213
      {
        return permutation_[idx];
      }

214
215
216
217
218
      bool noPermutation () const
      {
        return noPermutation_;
      }

Praetorius, Simon's avatar
Praetorius, Simon committed
219
220
221
    private:
      std::uint8_t type_;
      std::vector<int> permutation_;
Praetorius, Simon's avatar
Praetorius, Simon committed
222
      bool noPermutation_ = true;
Praetorius, Simon's avatar
Praetorius, Simon committed
223
    };
Praetorius, Simon's avatar
Praetorius, Simon committed
224
    GeometryType to_geometry (std::uint8_t);
Praetorius, Simon's avatar
Praetorius, Simon committed
225

226
227
228
229

    class FieldInfo
    {
    public:
230
231
      template <class... Args>
      explicit FieldInfo (std::string name, Args... args)
232
        : name_(std::move(name))
233
234
235
236
237
238
239
        , 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_);
      }
240
241
242
243

      // Construct from dune-grid FieldInfo
      FieldInfo (Dune::VTK::FieldInfo info)
        : FieldInfo(info.name(), info.size(), rangeTypeOf(info.type()), dataTypeOf(info.precision()))
244
245
246
247
248
249
250
251
252
      {}

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

      /// The number of components in the data field.
253
      int size () const
254
255
256
257
      {
        return ncomps_;
      }

258
259
260
261
262
263
      /// Return the category of the stored range
      Vtk::RangeTypes rangeType () const
      {
        return rangeType_;
      }

264
265
266
267
268
269
      /// Return the data tpe of the data field.
      Vtk::DataTypes dataType () const
      {
        return dataType_;
      }

270
271
272
    private:
      std::string name_;
      int ncomps_;
273
      Vtk::RangeTypes rangeType_;
274
      Vtk::DataTypes dataType_;
275
276
    };

Praetorius, Simon's avatar
Praetorius, Simon committed
277
  } // end namespace Vtk
278
} // end namespace Dune