forked from martemyev/tethex
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tethex.h
1214 lines (1021 loc) · 37.4 KB
/
tethex.h
1
2
3
4
5
6
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
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
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
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
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* tethex - tetrahedra to hexahedra conversion
* Copyright (c) 2013 Mikhail Artemyev
* Report issues: github.com/martemyev/tethex/issues
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
#ifndef TETHEX_TETHEX_H
#define TETHEX_TETHEX_H
#include <iostream>
#include <map>
#include <sstream>
#include <stdexcept>
#include <string>
#include <vector>
namespace tethex {
//-------------------------------------------------------
//
// d2s - convert data to string
//
//-------------------------------------------------------
template <typename T>
inline std::string d2s(T data)
{
std::ostringstream o;
if (!(o << data))
throw std::runtime_error("Bad conversion of data to string!");
return o.str();
}
//-------------------------------------------------------
//
// expect and require
// (the idea was firstly discovered in deal.II sources,
// so thanks to its authors for that)
//
//-------------------------------------------------------
#if DEBUG
#define expect(condition, message) \
if (!(condition)) \
requirement_fails(__FILE__, \
__LINE__, \
message)
#else
// in release (or release-like) versions
// nothing happens
#define expect(condition, message) { }
#endif // DEBUG
#define require(condition, message) \
if (!(condition)) \
requirement_fails(__FILE__, \
__LINE__, \
message)
/**
* Throw an informative exception,
* if requirement or expectation fails
*/
void requirement_fails(const char *file,
int line,
std::string message);
//-------------------------------------------------------
//
// Point
//
//-------------------------------------------------------
/**
* Point in 3-dimensional space.
* It's used for 2-dimensional triangles as well, and
* in this case one of the coordinates is 0 (usually it's z-coordinate).
*/
class Point
{
public:
/**
* The number of Cartesian coordinates, that describe the point.
* Here we always use 3 coordinates to describe a point.
*/
static const int n_coord = 3;
/**
* Default constructor.
* Coordinates are initialized by 0.
*/
Point();
/**
* Constructor with parameter.
* Coordinates are initialized by array of numbers.
* @param coordinates - array of point coordinates
*/
Point(const double coordinates[]);
/**
* Constructor with parameters.
* Coordinates are initialized by numbers.
* @param x_coord - x-coordinate of the point
* @param y_coord - y-coordinate of the point
* @param z_coord - z-coordinate of the point
*/
Point(double x_coord,
double y_coord = 0,
double z_coord = 0);
/**
* Copy constructor
*/
Point(const Point &p);
/**
* Copy assignment operator
*/
Point& operator =(const Point &p);
/**
* Get the coordinate of the point
* @param number - the serial number of coordinate [0, n_coord)
*/
double get_coord(int number) const;
/**
* Set the value of specific coordinate
* @param number - the number of coordinate that we want to set
* @param value - new value of coordinate
*/
void set_coord(int number, double value);
private:
/**
* Cartesian coordinates of the point
*/
double coord[n_coord];
};
//-------------------------------------------------------
//
// MeshElement
//
//-------------------------------------------------------
/**
* This class implements the most part of functionality of
* all elements of mesh: triangles, tetrahedra, quadrangles, hexahedra, etc.
* All these elements are declared as pointers to base (this) class.
* It's not an abstract class, because it has no pure virtual functions,
* but you can't create objects of this class, because its constructor is protected.
*/
class MeshElement
{
public:
/**
* Destructor
*/
virtual ~MeshElement();
/**
* Get the number of vertices
*/
int get_n_vertices() const;
/**
* Get the number of edges
*/
int get_n_edges() const;
/**
* Get the number of faces
*/
int get_n_faces() const;
/**
* Get type of the element that is used in Gmsh
*/
int get_gmsh_el_type() const;
/**
* Get the material ID of the element.
* It's a number that describes the physical domain
* to which the element belongs.
*/
int get_material_id() const;
/**
* Get the number of vertex describing the element
* @param number - local number of vertex [0, n_vertices)
* @return global number of vertex (among other mesh vertices)
*/
int get_vertex(int number) const;
/**
* Get the number of edge describing the element
* @param number - local number of edge [0, n_edges)
* @return global number of edge (among other mesh edges)
*/
int get_edge(int number) const;
/**
* Get the number of face describing the element
* @param number - local number of face [0, n_faces)
* @return global number of face (among other mesh faces)
*/
int get_face(int number) const;
/**
* Set the number of vertex
* @param local_number - the number of vertex inside the element [0, n_vertices)
* @param global_number - the number of vertex among other vertices of the mesh
*/
void set_vertex(int local_number, int global_number);
/**
* Set the number of edge
* @param local_number - the number of edge inside the element [0, n_edges)
* @param global_number - the number of edge among other edges of the mesh
*/
void set_edge(int local_number, int global_number);
/**
* Set the number of face
* @param local_number - the number of face inside the element [0, n_faces)
* @param global_number - the number of face among other faces of the mesh
*/
void set_face(int local_number, int global_number);
/**
* Set all faces once at time
* @param face_numbers - the numbers of all cell faces
*/
void set_faces(const std::vector<int> &face_numbers);
/**
* Check - whether the element contains the vertex or not
* @param vertex - the number of vertex that we want to check
*/
bool contains(const int vertex) const;
protected:
/**
* The number of vertices describing the element.
* It must be defined in each derived class,
* because it's 0 by default.
*/
int n_vertices;
/**
* Vertices (i.e. their global numbers) describing the element
*/
std::vector<int> vertices;
/**
* The number of edges describing the element.
* It must be defined in each derived class,
* because it's 0 by default.
*/
int n_edges;
/**
* Edges (i.e. their global numbers) describing the element
* It's not always used.
*/
std::vector<int> edges;
/**
* The number of faces describing the element.
* It must be defined in each derived class,
* because it's 0 by default.
*/
int n_faces;
/**
* Faces (i.e. their global numbers) describing the element
* It's not always used.
*/
std::vector<int> faces;
/**
* ID of the physical domain where the element takes place.
* It's necessary to distinguish media with different physical properties.
*/
int material_id;
/**
* Type of the element (its number actually) like in Gmsh.
* It must be defined in every derived class.
* It's 0 by default.
*/
int gmsh_el_type;
/**
* Constructor is protected to prevent creating MeshElement objects directly
* @param n_ver - number of vertices
* @param n_edg - number of edges
* @param n_fac - number of faces
* @param el_type - type of the element in Gmsh
*/
MeshElement(int n_ver = 0,
int n_edg = 0,
int n_fac = 0,
int el_type = 0);
/**
* Copy constructor
*/
MeshElement(const MeshElement &elem);
/**
* Copy assignment operator
*/
MeshElement& operator =(const MeshElement &elem);
};
//-------------------------------------------------------
//
// PhysPoint (physical entity)
//
//-------------------------------------------------------
/**
* PhysPoint keep 2 numbers - the number of the vertex associated with the point,
* and the number of physical domain
* where this point takes place (material identificator - in other words).
*/
class PhysPoint : public MeshElement
{
public:
/**
* Point is a vertex itself
*/
static const int n_vertices = 1;
/**
* It's 0-dimensional shape, and it's a boundary for edge
*/
static const int n_edges = 0;
/**
* It has no faces
*/
static const int n_faces = 0;
/**
* In Gmsh physical point is defined by number 15
*/
static const int gmsh_el_type = 15;
/**
* Constructor
*/
PhysPoint();
/**
* Constructor with parameters
* @param ver - the list of vertices
* @param mat_id - the material ID
*/
PhysPoint(const std::vector<int> &ver,
int mat_id = 0);
/**
* Constructor with parameters
* @param ver - a vertex
* @param mat_id - material ID
*/
PhysPoint(const int ver,
int mat_id = 0);
};
//-------------------------------------------------------
//
// Line
//
//-------------------------------------------------------
/**
* Line keeps 3 numbers - the number of beginning vertex,
* the number of the ending one, and a number of physical domain
* where this line takes place (material identificator - in other words).
* Line is not oriented.
*/
class Line : public MeshElement
{
public:
/**
* There are 2 vertices to describe a line
*/
static const int n_vertices = 2;
/**
* Line is edge itself, so the number of edges is 1
*/
static const int n_edges = 1;
/**
* It's 1D shape, so there is no faces here
*/
static const int n_faces = 0;
/**
* In Gmsh line (physical line) is defined by number 1
*/
static const int gmsh_el_type = 1;
/**
* Constructor
*/
Line();
/**
* Constructor with parameters
* @param ver - the list of vertices
* @param mat_id - the material ID
*/
Line(const std::vector<int> &ver,
const int mat_id = 0);
/**
* Constructor with parameters
* @param v1 - one vertex
* @param v2 - another vertex
* @param mat_id - material ID
*/
Line(int v1,
int v2,
int mat_id = 0);
/**
* Find common vertex between two lines
* @param line - second line for seeking common vertex
*/
int common_vertex(const Line& line) const;
/**
* Get another vertex (different from that we have)
* @param vertex - we have the number of one vertex (this one),
* and we want to find the number of another vertex
*/
int another_vertex(int vertex) const;
};
//-------------------------------------------------------
//
// Triangle
//
//-------------------------------------------------------
/**
* Triangle - 2-dimensional simplex.
* The simplest shape in 2D.
* It's an element of mesh,
* therefore it inherits the most part of
* functionality from MeshElement class.
*/
class Triangle : public MeshElement
{
public:
/**
* The number of vertices of triangle
*/
static const int n_vertices = 3;
/**
* The number of edges of triangle
*/
static const int n_edges = 3;
/**
* Triangle is 2D shape,
* so it's a face itself (for tetrahedron)
*/
static const int n_faces = 1;
/**
* In Gmsh triangle is defined by number 2
*/
static const int gmsh_el_type = 2;
/**
* Default constructor
*/
Triangle();
/**
* Constructor with parameters
* @param ver - triangle vertices
* @param mat_id - material ID
*/
Triangle(const std::vector<int> &ver,
int mat_id = 0);
/**
* Constructor with parameters
* @param v1 - first vertex
* @param v2 - second vertex
* @param v3 - third vertex
* @param mat_id - material ID
*/
Triangle(int v1,
int v2,
int v3,
int mat_id = 0);
};
//-------------------------------------------------------
//
// Tetrahedron
//
//-------------------------------------------------------
/**
* Tetrahedron - 3-dimensional simplex.
* The simplest shape in 3D.
* It's an element of mesh,
* therefore it inherits the most part of
* functionality from MeshElement class.
*/
class Tetrahedron : public MeshElement
{
public:
/**
* The number of vertices of tetrahedron
*/
static const int n_vertices = 4;
/**
* The number of edges of tetrahedron
*/
static const int n_edges = 6;
/**
* The number of faces of tetrahedron
*/
static const int n_faces = 4;
/**
* In Gmsh triangle is defined by number 2
*/
static const int gmsh_el_type = 4;
/**
* Default constructor.
*/
Tetrahedron();
/**
* Constructor with parameters
* @param ver - triangle vertices
* @param mat_id - material ID
*/
Tetrahedron(const std::vector<int> &ver,
int mat_id = 0);
/**
* Constructor with parameters
* @param v1 - first vertex
* @param v2 - second vertex
* @param v3 - third vertex
* @param v4 - fourth vertex
* @param mat_id - material ID
*/
Tetrahedron(int v1,
int v2,
int v3,
int v4,
int mat_id = 0);
};
//-------------------------------------------------------
//
// Quadrangle
//
//-------------------------------------------------------
/**
* Quadrangle - 2-dimensional shape with 4 straight edges.
* It's an element of mesh,
* therefore it inherits the most part of
* functionality from MeshElement class.
*/
class Quadrangle : public MeshElement
{
public:
/**
* The number of vertices of quadrangle
*/
static const int n_vertices = 4;
/**
* The number of edges of quadrangle
*/
static const int n_edges = 4;
/**
* Quadrangle is 2D shape,
* so it's a face itself (for hexahedron)
*/
static const int n_faces = 1;
/**
* In Gmsh quadrangle is defined by number 3
*/
static const int gmsh_el_type = 3;
/**
* Default constructor.
*/
Quadrangle();
/**
* Constructor with parameters
* @param ver - quadrangle vertices
* @param mat_id - material ID
*/
Quadrangle(const std::vector<int> &ver,
int mat_id = 0);
/**
* Constructor with parameters
* @param v1 - first vertex
* @param v2 - second vertex
* @param v3 - third vertex
* @param v4 - fourth vertex
* @param mat_id - material ID
*/
Quadrangle(int v1,
int v2,
int v3,
int v4,
int mat_id = 0);
};
//-------------------------------------------------------
//
// Hexahedron
//
//-------------------------------------------------------
/**
* Hexahedron - 3-dimensional shape with 6 plane faces.
* It's an element of mesh,
* therefore it inherits the most part of
* functionality from MeshElement class.
*/
class Hexahedron : public MeshElement
{
public:
/**
* The number of vertices of hexahedron
*/
static const int n_vertices = 8;
/**
* The number of edges of hexahedron
*/
static const int n_edges = 12;
/**
* The number of faces of hexahedron
*/
static const int n_faces = 6;
/**
* In Gmsh hexahedron is defined by number 5
*/
static const int gmsh_el_type = 5;
/**
* Default constructor.
*/
Hexahedron();
/**
* Constructor with parameters
* @param ver - hexahedron vertices
* @param mat_id - material ID
*/
Hexahedron(const std::vector<int> &ver,
int mat_id = 0);
/**
* Constructor with parameters
* @param v1 - first vertex of hexahedron
* @param v8 - 8-th vertex of hexahedron
* @param mat_id - material ID
*/
Hexahedron(int v1,
int v2,
int v3,
int v4,
int v5,
int v6,
int v7,
int v8,
int mat_id = 0);
};
//-------------------------------------------------------
//
// IncidenceMatrix
//
//-------------------------------------------------------
/**
* Incidence matrix describes the relations
* (connections) between mesh nodes.
* It's used for edge numeration and for fast
* finding of edge number knowing 2 vertices,
* that define the edge.
* Because this matrix is symmetric
* we consider its lower triangle only.
*/
class IncidenceMatrix
{
public:
/**
* Constructor
* @param n_vertices - the number of all mesh vertices
* @param cells - the list of all mesh cells
*/
IncidenceMatrix(int n_vertices,
const std::vector<MeshElement*> &cells);
/**
* Destructor
*/
~IncidenceMatrix();
/**
* Find a serial number of the non zero element in the matrix.
* This number usually coincides with the number of edge.
* Since we keep only lower triangle of incidence matrix,
* row_number has to be bigger than col_number.
* @param row_number - the number of row where we seek (or one edge vertex)
* @param col_number - the number of column where we seek (or another edge vertex)
* @return Serial number of the non zero element in the matrix.
*/
int find(int row_number, int col_number) const;
/**
* Get the number of non zero elements in the matrix.
* Can be used to know the number of mesh edges.
*/
int get_n_nonzero() const;
private:
/**
* The dimension of the matrix
*/
int dim;
/**
* The number of nonzero elements of lower matrix triangle
*/
int n_non_zero;
/**
* The number of nonzero elements in each row of lower matrix triangle
*/
int *row;
/**
* The numbers of nonzero elements of lower matrix triangle
*/
int *col;
/**
* No copies
*/
IncidenceMatrix(const IncidenceMatrix&);
IncidenceMatrix& operator =(const IncidenceMatrix&);
};
//-------------------------------------------------------
//
// Mesh
//
//-------------------------------------------------------
/**
* Main class that stores all data during program execution.
*/
class Mesh
{
public:
/**
* Constructor - nothing special
*/
Mesh();
/**
* Destructor - to clean the memory
*/
~Mesh();
/**
* Read the mesh from file
* @param file - the name of the mesh file
*/
void read(const std::string &file);
/**
* Conversion from simplices to bricks.
* Specifically, in 2D - conversion from triangles to quadrangles,
* in 3D - conversion from tetrahedra to hexahedra.
*/
void convert();
/**
* Write the resulting brick mesh into the file
* @param file - the name of mesh file where we write the results of conversion
*/
void write(const std::string &file);
/**
* Get the number of vertices
*/
int get_n_vertices() const;
/**
* Get the number of physical points
*/
int get_n_points() const;
/**
* Get the number of lines (physical lines)
*/
int get_n_lines() const;
/**
* Get the number of edges
*/
int get_n_edges() const;
/**
* Get the number of triangles
*/
int get_n_triangles() const;
/**
* Get the number of faces
*/
int get_n_faces() const;
/**
* Get the number of tetrahedra
*/
int get_n_tetrahedra() const;
/**
* Get the number of tetrahedra
*/
int get_n_quadrangles() const;
/**
* Get the number of hexahedra
*/
int get_n_hexahedra() const;
/**
* Print short information
* about mesh into choosing stream.
*/
void info(std::ostream &out = std::cout) const;
/**
* Print some statistics (detailed information)
* about mesh into choosing stream.
*/
void statistics(std::ostream &out = std::cout) const;
/**
* Get the copy of vertex
* @param number - the number of vertex
*/
Point get_vertex(int number) const;
/**
* Get the physical point
* @param number - the number of point
*/
MeshElement& get_point(int number) const;
/**
* Get the mesh edge
* @param number - the number of edge
*/
MeshElement& get_edge(int number) const;
/**
* Get the physical line
* @param number - the number of line
*/
MeshElement& get_line(int number) const;
/**
* Get the mesh face
* @param number - the number of face
*/
MeshElement& get_face(int number) const;
/**
* Get the mesh triangle
* @param number - the number of triangle
*/
MeshElement& get_triangle(int number) const;
/**
* Get the mesh tetrahedron
* @param number - the number of tetrahedron
*/
MeshElement& get_tetrahedron(int number) const;
/**
* Get the mesh quadrangle
* @param number - the number of quadrangle
*/
MeshElement& get_quadrangle(int number) const;
/**
* Get the mesh hexahedron
* @param number - the number of hexahedron
*/
MeshElement& get_hexahedron(int number) const;
private:
/**
* Mesh vertices (nodes in terms of Gmsh)
*/
std::vector<Point> vertices;
/**
* Physical points.
*They are not treated - just copied into new mesh file.
*/
std::vector<MeshElement*> points;
/**
* Mesh lines - mean physical lines
*/
std::vector<MeshElement*> lines;
/**
* Mesh edges (oriented lines)
*/
std::vector<MeshElement*> edges;
/**
* Mesh faces
*/
std::vector<MeshElement*> faces;
/**
* Mesh triangles
*/
std::vector<MeshElement*> triangles;
/**
* Mesh tetrahedra
*/