-
Notifications
You must be signed in to change notification settings - Fork 281
/
nemesis_io_helper.C
2618 lines (2062 loc) · 102 KB
/
nemesis_io_helper.C
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-2018 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
// C++ headers
#include <iomanip>
#include <set>
#include <sstream>
// Libmesh headers
#include "libmesh/nemesis_io_helper.h"
#include "libmesh/node.h"
#include "libmesh/elem.h"
#include "libmesh/boundary_info.h"
#include "libmesh/parallel.h"
#include "libmesh/mesh_base.h"
#include "libmesh/numeric_vector.h"
#if defined(LIBMESH_HAVE_NEMESIS_API) && defined(LIBMESH_HAVE_EXODUS_API)
namespace libMesh
{
// Initialize the various integer members to zero. We can check
// these later to see if they've been properly initialized...
// The parent ExodusII_IO_Helper is created with the run_only_on_proc0
// flag set to false, so that we can make use of its functionality
// on multiple processors.
Nemesis_IO_Helper::Nemesis_IO_Helper(const ParallelObject & parent,
bool verbose_in, bool single_precision) :
ExodusII_IO_Helper(parent, verbose_in, /*run_only_on_proc0=*/false, /*single_precision=*/single_precision),
nemesis_err_flag(0),
num_nodes_global(0),
num_elems_global(0),
num_elem_blks_global(0),
num_node_sets_global(0),
num_side_sets_global(0),
num_proc(0),
num_proc_in_file(0),
ftype('\0'),
num_internal_nodes(0),
num_border_nodes(0),
num_external_nodes(0),
num_internal_elems(0),
num_border_elems(0),
num_node_cmaps(0),
num_elem_cmaps(0)
{
// Warn about using untested code!
libmesh_experimental();
}
Nemesis_IO_Helper::~Nemesis_IO_Helper()
{
// Our destructor is called from Nemesis_IO. We close the Exodus file here since we have
// responsibility for managing the file's lifetime. Only call ex_update() if the file was
// opened for writing!
if (this->opened_for_writing)
{
this->ex_err = exII::ex_update(this->ex_id);
EX_EXCEPTIONLESS_CHECK_ERR(ex_err, "Error flushing buffers to file.");
}
this->close();
}
void Nemesis_IO_Helper::get_init_global()
{
nemesis_err_flag =
Nemesis::ne_get_init_global(ex_id,
&num_nodes_global,
&num_elems_global,
&num_elem_blks_global,
&num_node_sets_global,
&num_side_sets_global);
EX_CHECK_ERR(nemesis_err_flag, "Error reading initial global data!");
if (verbose)
{
libMesh::out << "[" << this->processor_id() << "] " << "num_nodes_global=" << num_nodes_global << std::endl;
libMesh::out << "[" << this->processor_id() << "] " << "num_elems_global=" << num_elems_global << std::endl;
libMesh::out << "[" << this->processor_id() << "] " << "num_elem_blks_global=" << num_elem_blks_global << std::endl;
libMesh::out << "[" << this->processor_id() << "] " << "num_node_sets_global=" << num_node_sets_global << std::endl;
libMesh::out << "[" << this->processor_id() << "] " << "num_side_sets_global=" << num_side_sets_global << std::endl;
}
}
void Nemesis_IO_Helper::get_ss_param_global()
{
if (num_side_sets_global > 0)
{
global_sideset_ids.resize(num_side_sets_global);
num_global_side_counts.resize(num_side_sets_global);
// df = "distribution factor", not really sure what that is. I don't yet have a file
// which has distribution factors so I guess we'll worry about it later...
num_global_side_df_counts.resize(num_side_sets_global);
nemesis_err_flag =
Nemesis::ne_get_ss_param_global(ex_id,
&global_sideset_ids[0],
&num_global_side_counts[0],
&num_global_side_df_counts[0]);
EX_CHECK_ERR(nemesis_err_flag, "Error reading global sideset parameters!");
if (verbose)
{
libMesh::out << "[" << this->processor_id() << "] " << "Global Sideset IDs, Side Counts, and DF counts:" << std::endl;
for (std::size_t bn=0; bn<global_sideset_ids.size(); ++bn)
{
libMesh::out << " [" << this->processor_id() << "] "
<< "global_sideset_ids["<<bn<<"]=" << global_sideset_ids[bn]
<< ", num_global_side_counts["<<bn<<"]=" << num_global_side_counts[bn]
<< ", num_global_side_df_counts["<<bn<<"]=" << num_global_side_df_counts[bn]
<< std::endl;
}
}
}
}
void Nemesis_IO_Helper::get_ns_param_global()
{
if (num_node_sets_global > 0)
{
global_nodeset_ids.resize(num_node_sets_global);
num_global_node_counts.resize(num_node_sets_global);
num_global_node_df_counts.resize(num_node_sets_global);
nemesis_err_flag =
Nemesis::ne_get_ns_param_global(ex_id,
&global_nodeset_ids[0],
&num_global_node_counts[0],
&num_global_node_df_counts[0]);
EX_CHECK_ERR(nemesis_err_flag, "Error reading global nodeset parameters!");
if (verbose)
{
libMesh::out << "[" << this->processor_id() << "] " << "Global Nodeset IDs, Node Counts, and DF counts:" << std::endl;
for (std::size_t bn=0; bn<global_nodeset_ids.size(); ++bn)
{
libMesh::out << " [" << this->processor_id() << "] "
<< "global_nodeset_ids["<<bn<<"]=" << global_nodeset_ids[bn]
<< ", num_global_node_counts["<<bn<<"]=" << num_global_node_counts[bn]
<< ", num_global_node_df_counts["<<bn<<"]=" << num_global_node_df_counts[bn]
<< std::endl;
}
}
}
}
void Nemesis_IO_Helper::get_eb_info_global()
{
global_elem_blk_ids.resize(num_elem_blks_global);
global_elem_blk_cnts.resize(num_elem_blks_global);
if (num_elem_blks_global > 0)
{
nemesis_err_flag =
Nemesis::ne_get_eb_info_global(ex_id,
&global_elem_blk_ids[0],
&global_elem_blk_cnts[0]);
EX_CHECK_ERR(nemesis_err_flag, "Error reading global element block info!");
}
if (verbose)
{
libMesh::out << "[" << this->processor_id() << "] " << "Global Element Block IDs and Counts:" << std::endl;
for (std::size_t bn=0; bn<global_elem_blk_ids.size(); ++bn)
{
libMesh::out << " [" << this->processor_id() << "] "
<< "global_elem_blk_ids["<<bn<<"]=" << global_elem_blk_ids[bn]
<< ", global_elem_blk_cnts["<<bn<<"]=" << global_elem_blk_cnts[bn]
<< std::endl;
}
}
}
void Nemesis_IO_Helper::get_init_info()
{
nemesis_err_flag =
Nemesis::ne_get_init_info(ex_id,
&num_proc,
&num_proc_in_file,
&ftype);
EX_CHECK_ERR(nemesis_err_flag, "Error reading initial info!");
if (verbose)
{
libMesh::out << "[" << this->processor_id() << "] " << "num_proc=" << num_proc << std::endl;
libMesh::out << "[" << this->processor_id() << "] " << "num_proc_in_file=" << num_proc_in_file << std::endl;
libMesh::out << "[" << this->processor_id() << "] " << "ftype=" << ftype << std::endl;
}
}
void Nemesis_IO_Helper::get_loadbal_param()
{
nemesis_err_flag =
Nemesis::ne_get_loadbal_param(ex_id,
&num_internal_nodes,
&num_border_nodes,
&num_external_nodes,
&num_internal_elems,
&num_border_elems,
&num_node_cmaps,
&num_elem_cmaps,
this->processor_id() // The ID of the processor for which info is to be read
);
EX_CHECK_ERR(nemesis_err_flag, "Error reading load balance parameters!");
if (verbose)
{
libMesh::out << "[" << this->processor_id() << "] " << "num_internal_nodes=" << num_internal_nodes << std::endl;
libMesh::out << "[" << this->processor_id() << "] " << "num_border_nodes=" << num_border_nodes << std::endl;
libMesh::out << "[" << this->processor_id() << "] " << "num_external_nodes=" << num_external_nodes << std::endl;
libMesh::out << "[" << this->processor_id() << "] " << "num_internal_elems=" << num_internal_elems << std::endl;
libMesh::out << "[" << this->processor_id() << "] " << "num_border_elems=" << num_border_elems << std::endl;
libMesh::out << "[" << this->processor_id() << "] " << "num_node_cmaps=" << num_node_cmaps << std::endl;
libMesh::out << "[" << this->processor_id() << "] " << "num_elem_cmaps=" << num_elem_cmaps << std::endl;
}
}
void Nemesis_IO_Helper::get_elem_map()
{
elem_mapi.resize(num_internal_elems);
elem_mapb.resize(num_border_elems);
nemesis_err_flag =
Nemesis::ne_get_elem_map(ex_id,
elem_mapi.empty() ? libmesh_nullptr : &elem_mapi[0],
elem_mapb.empty() ? libmesh_nullptr : &elem_mapb[0],
this->processor_id()
);
EX_CHECK_ERR(nemesis_err_flag, "Error reading element maps!");
if (verbose)
{
libMesh::out << "[" << this->processor_id() << "] elem_mapi[i] = ";
for (unsigned int i=0; i<static_cast<unsigned int>(num_internal_elems-1); ++i)
libMesh::out << elem_mapi[i] << ", ";
libMesh::out << "... " << elem_mapi.back() << std::endl;
libMesh::out << "[" << this->processor_id() << "] elem_mapb[i] = ";
for (unsigned int i=0; i<static_cast<unsigned int>(std::min(10, num_border_elems-1)); ++i)
libMesh::out << elem_mapb[i] << ", ";
libMesh::out << "... " << elem_mapb.back() << std::endl;
}
}
void Nemesis_IO_Helper::get_node_map()
{
node_mapi.resize(num_internal_nodes);
node_mapb.resize(num_border_nodes);
node_mape.resize(num_external_nodes);
nemesis_err_flag =
Nemesis::ne_get_node_map(ex_id,
node_mapi.empty() ? libmesh_nullptr : &node_mapi[0],
node_mapb.empty() ? libmesh_nullptr : &node_mapb[0],
node_mape.empty() ? libmesh_nullptr : &node_mape[0],
this->processor_id()
);
EX_CHECK_ERR(nemesis_err_flag, "Error reading node maps!");
if (verbose)
{
// Remark: The Exodus/Nemesis node numbering is always (?) 1-based! This means the first interior node id will
// always be == 1.
libMesh::out << "[" << this->processor_id() << "] " << "first interior node id=" << node_mapi[0] << std::endl;
libMesh::out << "[" << this->processor_id() << "] " << "last interior node id=" << node_mapi.back() << std::endl;
libMesh::out << "[" << this->processor_id() << "] " << "first boundary node id=" << node_mapb[0] << std::endl;
libMesh::out << "[" << this->processor_id() << "] " << "last boundary node id=" << node_mapb.back() << std::endl;
// The number of external nodes is sometimes zero, don't try to access
// node_mape.back() in this case!
if (num_external_nodes > 0)
{
libMesh::out << "[" << this->processor_id() << "] " << "first external node id=" << node_mape[0] << std::endl;
libMesh::out << "[" << this->processor_id() << "] " << "last external node id=" << node_mape.back() << std::endl;
}
}
}
void Nemesis_IO_Helper::get_cmap_params()
{
node_cmap_ids.resize(num_node_cmaps);
node_cmap_node_cnts.resize(num_node_cmaps);
elem_cmap_ids.resize(num_elem_cmaps);
elem_cmap_elem_cnts.resize(num_elem_cmaps);
nemesis_err_flag =
Nemesis::ne_get_cmap_params(ex_id,
node_cmap_ids.empty() ? libmesh_nullptr : &node_cmap_ids[0],
node_cmap_node_cnts.empty() ? libmesh_nullptr : &node_cmap_node_cnts[0],
elem_cmap_ids.empty() ? libmesh_nullptr : &elem_cmap_ids[0],
elem_cmap_elem_cnts.empty() ? libmesh_nullptr : &elem_cmap_elem_cnts[0],
this->processor_id());
EX_CHECK_ERR(nemesis_err_flag, "Error reading cmap parameters!");
if (verbose)
{
libMesh::out << "[" << this->processor_id() << "] ";
for (std::size_t i=0; i<node_cmap_ids.size(); ++i)
libMesh::out << "node_cmap_ids["<<i<<"]=" << node_cmap_ids[i] << " ";
libMesh::out << std::endl;
libMesh::out << "[" << this->processor_id() << "] ";
for (std::size_t i=0; i<node_cmap_node_cnts.size(); ++i)
libMesh::out << "node_cmap_node_cnts["<<i<<"]=" << node_cmap_node_cnts[i] << " ";
libMesh::out << std::endl;
libMesh::out << "[" << this->processor_id() << "] ";
for (std::size_t i=0; i<elem_cmap_ids.size(); ++i)
libMesh::out << "elem_cmap_ids["<<i<<"]=" << elem_cmap_ids[i] << " ";
libMesh::out << std::endl;
libMesh::out << "[" << this->processor_id() << "] ";
for (std::size_t i=0; i<elem_cmap_elem_cnts.size(); ++i)
libMesh::out << "elem_cmap_elem_cnts["<<i<<"]=" << elem_cmap_elem_cnts[i] << " ";
libMesh::out << std::endl;
}
}
void Nemesis_IO_Helper::get_node_cmap()
{
node_cmap_node_ids.resize(num_node_cmaps);
node_cmap_proc_ids.resize(num_node_cmaps);
for (std::size_t i=0; i<node_cmap_node_ids.size(); ++i)
{
node_cmap_node_ids[i].resize(node_cmap_node_cnts[i]);
node_cmap_proc_ids[i].resize(node_cmap_node_cnts[i]);
// Don't call ne_get_node_cmap() if there is nothing there to
// get, Nemesis throws an error in this case.
if (node_cmap_node_cnts[i] > 0)
{
nemesis_err_flag =
Nemesis::ne_get_node_cmap(ex_id,
node_cmap_ids[i],
&node_cmap_node_ids[i][0],
&node_cmap_proc_ids[i][0],
this->processor_id());
EX_CHECK_ERR(nemesis_err_flag, "Error reading node cmap node and processor ids!");
}
if (verbose)
{
libMesh::out << "[" << this->processor_id() << "] node_cmap_node_ids["<<i<<"]=";
for (std::size_t j=0; j<node_cmap_node_ids[i].size(); ++j)
libMesh::out << node_cmap_node_ids[i][j] << " ";
libMesh::out << std::endl;
// This is basically a vector, all entries of which are = node_cmap_ids[i]
// Not sure if it's always guaranteed to be that or what...
libMesh::out << "[" << this->processor_id() << "] node_cmap_proc_ids["<<i<<"]=";
for (std::size_t j=0; j<node_cmap_proc_ids[i].size(); ++j)
libMesh::out << node_cmap_proc_ids[i][j] << " ";
libMesh::out << std::endl;
}
}
}
void Nemesis_IO_Helper::get_elem_cmap()
{
elem_cmap_elem_ids.resize(num_elem_cmaps);
elem_cmap_side_ids.resize(num_elem_cmaps);
elem_cmap_proc_ids.resize(num_elem_cmaps);
for (std::size_t i=0; i<elem_cmap_elem_ids.size(); ++i)
{
elem_cmap_elem_ids[i].resize(elem_cmap_elem_cnts[i]);
elem_cmap_side_ids[i].resize(elem_cmap_elem_cnts[i]);
elem_cmap_proc_ids[i].resize(elem_cmap_elem_cnts[i]);
if (elem_cmap_elem_cnts[i] > 0)
{
nemesis_err_flag =
Nemesis::ne_get_elem_cmap(ex_id,
elem_cmap_ids[i],
&elem_cmap_elem_ids[i][0],
&elem_cmap_side_ids[i][0],
&elem_cmap_proc_ids[i][0],
this->processor_id());
EX_CHECK_ERR(nemesis_err_flag, "Error reading elem cmap elem, side, and processor ids!");
}
if (verbose)
{
libMesh::out << "[" << this->processor_id() << "] elem_cmap_elem_ids["<<i<<"]=";
for (std::size_t j=0; j<elem_cmap_elem_ids[i].size(); ++j)
libMesh::out << elem_cmap_elem_ids[i][j] << " ";
libMesh::out << std::endl;
// These must be the (local) side IDs (in the ExodusII face numbering scheme)
// of the sides shared across processors.
libMesh::out << "[" << this->processor_id() << "] elem_cmap_side_ids["<<i<<"]=";
for (std::size_t j=0; j<elem_cmap_side_ids[i].size(); ++j)
libMesh::out << elem_cmap_side_ids[i][j] << " ";
libMesh::out << std::endl;
// This is basically a vector, all entries of which are = elem_cmap_ids[i]
// Not sure if it's always guaranteed to be that or what...
libMesh::out << "[" << this->processor_id() << "] elem_cmap_proc_ids["<<i<<"]=";
for (std::size_t j=0; j<elem_cmap_proc_ids[i].size(); ++j)
libMesh::out << elem_cmap_proc_ids[i][j] << " ";
libMesh::out << std::endl;
}
}
}
void Nemesis_IO_Helper::put_init_info(unsigned num_proc_in,
unsigned num_proc_in_file_in,
const char * ftype_in)
{
nemesis_err_flag =
Nemesis::ne_put_init_info(ex_id,
num_proc_in,
num_proc_in_file_in,
const_cast<char *>(ftype_in));
EX_CHECK_ERR(nemesis_err_flag, "Error writing initial information!");
}
void Nemesis_IO_Helper::put_init_global(dof_id_type num_nodes_global_in,
dof_id_type num_elems_global_in,
unsigned num_elem_blks_global_in,
unsigned num_node_sets_global_in,
unsigned num_side_sets_global_in)
{
nemesis_err_flag =
Nemesis::ne_put_init_global(ex_id,
num_nodes_global_in,
num_elems_global_in,
num_elem_blks_global_in,
num_node_sets_global_in,
num_side_sets_global_in);
EX_CHECK_ERR(nemesis_err_flag, "Error writing initial global data!");
}
void Nemesis_IO_Helper::put_eb_info_global(std::vector<int> & global_elem_blk_ids_in,
std::vector<int> & global_elem_blk_cnts_in)
{
nemesis_err_flag =
Nemesis::ne_put_eb_info_global(ex_id,
&global_elem_blk_ids_in[0],
&global_elem_blk_cnts_in[0]);
EX_CHECK_ERR(nemesis_err_flag, "Error writing global element block information!");
}
void Nemesis_IO_Helper::put_ns_param_global(std::vector<int> & global_nodeset_ids_in,
std::vector<int> & num_global_node_counts_in,
std::vector<int> & num_global_node_df_counts_in)
{
// Only add nodesets if there are some
if (global_nodeset_ids.size())
{
nemesis_err_flag =
Nemesis::ne_put_ns_param_global(ex_id,
&global_nodeset_ids_in[0],
&num_global_node_counts_in[0],
&num_global_node_df_counts_in[0]);
}
EX_CHECK_ERR(nemesis_err_flag, "Error writing global nodeset parameters!");
}
void Nemesis_IO_Helper::put_ss_param_global(std::vector<int> & global_sideset_ids_in,
std::vector<int> & num_global_side_counts_in,
std::vector<int> & num_global_side_df_counts_in)
{
// Only add sidesets if there are some
if (global_sideset_ids.size())
{
nemesis_err_flag =
Nemesis::ne_put_ss_param_global(ex_id,
&global_sideset_ids_in[0],
&num_global_side_counts_in[0],
&num_global_side_df_counts_in[0]);
}
EX_CHECK_ERR(nemesis_err_flag, "Error writing global sideset parameters!");
}
void Nemesis_IO_Helper::put_loadbal_param(unsigned num_internal_nodes_in,
unsigned num_border_nodes_in,
unsigned num_external_nodes_in,
unsigned num_internal_elems_in,
unsigned num_border_elems_in,
unsigned num_node_cmaps_in,
unsigned num_elem_cmaps_in)
{
nemesis_err_flag =
Nemesis::ne_put_loadbal_param(ex_id,
num_internal_nodes_in,
num_border_nodes_in,
num_external_nodes_in,
num_internal_elems_in,
num_border_elems_in,
num_node_cmaps_in,
num_elem_cmaps_in,
this->processor_id());
EX_CHECK_ERR(nemesis_err_flag, "Error writing loadbal parameters!");
}
void Nemesis_IO_Helper::put_cmap_params(std::vector<int> & node_cmap_ids_in,
std::vector<int> & node_cmap_node_cnts_in,
std::vector<int> & elem_cmap_ids_in,
std::vector<int> & elem_cmap_elem_cnts_in)
{
// We might not have cmaps on every processor in some corner
// cases
if (node_cmap_ids.size())
{
nemesis_err_flag =
Nemesis::ne_put_cmap_params(ex_id,
&node_cmap_ids_in[0],
&node_cmap_node_cnts_in[0],
&elem_cmap_ids_in[0],
&elem_cmap_elem_cnts_in[0],
this->processor_id());
}
EX_CHECK_ERR(nemesis_err_flag, "Error writing cmap parameters!");
}
void Nemesis_IO_Helper::put_node_cmap(std::vector<std::vector<int>> & node_cmap_node_ids_in,
std::vector<std::vector<int>> & node_cmap_proc_ids_in)
{
// Print to screen what we are about to print to Nemesis file
if (verbose)
{
for (std::size_t i=0; i<node_cmap_node_ids_in.size(); ++i)
{
libMesh::out << "[" << this->processor_id() << "] put_node_cmap() : nodes communicated to proc "
<< this->node_cmap_ids[i]
<< " = ";
for (std::size_t j=0; j<node_cmap_node_ids_in[i].size(); ++j)
libMesh::out << node_cmap_node_ids_in[i][j] << " ";
libMesh::out << std::endl;
}
for (std::size_t i=0; i<node_cmap_node_ids_in.size(); ++i)
{
libMesh::out << "[" << this->processor_id() << "] put_node_cmap() : processor IDs = ";
for (std::size_t j=0; j<node_cmap_proc_ids_in[i].size(); ++j)
libMesh::out << node_cmap_proc_ids_in[i][j] << " ";
libMesh::out << std::endl;
}
}
for (std::size_t i=0; i<node_cmap_node_ids_in.size(); ++i)
{
nemesis_err_flag =
Nemesis::ne_put_node_cmap(ex_id,
this->node_cmap_ids[i],
&node_cmap_node_ids_in[i][0],
&node_cmap_proc_ids_in[i][0],
this->processor_id());
EX_CHECK_ERR(nemesis_err_flag, "Error writing node communication map to file!");
}
}
void Nemesis_IO_Helper::put_node_map(std::vector<int> & node_mapi_in,
std::vector<int> & node_mapb_in,
std::vector<int> & node_mape_in)
{
nemesis_err_flag =
Nemesis::ne_put_node_map(ex_id,
node_mapi_in.empty() ? libmesh_nullptr : &node_mapi_in[0],
node_mapb_in.empty() ? libmesh_nullptr : &node_mapb_in[0],
node_mape_in.empty() ? libmesh_nullptr : &node_mape_in[0], // Don't take address of empty vector...
this->processor_id());
EX_CHECK_ERR(nemesis_err_flag, "Error writing Nemesis internal and border node maps to file!");
}
void Nemesis_IO_Helper::put_elem_cmap(std::vector<std::vector<int>> & elem_cmap_elem_ids_in,
std::vector<std::vector<int>> & elem_cmap_side_ids_in,
std::vector<std::vector<int>> & elem_cmap_proc_ids_in)
{
for (std::size_t i=0; i<elem_cmap_ids.size(); ++i)
{
nemesis_err_flag =
Nemesis::ne_put_elem_cmap(ex_id,
this->elem_cmap_ids[i],
&elem_cmap_elem_ids_in[i][0],
&elem_cmap_side_ids_in[i][0],
&elem_cmap_proc_ids_in[i][0],
this->processor_id());
EX_CHECK_ERR(nemesis_err_flag, "Error writing elem communication map to file!");
}
}
void Nemesis_IO_Helper::put_elem_map(std::vector<int> & elem_mapi_in,
std::vector<int> & elem_mapb_in)
{
nemesis_err_flag =
Nemesis::ne_put_elem_map(ex_id,
elem_mapi_in.empty() ? libmesh_nullptr : &elem_mapi_in[0],
elem_mapb_in.empty() ? libmesh_nullptr : &elem_mapb_in[0],
this->processor_id());
EX_CHECK_ERR(nemesis_err_flag, "Error writing Nemesis internal and border element maps to file!");
}
void Nemesis_IO_Helper::put_n_coord(unsigned start_node_num,
unsigned num_nodes_in,
std::vector<Real> & x_coor,
std::vector<Real> & y_coor,
std::vector<Real> & z_coor)
{
nemesis_err_flag =
Nemesis::ne_put_n_coord(ex_id,
start_node_num,
num_nodes_in,
&x_coor[0],
&y_coor[0],
&z_coor[0]);
EX_CHECK_ERR(nemesis_err_flag, "Error writing coords to file!");
}
// Note: we can't reuse the ExodusII_IO_Helper code directly, since it only runs
// on processor 0. TODO: We could have the body of this function as a separate
// function and then ExodusII_IO_Helper would only call it if on processor 0...
void Nemesis_IO_Helper::create(std::string filename)
{
// Fall back on double precision when necessary since ExodusII
// doesn't seem to support long double
int
comp_ws = 0,
io_ws = 0;
if (_single_precision)
{
comp_ws = sizeof(float);
io_ws = sizeof(float);
}
else
{
comp_ws = cast_int<int>(std::min(sizeof(Real), sizeof(double)));
io_ws = cast_int<int>(std::min(sizeof(Real), sizeof(double)));
}
this->ex_id = exII::ex_create(filename.c_str(), EX_CLOBBER, &comp_ws, &io_ws);
EX_CHECK_ERR(ex_id, "Error creating Nemesis mesh file.");
if (verbose)
libMesh::out << "File created successfully." << std::endl;
this->opened_for_writing = true;
}
void Nemesis_IO_Helper::initialize(std::string title_in, const MeshBase & mesh, bool /*use_discontinuous*/)
{
// Make sure that the reference passed in is really a DistributedMesh
// const DistributedMesh & pmesh = cast_ref<const DistributedMesh &>(mesh);
const MeshBase & pmesh = mesh;
// According to Nemesis documentation, first call when writing should be to
// ne_put_init_info(). Our reader doesn't actually call this, but we should
// strive to be as close to a normal nemesis file as possible...
this->put_init_info(this->n_processors(), 1, "p");
// Gather global "initial" information for Nemesis. This consists of
// three parts labeled I, II, and III below...
//
// I.) Need to compute the number of global element blocks. To be consistent with
// Exodus, we also incorrectly associate the number of element blocks with the
// number of libmesh subdomains...
//
this->compute_num_global_elem_blocks(pmesh);
//
// II.) Determine the global number of nodesets by communication.
// This code relies on BoundaryInfo storing side and node
// boundary IDs separately at the time they are added to the
// BoundaryInfo object.
//
this->compute_num_global_nodesets(pmesh);
//
// III.) Need to compute the global number of sidesets by communication:
// This code relies on BoundaryInfo storing side and node
// boundary IDs separately at the time they are added to the
// BoundaryInfo object.
//
this->compute_num_global_sidesets(pmesh);
// Now write the global data obtained in steps I, II, and III to the Nemesis file
this->put_init_global(pmesh.parallel_n_nodes(),
pmesh.parallel_n_elem(),
this->num_elem_blks_global, /* I. */
this->num_node_sets_global, /* II. */
this->num_side_sets_global /* III. */
);
// Next, we'll write global element block information to the file. This was already
// gathered in step I. above
this->put_eb_info_global(this->global_elem_blk_ids,
this->global_elem_blk_cnts);
// Next, write global nodeset information to the file. This was already gathered in
// step II. above.
this->num_global_node_df_counts.clear();
this->num_global_node_df_counts.resize(this->global_nodeset_ids.size()); // distribution factors all zero...
this->put_ns_param_global(this->global_nodeset_ids,
this->num_global_node_counts,
this->num_global_node_df_counts);
// Next, write global sideset information to the file. This was already gathered in
// step III. above.
this->num_global_side_df_counts.clear();
this->num_global_side_df_counts.resize(this->global_sideset_ids.size()); // distribution factors all zero...
this->put_ss_param_global(this->global_sideset_ids,
this->num_global_side_counts,
this->num_global_side_df_counts);
// Before we go any further we need to derive consistent node and
// element numbering schemes for all local elems and nodes connected
// to local elements.
//
// Must be called *after* the local_subdomain_counts map has been constructed
// by the compute_num_global_elem_blocks() function!
this->build_element_and_node_maps(pmesh);
// Next step is to write "load balance" parameters. Several things need to
// be computed first though...
// First we'll collect IDs of border nodes.
this->compute_border_node_ids(pmesh);
// Next we'll collect numbers of internal and border elements, and internal nodes.
// Note: "A border node does not a border element make...", that is, just because one
// of an element's nodes has been identified as a border node, the element is not
// necessarily a border element. It must have a side on the boundary between processors,
// i.e. have a face neighbor with a different processor id...
this->compute_internal_and_border_elems_and_internal_nodes(pmesh);
// Finally we are ready to write the loadbal information to the file
this->put_loadbal_param(this->num_internal_nodes,
this->num_border_nodes,
this->num_external_nodes,
this->num_internal_elems,
this->num_border_elems,
this->num_node_cmaps,
this->num_elem_cmaps);
// Now we need to compute the "communication map" parameters. These are basically
// lists of nodes and elements which need to be communicated between different processors
// when the mesh file is read back in.
this->compute_communication_map_parameters();
// Write communication map parameters to file.
this->put_cmap_params(this->node_cmap_ids,
this->node_cmap_node_cnts,
this->elem_cmap_ids,
this->elem_cmap_elem_cnts);
// Ready the node communication maps. The node IDs which
// are communicated are the ones currently stored in
// proc_nodes_touched_intersections.
this->compute_node_communication_maps();
// Write the packed node communication vectors to file.
this->put_node_cmap(this->node_cmap_node_ids,
this->node_cmap_proc_ids);
// Ready the node maps. These have nothing to do with communication, they map
// the nodes to internal, border, and external nodes in the file.
this->compute_node_maps();
// Call the Nemesis API to write the node maps to file.
this->put_node_map(this->node_mapi,
this->node_mapb,
this->node_mape);
// Ready the element communication maps. This includes border
// element IDs, sides which are on the border, and the processors to which
// they are to be communicated...
this->compute_elem_communication_maps();
// Call the Nemesis API to write the packed element communication maps vectors to file
this->put_elem_cmap(this->elem_cmap_elem_ids,
this->elem_cmap_side_ids,
this->elem_cmap_proc_ids);
// Ready the Nemesis element maps (internal and border) for writing to file.
this->compute_element_maps();
// Call the Nemesis API to write the internal and border element IDs.
this->put_elem_map(this->elem_mapi,
this->elem_mapb);
// Now write Exodus-specific initialization information, some of which is
// different when you are using Nemesis.
this->write_exodus_initialization_info(pmesh, title_in);
} // end initialize()
void Nemesis_IO_Helper::write_exodus_initialization_info(const MeshBase & pmesh,
const std::string & title_in)
{
// This follows the convention of Exodus: we always write out the mesh as LIBMESH_DIM-dimensional,
// even if it is 2D...
this->num_dim = LIBMESH_DIM;
this->num_elem = static_cast<unsigned int>(std::distance (pmesh.active_local_elements_begin(),
pmesh.active_local_elements_end()));
// Exodus will also use *global* number of side and node sets,
// though it will not write out entries for all of them...
this->num_side_sets =
cast_int<int>(this->global_sideset_ids.size());
this->num_node_sets =
cast_int<int>(this->global_nodeset_ids.size());
// We need to write the global number of blocks, even though this processor might not have
// elements in some of them!
this->num_elem_blk = this->num_elem_blks_global;
ex_err = exII::ex_put_init(ex_id,
title_in.c_str(),
this->num_dim,
this->num_nodes,
this->num_elem,
this->num_elem_blk,
this->num_node_sets,
this->num_side_sets);
EX_CHECK_ERR(ex_err, "Error initializing new Nemesis file.");
}
void Nemesis_IO_Helper::compute_element_maps()
{
// Make sure we don't have any leftover info
this->elem_mapi.clear();
this->elem_mapb.clear();
// Copy set contents into vectors
this->elem_mapi.resize(this->internal_elem_ids.size());
this->elem_mapb.resize(this->border_elem_ids.size());
{
unsigned cnt = 0;
std::set<unsigned>::iterator
it = this->internal_elem_ids.begin(),
end = this->internal_elem_ids.end();
for (; it != end; ++it, ++cnt)
{
std::map<int, int>::iterator elem_it = libmesh_elem_num_to_exodus.find(*it);
if (elem_it == libmesh_elem_num_to_exodus.end())
libmesh_error_msg("Elem number " << *it << " not found in libmesh_elem_num_to_exodus map.");
this->elem_mapi[cnt] = elem_it->second;
}
}
{
unsigned cnt = 0;
std::set<unsigned>::iterator
it = this->border_elem_ids.begin(),
end = this->border_elem_ids.end();
for (; it != end; ++it, ++cnt)
{
std::map<int, int>::iterator elem_it = libmesh_elem_num_to_exodus.find(*it);
if (elem_it == libmesh_elem_num_to_exodus.end())
libmesh_error_msg("Elem number " << *it << " not found in libmesh_elem_num_to_exodus map.");
this->elem_mapb[cnt] = elem_it->second;
}
}