Merge pull request #6211 from akva2/nlddreporting_minor_janitoring

NlddReporting: some minor refactoring
This commit is contained in:
Bård Skaflestad authored and GitHub committed 2025-05-07 14:26:07 +02:00
commit 1648de24f8
2 files changed
+104 -68

No files matched your search

+53
View File
@@ -33,6 +33,59 @@
namespace Opm {
namespace details {
void printDistributionSummary(const DomainInfo& info)
{
std::ostringstream ss;
ss << "\nNLDD domain distribution summary:\n"
<< " rank owned cells overlap cells total cells wells domains\n"
<< "--------------------------------------------------------------------\n";
int total_owned = std::accumulate(info.all_owned.begin(), info.all_owned.end(), 0);
int total_overlap = std::accumulate(info.all_overlap.begin(), info.all_overlap.end(), 0);
int total_wells = std::accumulate(info.all_wells.begin(), info.all_wells.end(), 0);
int total_domains = std::accumulate(info.all_domains.begin(), info.all_domains.end(), 0);
for (std::size_t r = 0; r < info.all_owned.size(); ++r) {
ss << std::setw(6) << r
<< std::setw(13) << info.all_owned[r]
<< std::setw(15) << info.all_overlap[r]
<< std::setw(14) << (info.all_owned[r] + info.all_overlap[r])
<< std::setw(8) << info.all_wells[r]
<< std::setw(9) << info.all_domains[r] << '\n';
}
ss << "--------------------------------------------------------------------\n"
<< " sum"
<< std::setw(13) << total_owned
<< std::setw(15) << total_overlap
<< std::setw(14) << (total_owned + total_overlap)
<< std::setw(8) << total_wells
<< std::setw(9) << total_domains << '\n';
OpmLog::info(ss.str());
}
void writeNlddFile(const std::string& fname,
std::string_view header,
const std::vector<int>& data)
{
std::ofstream resInsightFile { fname };
// Write header
resInsightFile << header << '\n';
// Write all data
for (const auto& val : data) {
resInsightFile << val << '\n';
}
resInsightFile << "/" << '\n';
}
} // namespace details
/**
* Reports NLDD statistics after simulation.
*
+51 -68
View File
@@ -23,25 +23,60 @@
#include <dune/grid/common/gridenums.hh>
#include <dune/grid/common/partitionset.hh>
#include <opm/common/ErrorMacros.hpp>
#include <opm/common/OpmLog/OpmLog.hpp>
#include <opm/simulators/timestepping/SimulatorReport.hpp>
#include <opm/simulators/utils/DeferredLogger.hpp>
#include <opm/simulators/utils/ParallelCommunication.hpp>
#include <fmt/format.h>
#include <algorithm>
#include <cmath>
#include <filesystem>
#include <fstream>
#include <limits>
#include <numeric>
#include <sstream>
#include <string_view>
#include <vector>
#include <fmt/format.h>
namespace Opm {
namespace details {
/**
* Struct holding information about domains used for printing a summary.
*/
struct DomainInfo
{
std::vector<int> all_owned; //!< All owned cells
std::vector<int> all_overlap; //!< All overlap cells
std::vector<int> all_wells; //!< All wells
std::vector<int> all_domains; //!< All domains
explicit DomainInfo(std::size_t size)
: all_owned(size)
, all_overlap(size)
, all_wells(size)
, all_domains(size)
{}
};
/**
* Print a summary of domain distribution to the log
* @param info Struct holding info about the domains
*/
void printDistributionSummary(const DomainInfo& info);
/**
* Write NLDD information to a text file
* @param file File to write to
* @param header Header in file
* @param data Data to write to file
*/
void writeNlddFile(const std::string& file,
std::string_view header,
const std::vector<int>& data);
} // namespace details
/**
* Reports NLDD statistics after simulation.
*
@@ -111,16 +146,8 @@ void writeNonlinearIterationsPerCell(
// Only rank 0 writes the file
if (rank == 0) {
auto fname = odir / "ResInsight_nonlinear_iterations.txt";
std::ofstream resInsightFile { fname };
// Write header
resInsightFile << "NLDD_ITER" << '\n';
// Write all cells, including inactive ones
for (const auto& val : full_iterations) {
resInsightFile << val << '\n';
}
resInsightFile << "/" << '\n';
details::writeNlddFile(odir / "ResInsight_nonlinear_iterations.txt",
"NLDD_ITER", full_iterations);
}
}
@@ -162,17 +189,8 @@ void writePartitions(
// Only rank 0 writes the file
if (rank == 0) {
auto fname = odir / "ResInsight_compatible_partition.txt";
std::ofstream resInsightFile { fname };
// Write header
resInsightFile << "NLDD_DOM" << '\n';
// Write all cells, including inactive ones
for (const auto& val : full_partition) {
resInsightFile << val << '\n';
}
resInsightFile << "/" << '\n';
details::writeNlddFile(odir / "ResInsight_compatible_partition.txt",
"NLDD_DOM", full_partition);
}
const auto nDigit = 1 + static_cast<int>(std::floor(std::log10(comm.size())));
@@ -233,50 +251,15 @@ void printDomainDistributionSummary(
}
// Gather data from all ranks
std::vector<int> all_owned(comm.size());
std::vector<int> all_overlap(comm.size());
std::vector<int> all_wells(comm.size());
std::vector<int> all_domains(comm.size());
details::DomainInfo info(comm.size());
comm.gather(&owned_cells, all_owned.data(), 1, 0);
comm.gather(&overlap_cells, all_overlap.data(), 1, 0);
comm.gather(&num_wells, all_wells.data(), 1, 0);
comm.gather(&num_domains, all_domains.data(), 1, 0);
comm.gather(&owned_cells, info.all_owned.data(), 1, 0);
comm.gather(&overlap_cells, info.all_overlap.data(), 1, 0);
comm.gather(&num_wells, info.all_wells.data(), 1, 0);
comm.gather(&num_domains, info.all_domains.data(), 1, 0);
if (rank == 0) {
std::ostringstream ss;
ss << "\nNLDD domain distribution summary:\n"
<< " rank owned cells overlap cells total cells wells domains\n"
<< "--------------------------------------------------------------------\n";
int total_owned = 0;
int total_overlap = 0;
int total_wells = 0;
int total_domains = 0;
for (int r = 0; r < comm.size(); ++r) {
ss << std::setw(6) << r
<< std::setw(13) << all_owned[r]
<< std::setw(15) << all_overlap[r]
<< std::setw(14) << (all_owned[r] + all_overlap[r])
<< std::setw(8) << all_wells[r]
<< std::setw(9) << all_domains[r] << '\n';
total_owned += all_owned[r];
total_overlap += all_overlap[r];
total_wells += all_wells[r];
total_domains += all_domains[r];
}
ss << "--------------------------------------------------------------------\n"
<< " sum"
<< std::setw(13) << total_owned
<< std::setw(15) << total_overlap
<< std::setw(14) << (total_owned + total_overlap)
<< std::setw(8) << total_wells
<< std::setw(9) << total_domains << '\n';
OpmLog::info(ss.str());
details::printDistributionSummary(info);
}
}