continuousgridcreator.hh 2.64 KB
Newer Older
Praetorius, Simon's avatar
Praetorius, Simon committed
1
2
3
4
5
6
7
8
9
10
11
#pragma once

#include <cassert>
#include <cstdint>
#include <limits>
#include <vector>

#include <dune/common/exceptions.hh>
#include <dune/common/hybridutilities.hh>
#include <dune/grid/common/gridfactory.hh>

Stenger, Florian's avatar
Stenger, Florian committed
12
#include <dune/vtk/types.hh>
Praetorius, Simon's avatar
Praetorius, Simon committed
13
#include <dune/vtk/gridcreatorinterface.hh>
14
#include <dune/vtk/gridfunctions/continuousgridfunction.hh>
Praetorius, Simon's avatar
Praetorius, Simon committed
15
16
17

namespace Dune
{
Stenger, Florian's avatar
Stenger, Florian committed
18
  namespace Vtk
Praetorius, Simon's avatar
Praetorius, Simon committed
19
  {
Stenger, Florian's avatar
Stenger, Florian committed
20
21
22
23
24
25
26
27
28
    // Create a grid where the input points and connectivity is already
    // connected correctly.
    template <class Grid>
    struct ContinuousGridCreator
        : public GridCreatorInterface<Grid, ContinuousGridCreator<Grid>>
    {
      using Self = ContinuousGridCreator;
      using Super = GridCreatorInterface<Grid, ContinuousGridCreator>;
      using GlobalCoordinate = typename Super::GlobalCoordinate;
Praetorius, Simon's avatar
Praetorius, Simon committed
29

30
      using Super::Super;
Stenger, Florian's avatar
Stenger, Florian committed
31
      using Super::factory;
Praetorius, Simon's avatar
Praetorius, Simon committed
32

Stenger, Florian's avatar
Stenger, Florian committed
33
34
35
36
37
38
      void insertVerticesImpl (std::vector<GlobalCoordinate> const& points,
                              std::vector<std::uint64_t> const& /*point_ids*/)
      {
        for (auto const& p : points)
          factory().insertVertex(p);
      }
Praetorius, Simon's avatar
Praetorius, Simon committed
39

Stenger, Florian's avatar
Stenger, Florian committed
40
41
42
43
44
45
46
47
      void insertElementsImpl (std::vector<std::uint8_t> const& types,
                              std::vector<std::int64_t> const& offsets,
                              std::vector<std::int64_t> const& connectivity)
      {
        std::size_t idx = 0;
        for (std::size_t i = 0; i < types.size(); ++i) {
          auto type = Vtk::to_geometry(types[i]);
          Vtk::CellType cellType{type};
48
          [[maybe_unused]] auto refElem = referenceElement<double,Grid::dimension>(type);
Praetorius, Simon's avatar
Praetorius, Simon committed
49

Stenger, Florian's avatar
Stenger, Florian committed
50
51
52
          int nNodes = offsets[i] - (i == 0 ? 0 : offsets[i-1]);
          assert(nNodes == refElem.size(Grid::dimension));
          std::vector<unsigned int> vtk_cell; vtk_cell.reserve(nNodes);
Praetorius, Simon's avatar
Praetorius, Simon committed
53
          for (int j = 0; j < nNodes; ++j)
Stenger, Florian's avatar
Stenger, Florian committed
54
55
56
57
58
59
60
61
62
            vtk_cell.push_back( connectivity[idx++] );

          if (cellType.noPermutation())
            factory().insertElement(type,vtk_cell);
          else {
            // apply index permutation
            std::vector<unsigned int> cell(nNodes);
            for (int j = 0; j < nNodes; ++j)
              cell[j] = vtk_cell[cellType.permutation(j)];
Praetorius, Simon's avatar
Praetorius, Simon committed
63

Stenger, Florian's avatar
Stenger, Florian committed
64
65
            factory().insertElement(type,cell);
          }
Praetorius, Simon's avatar
Praetorius, Simon committed
66
67
        }
      }
Stenger, Florian's avatar
Stenger, Florian committed
68
    };
Praetorius, Simon's avatar
Praetorius, Simon committed
69

70
71
72
73
74
75
76
77
78
79
80
    // deduction guides
    template <class Grid>
    ContinuousGridCreator(GridFactory<Grid>&)
      -> ContinuousGridCreator<Grid>;

    template <class GridType, class FieldType, class Context>
    struct AssociatedGridFunction<ContinuousGridCreator<GridType>, FieldType, Context>
    {
      using type = ContinuousGridFunction<GridType, FieldType, Context>;
    };

Stenger, Florian's avatar
Stenger, Florian committed
81
  } // end namespace Vtk
Praetorius, Simon's avatar
Praetorius, Simon committed
82
} // end namespace Dune