EntityRefiner.h 9.3 KB
Newer Older
1
2
3
4
5
6
// Copyright (c) 2004-2022 Tomáš Oberhuber et al.
//
// This file is part of TNL - Template Numerical Library (https://tnl-project.org/)
//
// SPDX-License-Identifier: MIT

7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#pragma once

#include <TNL/Meshes/MeshEntity.h>
#include <TNL/Meshes/Topologies/Triangle.h>
#include <TNL/Meshes/Topologies/Quadrangle.h>
#include <TNL/Meshes/Topologies/Tetrahedron.h>
#include <TNL/Meshes/Topologies/Hexahedron.h>

namespace TNL {
namespace Meshes {

enum class EntityRefinerVersion
{
   EdgeBisection
};

template< typename MeshConfig,
          typename Topology,
          EntityRefinerVersion EntityRefinerVersion_ = EntityRefinerVersion::EdgeBisection >
struct EntityRefiner;

template< typename MeshConfig >
struct EntityRefiner< MeshConfig, Topologies::Triangle, EntityRefinerVersion::EdgeBisection >
{
   using Device = Devices::Host;
   using Topology = Topologies::Triangle;
   using MeshEntityType = MeshEntity< MeshConfig, Device, Topology >;
   using GlobalIndexType = typename MeshConfig::GlobalIndexType;

   // returns: number of *new* points, number of *all* refined entities
37
38
   static std::pair< GlobalIndexType, GlobalIndexType >
   getExtraPointsAndEntitiesCount( const MeshEntityType& entity )
39
40
41
42
   {
      return { 3, 4 };
   }

43
44
45
   template< typename AddPointFunctor, typename AddCellFunctor >
   static void
   decompose( const MeshEntityType& entity, AddPointFunctor&& addPoint, AddCellFunctor&& addCell )
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
   {
      const auto v0 = entity.template getSubentityIndex< 0 >( 0 );
      const auto v1 = entity.template getSubentityIndex< 0 >( 1 );
      const auto v2 = entity.template getSubentityIndex< 0 >( 2 );

      const auto& mesh = entity.getMesh();
      const auto v0_p = mesh.getPoint( v0 );
      const auto v1_p = mesh.getPoint( v1 );
      const auto v2_p = mesh.getPoint( v2 );

      // add new points: midpoints of triangle edges
      const auto w0 = addPoint( 0.5 * ( v1_p + v2_p ) );
      const auto w1 = addPoint( 0.5 * ( v0_p + v2_p ) );
      const auto w2 = addPoint( 0.5 * ( v0_p + v1_p ) );

      // add refined triangles
      addCell( v0, w1, w2 );
      addCell( v1, w0, w2 );
      addCell( v2, w0, w1 );
      addCell( w0, w1, w2 );
   }
};

template< typename MeshConfig >
struct EntityRefiner< MeshConfig, Topologies::Quadrangle, EntityRefinerVersion::EdgeBisection >
{
   using Device = Devices::Host;
   using Topology = Topologies::Quadrangle;
   using MeshEntityType = MeshEntity< MeshConfig, Device, Topology >;
   using GlobalIndexType = typename MeshConfig::GlobalIndexType;

   // returns: number of *new* points, number of *all* refined entities
78
79
   static std::pair< GlobalIndexType, GlobalIndexType >
   getExtraPointsAndEntitiesCount( const MeshEntityType& entity )
80
81
82
83
   {
      return { 5, 4 };
   }

84
85
86
   template< typename AddPointFunctor, typename AddCellFunctor >
   static void
   decompose( const MeshEntityType& entity, AddPointFunctor&& addPoint, AddCellFunctor&& addCell )
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
   {
      const auto v0 = entity.template getSubentityIndex< 0 >( 0 );
      const auto v1 = entity.template getSubentityIndex< 0 >( 1 );
      const auto v2 = entity.template getSubentityIndex< 0 >( 2 );
      const auto v3 = entity.template getSubentityIndex< 0 >( 3 );

      const auto& mesh = entity.getMesh();
      const auto v0_p = mesh.getPoint( v0 );
      const auto v1_p = mesh.getPoint( v1 );
      const auto v2_p = mesh.getPoint( v2 );
      const auto v3_p = mesh.getPoint( v3 );

      // add new points
      const auto w0 = addPoint( 0.5 * ( v0_p + v1_p ) );
      const auto w1 = addPoint( 0.5 * ( v1_p + v2_p ) );
      const auto w2 = addPoint( 0.5 * ( v2_p + v3_p ) );
      const auto w3 = addPoint( 0.5 * ( v3_p + v0_p ) );
      const auto c = addPoint( 0.25 * ( v0_p + v1_p + v2_p + v3_p ) );

      // add refined quadrangles
      addCell( v0, w0, c, w3 );
      addCell( w0, v1, w1, c );
      addCell( c, w1, v2, w2 );
      addCell( w3, c, w2, v3 );
   }
};

template< typename MeshConfig >
struct EntityRefiner< MeshConfig, Topologies::Tetrahedron, EntityRefinerVersion::EdgeBisection >
{
   using Device = Devices::Host;
   using Topology = Topologies::Tetrahedron;
   using MeshEntityType = MeshEntity< MeshConfig, Device, Topology >;
   using GlobalIndexType = typename MeshConfig::GlobalIndexType;

   // returns: number of *new* points, number of *all* refined entities
123
124
   static std::pair< GlobalIndexType, GlobalIndexType >
   getExtraPointsAndEntitiesCount( const MeshEntityType& entity )
125
126
127
128
   {
      return { 6, 8 };
   }

129
130
131
   template< typename AddPointFunctor, typename AddCellFunctor >
   static void
   decompose( const MeshEntityType& entity, AddPointFunctor&& addPoint, AddCellFunctor&& addCell )
132
133
134
135
136
137
138
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
   {
      const auto v0 = entity.template getSubentityIndex< 0 >( 0 );
      const auto v1 = entity.template getSubentityIndex< 0 >( 1 );
      const auto v2 = entity.template getSubentityIndex< 0 >( 2 );
      const auto v3 = entity.template getSubentityIndex< 0 >( 3 );

      const auto& mesh = entity.getMesh();
      const auto v0_p = mesh.getPoint( v0 );
      const auto v1_p = mesh.getPoint( v1 );
      const auto v2_p = mesh.getPoint( v2 );
      const auto v3_p = mesh.getPoint( v3 );

      // add new points: midpoints of triangle edges
      const auto w0 = addPoint( 0.5 * ( v1_p + v2_p ) );
      const auto w1 = addPoint( 0.5 * ( v0_p + v2_p ) );
      const auto w2 = addPoint( 0.5 * ( v0_p + v1_p ) );
      const auto w3 = addPoint( 0.5 * ( v0_p + v3_p ) );
      const auto w4 = addPoint( 0.5 * ( v1_p + v3_p ) );
      const auto w5 = addPoint( 0.5 * ( v2_p + v3_p ) );

      // add refined tetrahedrons
      addCell( v0, w1, w2, w3 );
      addCell( v1, w0, w2, w4 );
      addCell( v2, w0, w1, w5 );
      addCell( v3, w3, w4, w5 );

      addCell( w5, w0, w1, w2 );
      addCell( w5, w1, w2, w3 );
      addCell( w5, w0, w2, w4 );
      addCell( w5, w3, w4, w2 );
   }
};

template< typename MeshConfig >
struct EntityRefiner< MeshConfig, Topologies::Hexahedron, EntityRefinerVersion::EdgeBisection >
{
   using Device = Devices::Host;
   using Topology = Topologies::Hexahedron;
   using MeshEntityType = MeshEntity< MeshConfig, Device, Topology >;
   using GlobalIndexType = typename MeshConfig::GlobalIndexType;

   // returns: number of *new* points, number of *all* refined entities
174
175
   static std::pair< GlobalIndexType, GlobalIndexType >
   getExtraPointsAndEntitiesCount( const MeshEntityType& entity )
176
177
178
179
   {
      return { 19, 8 };
   }

180
181
182
   template< typename AddPointFunctor, typename AddCellFunctor >
   static void
   decompose( const MeshEntityType& entity, AddPointFunctor&& addPoint, AddCellFunctor&& addCell )
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
   {
      const auto v0 = entity.template getSubentityIndex< 0 >( 0 );
      const auto v1 = entity.template getSubentityIndex< 0 >( 1 );
      const auto v2 = entity.template getSubentityIndex< 0 >( 2 );
      const auto v3 = entity.template getSubentityIndex< 0 >( 3 );
      const auto v4 = entity.template getSubentityIndex< 0 >( 4 );
      const auto v5 = entity.template getSubentityIndex< 0 >( 5 );
      const auto v6 = entity.template getSubentityIndex< 0 >( 6 );
      const auto v7 = entity.template getSubentityIndex< 0 >( 7 );

      const auto& mesh = entity.getMesh();
      const auto v0_p = mesh.getPoint( v0 );
      const auto v1_p = mesh.getPoint( v1 );
      const auto v2_p = mesh.getPoint( v2 );
      const auto v3_p = mesh.getPoint( v3 );
      const auto v4_p = mesh.getPoint( v4 );
      const auto v5_p = mesh.getPoint( v5 );
      const auto v6_p = mesh.getPoint( v6 );
      const auto v7_p = mesh.getPoint( v7 );

      // add new points: centers of bottom edges
      const auto b0 = addPoint( 0.5 * ( v0_p + v1_p ) );
      const auto b1 = addPoint( 0.5 * ( v1_p + v2_p ) );
      const auto b2 = addPoint( 0.5 * ( v2_p + v3_p ) );
      const auto b3 = addPoint( 0.5 * ( v3_p + v0_p ) );
      // add new points: centers of upper edges
      const auto u0 = addPoint( 0.5 * ( v4_p + v5_p ) );
      const auto u1 = addPoint( 0.5 * ( v5_p + v6_p ) );
      const auto u2 = addPoint( 0.5 * ( v6_p + v7_p ) );
      const auto u3 = addPoint( 0.5 * ( v7_p + v4_p ) );
      // add new points: centers of middle (vertical) edges
      const auto m0 = addPoint( 0.5 * ( v0_p + v4_p ) );
      const auto m1 = addPoint( 0.5 * ( v1_p + v5_p ) );
      const auto m2 = addPoint( 0.5 * ( v2_p + v6_p ) );
      const auto m3 = addPoint( 0.5 * ( v3_p + v7_p ) );
      // add new points: centers of faces
      const auto f0 = addPoint( 0.25 * ( v0_p + v1_p + v2_p + v3_p ) );
      const auto f1 = addPoint( 0.25 * ( v0_p + v1_p + v5_p + v4_p ) );
      const auto f2 = addPoint( 0.25 * ( v1_p + v2_p + v6_p + v5_p ) );
      const auto f3 = addPoint( 0.25 * ( v2_p + v3_p + v7_p + v6_p ) );
      const auto f4 = addPoint( 0.25 * ( v3_p + v0_p + v4_p + v7_p ) );
      const auto f5 = addPoint( 0.25 * ( v4_p + v5_p + v6_p + v7_p ) );
      // add new points: center of the cell
226
      const auto cc = addPoint( 0.125 * ( v0_p + v1_p + v2_p + v3_p + v4_p + v5_p + v6_p + v7_p ) );
227
228
229
230
231
232
233
234
235
236
237
238
239

      // add refined hexahedrons
      addCell( v0, b0, f0, b3, m0, f1, cc, f4 );
      addCell( b0, v1, b1, f0, f1, m1, f2, cc );
      addCell( f0, b1, v2, b2, cc, f2, m2, f3 );
      addCell( b3, f0, b2, v3, f4, cc, f3, m3 );
      addCell( m0, f1, cc, f4, v4, u0, f5, u3 );
      addCell( f1, m1, f2, cc, u0, v5, u1, f5 );
      addCell( cc, f2, m2, f3, f5, u1, v6, u2 );
      addCell( f4, cc, f3, m3, u3, f5, u2, v7 );
   }
};

240
241
}  // namespace Meshes
}  // namespace TNL