-
Notifications
You must be signed in to change notification settings - Fork 72
Expand file tree
/
Copy pathconduit_relay_io_blueprint.cpp
More file actions
2384 lines (2071 loc) · 76.4 KB
/
Copy pathconduit_relay_io_blueprint.cpp
File metadata and controls
2384 lines (2071 loc) · 76.4 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
// Copyright (c) Lawrence Livermore National Security, LLC and other Conduit
// Project developers. See top-level LICENSE AND COPYRIGHT files for dates and
// other details. No copyright assignment is required to contribute to Conduit.
//-----------------------------------------------------------------------------
///
/// file: conduit_relay_io_blueprint.cpp
///
//-----------------------------------------------------------------------------
#include "conduit_relay_io.hpp"
#include "conduit_relay_io_handle.hpp"
#include "conduit_blueprint.hpp"
#include "conduit_fmt/conduit_fmt.h"
#ifdef CONDUIT_RELAY_IO_MPI_ENABLED
#include "conduit_relay_mpi.hpp"
#include "conduit_relay_mpi_io_blueprint.hpp"
#include "conduit_blueprint_mpi.hpp"
#ifdef CONDUIT_RELAY_IO_SILO_ENABLED
#include "conduit_relay_mpi_io_silo.hpp"
#endif
#ifdef CONDUIT_RELAY_IO_CGNS_ENABLED
#include "conduit_relay_mpi_io_cgns.hpp"
#endif
#else
#include "conduit_relay_io_blueprint.hpp"
#endif
#ifdef CONDUIT_RELAY_IO_SILO_ENABLED
#include "conduit_relay_io_silo.hpp"
#endif
#ifdef CONDUIT_RELAY_IO_CGNS_ENABLED
#include "conduit_relay_io_cgns.hpp"
#endif
#ifdef CONDUIT_RELAY_IO_MPI_ENABLED
// Define an argument macro that adds the communicator argument.
#define CONDUIT_RELAY_COMMUNICATOR_ARG(ARG) ,ARG
#else
// Define an argument macro that does not add the communicator argument.
#define CONDUIT_RELAY_COMMUNICATOR_ARG(ARG)
#endif
// std includes
#include <limits>
#include <set>
//-----------------------------------------------------------------------------
// standard lib includes
//-----------------------------------------------------------------------------
#include <iostream>
//-----------------------------------------------------------------------------
// -- begin conduit:: --
//-----------------------------------------------------------------------------
namespace conduit
{
//-----------------------------------------------------------------------------
// -- begin conduit::relay --
//-----------------------------------------------------------------------------
namespace relay
{
#ifdef CONDUIT_RELAY_IO_MPI_ENABLED
//-----------------------------------------------------------------------------
// -- begin conduit::relay::mpi --
//-----------------------------------------------------------------------------
namespace mpi
{
#endif
//-----------------------------------------------------------------------------
// -- begin conduit::relay::io
//-----------------------------------------------------------------------------
namespace io
{
//-----------------------------------------------------------------------------
// -- begin conduit::relay::io::<mpi>::blueprint
//-----------------------------------------------------------------------------
namespace blueprint
{
//-----------------------------------------------------------------------------
void gen_domain_to_file_map(index_t num_domains,
index_t num_files,
Node &out)
{
index_t num_domains_per_file = num_domains / num_files;
index_t left_overs = num_domains % num_files;
out["global_domains_per_file"].set(DataType::index_t(num_files));
out["global_domain_offsets"].set(DataType::index_t(num_files));
out["global_domain_to_file"].set(DataType::index_t(num_domains));
index_t_array v_domains_per_file = out["global_domains_per_file"].value();
index_t_array v_domains_offsets = out["global_domain_offsets"].value();
index_t_array v_domain_to_file = out["global_domain_to_file"].value();
// setup domains per file
for(index_t f=0; f < num_files; f++)
{
v_domains_per_file[f] = num_domains_per_file;
if( f < left_overs)
v_domains_per_file[f]+=1;
}
if(num_files > 1)
{
// prefix sum to calc offsets
for(index_t f=1; f < num_files; f++)
{
v_domains_offsets[f] += (v_domains_per_file[f-1] +
v_domains_offsets[f-1]);
}
}
// do assignment, create simple map
index_t f_idx = 0;
for(index_t d=0; d < num_domains; d++)
{
// see if we are at the offset for the next file
if(f_idx + 1 < num_files)
{
if(d >= v_domains_offsets[f_idx+1])
f_idx++;
}
v_domain_to_file[d] = f_idx;
}
}
//-----------------------------------------------------------------------------
// -- begin conduit::relay::<mpi>::blueprint::detail --
//-----------------------------------------------------------------------------
//
// Lots of helpers pulled in from Ascent for dealing with
// with mesh blueprint writing and reading.
// TODO: Could use cleanup.
//
namespace detail
{
class BlueprintPathGeneratorImpl
{
public:
BlueprintPathGeneratorImpl()
{}
virtual ~BlueprintPathGeneratorImpl()
{}
virtual std::string GenerateFilePath(index_t tree_id) const =0;
virtual std::string GenerateTreePath(index_t tree_id) const =0;
};
class BlueprintLegacyPathGenerator: public BlueprintPathGeneratorImpl
{
public:
//-------------------------------------------------------------------//
BlueprintLegacyPathGenerator(const std::string &file_pattern,
const std::string &tree_pattern,
index_t num_files,
index_t num_trees,
const std::string &protocol)
: m_file_pattern(file_pattern),
m_tree_pattern(tree_pattern),
m_num_files(num_files),
m_num_trees(num_trees),
m_protocol(protocol)
{
// if we need domain to file map, gen it
if( m_num_files > 1 && (m_num_trees != m_num_files) )
{
gen_domain_to_file_map(m_num_trees,
m_num_files,
m_d2f_map);
}
}
//-------------------------------------------------------------------//
virtual ~BlueprintLegacyPathGenerator()
{
}
//-------------------------------------------------------------------//
std::string Expand(const std::string pattern,
index_t idx) const
{
//
// This currently handles format strings:
// "%d" "%02d" "%03d" "%04d" "%05d" "%06d" "%07d" "%08d" "%09d"
//
std::size_t pattern_idx = pattern.find("%d");
if(pattern_idx != std::string::npos)
{
std::string res = pattern;
res.replace(pattern_idx,
4,
conduit_fmt::format("{:d}",idx));
return res;
}
for(int i=2; i<10; i++)
{
std::string pat = "%0" + conduit_fmt::format("{:d}",i) + "d";
pattern_idx = pattern.find(pat);
if(pattern_idx != std::string::npos)
{
pat = "{:0" + conduit_fmt::format("{:d}",i) + "d}";
std::string res = pattern;
res.replace(pattern_idx,
4,
conduit_fmt::format(pat,idx));
return res;
}
}
return pattern;
}
//-------------------------------------------------------------------//
virtual std::string GenerateFilePath(index_t tree_id) const
{
index_t file_id = -1;
if(m_num_trees == m_num_files)
{
file_id = tree_id;
}
else if(m_num_files == 1)
{
file_id = 0;
}
else
{
index_t_accessor v_d2f = m_d2f_map["global_domain_to_file"].value();
file_id = v_d2f[tree_id];
}
return Expand(m_file_pattern,file_id);
}
//-------------------------------------------------------------------//
virtual std::string GenerateTreePath(index_t tree_id) const
{
// the tree path should always end in a /
std::string res = Expand(m_tree_pattern,tree_id);
if( (res.size() > 0) && (res[res.size()-1] != '/') )
{
res += "/";
}
return res;
}
private:
std::string m_file_pattern;
std::string m_tree_pattern;
index_t m_num_files;
index_t m_num_trees;
std::string m_protocol;
Node m_d2f_map;
};
class BlueprintPartitonMapPathGenerator: public BlueprintPathGeneratorImpl
{
public:
//-------------------------------------------------------------------//
BlueprintPartitonMapPathGenerator(const std::string &part_pattern,
const conduit::Node &part_map)
: m_part_pattern(part_pattern),
m_part_map(part_map),
m_dom_to_tree()
{
// init the map that takes us from domain_id (what we want)
// to part map entry
index_t_accessor doms = part_map["domain"].value();
index_t num_domains = doms.max() + 1;
// NOTE: Most cases will be compact, but what about
// cases that are not?
m_dom_to_tree.set(DataType::index_t(num_domains));
index_t_array dom_to_tree_vals = m_dom_to_tree.value();
for(index_t i=0;i<num_domains;i++)
{
dom_to_tree_vals[doms[i]] = i;
}
}
//-------------------------------------------------------------------//
BlueprintPartitonMapPathGenerator(const std::string &part_pattern)
: m_part_pattern(part_pattern),
m_part_map(),
m_dom_to_tree()
{
// empty
}
//-------------------------------------------------------------------//
virtual ~BlueprintPartitonMapPathGenerator()
{
// empty
}
//-------------------------------------------------------------------//
virtual std::string GenerateFullPath(index_t tree_id) const
{
if( m_part_map.number_of_children() == 0 )
{
// special case, not format sub needed
return m_part_pattern;
}
else
{
index_t_array dom_to_tree_vals = m_dom_to_tree.value();
index_t dom_lookup = dom_to_tree_vals[tree_id];
return conduit::utils::format(m_part_pattern,
m_part_map,
dom_lookup);
}
}
//-------------------------------------------------------------------//
virtual std::string GenerateFilePath(index_t tree_id) const
{
std::string res,tmp;
utils::split_string(GenerateFullPath(tree_id),
":/",
res, // result is before sep
tmp);
return res;
}
//-------------------------------------------------------------------//
virtual std::string GenerateTreePath(index_t tree_id) const
{
std::string res,tmp;
utils::split_string(GenerateFullPath(tree_id),
":/",
tmp,
res); // result is after sep
// tree path should always end with "/"
if( (res.size() > 0) && (res[res.size()-1] != '/') )
{
res += "/";
}
return res;
}
private:
std::string m_part_pattern;
Node m_part_map;
// extra map for non trivial domain
Node m_dom_to_tree;
};
class BlueprintTreePathGenerator
{
public:
BlueprintTreePathGenerator()
: m_impl(nullptr)
{
}
void Cleanup()
{
if(m_impl != nullptr)
{
delete m_impl;
m_impl = nullptr;
}
}
//-------------------------------------------------------------------//
void Init(const std::string &file_pattern,
const std::string &tree_pattern,
index_t num_files,
index_t num_trees,
const std::string &protocol)
{
Cleanup();
m_impl = new BlueprintLegacyPathGenerator(file_pattern,
tree_pattern,
num_files,
num_trees,
protocol);
}
//-------------------------------------------------------------------//
void Init(const std::string &part_pattern,
const conduit::Node &part_map)
{
Cleanup();
m_impl = new BlueprintPartitonMapPathGenerator(part_pattern,
part_map);
}
//-------------------------------------------------------------------//
void Init(const std::string &part_pattern)
{
Cleanup();
m_impl = new BlueprintPartitonMapPathGenerator(part_pattern);
}
~BlueprintTreePathGenerator()
{
Cleanup();
}
std::string GenerateFilePath(index_t tree_id) const
{
return m_impl->GenerateFilePath(tree_id);
}
std::string GenerateTreePath(index_t tree_id) const
{
return m_impl->GenerateTreePath(tree_id);
}
private:
BlueprintPathGeneratorImpl *m_impl;
};
bool global_someone_agrees(bool vote
CONDUIT_RELAY_COMMUNICATOR_ARG(MPI_Comm mpi_comm))
{
bool agreement = vote;
#ifdef CONDUIT_RELAY_IO_MPI_ENABLED
int local_boolean = vote ? 1 : 0;
int global_boolean;
MPI_Allreduce((void *)(&local_boolean),
(void *)(&global_boolean),
1,
MPI_INT,
MPI_SUM,
mpi_comm);
if(global_boolean > 0)
{
agreement = true;
}
else
{
agreement = false;
}
#endif
return agreement;
}
bool quick_mesh_check(const conduit::Node &n)
{
return n.has_child("topologies") &&
n["topologies"].number_of_children() > 0;
}
// mfem needs these special fields so look for them
void check_for_attributes(const conduit::Node &input,
std::vector<std::string> &list)
{
const index_t num_doms = input.number_of_children();
std::set<std::string> specials;
for(index_t d = 0; d < num_doms; ++d)
{
const conduit::Node &dom = input.child(d);
if(dom.has_path("fields"))
{
const conduit::Node &fields = dom["fields"];
std::vector<std::string> fnames = fields.child_names();
for(size_t i = 0; i < fnames.size(); ++i)
{
if(fnames[i].find("_attribute") != std::string::npos)
{
specials.insert(fnames[i]);
}
}
}
}
for(auto it = specials.begin(); it != specials.end(); ++it)
{
list.push_back(*it);
}
}
void filter_fields(const conduit::Node &input,
conduit::Node &output,
std::vector<std::string> fields
CONDUIT_RELAY_COMMUNICATOR_ARG(MPI_Comm mpi_comm))
{
// assume this is multi-domain
//
check_for_attributes(input, fields);
const index_t num_doms = input.number_of_children();
for(index_t d = 0; d < num_doms; ++d)
{
const conduit::Node &dom = input.child(d);
conduit::Node &out_dom = output.append();
for(size_t f = 0; f < fields.size(); ++f)
{
const std::string fname = fields[f];
if(dom.has_path("fields/" + fname))
{
const std::string fpath = "fields/" + fname;
out_dom[fpath].set_external(dom[fpath]);
// check for topologies
const std::string topo = dom[fpath + "/topology"].as_string();
const std::string tpath = "topologies/" + topo;
if(!out_dom.has_path(tpath))
{
out_dom[tpath].set_external(dom[tpath]);
if(dom.has_path(tpath + "/grid_function"))
{
const std::string gf_name = dom[tpath + "/grid_function"].as_string();
const std::string gf_path = "fields/" + gf_name;
out_dom[gf_path].set_external(dom[gf_path]);
}
if(dom.has_path(tpath + "/boundary_topology"))
{
const std::string bname = dom[tpath + "/boundary_topology"].as_string();
const std::string bpath = "topologies/" + bname;
out_dom[bpath].set_external(dom[bpath]);
}
}
// check for coord sets
const std::string coords = dom[tpath + "/coordset"].as_string();
const std::string cpath = "coordsets/" + coords;
if(!out_dom.has_path(cpath))
{
out_dom[cpath].set_external(dom[cpath]);
}
}
}
if(dom.has_path("state"))
{
out_dom["state"].set_external(dom["state"]);
}
}
const index_t num_out_doms = output.number_of_children();
bool has_data = false;
// check to see if this resulted in any data
for(index_t d = 0; d < num_out_doms; ++d)
{
const conduit::Node &dom = output.child(d);
if(dom.has_path("fields"))
{
index_t fsize = dom["fields"].number_of_children();
if(fsize != 0)
{
has_data = true;
break;
}
}
}
#ifdef CONDUIT_RELAY_IO_MPI_ENABLED
has_data = detail::global_someone_agrees(has_data,mpi_comm);
#else
has_data = detail::global_someone_agrees(has_data);
#endif
if(!has_data)
{
CONDUIT_ERROR("Relay: field selection resulted in no data."
"This can occur if the fields did not exist "
"in the simulation data or if the fields were "
"created as a result of a pipeline, but the "
"relay extract did not receive the result of "
"a pipeline");
}
}
//---------------------------------------------------------------------------//
std::string
identify_protocol(const std::string &path)
{
std::string file_path, obj_base;
conduit::utils::split_file_path(path,
std::string(":"),
file_path,
obj_base);
std::string file_name_base, file_name_ext;
conduit::utils::rsplit_string(file_path,
std::string("."),
file_name_ext,
file_name_base);
std::cout << "identify_protocol: file_path = " << file_path << std::endl;
std::cout << "identify_protocol: file_name_base = " << file_name_base << std::endl;
std::cout << "identify_protocol: file_name_ext = " << file_name_ext << std::endl;
// default
std::string io_type = "bin";
// if hdf5 is supported, it should be the default for mesh i/o
Node io_protos;
relay::io::about(io_protos["io"]);
if( io_protos["io/protocols/hdf5"].as_string() == "enabled" )
{
io_type = "hdf5";
}
if(file_name_ext.find("blueprint_root") == 0)
{
std::string file_name_true_ext = file_name_ext.substr(
std::string("blueprint_root").length(), file_name_ext.length());
// TODO: Add support for yaml protocol
if(file_name_true_ext == "")
{
io_type = "json";
}
else if(file_name_true_ext == "_hdf5" || file_name_true_ext == "_h5")
{
io_type = "hdf5";
}
else if(file_name_true_ext == "_silo")
{
io_type = "silo";
}
}
return io_type;
}
//-----------------------------------------------------------------------------
// -- end conduit::relay::<mpi>::io_blueprint::detail --
//-----------------------------------------------------------------------------
};
//
// This expects a single or multi_domain blueprint mesh and will iterate
// through all domains to see if they are valid. Returns true
// if it contains valid data and false if there is no valid
// data.
//
// This is needed because after pipelines, it is possible to
// have no data left in a domain because of something like a
// clip
//
bool clean_mesh(const conduit::Node &data,
conduit::Node &output
CONDUIT_RELAY_COMMUNICATOR_ARG(MPI_Comm mpi_comm))
{
output.reset();
const index_t potential_doms = data.number_of_children();
bool maybe_multi_dom = true;
if(!data.dtype().is_object() && !data.dtype().is_list())
{
maybe_multi_dom = false;
}
if(maybe_multi_dom)
{
// check all the children for valid domains
for(int i = 0; i < potential_doms; ++i)
{
// we expect folks to use their best behaivor
// (mesh bp verify is true before passing data)
// so we can assume we have valid mesh bp.
// if a child looks like a mesh, we have one
conduit::Node info;
const conduit::Node &child = data.child(i);
bool is_valid = detail::quick_mesh_check(child);
if(is_valid)
{
conduit::Node &dest_dom = output.append();
dest_dom.set_external(child);
// note: this algo may re-write domain ids
// so make sure this isn't set as external
if(dest_dom.has_path("state/domain_id"))
{
index_t dom_id = dest_dom["state/domain_id"].to_index_t();
dest_dom["state/domain_id"].reset();
dest_dom["state/domain_id"] = dom_id;
}
}
}
}
// if there is nothing in the output, lets see if it is a
// valid single domain
if(output.number_of_children() == 0)
{
// check to see if this is a single valid domain
conduit::Node info;
bool is_valid = detail::quick_mesh_check(data);
if(is_valid)
{
conduit::Node &dest_dom = output.append();
dest_dom.set_external(data);
}
}
#ifdef CONDUIT_RELAY_IO_MPI_ENABLED
conduit::blueprint::mpi::mesh::generate_domain_ids(output, mpi_comm);
#else
conduit::blueprint::mesh::generate_domain_ids(output);
#endif
return output.number_of_children() > 0;
}
//-----------------------------------------------------------------------------
// Sig variants of save_mesh
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
void save_mesh(const Node &mesh,
const std::string &path
CONDUIT_RELAY_COMMUNICATOR_ARG(MPI_Comm mpi_comm))
{
// empty opts
Node opts;
#ifdef CONDUIT_RELAY_IO_MPI_ENABLED
save_mesh(mesh,
path,
detail::identify_protocol(path),
opts,
mpi_comm);
#else
save_mesh(mesh,
path,
detail::identify_protocol(path),
opts);
#endif
}
//-----------------------------------------------------------------------------
void save_mesh(const Node &mesh,
const std::string &path,
const std::string &protocol
CONDUIT_RELAY_COMMUNICATOR_ARG(MPI_Comm mpi_comm))
{
// empty opts
Node opts;
#ifdef CONDUIT_RELAY_IO_MPI_ENABLED
save_mesh(mesh,
path,
protocol,
opts,
mpi_comm);
#else
save_mesh(mesh,
path,
protocol,
opts);
#endif
}
//-----------------------------------------------------------------------------
// Main Mesh Blueprint Save, taken from Ascent
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
/// The following options can be passed via the opts Node:
//-----------------------------------------------------------------------------
/// opts:
/// file_style: "default", "root_only", "multi_file"
/// when # of domains == 1, "default" ==> "root_only"
/// else, "default" ==> "multi_file"
///
/// suffix: "default", "cycle", "none"
/// when cycle is present, "default" ==> "cycle"
/// else, "default" ==> "none"
///
/// mesh_name: (used if present, default ==> "mesh")
///
/// number_of_files: {# of files}
/// when "multi_file":
/// <= 0, use # of files == # of domains
/// > 0, # of files == number_of_files
///
//-----------------------------------------------------------------------------
void save_mesh(const Node &mesh,
const std::string &path,
const std::string &protocol,
const Node &opts
CONDUIT_RELAY_COMMUNICATOR_ARG(MPI_Comm mpi_comm))
{
// we force overwrite to true, so we need a copy of the const opts passed.
Node save_opts;
save_opts.set(opts);
save_opts["truncate"] = "true";
#ifdef CONDUIT_RELAY_IO_MPI_ENABLED
write_mesh(mesh,
path,
protocol,
save_opts,
mpi_comm);
#else
write_mesh(mesh,
path,
protocol,
save_opts);
#endif
}
//-----------------------------------------------------------------------------
// Sig variants of write_mesh
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
void write_mesh(const Node &mesh,
const std::string &path
CONDUIT_RELAY_COMMUNICATOR_ARG(MPI_Comm mpi_comm))
{
// empty opts
Node opts;
#ifdef CONDUIT_RELAY_IO_MPI_ENABLED
write_mesh(mesh,
path,
detail::identify_protocol(path),
opts,
mpi_comm);
#else
write_mesh(mesh,
path,
detail::identify_protocol(path),
opts);
#endif
}
//-----------------------------------------------------------------------------
void write_mesh(const Node &mesh,
const std::string &path,
const std::string &protocol
CONDUIT_RELAY_COMMUNICATOR_ARG(MPI_Comm mpi_comm))
{
// empty opts
Node opts;
#ifdef CONDUIT_RELAY_IO_MPI_ENABLED
write_mesh(mesh,
path,
protocol,
opts,
mpi_comm);
#else
write_mesh(mesh,
path,
protocol,
opts);
#endif
}
//-----------------------------------------------------------------------------
// Main Mesh Blueprint Save, taken from Ascent
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
/// The following options can be passed via the opts Node:
//-----------------------------------------------------------------------------
/// opts:
/// file_style: "default", "root_only", "multi_file"
/// when # of domains == 1, "default" ==> "root_only"
/// else, "default" ==> "multi_file"
///
/// suffix: "default", "cycle", "none"
/// when cycle is present, "default" ==> "cycle"
/// else, "default" ==> "none"
///
/// mesh_name: (used if present, default ==> "mesh")
///
/// number_of_files: {# of files}
/// when "multi_file":
/// <= 0, use # of files == # of domains
/// > 0, # of files == number_of_files
///
//-----------------------------------------------------------------------------
void write_mesh(const Node &mesh,
const std::string &path,
const std::string &file_protocol,
const Node &opts
CONDUIT_RELAY_COMMUNICATOR_ARG(MPI_Comm mpi_comm))
{
if(file_protocol == "silo")
{
#ifdef CONDUIT_RELAY_IO_SILO_ENABLED
#ifdef CONDUIT_RELAY_IO_MPI_ENABLED
return conduit::relay::mpi::io::silo::write_mesh(mesh,
path,
opts,
mpi_comm);
#else
return conduit::relay::io::silo::write_mesh(mesh,
path,
opts);
#endif
#else // conduit lacks silo support
CONDUIT_ERROR("write_mesh invalid protocol option: `silo`"
<< "conduit build lacks silo support\n");
#endif
}
if(file_protocol == "cgns")
{
#ifdef CONDUIT_RELAY_IO_CGNS_ENABLED
#ifdef CONDUIT_RELAY_IO_MPI_ENABLED
return conduit::relay::mpi::io::cgns::write_mesh(mesh,
path,
opts,
mpi_comm);
#else
return conduit::relay::io::cgns::write_mesh(mesh,
path,
opts);
#endif
#else
CONDUIT_ERROR("write_mesh invalid protocol option: `cgns`"
<< "conduit build lacks CGNS support\n");
#endif
}
// The assumption here is that everything is multi domain
std::string opts_file_style = "default";
std::string opts_suffix = "default";
std::string opts_mesh_name = "mesh";
int opts_num_files = -1;
bool opts_truncate = false;
// check for + validate file_style option
if(opts.has_child("file_style") && opts["file_style"].dtype().is_string())
{
opts_file_style = opts["file_style"].as_string();
if(opts_file_style != "default" &&
opts_file_style != "root_only" &&
opts_file_style != "multi_file" )
{
CONDUIT_ERROR("write_mesh invalid file_style option: \""
<< opts_file_style << "\"\n"
" expected: \"default\", \"root_only\", "
"or \"multi_file\"");
}
}
// check for + validate suffix option
if(opts.has_child("suffix") && opts["suffix"].dtype().is_string())
{
opts_suffix = opts["suffix"].as_string();
if(opts_suffix != "default" &&
opts_suffix != "cycle" &&
opts_suffix != "none" )
{
CONDUIT_ERROR("write_mesh invalid suffix option: \""
<< opts_suffix << "\"\n"
" expected: \"default\", \"cycle\", or \"none\"");
}
}
// check for + validate suffix option
if(opts.has_child("mesh_name") && opts["mesh_name"].dtype().is_string())
{
opts_mesh_name = opts["mesh_name"].as_string();
}
// check for number_of_files, 0 or -1 implies #files => # domains
if(opts.has_child("number_of_files") && opts["number_of_files"].dtype().is_integer())
{
opts_num_files = (int) opts["number_of_files"].to_int();
}
// check for truncate (overwrite)
if(opts.has_child("truncate") && opts["truncate"].dtype().is_string())
{
const std::string ow_string = opts["truncate"].as_string();
if(ow_string == "true")