forked from martemyev/tethex
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtethex.cpp
1945 lines (1600 loc) · 62.3 KB
/
tethex.cpp
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
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/
#include "tethex.h"
#include "config.h"
#include <algorithm>
#include <fstream>
#include <cmath>
namespace tethex {
//-------------------------------------------------------
//
// expect and require
//
//-------------------------------------------------------
void requirement_fails(const char *file,
int line,
std::string message)
{
std::string exc = "Exception:\nfile = " + std::string(file) +
"\nline = " + d2s(line) +
"\nmessage = " + message + "\n";
throw std::runtime_error(exc);
}
//-------------------------------------------------------
//
// Point
//
//-------------------------------------------------------
Point::Point()
{
for (int i = 0; i < n_coord; ++i)
coord[i] = 0.;
}
Point::Point(const double coordinates[])
{
for (int i = 0; i < n_coord; ++i)
coord[i] = coordinates[i];
}
Point::Point(const double x_coord,
const double y_coord,
const double z_coord)
{
coord[0] = x_coord;
if (n_coord > 1) coord[1] = y_coord;
if (n_coord > 2) coord[2] = z_coord;
}
Point::Point(const Point &p)
{
for (int i = 0; i < n_coord; ++i)
coord[i] = p.coord[i];
}
Point& Point::operator =(const Point &p)
{
for (int i = 0; i < n_coord; ++i)
coord[i] = p.coord[i];
return *this;
}
double Point::get_coord(int number) const
{
expect(number >= 0 && number < n_coord,
"The number of coordinate is incorrect: " +
d2s(number) + ". It should be in the range: [0, " +
d2s(n_coord) + ")");
return coord[number];
}
void Point::set_coord(int number, double value)
{
expect(number >= 0 && number < n_coord,
"The number of coordinate is incorrect: " +
d2s(number) + ". It should be in the range: [0, " +
d2s(n_coord) + ")");
coord[number] = value;
}
//-------------------------------------------------------
//
// MeshElement
//
//-------------------------------------------------------
MeshElement::MeshElement(int n_ver,
int n_edg,
int n_fac,
int el_type)
: n_vertices(n_ver)
, vertices()
, n_edges(n_edg)
, edges()
, n_faces(n_fac)
, faces()
, material_id(0)
, gmsh_el_type(el_type)
{
vertices.resize(n_vertices, 0);
edges.resize(n_edges, 0);
faces.resize(n_faces, 0);
}
MeshElement::~MeshElement()
{ }
inline int MeshElement::get_n_vertices() const
{
expect(n_vertices == vertices.size(),
"Memory for vertices is not allocated properly (size is " + d2s(vertices.size()) +
"), or n_vertices (" + d2s(n_vertices) + ") is set to wrong number");
return n_vertices;
}
inline int MeshElement::get_n_edges() const
{
expect(n_edges == edges.size(),
"Memory for edges is not allocated properly (size is " + d2s(edges.size()) +
"), or n_edges (" + d2s(n_edges) + ") is set to wrong number");
return n_edges;
}
inline int MeshElement::get_n_faces() const
{
expect(n_faces == faces.size(),
"Memory for faces is not allocated properly (size is " + d2s(faces.size()) +
"), or n_faces (" + d2s(n_faces) + ") is set to wrong number");
return n_faces;
}
inline int MeshElement::get_gmsh_el_type() const
{
return gmsh_el_type;
}
inline int MeshElement::get_material_id() const
{
return material_id;
}
MeshElement::MeshElement(const MeshElement &elem)
: n_vertices(elem.n_vertices)
, vertices(elem.vertices)
, n_edges(elem.n_edges)
, edges(elem.edges)
, n_faces(elem.n_faces)
, faces(elem.faces)
, material_id(elem.material_id)
, gmsh_el_type(elem.gmsh_el_type)
{ }
MeshElement& MeshElement::operator =(const MeshElement &elem)
{
n_vertices = elem.n_vertices;
n_edges = elem.n_edges;
n_faces = elem.n_faces;
gmsh_el_type = elem.gmsh_el_type;
material_id = elem.material_id;
vertices = elem.vertices;
edges = elem.edges;
faces = elem.faces;
return *this;
}
int MeshElement::get_vertex(int number) const
{
expect(number >= 0 && number < n_vertices,
"The local number of vertex is incorrect: " + d2s(number) +
". It has to be in range [0, " + d2s(n_vertices) + ").");
return vertices[number];
}
int MeshElement::get_edge(int number) const
{
expect(number >= 0 && number < n_edges,
"The local number of edge is incorrect: " + d2s(number) +
". It has to be in range [0, " + d2s(n_edges) + ").");
return edges[number];
}
int MeshElement::get_face(int number) const
{
expect(number >= 0 && number < n_faces,
"The local number of face is incorrect: " + d2s(number) +
". It has to be in range [0, " + d2s(n_faces) + ").");
return faces[number];
}
void MeshElement::set_vertex(int local_number, int global_number)
{
expect(local_number >= 0 && local_number < get_n_vertices(),
"Local number (" + d2s(local_number) +
") is incorrect. It must be in the range [0, " + d2s(n_edges) + ")");
vertices[local_number] = global_number;
}
void MeshElement::set_edge(int local_number, int global_number)
{
expect(local_number >= 0 && local_number < get_n_edges(),
"Local number (" + d2s(local_number) +
") is incorrect. It must be in the range [0, " + d2s(n_edges) + ")");
edges[local_number] = global_number;
}
void MeshElement::set_face(int local_number, int global_number)
{
expect(local_number >= 0 && local_number < get_n_faces(),
"Local number (" + d2s(local_number) +
") is incorrect. It must be in the range [0, " + d2s(n_faces) + ")");
faces[local_number] = global_number;
}
void MeshElement::set_faces(const std::vector<int> &face_numbers)
{
expect(face_numbers.size() == get_n_faces(),
"Array of face numbers has another size (" + d2s(face_numbers.size()) +
") than it must be (" + d2s(get_n_faces()) + ")");
faces = face_numbers;
}
bool MeshElement::contains(int vertex) const
{
for (int i = 0; i < n_vertices; ++i)
if (vertex == vertices[i])
return true;
return false;
}
//-------------------------------------------------------
//
// PhysPoint
//
//-------------------------------------------------------
PhysPoint::PhysPoint()
: MeshElement(n_vertices, n_edges, n_faces, gmsh_el_type)
{ }
PhysPoint::PhysPoint(const std::vector<int> &ver,
int mat_id)
: MeshElement(n_vertices, n_edges, n_faces, gmsh_el_type)
{
expect(vertices.size() == ver.size(),
"The size of list of vertices (" + d2s(ver.size()) +
") is not equal to really needed number of vertices (" + d2s(n_vertices) + ")");
vertices = ver;
material_id = mat_id;
}
PhysPoint::PhysPoint(int ver, int mat_id)
: MeshElement(n_vertices, n_edges, n_faces, gmsh_el_type)
{
vertices[0] = ver;
material_id = mat_id;
}
//-------------------------------------------------------
//
// Line
//
//-------------------------------------------------------
Line::Line()
: MeshElement(n_vertices, n_edges, n_faces, gmsh_el_type)
{ }
Line::Line(const std::vector<int> &ver,
int mat_id)
: MeshElement(n_vertices, n_edges, n_faces, gmsh_el_type)
{
expect(vertices.size() == ver.size(),
"The size of list of vertices (" + d2s(ver.size()) +
") is not equal to really needed number of vertices (" + d2s(n_vertices) + ")");
vertices = ver;
material_id = mat_id;
}
Line::Line(int v1,
int v2,
int mat_id)
: MeshElement(n_vertices, n_edges, n_faces, gmsh_el_type)
{
vertices[0] = v1;
vertices[1] = v2;
material_id = mat_id;
}
int Line::common_vertex(const Line& line) const
{
for (int i = 0; i < n_vertices; ++i)
if (line.contains(vertices[i]))
return vertices[i];
require(false, "There is no common vertex between these two lines!");
return 0; // to calm compiler down
}
int Line::another_vertex(int vertex) const
{
if (vertex == vertices[0])
return vertices[1];
else if (vertex == vertices[1])
return vertices[0];
else
require(false, "This line doesn't contain the vertex. So we can't find another one.");
return 0; // to calm compiler down
}
//-------------------------------------------------------
//
// Triangle
//
//-------------------------------------------------------
Triangle::Triangle()
: MeshElement(n_vertices, n_edges, n_faces, gmsh_el_type)
{ }
Triangle::Triangle(const std::vector<int> &ver,
int mat_id)
: MeshElement(n_vertices, n_edges, n_faces, gmsh_el_type)
{
expect(vertices.size() == ver.size(),
"The size of list of vertices (" + d2s(ver.size()) +
") is not equal to really needed number of vertices (" + d2s(n_vertices) + ")");
vertices = ver;
material_id = mat_id;
}
Triangle::Triangle(int v1,
int v2,
int v3,
int mat_id)
: MeshElement(n_vertices, n_edges, n_faces, gmsh_el_type)
{
vertices[0] = v1;
vertices[1] = v2;
vertices[2] = v3;
material_id = mat_id;
}
//-------------------------------------------------------
//
// Tetrahedron
//
//-------------------------------------------------------
Tetrahedron::Tetrahedron()
: MeshElement(n_vertices, n_edges, n_faces, gmsh_el_type)
{ }
Tetrahedron::Tetrahedron(const std::vector<int> &ver,
int mat_id)
: MeshElement(n_vertices, n_edges, n_faces, gmsh_el_type)
{
expect(vertices.size() == ver.size(),
"The size of list of vertices (" + d2s(ver.size()) +
") is not equal to really needed number of vertices (" + d2s(n_vertices) + ")");
vertices = ver;
material_id = mat_id;
}
Tetrahedron::Tetrahedron(int v1,
int v2,
int v3,
int v4,
int mat_id)
: MeshElement(n_vertices, n_edges, n_faces, gmsh_el_type)
{
vertices[0] = v1;
vertices[1] = v2;
vertices[2] = v3;
vertices[3] = v4;
material_id = mat_id;
}
//-------------------------------------------------------
//
// Quadrangle
//
//-------------------------------------------------------
Quadrangle::Quadrangle()
: MeshElement(n_vertices, n_edges, n_faces, gmsh_el_type)
{ }
Quadrangle::Quadrangle(const std::vector<int> &ver,
int mat_id)
: MeshElement(n_vertices, n_edges, n_faces, gmsh_el_type)
{
expect(vertices.size() == ver.size(),
"The size of list of vertices (" + d2s(ver.size()) +
") is not equal to really needed number of vertices (" + d2s(n_vertices) + ")");
vertices = ver;
material_id = mat_id;
}
Quadrangle::Quadrangle(int v1,
int v2,
int v3,
int v4,
int mat_id)
: MeshElement(n_vertices, n_edges, n_faces, gmsh_el_type)
{
vertices[0] = v1;
vertices[1] = v2;
vertices[2] = v3;
vertices[3] = v4;
material_id = mat_id;
}
//-------------------------------------------------------
//
// Hexahedron
//
//-------------------------------------------------------
Hexahedron::Hexahedron()
: MeshElement(n_vertices, n_edges, n_faces, gmsh_el_type)
{ }
Hexahedron::Hexahedron(const std::vector<int> &ver,
int mat_id)
: MeshElement(n_vertices, n_edges, n_faces, gmsh_el_type)
{
expect(vertices.size() == ver.size(),
"The size of list of vertices (" + d2s(ver.size()) +
") is not equal to really needed number of vertices (" + d2s(n_vertices) + ")");
vertices = ver;
material_id = mat_id;
}
Hexahedron::Hexahedron(int v1,
int v2,
int v3,
int v4,
int v5,
int v6,
int v7,
int v8,
int mat_id)
: MeshElement(n_vertices, n_edges, n_faces, gmsh_el_type)
{
vertices[0] = v1;
vertices[1] = v2;
vertices[2] = v3;
vertices[3] = v4;
vertices[4] = v5;
vertices[5] = v6;
vertices[6] = v7;
vertices[7] = v8;
material_id = mat_id;
}
//-------------------------------------------------------
//
// IncidenceMatrix
//
//-------------------------------------------------------
IncidenceMatrix::IncidenceMatrix(int n_vertices,
const std::vector<MeshElement*> &cells)
: dim(n_vertices)
, n_non_zero(0)
, row(nullptr)
, col(nullptr)
{
std::vector<int> *vec = new std::vector<int>[dim]; // for lower triangle
// look through all mesh cells
for (size_t cell = 0; cell < cells.size(); ++cell)
{
// look at all pairs of cell vertices
for (int i = 0; i < cells[cell]->get_n_vertices(); ++i)
{
const int ii = cells[cell]->get_vertex(i);
for (int j = 0; j < cells[cell]->get_n_vertices(); ++j)
{
const int jj = cells[cell]->get_vertex(j);
if (ii > jj) // we consider only lower triangle of matrix
{
// add to vector, if the vector doesn't contain this number
if (std::find(vec[ii].begin(), vec[ii].end(), jj) == vec[ii].end())
vec[ii].push_back(jj);
}
}
}
}
// sorting the vectors
for (int i = 0; i < dim; ++i)
std::sort(vec[i].begin(), vec[i].end());
// the number of non zero elements in each row of lower triangle
row = new int[dim + 1];
row[0] = 0;
for (int i = 0; i < dim; ++i)
row[i + 1] = row[i] + vec[i].size();
n_non_zero = row[dim]; // the number of all non zero elements in lower triangle
// numbers of non zero elements in lower triangle
col = new int[n_non_zero];
int k = 0;
for (int i = 0; i < dim; ++i)
{
for (size_t j = 0; j < vec[i].size(); ++j)
{
col[k] = vec[i][j];
k++;
}
}
// free the memory
for (int i = 0; i < dim; ++i)
vec[i].clear();
delete[] vec;
}
IncidenceMatrix::~IncidenceMatrix()
{
delete[] row;
delete[] col;
}
int IncidenceMatrix::find(int row_number,
int col_number) const
{
// because we seek in lower triangle, row must be bigger than col
expect(row_number > col_number,
"We seek values in lower triangle, so row should be bigger than column. But in this case row_number = " +
d2s(row_number) + ", col_number = " + d2s(col_number) + "!");
for (int i = row[row_number]; i < row[row_number + 1]; ++i)
{
if (col[i] == col_number)
return i;
}
// if the number cannot be found in this row, we throw exception
require(false,
"The right value wasn't found for such row and column numbers: row_number = " +
d2s(row_number) + ", col_number = " + d2s(col_number) + "!");
return 0; // to calm compiler down about returned value
}
inline int IncidenceMatrix::get_n_nonzero() const
{
return n_non_zero;
}
//-------------------------------------------------------
//
// Mesh
//
//-------------------------------------------------------
Mesh::Mesh()
: vertices()
, points()
, lines()
, edges()
, faces()
, triangles()
, tetrahedra()
, quadrangles()
, hexahedra()
, n_converted_quadrangles(0)
, n_converted_hexahedra(0)
, physical_names()
{ }
Mesh::~Mesh()
{
clean();
}
void Mesh::clean()
{
for (size_t i = 0; i < points.size(); ++i)
delete points[i];
for (size_t i = 0; i < lines.size(); ++i)
delete lines[i];
for (size_t i = 0; i < edges.size(); ++i)
delete edges[i];
for (size_t i = 0; i < faces.size(); ++i)
delete faces[i];
for (size_t i = 0; i < triangles.size(); ++i)
delete triangles[i];
for (size_t i = 0; i < tetrahedra.size(); ++i)
delete tetrahedra[i];
for (size_t i = 0; i < quadrangles.size(); ++i)
delete quadrangles[i];
for (size_t i = 0; i < hexahedra.size(); ++i)
delete hexahedra[i];
}
void Mesh::read(const std::string &file)
{
std::ifstream in(file.c_str());
require(in, "File " + file + " cannot be opened!");
clean(); // free the memory for mesh elements
std::string str;
in >> str; // the first string of Gmsh file is "$MeshFormat"
expect(str == "$MeshFormat", "The first string of the Gmsh file " + file +
" doesn't equal to \"$MeshFormat\". The actual string is \"" + str + "\"");
// read the information about the mesh
double version;
int binary, dsize;
in >> version >> binary >> dsize;
// The function has been testing for meshes corresponding
// to Gmsh versions since 2.6.0, therefore
// in debug mode you'll have an exception if the mesh version is less than 2.2.
// There is no exception in release mode though.
// So to read the mesh with version 2.1 and less, check that DEBUG variable is set to 0.
// But NOTE that msh files of 1.0 format have absolutely different structure!
expect(version >= 2.2, "The version of Gmsh's mesh is too old (" + d2s(version) +
"). The library was tested for versions 2.2+.");
expect(dsize == sizeof(double), "The size of Gmsh's double (" + d2s(dsize) +
") doesn't equal to size of double type (" + d2s(sizeof(double)) + ")");
getline(in, str); // read some empty string
// there is additional 1 (the number - one) in binary format
if (binary)
{
int one;
in.read(reinterpret_cast<char*>(&one), sizeof(int));
require(one == 1, "The binary one (" + d2s(one) + ") doesn't equal to 1!");
}
// we make a map between serial number of the vertex and its number in the file.
// it will help us when we create mesh elements
std::map<int, int> vertices_map;
// read lines of mesh file.
// if we face specific keyword, we'll treat the section.
while (in >> str)
{
if (str == "$PhysicalNames") // read the section of names of physical entities
{
int n_names;
in >> n_names;
getline(in, str);
physical_names.resize(n_names);
for (int i = 0; i < n_names; ++i)
getline(in, physical_names[i]);
}
else if (str == "$Nodes") // read the mesh vertices
{
int n_vertices; // the number of all mesh vertices (that are saved in the file)
in >> n_vertices; // read that number
vertices.resize(n_vertices); // allocate the memory for mesh vertices
getline(in, str); // read some empty string
int number; // the number of the vertex
// Cartesian coordinates of the vertex (Gmsh produces 3D mesh regardless
// its real dimension)
double coord[Point::n_coord];
// read vertices
for (int ver = 0; ver < n_vertices; ++ver)
{
if (binary) // binary format
{
in.read(reinterpret_cast<char*>(&number), sizeof(int));
in.read(reinterpret_cast<char*>(coord), Point::n_coord * sizeof(double));
}
else // ASCII format
{
in >> number;
for (int i = 0; i < Point::n_coord; ++i)
in >> coord[i];
}
vertices[ver] = Point(coord); // save the vertex
vertices_map[number] = ver; // add the number of vertex to the map
}
expect(n_vertices == vertices_map.size(),
"Vertices numbers are not unique: n_vertices = " + d2s(n_vertices) +
" vertices_map.size() = " + d2s(vertices_map.size()));
} // read the vertices
else if (str == "$Elements") // read the mesh elements
{
int n_elements; // the number of mesh elements
in >> n_elements; // read that number
getline(in, str); // empty string
int number; // the number of the element [1, nElements]
int el_type; // the type of the element (1 - line, 2 - triangle, etc)
int n_tags; // the number of tags describing the element
int phys_domain; // the physical domain where the element takes place
// int elem_domain; // the elementary domain where the element takes place
// int partition; // the partition in which the element takes place
// the map between the type of the element,
// and the number of nodes that describe it
std::map<int, int> type_nodes;
type_nodes[1] = 2; // 2-nodes line
type_nodes[2] = 3; // 3-nodes triangle
type_nodes[3] = 4; // 4-nodes quadrangle
type_nodes[4] = 4; // 4-nodes tetrahedron
type_nodes[5] = 8; // 8-nodes hexahedron
type_nodes[15]= 1; // 1-node point
if (binary) // binary format
{
/*
int n_elem_part = 0; // some part of the elements
int header[3]; // the header of the element
int amount; // amount of mesh elements of the same type
while (n_elem_part < n_elements)
{
in.read(reinterpret_cast<char*>(header), 3 * sizeof(int)); // read the header
el_type = header[0];
amount = header[1];
n_tags = header[2];
n_elem_part += amount;
// the number of nodes
const int n_elem_nodes = type_nodes.find(el_type) != ;
switch (el_type)
{
case 1: // 2-node line
const int n_data = 1 + n_tags + n_elem_nodes; // how much data we need to read
int data[n_data]; // allocate memory for data
int nodes[n_elem_nodes]; // allocate memory for nodes
for (int el = 0; el < amount; ++el)
{
in.read(reinterpret_cast<char*>(data), n_data * sizeof(int)); // read the data
number = data[0];
phys_domain = (n_tags > 0) ? data[1] : 0; // physical domain - the most important value
elem_domain = (n_tags > 1) ? data[2] : 0; // elementary domain
partition = (n_tags > 2) ? data[3] : 0; // partition (Metis, Chaco, etc)
for (int i = 0; i < n_elem_nodes; ++i)
nodes[i] = vertices_map[data[n_tags + 1 + i]]; // nodes can be numerated not sequentially
// add new element in the list
lines.push_back(Edge(nodes[0], nodes[1], phys_domain));
}
break;
case 2: // 3-node triangle
const int n_elem_nodes = 3; // the number of nodes
const int n_data = 1 + n_tags + n_elem_nodes; // how much data we need to read
int data[n_data]; // allocate memory for data
int nodes[n_elem_nodes]; // allocate memory for nodes
for (int el = 0; el < amount; ++el)
{
in.read(reinterpret_cast<char*>(data), n_data * sizeof(int)); // read the data
number = data[0];
phys_domain = (n_tags > 0) ? data[1] : 0; // physical domain - the most important value
elem_domain = (n_tags > 1) ? data[2] : 0; // elementary domain
partition = (n_tags > 2) ? data[3] : 0; // partition (Metis, Chaco, etc)
for (int i = 0; i < n_elem_nodes; ++i)
nodes[i] = vertices_map[data[n_tags + 1 + i]]; // nodes can be numerated not sequentially
// add new element in the list
triangles.push_back(Triangle(nodes[0], nodes[1], nodes[2], phys_domain));
}
break;
case 4: // 4-node tetrahedron
break;
default: // other elements are not interesting for us
break;
}
int nElemNodes = mev->second; // the number of nodes
int nData = 1 + nTags + nElemNodes; // how much data we need to read
std::vector<int> data(nData); // allocate memory for data
std::vector<int> nodes(nElemNodes); // allocate memory for nodes
// read the elements of the same type
for (int el = 0; el < amount; ++el)
{
in.read(reinterpret_cast<char*>(&data[0]), nData * sizeof(int)); // read the data
number = data[0];
physDomain = (nTags > 0) ? data[1] : 0; // physical domain - the most important value
elemDomain = (nTags > 1) ? data[2] : 0; // elementary domain
partition = (nTags > 2) ? data[3] : 0; // partition (Metis, Chaco, etc)
for (int i = 0; i < nElemNodes; ++i)
nodes[i] = data[nTags + 1 + i] - minNodeNumber; // Gmsh numerates the nodes from 'minNodeNumber' (it's usually 1)
// add new element in the list
_elements[elName].push_back(MeshElement(nodes, physDomain));
}
nodes.clear();
data.clear();
}
// check some expectations
expect(nElemPart == nElements,
ExcMessage("The accumulate number of different elements (" + d2s(nElemPart) +\
") is not equal to the amount of all elements in the mesh (" + d2s(nElements) + ")!"));
*/
} // binary format
else // ASCII format
{
for (int el = 0; el < n_elements; ++el)
{
in >> number >> el_type >> n_tags;
std::vector<int> data(n_tags); // allocate the memory for some data
for (int i = 0; i < n_tags; ++i) // read this information
in >> data[i];
phys_domain = (n_tags > 0) ? data[0] : 0; // physical domain - the most important value
// elem_domain = (n_tags > 1) ? data[1] : 0; // elementary domain
// partition = (n_tags > 2) ? data[2] : 0; // partition (Metis, Chaco, etc)
data.clear(); // other data isn't interesting for us
// how many vertices (nodes) describe the element
std::map<int, int>::const_iterator el_type_iter =
type_nodes.find(el_type);
require(el_type_iter != type_nodes.end(),
"This type of the Gmsh's element (" + d2s(el_type) +
") in the mesh file \"" + file + "\" is unknown!");
const int n_elem_nodes = el_type_iter->second; // the number of nodes
std::vector<int> nodes(n_elem_nodes); // allocate memory for nodes
for (int i = 0; i < n_elem_nodes; ++i)
{
in >> nodes[i]; // read the numbers of nodes
// vertices can be numerated not sequentially (or not from 0)
nodes[i] = vertices_map.find(nodes[i])->second;
}
switch (el_type)
{
case 15: // 1-node point
points.push_back(new PhysPoint(nodes, phys_domain));
break;
case 1: // 2-nodes line
lines.push_back(new Line(nodes, phys_domain));
break;
case 2: // 3-nodes triangle
triangles.push_back(new Triangle(nodes, phys_domain));
break;
case 3: // 4-nodes quadrangle
quadrangles.push_back(new Quadrangle(nodes, phys_domain));
break;
case 4: //4-nodes tetrahedron
tetrahedra.push_back(new Tetrahedron(nodes, phys_domain));
break;
case 5: // 8-nodes hexahedron
hexahedra.push_back(new Hexahedron(nodes, phys_domain));
break;
default:
require(false, "Unknown type of the Gmsh's element (" +
d2s(el_type) + ") in the file " + file + "!");
}
nodes.clear();
}
// check some expectations
expect(number == n_elements, "The number of the last read Gmsh's "
"element (" + d2s(number) + ") is not equal to the amount of "
"all elements in the mesh (" + d2s(n_elements) + ")!");
} // ASCII format
// requirements after reading elements
require(!triangles.empty() || !tetrahedra.empty() ||
!quadrangles.empty() || !hexahedra.empty(),
"There are no any 2D or 3D elements in the mesh!");
// we prevent mixing of simplices and bricks in one mesh.
// at least for the moment
if (!triangles.empty() || !tetrahedra.empty())
require(quadrangles.empty() && hexahedra.empty(), "There are simplices "
"and bricks in the same mesh. It's prohibited. Mesh file " + file);
} // read the elements
}
in.close(); // close the file
}
void Mesh::convert()
{
if (!tetrahedra.empty())
convert_3D();
else if (!triangles.empty())
convert_2D();
if (!hexahedra.empty())
convert_hexahedra();
if (!quadrangles.empty())
convert_quadrangles();
}
void Mesh::set_new_vertices(const std::vector<MeshElement*> &elements,
int n_old_vertices,
int shift)
{
for (size_t elem = 0; elem < elements.size(); ++elem)
{
for (int coord = 0; coord < Point::n_coord; ++coord)
{
double coordinate = 0.;
for (int ver = 0; ver < elements[elem]->get_n_vertices(); ++ver)
{
const int cur_vertex = elements[elem]->get_vertex(ver);
expect(cur_vertex < n_old_vertices,
"The element has a vertex (" + d2s(cur_vertex) +
") that is more than the number of old vertices (" + d2s(n_old_vertices) + ")");
coordinate += vertices[cur_vertex].get_coord(coord);
}
vertices[n_old_vertices + shift + elem].set_coord(coord,
coordinate / elements[elem]->get_n_vertices());
}
}
}
void Mesh::convert_2D()
{