-
Notifications
You must be signed in to change notification settings - Fork 306
Expand file tree
/
Copy pathexodusII_io_helper.C
More file actions
4929 lines (4010 loc) · 175 KB
/
Copy pathexodusII_io_helper.C
File metadata and controls
4929 lines (4010 loc) · 175 KB
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
// The libMesh Finite Element Library.
// Copyright (C) 2002-2024 Benjamin S. Kirk, John W. Peterson, Roy H. Stogner
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
// This library 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
// Lesser General Public License for more details.
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#include "libmesh/exodusII_io_helper.h"
#ifdef LIBMESH_HAVE_EXODUS_API
// libMesh includes
#include "libmesh/boundary_info.h"
#include "libmesh/enum_elem_type.h"
#include "libmesh/elem.h"
#include "libmesh/equation_systems.h"
#include "libmesh/fpe_disabler.h"
#include "libmesh/remote_elem.h"
#include "libmesh/system.h"
#include "libmesh/numeric_vector.h"
#include "libmesh/enum_to_string.h"
#include "libmesh/enum_elem_type.h"
#include "libmesh/int_range.h"
#include "libmesh/utility.h"
#include "libmesh/libmesh_logging.h"
#ifdef DEBUG
#include "libmesh/mesh_tools.h" // for elem_types warning
#endif
#include <libmesh/ignore_warnings.h>
namespace exII {
extern "C" {
#include "exodusII.h" // defines MAX_LINE_LENGTH, MAX_STR_LENGTH used later
}
}
#include <libmesh/restore_warnings.h>
// C++ includes
#include <algorithm>
#include <cfenv> // workaround for HDF5 bug
#include <cstdlib> // std::strtol
#include <sstream>
#include <unordered_map>
// Anonymous namespace for file local data and helper functions
namespace
{
using namespace libMesh;
// File scope constant node/edge/face mapping arrays.
// 2D inverse face map definitions.
// These take a libMesh ID and turn it into an Exodus ID
const std::vector<int> trishell3_inverse_edge_map = {3, 4, 5};
const std::vector<int> quadshell4_inverse_edge_map = {3, 4, 5, 6};
// 3D node map definitions
// The hex27, prism20-21, and tet14 appear to be the only elements
// with a non-identity mapping between Exodus' node numbering and
// libmesh's. Exodus doesn't even number prisms hierarchically!
const std::vector<int> hex27_node_map = {
// Vertex and mid-edge nodes
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
// Mid-face nodes and center node
21, 25, 24, 26, 23, 22, 20};
//20 21 22 23 24 25 26 // LibMesh indices
const std::vector<int> hex27_inverse_node_map = {
// Vertex and mid-edge nodes
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
// Mid-face nodes and center node
26, 20, 25, 24, 22, 21, 23};
//20 21 22 23 24 25 26
const std::vector<int> prism20_node_map = {
// Vertices
0, 1, 2, 3, 4, 5,
// Matching mid-edge nodes
6, 7, 8, 9, 10, 11, 12, 13, 14,
// Non-matching nodes
19, 17, 18, 15, 16};
//15 16 17 18 19 // LibMesh indices
const std::vector<int> prism20_inverse_node_map = {
// Vertices
0, 1, 2, 3, 4, 5,
// Matching mid-edge nodes
6, 7, 8, 9, 10, 11, 12, 13, 14,
// Non-matching nodes
18, 19, 16, 17, 15};
//15 16 17 18 19
const std::vector<int> prism21_node_map = {
// Vertices
0, 1, 2, 3, 4, 5,
// Matching mid-edge nodes
6, 7, 8, 9, 10, 11, 12, 13, 14,
// Non-matching nodes
20, 18, 19, 16, 17, 15};
//15 16 17 18 19 20 // LibMesh indices
const std::vector<int> prism21_inverse_node_map = {
// Vertices
0, 1, 2, 3, 4, 5,
// Matching mid-edge nodes
6, 7, 8, 9, 10, 11, 12, 13, 14,
// Non-matching nodes
20, 18, 19, 16, 17, 15};
//15 16 17 18 19 20
const std::vector<int> tet14_node_map = {
// Vertex and mid-edge nodes
0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
// Mid-face nodes
10, 13, 11, 12};
//10 11 12 13 // LibMesh indices
const std::vector<int> tet14_inverse_node_map = {
// Vertex and mid-edge nodes
0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
// Mid-face nodes
10, 12, 13, 11};
//10 11 12 13
// 3D face map definitions
const std::vector<int> tet_face_map = {1, 2, 3, 0};
const std::vector<int> hex_face_map = {1, 2, 3, 4, 0, 5};
const std::vector<int> prism_face_map = {1, 2, 3, 0, 4};
// These take a libMesh ID and turn it into an Exodus ID
const std::vector<int> tet_inverse_face_map = {4, 1, 2, 3};
const std::vector<int> hex_inverse_face_map = {5, 1, 2, 3, 4, 6};
const std::vector<int> prism_inverse_face_map = {4, 1, 2, 3, 5};
// 3D element edge maps. Map 0-based Exodus id -> libMesh id.
// Commented out until we have code that needs it, to keep compiler
// warnings happy.
// const std::vector<int> hex_edge_map =
// {0,1,2,3,8,9,10,11,4,5,7,6};
// 3D inverse element edge maps. Map libmesh edge ids to 1-based Exodus edge ids.
// Commented out until we have code that needs it, to keep compiler
// warnings happy.
// const std::vector<int> hex_inverse_edge_map =
// {1,2,3,4,9,10,12,11,5,6,7,8};
/**
* \returns The value obtained from a generic exII::ex_inquire() call.
*/
int inquire(libMesh::ExodusII_IO_Helper & e2h, exII::ex_inquiry req_info_in, std::string error_msg="")
{
int ret_int = 0;
char ret_char = 0;
float ret_float = 0.;
e2h.ex_err = exII::ex_inquire(e2h.ex_id,
req_info_in,
&ret_int,
&ret_float,
&ret_char);
EX_CHECK_ERR(e2h.ex_err, error_msg);
return ret_int;
}
// Bezier Extraction test: if we see BEx data we had better be in a
// Bezier element block
inline bool is_bezier_elem(const char * elem_type_str)
{
// Reading Bezier Extraction from Exodus files requires ExodusII v8
#if EX_API_VERS_NODOT < 800
libMesh::libmesh_ignore(elem_type_str);
return false;
#else
if (strlen(elem_type_str) <= 4)
return false;
return (std::string(elem_type_str, elem_type_str+4) == "BEX_");
#endif
}
std::map<subdomain_id_type, std::vector<unsigned int>>
build_subdomain_map(const MeshBase & mesh, bool add_sides, subdomain_id_type & subdomain_id_end)
{
std::map<subdomain_id_type, std::vector<unsigned int>> subdomain_map;
// If we've been asked to add side elements, those will go in
// their own blocks.
if (add_sides)
{
std::set<subdomain_id_type> sbd_ids;
mesh.subdomain_ids(sbd_ids);
if (!sbd_ids.empty())
subdomain_id_end = *sbd_ids.rbegin()+1;
}
// Loop through element and map between block and element vector.
for (const auto & elem : mesh.active_element_ptr_range())
{
// We skip writing infinite elements to the Exodus file, so
// don't put them in the subdomain_map. That way the number of
// blocks should be correct.
if (elem->infinite())
continue;
subdomain_map[ elem->subdomain_id() ].push_back(elem->id());
// If we've been asked to add side elements, those will go in their own
// blocks. We don't have any ids to list for elements that don't
// explicitly exist in the mesh, but we do an entry to keep
// track of the number of elements we'll add in each new block.
if (add_sides)
for (auto s : elem->side_index_range())
{
if (EquationSystems::redundant_added_side(*elem,s))
continue;
auto & marker =
subdomain_map[subdomain_id_end + elem->side_type(s)];
if (marker.empty())
marker.push_back(1);
else
++marker[0];
}
}
if (!add_sides && !subdomain_map.empty())
subdomain_id_end = subdomain_map.rbegin()->first + 1;
return subdomain_map;
}
} // end anonymous namespace
namespace libMesh
{
// ExodusII_IO_Helper::Conversion static data
const int ExodusII_IO_Helper::Conversion::invalid_id = std::numeric_limits<int>::max();
ExodusII_IO_Helper::ExodusII_IO_Helper(const ParallelObject & parent,
bool v,
bool run_only_on_proc0,
bool single_precision) :
ParallelObject(parent),
ex_id(0),
ex_err(0),
header_info(), // zero-initialize
title(header_info.title),
num_dim(header_info.num_dim),
num_nodes(header_info.num_nodes),
num_elem(header_info.num_elem),
num_elem_blk(header_info.num_elem_blk),
num_edge(header_info.num_edge),
num_edge_blk(header_info.num_edge_blk),
num_node_sets(header_info.num_node_sets),
num_side_sets(header_info.num_side_sets),
num_elem_sets(header_info.num_elem_sets),
num_global_vars(0),
num_sideset_vars(0),
num_nodeset_vars(0),
num_elemset_vars(0),
num_elem_this_blk(0),
num_nodes_per_elem(0),
num_attr(0),
num_elem_all_sidesets(0),
num_elem_all_elemsets(0),
bex_num_elem_cvs(0),
num_time_steps(0),
num_nodal_vars(0),
num_elem_vars(0),
verbose(v),
opened_for_writing(false),
opened_for_reading(false),
_run_only_on_proc0(run_only_on_proc0),
_opened_by_create(false),
_elem_vars_initialized(false),
_global_vars_initialized(false),
_nodal_vars_initialized(false),
_use_mesh_dimension_instead_of_spatial_dimension(false),
_write_hdf5(true),
_end_elem_id(0),
_write_as_dimension(0),
_single_precision(single_precision)
{
title.resize(MAX_LINE_LENGTH+1);
elem_type.resize(MAX_STR_LENGTH);
init_element_equivalence_map();
init_conversion_map();
}
ExodusII_IO_Helper::~ExodusII_IO_Helper() = default;
int ExodusII_IO_Helper::get_exodus_version()
{
return EX_API_VERS_NODOT;
}
// Initialization function for conversion_map object
void ExodusII_IO_Helper::init_conversion_map()
{
auto convert_type = [this](ElemType type,
std::string_view exodus_type,
const std::vector<int> * node_map = nullptr,
const std::vector<int> * inverse_node_map = nullptr,
const std::vector<int> * side_map = nullptr,
const std::vector<int> * inverse_side_map = nullptr,
const std::vector<int> * shellface_map = nullptr,
const std::vector<int> * inverse_shellface_map = nullptr,
size_t shellface_index_offset = 0)
{
std::unique_ptr<Elem> elem = Elem::build(type);
auto & conv = conversion_map[elem->dim()][type];
conv.libmesh_type = type;
conv.exodus_type = exodus_type;
conv.node_map = node_map;
conv.inverse_node_map = inverse_node_map;
conv.side_map = side_map;
conv.inverse_side_map = inverse_side_map;
conv.shellface_map = shellface_map;
conv.inverse_shellface_map = inverse_shellface_map;
conv.shellface_index_offset = shellface_index_offset;
conv.n_nodes = elem->n_nodes();
for (int d = elem->dim()+1; d <= 3; ++d)
conversion_map[d][type] = conv;
};
convert_type(NODEELEM, "SPHERE");
convert_type(EDGE2, "BAR2");
convert_type(EDGE3, "BAR3");
convert_type(EDGE4, "BAR4");
convert_type(QUAD4, "QUAD4");
convert_type(QUAD8, "QUAD8");
convert_type(QUAD9, "QUAD9");
convert_type(QUADSHELL4, "SHELL4", nullptr, nullptr, nullptr,
/* inverse_side_map = */ &quadshell4_inverse_edge_map,
nullptr, nullptr, /* shellface_index_offset = */ 2);
convert_type(QUADSHELL8, "SHELL8", nullptr, nullptr, nullptr,
/* inverse_side_map = */ &quadshell4_inverse_edge_map,
nullptr, nullptr, /* shellface_index_offset = */ 2);
convert_type(QUADSHELL9, "SHELL9", nullptr, nullptr, nullptr,
/* inverse_side_map = */ &quadshell4_inverse_edge_map,
nullptr, nullptr, /* shellface_index_offset = */ 2);
convert_type(TRI3, "TRI3");
convert_type(TRI6, "TRI6");
convert_type(TRI7, "TRI7");
// Exodus does weird things to triangle side mapping in 3D. See
// https://sandialabs.github.io/seacas-docs/html/element_types.html#tri
conversion_map[3][TRI3].inverse_side_map = &trishell3_inverse_edge_map;
conversion_map[3][TRI3].shellface_index_offset = 2;
conversion_map[3][TRI6].inverse_side_map = &trishell3_inverse_edge_map;
conversion_map[3][TRI6].shellface_index_offset = 2;
conversion_map[3][TRI7].inverse_side_map = &trishell3_inverse_edge_map;
conversion_map[3][TRI7].shellface_index_offset = 2;
convert_type(TRISHELL3, "TRISHELL3", nullptr, nullptr, nullptr,
/* inverse_side_map = */ &trishell3_inverse_edge_map,
nullptr, nullptr, /* shellface_index_offset = */ 2);
convert_type(TRI3SUBDIVISION, "TRI3");
convert_type(HEX8, "HEX8", nullptr, nullptr,
&hex_face_map, &hex_inverse_face_map);
convert_type(HEX20, "HEX20", nullptr, nullptr,
&hex_face_map, &hex_inverse_face_map);
convert_type(HEX27, "HEX27", &hex27_node_map,
&hex27_inverse_node_map,
&hex_face_map, &hex_inverse_face_map);
convert_type(TET4, "TETRA4", nullptr, nullptr,
&tet_face_map, &tet_inverse_face_map);
convert_type(TET10, "TETRA10", nullptr, nullptr,
&tet_face_map, &tet_inverse_face_map);
convert_type(TET14, "TETRA14", &tet14_node_map,
&tet14_inverse_node_map,
&tet_face_map, &tet_inverse_face_map);
convert_type(PRISM6, "WEDGE", nullptr, nullptr,
&prism_face_map, &prism_inverse_face_map);
convert_type(PRISM15, "WEDGE15", nullptr, nullptr,
&prism_face_map, &prism_inverse_face_map);
convert_type(PRISM18, "WEDGE18", nullptr, nullptr,
&prism_face_map, &prism_inverse_face_map);
convert_type(PRISM20, "WEDGE20", &prism20_node_map,
&prism20_inverse_node_map,
&prism_face_map, &prism_inverse_face_map);
convert_type(PRISM21, "WEDGE21", &prism21_node_map,
&prism21_inverse_node_map,
&prism_face_map, &prism_inverse_face_map);
convert_type(PYRAMID5, "PYRAMID5");
convert_type(PYRAMID13, "PYRAMID13");
convert_type(PYRAMID14, "PYRAMID14");
convert_type(PYRAMID18, "PYRAMID18");
}
// This function initializes the element_equivalence_map the first time it
// is called, and returns early all other times.
void ExodusII_IO_Helper::init_element_equivalence_map()
{
// We use an ExodusII SPHERE element to represent a NodeElem
element_equivalence_map["SPHERE"] = NODEELEM;
// EDGE2 equivalences
element_equivalence_map["EDGE"] = EDGE2;
element_equivalence_map["EDGE2"] = EDGE2;
element_equivalence_map["TRUSS"] = EDGE2;
element_equivalence_map["BEAM"] = EDGE2;
element_equivalence_map["BAR"] = EDGE2;
element_equivalence_map["TRUSS2"] = EDGE2;
element_equivalence_map["BEAM2"] = EDGE2;
element_equivalence_map["BAR2"] = EDGE2;
// EDGE3 equivalences
element_equivalence_map["EDGE3"] = EDGE3;
element_equivalence_map["TRUSS3"] = EDGE3;
element_equivalence_map["BEAM3"] = EDGE3;
element_equivalence_map["BAR3"] = EDGE3;
// EDGE4 equivalences
element_equivalence_map["EDGE4"] = EDGE4;
element_equivalence_map["TRUSS4"] = EDGE4;
element_equivalence_map["BEAM4"] = EDGE4;
element_equivalence_map["BAR4"] = EDGE4;
// This whole design is going to need to be refactored whenever we
// support higher-order IGA, with one element type having variable
// polynomiaal degree...
element_equivalence_map["BEX_CURVE"] = EDGE3;
// QUAD4 equivalences
element_equivalence_map["QUAD"] = QUAD4;
element_equivalence_map["QUAD4"] = QUAD4;
// QUADSHELL4 equivalences
element_equivalence_map["SHELL"] = QUADSHELL4;
element_equivalence_map["SHELL4"] = QUADSHELL4;
// QUAD8 equivalences
element_equivalence_map["QUAD8"] = QUAD8;
// QUADSHELL8 equivalences
element_equivalence_map["SHELL8"] = QUADSHELL8;
// QUAD9 equivalences
element_equivalence_map["QUAD9"] = QUAD9;
// This only supports p==2 IGA:
element_equivalence_map["BEX_QUAD"] = QUAD9;
// QUADSHELL9 equivalences
element_equivalence_map["SHELL9"] = QUADSHELL9;
// TRI3 equivalences
element_equivalence_map["TRI"] = TRI3;
element_equivalence_map["TRI3"] = TRI3;
element_equivalence_map["TRIANGLE"] = TRI3;
// TRISHELL3 equivalences
element_equivalence_map["TRISHELL"] = TRISHELL3;
element_equivalence_map["TRISHELL3"] = TRISHELL3;
// TRI6 equivalences
element_equivalence_map["TRI6"] = TRI6;
// element_equivalence_map["TRISHELL6"] = TRI6;
// This only supports p==2 IGA:
element_equivalence_map["BEX_TRIANGLE"] = TRI6;
// TRI7 equivalences
element_equivalence_map["TRI7"] = TRI7;
// HEX8 equivalences
element_equivalence_map["HEX"] = HEX8;
element_equivalence_map["HEX8"] = HEX8;
// HEX20 equivalences
element_equivalence_map["HEX20"] = HEX20;
// HEX27 equivalences
element_equivalence_map["HEX27"] = HEX27;
// This only supports p==2 IGA:
element_equivalence_map["BEX_HEX"] = HEX27;
// TET4 equivalences
element_equivalence_map["TETRA"] = TET4;
element_equivalence_map["TETRA4"] = TET4;
// TET10 equivalences
element_equivalence_map["TETRA10"] = TET10;
// This only supports p==2 IGA:
element_equivalence_map["BEX_TETRA"] = TET10;
// TET14 (in Exodus 8) equivalence
element_equivalence_map["TETRA14"] = TET14;
// PRISM6 equivalences
element_equivalence_map["WEDGE"] = PRISM6;
element_equivalence_map["WEDGE6"] = PRISM6;
// PRISM15 equivalences
element_equivalence_map["WEDGE15"] = PRISM15;
// PRISM18 equivalences
element_equivalence_map["WEDGE18"] = PRISM18;
// This only supports p==2 IGA:
element_equivalence_map["BEX_WEDGE"] = PRISM18;
// PRISM20 equivalences
element_equivalence_map["WEDGE20"] = PRISM20;
// PRISM21 equivalences
element_equivalence_map["WEDGE21"] = PRISM21;
// PYRAMID equivalences
element_equivalence_map["PYRAMID"] = PYRAMID5;
element_equivalence_map["PYRAMID5"] = PYRAMID5;
element_equivalence_map["PYRAMID13"] = PYRAMID13;
element_equivalence_map["PYRAMID14"] = PYRAMID14;
element_equivalence_map["PYRAMID18"] = PYRAMID18;
}
const ExodusII_IO_Helper::Conversion &
ExodusII_IO_Helper::get_conversion(const ElemType type) const
{
auto & maps_for_dim = libmesh_map_find(conversion_map, this->num_dim);
return libmesh_map_find(maps_for_dim, type);
}
const ExodusII_IO_Helper::Conversion &
ExodusII_IO_Helper::get_conversion(std::string type_str) const
{
// Do only upper-case comparisons
std::transform(type_str.begin(), type_str.end(), type_str.begin(), ::toupper);
return get_conversion (libmesh_map_find(element_equivalence_map, type_str));
}
const char * ExodusII_IO_Helper::get_elem_type() const
{
return elem_type.data();
}
void ExodusII_IO_Helper::message(std::string_view msg)
{
if (verbose) libMesh::out << msg << std::endl;
}
void ExodusII_IO_Helper::message(std::string_view msg, int i)
{
if (verbose) libMesh::out << msg << i << "." << std::endl;
}
ExodusII_IO_Helper::MappedOutputVector::
MappedOutputVector(const std::vector<Real> & our_data_in,
bool single_precision_in)
: our_data(our_data_in),
single_precision(single_precision_in)
{
if (single_precision)
{
if (sizeof(Real) != sizeof(float))
{
float_vec.resize(our_data.size());
// boost float128 demands explicit downconversions
for (std::size_t i : index_range(our_data))
float_vec[i] = float(our_data[i]);
}
}
else if (sizeof(Real) != sizeof(double))
{
double_vec.resize(our_data.size());
// boost float128 demands explicit downconversions
for (std::size_t i : index_range(our_data))
double_vec[i] = double(our_data[i]);
}
}
void *
ExodusII_IO_Helper::MappedOutputVector::data()
{
if (single_precision)
{
if (sizeof(Real) != sizeof(float))
return static_cast<void*>(float_vec.data());
}
else if (sizeof(Real) != sizeof(double))
return static_cast<void*>(double_vec.data());
// Otherwise return a (suitably casted) pointer to the original underlying data.
return const_cast<void *>(static_cast<const void *>(our_data.data()));
}
ExodusII_IO_Helper::MappedInputVector::
MappedInputVector(std::vector<Real> & our_data_in,
bool single_precision_in)
: our_data(our_data_in),
single_precision(single_precision_in)
{
// Allocate temporary space to store enough floats/doubles, if required.
if (single_precision)
{
if (sizeof(Real) != sizeof(float))
float_vec.resize(our_data.size());
}
else if (sizeof(Real) != sizeof(double))
double_vec.resize(our_data.size());
}
ExodusII_IO_Helper::MappedInputVector::
~MappedInputVector()
{
if (single_precision)
{
if (sizeof(Real) != sizeof(float))
our_data.assign(float_vec.begin(), float_vec.end());
}
else if (sizeof(Real) != sizeof(double))
our_data.assign(double_vec.begin(), double_vec.end());
}
void *
ExodusII_IO_Helper::MappedInputVector::data()
{
if (single_precision)
{
if (sizeof(Real) != sizeof(float))
return static_cast<void*>(float_vec.data());
}
else if (sizeof(Real) != sizeof(double))
return static_cast<void*>(double_vec.data());
// Otherwise return a (suitably casted) pointer to the original underlying data.
return static_cast<void *>(our_data.data());
}
void ExodusII_IO_Helper::open(const char * filename, bool read_only)
{
// Version of Exodus you are using
float ex_version = 0.;
int comp_ws = 0;
if (_single_precision)
comp_ws = cast_int<int>(sizeof(float));
// Fall back on double precision when necessary since ExodusII
// doesn't seem to support long double
else
comp_ws = cast_int<int>(std::min(sizeof(Real), sizeof(double)));
// Word size in bytes of the floating point data as they are stored
// in the ExodusII file. "If this argument is 0, the word size of the
// floating point data already stored in the file is returned"
int io_ws = 0;
{
FPEDisabler disable_fpes;
ex_id = exII::ex_open(filename,
read_only ? EX_READ : EX_WRITE,
&comp_ws,
&io_ws,
&ex_version);
}
std::string err_msg = std::string("Error opening ExodusII mesh file: ") + std::string(filename);
EX_CHECK_ERR(ex_id, err_msg);
if (verbose) libMesh::out << "File opened successfully." << std::endl;
if (read_only)
opened_for_reading = true;
else
opened_for_writing = true;
current_filename = std::string(filename);
}
ExodusHeaderInfo
ExodusII_IO_Helper::read_header() const
{
// Read init params using newer API that reads into a struct. For
// backwards compatibility, assign local member values from struct
// afterwards. Note: using the new API allows us to automatically
// read edge and face block/set information if it's present in the
// file.
exII::ex_init_params params = {};
int err_flag = exII::ex_get_init_ext(ex_id, ¶ms);
EX_CHECK_ERR(err_flag, "Error retrieving header info.");
// Extract required data into our struct
ExodusHeaderInfo h;
h.title.assign(params.title, params.title + MAX_LINE_LENGTH);
h.num_dim = params.num_dim;
h.num_nodes = params.num_nodes;
h.num_elem = params.num_elem;
h.num_elem_blk = params.num_elem_blk;
h.num_node_sets = params.num_node_sets;
h.num_side_sets = params.num_side_sets;
h.num_elem_sets = params.num_elem_sets;
h.num_edge_blk = params.num_edge_blk;
h.num_edge = params.num_edge;
// And return it
return h;
}
void ExodusII_IO_Helper::read_and_store_header_info()
{
// Read header params from file, storing them in this class's
// ExodusHeaderInfo struct. This automatically updates the local
// num_dim, num_elem, etc. references.
this->header_info = this->read_header();
// Read the number of timesteps which are present in the file
this->read_num_time_steps();
ex_err = exII::ex_get_variable_param(ex_id, exII::EX_NODAL, &num_nodal_vars);
EX_CHECK_ERR(ex_err, "Error reading number of nodal variables.");
ex_err = exII::ex_get_variable_param(ex_id, exII::EX_ELEM_BLOCK, &num_elem_vars);
EX_CHECK_ERR(ex_err, "Error reading number of elemental variables.");
ex_err = exII::ex_get_variable_param(ex_id, exII::EX_GLOBAL, &num_global_vars);
EX_CHECK_ERR(ex_err, "Error reading number of global variables.");
ex_err = exII::ex_get_variable_param(ex_id, exII::EX_SIDE_SET, &num_sideset_vars);
EX_CHECK_ERR(ex_err, "Error reading number of sideset variables.");
ex_err = exII::ex_get_variable_param(ex_id, exII::EX_NODE_SET, &num_nodeset_vars);
EX_CHECK_ERR(ex_err, "Error reading number of nodeset variables.");
ex_err = exII::ex_get_variable_param(ex_id, exII::EX_ELEM_SET, &num_elemset_vars);
EX_CHECK_ERR(ex_err, "Error reading number of elemset variables.");
message("Exodus header info retrieved successfully.");
}
void ExodusII_IO_Helper::read_qa_records()
{
// The QA records are four MAX_STR_LENGTH-byte character strings.
int num_qa_rec =
inquire(*this, exII::EX_INQ_QA, "Error retrieving number of QA records");
if (verbose)
libMesh::out << "Found "
<< num_qa_rec
<< " QA record(s) in the Exodus file."
<< std::endl;
if (num_qa_rec > 0)
{
// Actual (num_qa_rec x 4) storage for strings. The object we
// pass to the Exodus API will just contain pointers into the
// qa_storage object, which will have all automatic memory
// management.
std::vector<std::vector<std::vector<char>>> qa_storage(num_qa_rec);
for (auto i : make_range(num_qa_rec))
{
qa_storage[i].resize(4);
for (auto j : make_range(4))
qa_storage[i][j].resize(MAX_STR_LENGTH+1);
}
// inner_array_t is a fixed-size array of 4 strings
typedef char * inner_array_t[4];
// There is at least one compiler (Clang 12.0.1) that complains about
// "a non-scalar type used in a pseudo-destructor expression" when
// we try to instantiate a std::vector of inner_array_t objects as in:
// std::vector<inner_array_t> qa_record(num_qa_rec);
// So, we instead attempt to achieve the same effect with a std::unique_ptr.
auto qa_record = std::make_unique<inner_array_t[]>(num_qa_rec);
// Create data structure to be passed to Exodus API by setting
// pointers to the actual strings which are in qa_storage.
for (auto i : make_range(num_qa_rec))
for (auto j : make_range(4))
qa_record[i][j] = qa_storage[i][j].data();
ex_err = exII::ex_get_qa (ex_id, qa_record.get());
EX_CHECK_ERR(ex_err, "Error reading the QA records.");
// Print the QA records
if (verbose)
{
for (auto i : make_range(num_qa_rec))
{
libMesh::out << "QA Record: " << i << std::endl;
for (auto j : make_range(4))
libMesh::out << qa_record[i][j] << std::endl;
}
}
}
}
void ExodusII_IO_Helper::print_header()
{
if (verbose)
libMesh::out << "Title: \t" << title.data() << std::endl
<< "Mesh Dimension: \t" << num_dim << std::endl
<< "Number of Nodes: \t" << num_nodes << std::endl
<< "Number of elements: \t" << num_elem << std::endl
<< "Number of elt blocks: \t" << num_elem_blk << std::endl
<< "Number of node sets: \t" << num_node_sets << std::endl
<< "Number of side sets: \t" << num_side_sets << std::endl
<< "Number of elem sets: \t" << num_elem_sets << std::endl;
}
void ExodusII_IO_Helper::read_nodes()
{
LOG_SCOPE("read_nodes()", "ExodusII_IO_Helper");
x.resize(num_nodes);
y.resize(num_nodes);
z.resize(num_nodes);
if (num_nodes)
{
ex_err = exII::ex_get_coord
(ex_id,
MappedInputVector(x, _single_precision).data(),
MappedInputVector(y, _single_precision).data(),
MappedInputVector(z, _single_precision).data());
EX_CHECK_ERR(ex_err, "Error retrieving nodal data.");
message("Nodal data retrieved successfully.");
}
// If a nodal attribute bex_weight exists, we get spline weights
// from it
int n_nodal_attr = 0;
ex_err = exII::ex_get_attr_param(ex_id, exII::EX_NODAL, 0, & n_nodal_attr);
EX_CHECK_ERR(ex_err, "Error getting number of nodal attributes.");
if (n_nodal_attr > 0)
{
std::vector<std::vector<char>> attr_name_data
(n_nodal_attr, std::vector<char>(MAX_STR_LENGTH + 1));
std::vector<char *> attr_names(n_nodal_attr);
for (auto i : index_range(attr_names))
attr_names[i] = attr_name_data[i].data();
ex_err = exII::ex_get_attr_names(ex_id, exII::EX_NODAL, 0, attr_names.data());
EX_CHECK_ERR(ex_err, "Error getting nodal attribute names.");
for (auto i : index_range(attr_names))
if (std::string("bex_weight") == attr_names[i])
{
w.resize(num_nodes);
ex_err =
exII::ex_get_one_attr (ex_id, exII::EX_NODAL, 0, i+1,
MappedInputVector(w, _single_precision).data());
EX_CHECK_ERR(ex_err, "Error getting Bezier Extraction nodal weights");
}
}
}
void ExodusII_IO_Helper::read_node_num_map ()
{
node_num_map.resize(num_nodes);
// Note: we cannot use the exII::ex_get_num_map() here because it
// (apparently) does not behave like ex_get_node_num_map() when
// there is no node number map in the file: it throws an error
// instead of returning a default identity array (1,2,3,...).
ex_err = exII::ex_get_node_num_map
(ex_id, node_num_map.empty() ? nullptr : node_num_map.data());
EX_CHECK_ERR(ex_err, "Error retrieving nodal number map.");
message("Nodal numbering map retrieved successfully.");
if (verbose)
{
libMesh::out << "[" << this->processor_id() << "] node_num_map[i] = ";
for (unsigned int i=0; i<static_cast<unsigned int>(std::min(10, num_nodes-1)); ++i)
libMesh::out << node_num_map[i] << ", ";
libMesh::out << "... " << node_num_map.back() << std::endl;
}
}
void ExodusII_IO_Helper::read_bex_cv_blocks()
{
// If a bex blob exists, we look for Bezier Extraction coefficient
// data there.
// These APIs require newer Exodus than 5.22
#if EX_API_VERS_NODOT >= 800
int n_blobs = exII::ex_inquire_int(ex_id, exII::EX_INQ_BLOB);
if (n_blobs > 0)
{
std::vector<exII::ex_blob> blobs(n_blobs);
std::vector<std::vector<char>> blob_names(n_blobs);
for (auto i : make_range(n_blobs))
{
blob_names[i].resize(MAX_STR_LENGTH+1);
blobs[i].name = blob_names[i].data();
}
ex_err = exII::ex_get_blobs(ex_id, blobs.data());
EX_CHECK_ERR(ex_err, "Error getting blobs.");
bool found_blob = false;
const exII::ex_blob * my_blob = &blobs[0];
for (const auto & blob : blobs)
{
if (std::string("bex_cv_blob") == blob.name)
{
found_blob = true;
my_blob = &blob;
}
}
if (!found_blob)
libmesh_error_msg("Found no bex_cv_blob for bezier elements");
const int n_blob_attr =
exII::ex_get_attribute_count(ex_id, exII::EX_BLOB,
my_blob->id);
std::vector<exII::ex_attribute> attributes(n_blob_attr);
ex_err = exII::ex_get_attribute_param(ex_id, exII::EX_BLOB,
my_blob->id,
attributes.data());
EX_CHECK_ERR(ex_err, "Error getting bex blob attribute parameters.");
int bex_num_dense_cv_blocks = 0;
std::vector<int> bex_dense_cv_info;
for (auto & attr : attributes)
{
if (std::string("bex_dense_cv_info") == attr.name)
{
const std::size_t value_count = attr.value_count;
if (value_count % 2)
libmesh_error_msg("Found odd number of bex_dense_cv_info");
bex_dense_cv_info.resize(value_count);
attr.values = bex_dense_cv_info.data();
exII::ex_get_attribute(ex_id, &attr);
bex_num_dense_cv_blocks = value_count / 2;
libmesh_error_msg_if(bex_num_dense_cv_blocks > 1,
"Found more than 1 dense bex CV block; unsure how to handle that");
}
}
if (bex_dense_cv_info.empty())
libmesh_error_msg("No bex_dense_cv_info found");
int n_blob_vars;
exII::ex_get_variable_param(ex_id, exII::EX_BLOB, &n_blob_vars);
std::vector<char> var_name (MAX_STR_LENGTH + 1);
for (auto v_id : make_range(1,n_blob_vars+1))
{
ex_err = exII::ex_get_variable_name(ex_id, exII::EX_BLOB, v_id, var_name.data());
EX_CHECK_ERR(ex_err, "Error reading bex blob var name.");
if (std::string("bex_dense_cv_blocks") == var_name.data())
{