| 1 |
// Copyright(C) 1999-2024 National Technology & Engineering Solutions |
| 2 |
// of Sandia, LLC (NTESS). Under the terms of Contract DE-NA0003525 with |
| 3 |
// NTESS, the U.S. Government retains certain rights in this software. |
| 4 |
// |
| 5 |
// See packages/seacas/LICENSE for details |
| 6 |
|
| 7 |
#include <cstdlib> |
| 8 |
#include <exception> |
| 9 |
#include <fmt/core.h> |
| 10 |
#include <fmt/format.h> |
| 11 |
#include <fmt/ranges.h> |
| 12 |
#include <limits> |
| 13 |
#include <stdint.h> |
| 14 |
#include <stdio.h> |
| 15 |
#include <string> |
| 16 |
#include <tokenize.h> |
| 17 |
#include <vector> |
| 18 |
|
| 19 |
#include "Ionit_Initializer.h" |
| 20 |
#include "Ioss_Compare.h" |
| 21 |
#include "Ioss_CopyDatabase.h" |
| 22 |
#include "Ioss_DBUsage.h" |
| 23 |
#include "Ioss_DataSize.h" |
| 24 |
#include "Ioss_DatabaseIO.h" |
| 25 |
#include "Ioss_DynamicTopology.h" |
| 26 |
#include "Ioss_DynamicTopologyBroker.h" |
| 27 |
#include "Ioss_DynamicTopologyFileControl.h" |
| 28 |
#include "Ioss_DynamicTopologyObserver.h" |
| 29 |
#include "Ioss_FileInfo.h" |
| 30 |
#include "Ioss_GetLongOpt.h" |
| 31 |
#include "Ioss_IOFactory.h" |
| 32 |
#include "Ioss_MemoryUtils.h" |
| 33 |
#include "Ioss_MeshCopyOptions.h" |
| 34 |
#include "Ioss_MeshType.h" |
| 35 |
#include "Ioss_ParallelUtils.h" |
| 36 |
#include "Ioss_Property.h" |
| 37 |
#include "Ioss_PropertyManager.h" |
| 38 |
#include "Ioss_Region.h" |
| 39 |
#include "Ioss_ScopeGuard.h" |
| 40 |
#include "Ioss_SerializeIO.h" |
| 41 |
#include "Ioss_SurfaceSplit.h" |
| 42 |
#include "Ioss_Utils.h" |
| 43 |
#include "Ioss_VariableType.h" |
| 44 |
#include "shell_interface.h" |
| 45 |
|
| 46 |
// ======================================================================== |
| 47 |
|
| 48 |
namespace { |
| 49 |
std::string codename; |
| 50 |
std::string version = "7.0 (2024/11/08)"; |
| 51 |
|
| 52 |
bool mem_stats = false; |
| 53 |
|
| 54 |
bool file_copy(IOShell::Interface &interFace, int rank); |
| 55 |
bool file_compare(IOShell::Interface &interFace); |
| 56 |
|
| 57 |
Ioss::PropertyManager set_properties(IOShell::Interface &interFace); |
| 58 |
Ioss::MeshCopyOptions set_mesh_copy_options(IOShell::Interface &interFace) |
| 59 |
{ |
| 60 |
Ioss::MeshCopyOptions options{}; |
| 61 |
options.selected_times = interFace.selected_times; |
| 62 |
options.rel_tolerance = interFace.rel_tolerance; |
| 63 |
options.abs_tolerance = interFace.abs_tolerance; |
| 64 |
options.tol_floor = interFace.tol_floor; |
| 65 |
options.verbose = !interFace.quiet; |
| 66 |
options.output_summary = true; |
| 67 |
options.memory_statistics = interFace.memory_statistics; |
| 68 |
options.debug = interFace.debug; |
| 69 |
options.ints_64_bit = interFace.ints_64_bit; |
| 70 |
options.delete_timesteps = interFace.delete_timesteps; |
| 71 |
options.minimum_time = interFace.minimum_time; |
| 72 |
options.maximum_time = interFace.maximum_time; |
| 73 |
options.time_scale = interFace.time_scale; |
| 74 |
options.time_offset = interFace.time_offset; |
| 75 |
options.data_storage_type = interFace.data_storage_type; |
| 76 |
options.delay = interFace.timestep_delay; |
| 77 |
options.reverse = interFace.reverse; |
| 78 |
options.add_proc_id = interFace.add_processor_id_field; |
| 79 |
options.boundary_sideset = interFace.boundary_sideset; |
| 80 |
options.ignore_qa_info = interFace.ignore_qa_info; |
| 81 |
options.omitted_blocks = !interFace.omitted_blocks.empty(); |
| 82 |
options.selected_change_sets = interFace.selectedChangeSets; |
| 83 |
|
| 84 |
options.omitted_sets = interFace.omitted_sets; |
| 85 |
Ioss::sort(options.omitted_sets); |
| 86 |
for (auto &name : options.omitted_sets) { |
| 87 |
name = Ioss::Utils::lowercase(name); |
| 88 |
} |
| 89 |
return options; |
| 90 |
} |
| 91 |
|
| 92 |
bool open_change_set(const std::string &cs_name, Ioss::Region ®ion, int rank) |
| 93 |
{ |
| 94 |
bool success = true; |
| 95 |
if (!cs_name.empty()) { |
| 96 |
success = region.load_internal_change_set_mesh(cs_name); |
| 97 |
if (!success) { |
| 98 |
if (rank == 0) { |
| 99 |
fmt::print(stderr, "ERROR: Unable to open change_set '{}' in file '{}'\n", cs_name, |
| 100 |
region.get_database()->get_filename()); |
| 101 |
} |
| 102 |
} |
| 103 |
} |
| 104 |
return success; |
| 105 |
} |
| 106 |
|
| 107 |
class Observer : public Ioss::DynamicTopologyObserver |
| 108 |
{ |
| 109 |
public: |
| 110 |
Observer() : Ioss::DynamicTopologyObserver(nullptr) {} |
| 111 |
Ioss::FileControlOption get_control_option() const |
| 112 |
{ |
| 113 |
return Ioss::FileControlOption::CONTROL_AUTO_GROUP_FILE; |
| 114 |
} |
| 115 |
bool needs_new_output_file() const { return false; } |
| 116 |
}; |
| 117 |
|
| 118 |
#ifdef SEACAS_HAVE_MPI |
| 119 |
void mpi_finalize() |
| 120 |
{ |
| 121 |
MPI_Comm parentcomm; |
| 122 |
MPI_Comm_get_parent(&parentcomm); |
| 123 |
if (parentcomm != MPI_COMM_NULL) { |
| 124 |
int istatus = EXIT_SUCCESS; |
| 125 |
MPI_Send(&istatus, 1, MPI_INT, 0, 0, parentcomm); |
| 126 |
} |
| 127 |
MPI_Finalize(); |
| 128 |
} |
| 129 |
#endif |
| 130 |
} // namespace |
| 131 |
|
| 132 |
int main(int argc, char *argv[]) |
| 133 |
{ |
| 134 |
#ifdef SEACAS_HAVE_MPI |
| 135 |
MPI_Init(&argc, &argv); |
| 136 |
ON_BLOCK_EXIT(mpi_finalize); |
| 137 |
#endif |
| 138 |
Ioss::ParallelUtils pu{}; |
| 139 |
int rank = pu.parallel_rank(); |
| 140 |
int num_proc = pu.parallel_size(); |
| 141 |
|
| 142 |
#ifdef SEACAS_HAVE_KOKKOS |
| 143 |
Kokkos::ScopeGuard kokkos(argc, argv); |
| 144 |
#endif |
| 145 |
|
| 146 |
IOShell::Interface interFace(version); |
| 147 |
bool success = interFace.parse_options(argc, argv, rank); |
| 148 |
if (!success) { |
| 149 |
exit(EXIT_FAILURE); |
| 150 |
} |
| 151 |
|
| 152 |
codename = Ioss::GetLongOption::basename(argv[0]); |
| 153 |
|
| 154 |
Ioss::SerializeIO::setGroupFactor(interFace.serialize_io_size); |
| 155 |
mem_stats = interFace.memory_statistics; |
| 156 |
|
| 157 |
Ioss::Init::Initializer io; |
| 158 |
|
| 159 |
// See if a custom field is defined... |
| 160 |
if (!interFace.customField.empty()) { |
| 161 |
auto suffices = Ioss::tokenize(interFace.customField, ","); |
| 162 |
if (suffices.size() > 1) { |
| 163 |
Ioss::VariableType::create_named_suffix_type("UserDefined", suffices); |
| 164 |
} |
| 165 |
} |
| 166 |
std::string in_file = interFace.inputFile[0]; |
| 167 |
std::string out_file = interFace.outputFile; |
| 168 |
|
| 169 |
if (rank == 0 && !interFace.quiet) { |
| 170 |
if (interFace.compare) { |
| 171 |
fmt::print(stderr, |
| 172 |
"Input 1: '{}', Type: {}\n" |
| 173 |
"Input 2: '{}', Type: {}\n" |
| 174 |
"\tTolerances: Absolute = {}, Relative = {}, Floor = {}\n\n", |
| 175 |
in_file, interFace.inFiletype, out_file, interFace.outFiletype, |
| 176 |
interFace.abs_tolerance, interFace.rel_tolerance, interFace.tol_floor); |
| 177 |
} |
| 178 |
else { |
| 179 |
fmt::print(stderr, |
| 180 |
"Input: '{}', Type: {}\n" |
| 181 |
"Output: '{}', Type: {}\n\n", |
| 182 |
in_file, interFace.inFiletype, out_file, interFace.outFiletype); |
| 183 |
} |
| 184 |
} |
| 185 |
|
| 186 |
#ifdef SEACAS_HAVE_KOKKOS |
| 187 |
if (rank == 0) |
| 188 |
fmt::print(stderr, "Kokkos default execution space configuration:\n"); |
| 189 |
Kokkos::DefaultExecutionSpace().print_configuration(std::cerr, false); |
| 190 |
if (rank == 0) |
| 191 |
fmt::print(stderr, "\n"); |
| 192 |
#endif |
| 193 |
|
| 194 |
double begin = Ioss::Utils::timer(); |
| 195 |
|
| 196 |
try { |
| 197 |
if (interFace.compare) { |
| 198 |
success = file_compare(interFace); |
| 199 |
} |
| 200 |
else { |
| 201 |
success = file_copy(interFace, rank); |
| 202 |
} |
| 203 |
} |
| 204 |
catch (std::exception &e) { |
| 205 |
if (rank == 0) { |
| 206 |
fmt::print(stderr, "\n{}\n\nio_shell terminated due to exception\n", e.what()); |
| 207 |
} |
| 208 |
exit(EXIT_FAILURE); |
| 209 |
} |
| 210 |
|
| 211 |
pu.barrier(); |
| 212 |
double end = Ioss::Utils::timer(); |
| 213 |
|
| 214 |
if (rank == 0 && !interFace.quiet) { |
| 215 |
if (num_proc > 1) { |
| 216 |
fmt::print(stderr, "\n\n\tTotal Execution Time = {:.5} seconds on {} processors.\n", |
| 217 |
end - begin, num_proc); |
| 218 |
} |
| 219 |
else { |
| 220 |
fmt::print(stderr, "\n\n\tTotal Execution Time = {:.5} seconds.\n", end - begin); |
| 221 |
} |
| 222 |
} |
| 223 |
if (mem_stats) { |
| 224 |
int64_t MiB = 1024 * 1024; |
| 225 |
#ifdef SEACAS_HAVE_MPI |
| 226 |
int64_t min, max, avg; |
| 227 |
int64_t hwmin, hwmax, hwavg; |
| 228 |
pu.memory_stats(min, max, avg); |
| 229 |
pu.hwm_memory_stats(hwmin, hwmax, hwavg); |
| 230 |
if (rank == 0) { |
| 231 |
fmt::print(stderr, "\n\tCurrent Memory: {}M {}M {}M\n", fmt::group_digits(min / MiB), |
| 232 |
fmt::group_digits(max / MiB), fmt::group_digits(avg / MiB)); |
| 233 |
fmt::print(stderr, "\tHigh Water Memory: {}M {}M {}M\n", fmt::group_digits(hwmin / MiB), |
| 234 |
fmt::group_digits(hwmax / MiB), fmt::group_digits(hwavg / MiB)); |
| 235 |
} |
| 236 |
#else |
| 237 |
int64_t mem = Ioss::MemoryUtils::get_memory_info(); |
| 238 |
int64_t hwm = Ioss::MemoryUtils::get_hwm_memory_info(); |
| 239 |
if (rank == 0) { |
| 240 |
fmt::print(stderr, |
| 241 |
"\n\tCurrent Memory: {}M\n" |
| 242 |
"\tHigh Water Memory: {}M\n", |
| 243 |
fmt::group_digits(mem / MiB), fmt::group_digits(hwm / MiB)); |
| 244 |
} |
| 245 |
#endif |
| 246 |
} |
| 247 |
if (rank == 0) { |
| 248 |
fmt::print(stderr, "\n{} execution {}.\n", codename, success ? "successful" : "failed"); |
| 249 |
} |
| 250 |
return success ? EXIT_SUCCESS : EXIT_FAILURE; |
| 251 |
} |
| 252 |
|
| 253 |
namespace { |
| 254 |
bool file_copy(IOShell::Interface &interFace, int rank) |
| 255 |
{ |
| 256 |
bool success = true; |
| 257 |
Ioss::PropertyManager properties = set_properties(interFace); |
| 258 |
|
| 259 |
for (const auto &inpfile : interFace.inputFile) { |
| 260 |
|
| 261 |
//======================================================================== |
| 262 |
// INPUT Database... |
| 263 |
//======================================================================== |
| 264 |
Ioss::DatabaseIO *dbi = |
| 265 |
Ioss::IOFactory::create(interFace.inFiletype, inpfile, Ioss::READ_MODEL, |
| 266 |
Ioss::ParallelUtils::comm_world(), properties); |
| 267 |
if (dbi == nullptr || !dbi->ok(true)) { |
| 268 |
std::exit(EXIT_FAILURE); |
| 269 |
} |
| 270 |
|
| 271 |
if (mem_stats) { |
| 272 |
dbi->progress("Database Creation"); |
| 273 |
} |
| 274 |
if (!interFace.lower_case_variable_names) { |
| 275 |
dbi->set_lower_case_variable_names(false); |
| 276 |
} |
| 277 |
if (interFace.outFiletype == "cgns") { |
| 278 |
// CGNS stores BCs (SideSets) on the zones which |
| 279 |
// correspond to element blocks. If split input sideblocks |
| 280 |
// by element block, then output is much easier. |
| 281 |
dbi->set_surface_split_type(Ioss::SPLIT_BY_ELEMENT_BLOCK); |
| 282 |
} |
| 283 |
else if (interFace.surface_split_type != Ioss::SPLIT_INVALID) { |
| 284 |
dbi->set_surface_split_type(Ioss::int_to_surface_split(interFace.surface_split_type)); |
| 285 |
} |
| 286 |
dbi->set_field_separator(interFace.fieldSuffixSeparator); |
| 287 |
|
| 288 |
dbi->set_field_recognition(!interFace.disable_field_recognition); |
| 289 |
|
| 290 |
if (interFace.ints_64_bit) { |
| 291 |
dbi->set_int_byte_size_api(Ioss::USE_INT64_API); |
| 292 |
} |
| 293 |
|
| 294 |
if (!interFace.omitted_blocks.empty()) { |
| 295 |
std::vector<std::string> inclusions{}; |
| 296 |
dbi->set_block_omissions(interFace.omitted_blocks, inclusions); |
| 297 |
} |
| 298 |
|
| 299 |
// NOTE: 'region' owns 'db' pointer at this time... |
| 300 |
Ioss::Region region(dbi, "region_1"); |
| 301 |
|
| 302 |
// Change_set specified... We will read the specified changeSet from the input file |
| 303 |
if (!interFace.changeSetName.empty()) { |
| 304 |
success = Ioss::Utils::check_valid_change_set_name(interFace.changeSetName, region, rank); |
| 305 |
if (success) { |
| 306 |
success = open_change_set(interFace.changeSetName, region, rank); |
| 307 |
} |
| 308 |
if (!success) { |
| 309 |
return success; |
| 310 |
} |
| 311 |
} |
| 312 |
|
| 313 |
if (region.mesh_type() == Ioss::MeshType::HYBRID) { |
| 314 |
if (rank == 0) { |
| 315 |
fmt::print(stderr, |
| 316 |
"\nERROR: io_shell does not support '{}' meshes. Only 'Unstructured' or " |
| 317 |
"'Structured' mesh is supported at this time.\n", |
| 318 |
region.mesh_type_string()); |
| 319 |
} |
| 320 |
return success; |
| 321 |
} |
| 322 |
|
| 323 |
// Get length of longest name on input file... |
| 324 |
int max_name_length = dbi->maximum_symbol_length(); |
| 325 |
if (max_name_length > 0) { |
| 326 |
properties.add(Ioss::Property("MAXIMUM_NAME_LENGTH", max_name_length)); |
| 327 |
} |
| 328 |
|
| 329 |
// Get integer size being used on the input file and propagate |
| 330 |
// to output file... |
| 331 |
int int_byte_size_api = dbi->int_byte_size_api(); |
| 332 |
if (!properties.exists("INTEGER_SIZE_API")) { |
| 333 |
if (interFace.ints_32_bit) { |
| 334 |
properties.add(Ioss::Property("INTEGER_SIZE_DB", 4)); |
| 335 |
} |
| 336 |
properties.add(Ioss::Property("INTEGER_SIZE_API", int_byte_size_api)); |
| 337 |
} |
| 338 |
if (int_byte_size_api == 8) { |
| 339 |
interFace.ints_64_bit = true; |
| 340 |
} |
| 341 |
|
| 342 |
//======================================================================== |
| 343 |
// OUTPUT Database... |
| 344 |
//======================================================================== |
| 345 |
bool append = false; |
| 346 |
if (interFace.append_step < std::numeric_limits<int>::max()) { |
| 347 |
properties.add(Ioss::Property("APPEND_OUTPUT_AFTER_STEP", interFace.append_step)); |
| 348 |
append = true; |
| 349 |
} |
| 350 |
|
| 351 |
if (interFace.append_time < std::numeric_limits<double>::max()) { |
| 352 |
properties.add(Ioss::Property("APPEND_OUTPUT_AFTER_TIME", interFace.append_time)); |
| 353 |
append = true; |
| 354 |
} |
| 355 |
|
| 356 |
if (append) { |
| 357 |
properties.add(Ioss::Property("APPEND_OUTPUT", Ioss::DB_APPEND)); |
| 358 |
} |
| 359 |
|
| 360 |
if (interFace.minimize_open_files) { |
| 361 |
properties.add(Ioss::Property("MINIMIZE_OPEN_FILES", "ON")); |
| 362 |
} |
| 363 |
|
| 364 |
Ioss::MeshCopyOptions options = set_mesh_copy_options(interFace); |
| 365 |
|
| 366 |
size_t ts_count = region.get_optional_property("state_count", 0); |
| 367 |
|
| 368 |
int flush_interval = interFace.flush_interval; // Default is zero -- do not flush until end |
| 369 |
properties.add(Ioss::Property("FLUSH_INTERVAL", flush_interval)); |
| 370 |
|
| 371 |
int change_set_count = dbi->num_internal_change_set(); |
| 372 |
|
| 373 |
if (interFace.split_times == 0 || interFace.delete_timesteps || ts_count == 0 || append || |
| 374 |
interFace.inputFile.size() > 1 || |
| 375 |
(change_set_count > 1 && interFace.changeSetName.empty())) { |
| 376 |
if (interFace.inputFile.size() > 1 || |
| 377 |
(change_set_count > 1 && interFace.changeSetName.empty())) { |
| 378 |
properties.add(Ioss::Property("ENABLE_FILE_GROUPS", "YES")); |
| 379 |
} |
| 380 |
Ioss::DatabaseIO *dbo = Ioss::IOFactory::create( |
| 381 |
interFace.outFiletype, interFace.outputFile, Ioss::WRITE_RESTART, |
| 382 |
Ioss::ParallelUtils::comm_world(), properties); |
| 383 |
if (dbo == nullptr || !dbo->ok(true)) { |
| 384 |
std::exit(EXIT_FAILURE); |
| 385 |
} |
| 386 |
|
| 387 |
dbo->set_field_separator(interFace.fieldSuffixSeparator); |
| 388 |
|
| 389 |
// NOTE: 'output_region' owns 'dbo' pointer at this time |
| 390 |
Ioss::Region output_region(dbo, "region_2"); |
| 391 |
// Set the qa information... |
| 392 |
output_region.property_add(Ioss::Property(std::string("code_name"), codename)); |
| 393 |
output_region.property_add(Ioss::Property(std::string("code_version"), version)); |
| 394 |
|
| 395 |
if (interFace.inputFile.size() > 1) { |
| 396 |
properties.add(Ioss::Property("APPEND_OUTPUT", Ioss::DB_APPEND_GROUP)); |
| 397 |
|
| 398 |
// Putting each file into its own change_set in the output file... |
| 399 |
// The name of the change_set will be the basename portion of the filename... |
| 400 |
Ioss::FileInfo file(inpfile); |
| 401 |
|
| 402 |
success = dbo->create_internal_change_set(file.tailname()); |
| 403 |
if (!success) { |
| 404 |
if (rank == 0) { |
| 405 |
fmt::print(stderr, "ERROR: Unable to create change set {} in output file.\n", |
| 406 |
file.tailname()); |
| 407 |
} |
| 408 |
return success; |
| 409 |
} |
| 410 |
} |
| 411 |
|
| 412 |
if (change_set_count > 1 && interFace.changeSetName.empty()) { |
| 413 |
bool first = true; |
| 414 |
Ioss::NameList cs_names; |
| 415 |
if (!options.selected_change_sets.empty() && options.selected_change_sets != "ALL") { |
| 416 |
cs_names = Ioss::tokenize(options.selected_change_sets, ","); |
| 417 |
success = true; |
| 418 |
for (const auto &cs_name : cs_names) { |
| 419 |
success &= Ioss::Utils::check_valid_change_set_name(cs_name, region); |
| 420 |
} |
| 421 |
if (!success) { |
| 422 |
return false; |
| 423 |
} |
| 424 |
} |
| 425 |
else { |
| 426 |
cs_names = dbi->internal_change_set_describe(); |
| 427 |
} |
| 428 |
auto observer = std::make_shared<Observer>(); |
| 429 |
output_region.register_mesh_modification_observer(observer); |
| 430 |
|
| 431 |
int steps = 0; |
| 432 |
for (const auto &cs_name : cs_names) { |
| 433 |
success = region.load_internal_change_set_mesh(cs_name); |
| 434 |
if (!success) { |
| 435 |
if (rank == 0) { |
| 436 |
fmt::print(stderr, "ERROR: Unable to open change set {} in input file.\n", cs_name); |
| 437 |
} |
| 438 |
return success; |
| 439 |
} |
| 440 |
if (steps > 0) { |
| 441 |
observer->set_topology_modification(Ioss::TOPOLOGY_UNKNOWN); |
| 442 |
output_region.start_new_output_database_entry(steps); |
| 443 |
output_region.reset_region(); |
| 444 |
output_region.get_database()->release_memory(); |
| 445 |
} |
| 446 |
steps++; |
| 447 |
if (rank == 0) { |
| 448 |
fmt::print(stderr, "Copying change set {}\n", cs_name); |
| 449 |
} |
| 450 |
if (!first) { |
| 451 |
options.ignore_qa_info = true; |
| 452 |
} |
| 453 |
Ioss::copy_database(region, output_region, options); |
| 454 |
first = false; |
| 455 |
} |
| 456 |
} |
| 457 |
else { |
| 458 |
// Do normal copy... |
| 459 |
Ioss::copy_database(region, output_region, options); |
| 460 |
} |
| 461 |
|
| 462 |
if (mem_stats) { |
| 463 |
dbo->release_memory(); |
| 464 |
} |
| 465 |
} |
| 466 |
else { |
| 467 |
// We are splitting out the timesteps into separate files. |
| 468 |
// Each file will contain `split_times` timesteps. If |
| 469 |
// 'split_cyclic` is > 0, then recycle filenames |
| 470 |
// (A,B,C,A,B,C,...) otherwise |
| 471 |
// keep creating new filenames (0001, 0002, 0003, ...) |
| 472 |
|
| 473 |
// Get list of all times on input database... |
| 474 |
std::vector<double> times; |
| 475 |
for (size_t step = 0; step < ts_count; step++) { |
| 476 |
double time = region.get_state_time(step + 1); |
| 477 |
if (time < interFace.minimum_time) { |
| 478 |
continue; |
| 479 |
} |
| 480 |
if (time > interFace.maximum_time) { |
| 481 |
break; |
| 482 |
} |
| 483 |
times.push_back(time); |
| 484 |
} |
| 485 |
ts_count = times.size(); |
| 486 |
|
| 487 |
int splits = (ts_count + interFace.split_times - 1) / interFace.split_times; |
| 488 |
int width = std::to_string(splits).length(); |
| 489 |
for (int split = 0; split < splits; split++) { |
| 490 |
int step_min = split * interFace.split_times; |
| 491 |
int step_max = step_min + interFace.split_times - 1; |
| 492 |
if (step_max >= (int)times.size()) { |
| 493 |
step_max = (int)times.size() - 1; |
| 494 |
} |
| 495 |
options.minimum_time = times[step_min]; |
| 496 |
options.maximum_time = times[step_max]; |
| 497 |
|
| 498 |
std::string filename = interFace.outputFile; |
| 499 |
if (interFace.split_cyclic > 0) { |
| 500 |
static const std::string suffix{"ABCDEFGHIJKLMNOPQRSTUVWXYZ"}; |
| 501 |
filename += "." + suffix.substr(split % interFace.split_cyclic, 1); |
| 502 |
} |
| 503 |
else { |
| 504 |
filename = fmt::format("{0}_{1:0{2}}", filename, split + 1, width); |
| 505 |
} |
| 506 |
|
| 507 |
if (rank == 0 && !interFace.quiet) { |
| 508 |
if (step_min == step_max) { |
| 509 |
fmt::print(stderr, "\tWriting step {} to {}\n", fmt::group_digits(step_min + 1), |
| 510 |
filename); |
| 511 |
} |
| 512 |
else { |
| 513 |
fmt::print(stderr, "\tWriting steps {}..{} to {}\n", fmt::group_digits(step_min + 1), |
| 514 |
fmt::group_digits(step_max + 1), filename); |
| 515 |
} |
| 516 |
} |
| 517 |
|
| 518 |
Ioss::DatabaseIO *dbo = |
| 519 |
Ioss::IOFactory::create(interFace.outFiletype, filename, Ioss::WRITE_RESTART, |
| 520 |
Ioss::ParallelUtils::comm_world(), properties); |
| 521 |
if (dbo == nullptr || !dbo->ok(true)) { |
| 522 |
std::exit(EXIT_FAILURE); |
| 523 |
} |
| 524 |
|
| 525 |
dbo->set_field_separator(interFace.fieldSuffixSeparator); |
| 526 |
|
| 527 |
// NOTE: 'output_region' owns 'dbo' pointer at this time |
| 528 |
Ioss::Region output_region(dbo, "region_2"); |
| 529 |
// Set the qa information... |
| 530 |
output_region.property_add(Ioss::Property(std::string("code_name"), codename)); |
| 531 |
output_region.property_add(Ioss::Property(std::string("code_version"), version)); |
| 532 |
|
| 533 |
Ioss::copy_database(region, output_region, options); |
| 534 |
if (mem_stats) { |
| 535 |
dbo->release_memory(); |
| 536 |
} |
| 537 |
options.verbose = false; |
| 538 |
} |
| 539 |
} |
| 540 |
if (mem_stats) { |
| 541 |
dbi->progress("Prior to Memory Released... "); |
| 542 |
dbi->release_memory(); |
| 543 |
dbi->progress("Memory Released... "); |
| 544 |
} |
| 545 |
} // loop over input files |
| 546 |
return true; |
| 547 |
} |
| 548 |
|
| 549 |
bool file_compare(IOShell::Interface &interFace) |
| 550 |
{ |
| 551 |
Ioss::PropertyManager properties = set_properties(interFace); |
| 552 |
const auto &inpfile = interFace.inputFile[0]; |
| 553 |
|
| 554 |
//======================================================================== |
| 555 |
// INPUT Database #1... |
| 556 |
//======================================================================== |
| 557 |
Ioss::DatabaseIO *dbi1 = |
| 558 |
Ioss::IOFactory::create(interFace.inFiletype, inpfile, Ioss::READ_MODEL, |
| 559 |
Ioss::ParallelUtils::comm_world(), properties); |
| 560 |
if (dbi1 == nullptr || !dbi1->ok(true)) { |
| 561 |
std::exit(EXIT_FAILURE); |
| 562 |
} |
| 563 |
|
| 564 |
if (mem_stats) { |
| 565 |
dbi1->progress("Database #1 Open"); |
| 566 |
} |
| 567 |
if (!interFace.lower_case_variable_names) { |
| 568 |
dbi1->set_lower_case_variable_names(false); |
| 569 |
} |
| 570 |
if (interFace.outFiletype == "cgns") { |
| 571 |
// CGNS stores BCs (SideSets) on the zones which |
| 572 |
// correspond to element blocks. If split input sideblocks |
| 573 |
// by element block, then output is much easier. |
| 574 |
dbi1->set_surface_split_type(Ioss::SPLIT_BY_ELEMENT_BLOCK); |
| 575 |
} |
| 576 |
else if (interFace.surface_split_type != Ioss::SPLIT_INVALID) { |
| 577 |
dbi1->set_surface_split_type(Ioss::int_to_surface_split(interFace.surface_split_type)); |
| 578 |
} |
| 579 |
dbi1->set_field_separator(interFace.fieldSuffixSeparator); |
| 580 |
|
| 581 |
dbi1->set_field_recognition(!interFace.disable_field_recognition); |
| 582 |
|
| 583 |
if (interFace.ints_64_bit) { |
| 584 |
dbi1->set_int_byte_size_api(Ioss::USE_INT64_API); |
| 585 |
} |
| 586 |
|
| 587 |
// NOTE: 'input_region1' owns 'dbi1' pointer at this time... |
| 588 |
Ioss::Region input_region1(dbi1, "region_1"); |
| 589 |
|
| 590 |
if (input_region1.mesh_type() == Ioss::MeshType::HYBRID) { |
| 591 |
fmt::print(stderr, |
| 592 |
"\nERROR: io_shell does not support '{}' meshes. Only 'Unstructured' or " |
| 593 |
"'Structured' mesh is supported at this time.\n", |
| 594 |
input_region1.mesh_type_string()); |
| 595 |
return false; |
| 596 |
} |
| 597 |
|
| 598 |
// Get integer size being used on input file #1 and set it in |
| 599 |
// the interFace. |
| 600 |
int int_byte_size_api = dbi1->int_byte_size_api(); |
| 601 |
if (int_byte_size_api == 8) { |
| 602 |
interFace.ints_64_bit = true; |
| 603 |
} |
| 604 |
|
| 605 |
//======================================================================== |
| 606 |
// INPUT Database #2... |
| 607 |
//======================================================================== |
| 608 |
Ioss::DatabaseIO *dbi2 = |
| 609 |
Ioss::IOFactory::create(interFace.outFiletype, interFace.outputFile, Ioss::READ_MODEL, |
| 610 |
Ioss::ParallelUtils::comm_world(), properties); |
| 611 |
if (dbi2 == nullptr || !dbi2->ok(true)) { |
| 612 |
std::exit(EXIT_FAILURE); |
| 613 |
} |
| 614 |
|
| 615 |
if (mem_stats) { |
| 616 |
dbi2->progress("Database #2 Open"); |
| 617 |
} |
| 618 |
if (!interFace.lower_case_variable_names) { |
| 619 |
dbi2->set_lower_case_variable_names(false); |
| 620 |
} |
| 621 |
if (interFace.outFiletype == "cgns") { |
| 622 |
// CGNS stores BCs (SideSets) on the zones which |
| 623 |
// correspond to element blocks. If split input sideblocks |
| 624 |
// by element block, then output is much easier. |
| 625 |
dbi2->set_surface_split_type(Ioss::SPLIT_BY_ELEMENT_BLOCK); |
| 626 |
} |
| 627 |
else if (interFace.surface_split_type != Ioss::SPLIT_INVALID) { |
| 628 |
dbi2->set_surface_split_type(Ioss::int_to_surface_split(interFace.surface_split_type)); |
| 629 |
} |
| 630 |
dbi2->set_field_separator(interFace.fieldSuffixSeparator); |
| 631 |
|
| 632 |
dbi2->set_field_recognition(!interFace.disable_field_recognition); |
| 633 |
|
| 634 |
if (interFace.ints_64_bit) { |
| 635 |
dbi2->set_int_byte_size_api(Ioss::USE_INT64_API); |
| 636 |
} |
| 637 |
|
| 638 |
// NOTE: 'input_region2' owns 'dbi2' pointer at this time... |
| 639 |
Ioss::Region input_region2(dbi2, "region_2"); |
| 640 |
|
| 641 |
if (input_region2.mesh_type() == Ioss::MeshType::HYBRID) { |
| 642 |
fmt::print(stderr, |
| 643 |
"\nERROR: io_shell does not support '{}' meshes. Only 'Unstructured' or " |
| 644 |
"'Structured' mesh is supported at this time.\n", |
| 645 |
input_region2.mesh_type_string()); |
| 646 |
return false; |
| 647 |
} |
| 648 |
|
| 649 |
// Get integer size being used on input file #1 and set it in |
| 650 |
// the interFace. |
| 651 |
int_byte_size_api = dbi2->int_byte_size_api(); |
| 652 |
if (int_byte_size_api == 8) { |
| 653 |
interFace.ints_64_bit = true; |
| 654 |
} |
| 655 |
|
| 656 |
//======================================================================== |
| 657 |
// COMPARE the databases... |
| 658 |
//======================================================================== |
| 659 |
auto options = set_mesh_copy_options(interFace); |
| 660 |
|
| 661 |
bool result = Ioss::Compare::compare_database(input_region1, input_region2, options); |
| 662 |
if (result) { |
| 663 |
fmt::print(stderr, "\n\nDATABASES are EQUAL"); |
| 664 |
} |
| 665 |
else { |
| 666 |
fmt::print(stderr, "\n\nDATABASES are NOT equal"); |
| 667 |
} |
| 668 |
return result; |
| 669 |
} |
| 670 |
|
| 671 |
Ioss::PropertyManager set_properties(IOShell::Interface &interFace) |
| 672 |
{ |
| 673 |
Ioss::PropertyManager properties; |
| 674 |
|
| 675 |
if (interFace.ints_64_bit) { |
| 676 |
properties.add(Ioss::Property("INTEGER_SIZE_DB", 8)); |
| 677 |
properties.add(Ioss::Property("INTEGER_SIZE_API", 8)); |
| 678 |
} |
| 679 |
|
| 680 |
if (interFace.ints_32_bit) { |
| 681 |
properties.add(Ioss::Property("INTEGER_SIZE_DB", 4)); |
| 682 |
} |
| 683 |
|
| 684 |
if (interFace.reals_32_bit) { |
| 685 |
properties.add(Ioss::Property("REAL_SIZE_DB", 4)); |
| 686 |
} |
| 687 |
|
| 688 |
if (interFace.in_memory_read) { |
| 689 |
properties.add(Ioss::Property("MEMORY_READ", 1)); |
| 690 |
} |
| 691 |
|
| 692 |
if (interFace.in_memory_write) { |
| 693 |
properties.add(Ioss::Property("MEMORY_WRITE", 1)); |
| 694 |
} |
| 695 |
|
| 696 |
if (interFace.delete_qa) { |
| 697 |
properties.add(Ioss::Property("IGNORE_QA_RECORDS", "YES")); |
| 698 |
} |
| 699 |
if (interFace.delete_info) { |
| 700 |
properties.add(Ioss::Property("IGNORE_INFO_RECORDS", "YES")); |
| 701 |
} |
| 702 |
|
| 703 |
if (interFace.compression_level > 0 || interFace.shuffle || interFace.szip || interFace.quant || |
| 704 |
interFace.zlib || interFace.zstd || interFace.bz2) { |
| 705 |
properties.add(Ioss::Property("FILE_TYPE", "netcdf4")); |
| 706 |
properties.add(Ioss::Property("COMPRESSION_LEVEL", interFace.compression_level)); |
| 707 |
properties.add(Ioss::Property("COMPRESSION_SHUFFLE", static_cast<int>(interFace.shuffle))); |
| 708 |
|
| 709 |
if (interFace.zlib) { |
| 710 |
properties.add(Ioss::Property("COMPRESSION_METHOD", "zlib")); |
| 711 |
} |
| 712 |
else if (interFace.szip) { |
| 713 |
properties.add(Ioss::Property("COMPRESSION_METHOD", "szip")); |
| 714 |
} |
| 715 |
else if (interFace.zstd) { |
| 716 |
properties.add(Ioss::Property("COMPRESSION_METHOD", "zstd")); |
| 717 |
} |
| 718 |
else if (interFace.bz2) { |
| 719 |
properties.add(Ioss::Property("COMPRESSION_METHOD", "bzip2")); |
| 720 |
} |
| 721 |
|
| 722 |
if (interFace.quant) { |
| 723 |
properties.add(Ioss::Property("COMPRESSION_QUANTIZE_NSD", interFace.quantize_nsd)); |
| 724 |
} |
| 725 |
} |
| 726 |
|
| 727 |
if (interFace.compose_output == "default") { |
| 728 |
if (interFace.outFiletype == "cgns") { |
| 729 |
properties.add(Ioss::Property("COMPOSE_RESULTS", "YES")); |
| 730 |
properties.add(Ioss::Property("COMPOSE_RESTART", "YES")); |
| 731 |
} |
| 732 |
else { |
| 733 |
properties.add(Ioss::Property("COMPOSE_RESULTS", "NO")); |
| 734 |
properties.add(Ioss::Property("COMPOSE_RESTART", "NO")); |
| 735 |
} |
| 736 |
} |
| 737 |
else if (interFace.compose_output == "external") { |
| 738 |
properties.add(Ioss::Property("COMPOSE_RESULTS", "NO")); |
| 739 |
properties.add(Ioss::Property("COMPOSE_RESTART", "NO")); |
| 740 |
} |
| 741 |
else if (interFace.compose_output != "none") { |
| 742 |
properties.add(Ioss::Property("COMPOSE_RESULTS", "YES")); |
| 743 |
properties.add(Ioss::Property("COMPOSE_RESTART", "YES")); |
| 744 |
} |
| 745 |
|
| 746 |
if (interFace.file_per_state) { |
| 747 |
properties.add(Ioss::Property("FILE_PER_STATE", "YES")); |
| 748 |
} |
| 749 |
|
| 750 |
if (interFace.netcdf4) { |
| 751 |
properties.add(Ioss::Property("FILE_TYPE", "netcdf4")); |
| 752 |
} |
| 753 |
|
| 754 |
if (interFace.netcdf5) { |
| 755 |
properties.add(Ioss::Property("FILE_TYPE", "netcdf5")); |
| 756 |
} |
| 757 |
|
| 758 |
if (interFace.inputFile.size() > 1) { |
| 759 |
properties.add(Ioss::Property("ENABLE_FILE_GROUPS", 1)); |
| 760 |
} |
| 761 |
|
| 762 |
if (interFace.debug) { |
| 763 |
properties.add(Ioss::Property("LOGGING", 1)); |
| 764 |
} |
| 765 |
|
| 766 |
if (interFace.detect_nans) { |
| 767 |
properties.add(Ioss::Property("NAN_DETECTION", 1)); |
| 768 |
} |
| 769 |
|
| 770 |
if (interFace.memory_statistics) { |
| 771 |
properties.add(Ioss::Property("ENABLE_TRACING", 1)); |
| 772 |
} |
| 773 |
|
| 774 |
if (interFace.outFiletype == "cgns" && interFace.inFiletype == "exodus") { |
| 775 |
properties.add(Ioss::Property("IGNORE_NODE_MAP", true)); |
| 776 |
properties.add(Ioss::Property("IGNORE_ELEMENT_MAP", true)); |
| 777 |
} |
| 778 |
else { |
| 779 |
if (interFace.ignore_node_map) { |
| 780 |
properties.add(Ioss::Property("IGNORE_NODE_MAP", true)); |
| 781 |
} |
| 782 |
if (interFace.ignore_elem_map) { |
| 783 |
properties.add(Ioss::Property("IGNORE_ELEM_MAP", true)); |
| 784 |
} |
| 785 |
} |
| 786 |
if (interFace.ignore_edge_map) { |
| 787 |
properties.add(Ioss::Property("IGNORE_EDGE_MAP", true)); |
| 788 |
} |
| 789 |
if (interFace.ignore_face_map) { |
| 790 |
properties.add(Ioss::Property("IGNORE_FACE_MAP", true)); |
| 791 |
} |
| 792 |
|
| 793 |
if (!interFace.decomp_method.empty()) { |
| 794 |
properties.add(Ioss::Property("DECOMPOSITION_METHOD", interFace.decomp_method)); |
| 795 |
if (interFace.decomp_method == "MAP" || interFace.decomp_method == "VARIABLE") { |
| 796 |
properties.add(Ioss::Property("DECOMPOSITION_EXTRA", interFace.decomp_extra)); |
| 797 |
} |
| 798 |
if (interFace.line_decomp) { |
| 799 |
properties.add(Ioss::Property("LINE_DECOMPOSITION", interFace.decomp_extra)); |
| 800 |
} |
| 801 |
} |
| 802 |
|
| 803 |
if (interFace.retain_empty_blocks) { |
| 804 |
properties.add(Ioss::Property("RETAIN_EMPTY_BLOCKS", "YES")); |
| 805 |
} |
| 806 |
return properties; |
| 807 |
} |
| 808 |
} // namespace |