| 1 |
// Copyright(C) 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 "Ioss_DynamicTopologyStateLocator.h" |
| 8 |
|
| 9 |
#include "Ioss_ChangeSet.h" |
| 10 |
#include "Ioss_ChangeSetFactory.h" |
| 11 |
#include "Ioss_CodeTypes.h" |
| 12 |
#include "Ioss_DBUsage.h" |
| 13 |
#include "Ioss_DatabaseIO.h" |
| 14 |
#include "Ioss_ParallelUtils.h" |
| 15 |
#include "Ioss_Region.h" |
| 16 |
|
| 17 |
#include <assert.h> |
| 18 |
#include <climits> |
| 19 |
#include <cstddef> |
| 20 |
#include <functional> |
| 21 |
#include <sstream> |
| 22 |
|
| 23 |
namespace Ioss { |
| 24 |
|
| 25 |
DynamicTopologyStateLocator::DynamicTopologyStateLocator(Region *region, bool loadAllFiles) |
| 26 |
: m_database(region->get_database()), |
| 27 |
m_ioDB(region->get_property("base_filename").get_string()), |
| 28 |
m_dbType(region->get_property("database_type").get_string()), |
| 29 |
m_fileCyclicCount(region->get_file_cyclic_count()), m_loadAllFiles(loadAllFiles) |
| 30 |
{ |
| 31 |
} |
| 32 |
|
| 33 |
DynamicTopologyStateLocator::DynamicTopologyStateLocator(Ioss::DatabaseIO *db, |
| 34 |
const std::string &dbName, |
| 35 |
const std::string &dbType, |
| 36 |
unsigned fileCyclicCount, |
| 37 |
bool loadAllFiles) |
| 38 |
: m_database(db), m_ioDB(dbName), m_dbType(dbType), m_fileCyclicCount(fileCyclicCount), |
| 39 |
m_loadAllFiles(loadAllFiles) |
| 40 |
{ |
| 41 |
} |
| 42 |
|
| 43 |
DynamicTopologyStateLocator::DynamicTopologyStateLocator(Ioss::DatabaseIO *db, |
| 44 |
unsigned fileCyclicCount, |
| 45 |
bool loadAllFiles) |
| 46 |
: m_database(db), |
| 47 |
m_ioDB(db->get_property_manager().get_optional("base_filename", db->get_filename())), |
| 48 |
m_dbType(db->get_property_manager().get_optional("database_type", "")), |
| 49 |
m_fileCyclicCount(fileCyclicCount), m_loadAllFiles(loadAllFiles) |
| 50 |
{ |
| 51 |
} |
| 52 |
|
| 53 |
DynamicTopologyStateLocator::~DynamicTopologyStateLocator() {} |
| 54 |
|
| 55 |
const ParallelUtils &DynamicTopologyStateLocator::util() const { return get_database()->util(); } |
| 56 |
|
| 57 |
DatabaseIO *DynamicTopologyStateLocator::get_database() const { return m_database; } |
| 58 |
|
| 59 |
std::tuple<std::string, int, double> |
| 60 |
DynamicTopologyStateLocator::locate_db_state(double targetTime) const |
| 61 |
{ |
| 62 |
auto db = get_database(); |
| 63 |
DatabaseState loc(db); |
| 64 |
|
| 65 |
locate_db_state_impl(targetTime, loc); |
| 66 |
|
| 67 |
return std::make_tuple(loc.changeSet, loc.state, loc.time); |
| 68 |
} |
| 69 |
|
| 70 |
std::tuple<std::string, int, double> DynamicTopologyStateLocator::get_db_max_time() const |
| 71 |
{ |
| 72 |
auto db = get_database(); |
| 73 |
DatabaseState loc(db); |
| 74 |
|
| 75 |
double init_time = -std::numeric_limits<double>::max(); |
| 76 |
StateLocatorCompare compare = [](double a, double b) { return (a > b); }; |
| 77 |
|
| 78 |
get_db_time_impl(init_time, compare, loc); |
| 79 |
|
| 80 |
return std::make_tuple(loc.changeSet, loc.state, loc.time); |
| 81 |
} |
| 82 |
|
| 83 |
std::tuple<std::string, int, double> DynamicTopologyStateLocator::get_db_min_time() const |
| 84 |
{ |
| 85 |
auto db = get_database(); |
| 86 |
DatabaseState loc(db); |
| 87 |
|
| 88 |
double init_time = std::numeric_limits<double>::max(); |
| 89 |
StateLocatorCompare compare = [](double a, double b) { return (a < b); }; |
| 90 |
|
| 91 |
get_db_time_impl(init_time, compare, loc); |
| 92 |
|
| 93 |
return std::make_tuple(loc.changeSet, loc.state, loc.time); |
| 94 |
} |
| 95 |
|
| 96 |
void DynamicTopologyStateLocator::locate_state_impl(Ioss::DatabaseIO *db, double targetTime, |
| 97 |
StateLocatorCompare comparator, |
| 98 |
DatabaseState &loc) const |
| 99 |
{ |
| 100 |
std::vector<double> timesteps = db->get_db_step_times(); |
| 101 |
size_t stepCount = timesteps.size(); |
| 102 |
|
| 103 |
double minTimeDiff = |
| 104 |
loc.state < 0 ? std::numeric_limits<double>::max() : std::fabs(loc.time - targetTime); |
| 105 |
|
| 106 |
for (size_t istep = 1; istep <= stepCount; istep++) { |
| 107 |
double stateTime = timesteps[istep - 1]; |
| 108 |
double stepTimeDiff = std::fabs(stateTime - targetTime); |
| 109 |
if (comparator(stepTimeDiff, minTimeDiff)) { |
| 110 |
minTimeDiff = stepTimeDiff; |
| 111 |
loc.time = stateTime; |
| 112 |
loc.state = static_cast<int>(istep); |
| 113 |
loc.changeSet = db->supports_internal_change_set() ? db->get_internal_change_set_name() |
| 114 |
: db->get_filename(); |
| 115 |
} |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
void DynamicTopologyStateLocator::locate_state(Ioss::DatabaseIO *db, double targetTime, |
| 120 |
DatabaseState &loc) const |
| 121 |
{ |
| 122 |
if (targetTime < 0.0) { |
| 123 |
// Round towards 0 |
| 124 |
StateLocatorCompare compare = [](double a, double b) { return (a <= b); }; |
| 125 |
locate_state_impl(db, targetTime, compare, loc); |
| 126 |
} |
| 127 |
else { |
| 128 |
// Round towards 0 |
| 129 |
StateLocatorCompare compare = [](double a, double b) { return (a < b); }; |
| 130 |
locate_state_impl(db, targetTime, compare, loc); |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
void DynamicTopologyStateLocator::locate_db_state_impl(double targetTime, |
| 135 |
DatabaseState &loc) const |
| 136 |
{ |
| 137 |
auto changeSet = |
| 138 |
Ioss::ChangeSetFactory::create(m_database, m_ioDB, m_dbType, m_fileCyclicCount); |
| 139 |
changeSet->populate_change_sets(m_loadAllFiles); |
| 140 |
|
| 141 |
for (size_t csIndex = 0; csIndex < changeSet->size(); csIndex++) { |
| 142 |
auto csdb = changeSet->open_change_set(csIndex, Ioss::QUERY_TIMESTEPS_ONLY); |
| 143 |
locate_state(csdb, targetTime, loc); |
| 144 |
changeSet->close_change_set(csIndex); |
| 145 |
} |
| 146 |
} |
| 147 |
|
| 148 |
void DynamicTopologyStateLocator::get_db_time_impl(double init_time, |
| 149 |
StateLocatorCompare comparator, |
| 150 |
DatabaseState &loc) const |
| 151 |
{ |
| 152 |
auto changeSet = |
| 153 |
Ioss::ChangeSetFactory::create(m_database, m_ioDB, m_dbType, m_fileCyclicCount); |
| 154 |
changeSet->populate_change_sets(m_loadAllFiles); |
| 155 |
|
| 156 |
double best_time = init_time; |
| 157 |
|
| 158 |
for (size_t csIndex = 0; csIndex < changeSet->size(); csIndex++) { |
| 159 |
auto csdb = changeSet->open_change_set(csIndex, Ioss::QUERY_TIMESTEPS_ONLY); |
| 160 |
|
| 161 |
std::vector<double> timesteps = csdb->get_db_step_times(); |
| 162 |
int stepCount = static_cast<int>(timesteps.size()); |
| 163 |
|
| 164 |
for (int i = 1; i <= stepCount; i++) { |
| 165 |
if (comparator(timesteps[i - 1], best_time)) { |
| 166 |
loc.time = timesteps[i - 1]; |
| 167 |
loc.state = i; |
| 168 |
loc.changeSet = changeSet->get_change_set_name(csIndex); |
| 169 |
best_time = timesteps[i - 1]; |
| 170 |
} |
| 171 |
} |
| 172 |
|
| 173 |
changeSet->close_change_set(csIndex); |
| 174 |
} |
| 175 |
} |
| 176 |
|
| 177 |
} // namespace Ioss |