6#include <spdlog/fmt/ostr.h>
10#include <unordered_set>
21 return filenamePrefix.empty() || filenamePrefix.ends_with(
'/') ||
22 filenamePrefix.ends_with(
'\\')
28static void outputGlobalMatrix(
GlobalMatrix const& mat, std::ostream& os)
35static void outputGlobalVector(
GlobalVector const& vec, std::ostream& os)
37 os <<
"(" << vec.
size() <<
")\n";
41std::ofstream openGlobalMatrixOutputFile(std::string
const& filenamePrefix,
42 std::size_t
const counter,
43 double const t,
int const process_id,
44 std::string
const& which_matrix,
45 std::string
const& extension)
47 auto const filename = fmt::format(
48 "{}{}ogs_global_matrix_cnt_{:03}_t_{:g}_pcs_{}_{}.{}", filenamePrefix,
50 which_matrix, extension);
52 std::ofstream fh{filename};
56 OGS_FATAL(
"Could not open file `{}' for global matrix debug output",
60 fh << std::setprecision(std::numeric_limits<double>::max_digits10);
66 std::string
const& env_var)
68 char const*
const prefix = std::getenv(env_var.c_str());
69 return prefix ? std::make_optional(prefix) : std::nullopt;
74 return fmt::format(
"{}{}ogs_local_matrix.log", filenamePrefix,
78Eigen::Map<
const Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic,
83 static_cast<Eigen::Index
>(std::round(std::sqrt(entries.size())));
85 return {entries.data(), num_r_c, num_r_c};
94 std::string
const& warn_msg)
96 std::istringstream sstr{str};
97 std::unordered_set<std::size_t> result;
100 while (sstr >> value)
102 [[likely]]
if (value >= 0)
104 result.insert(value);
108 if (!warn_msg.empty())
110 WARN(
"{}", warn_msg);
117 if (!sstr.eof() && !warn_msg.empty())
120 WARN(
"{}", warn_msg);
127 std::string
const& element_ids_str)
129 if (element_ids_str.empty())
134 if (element_ids_str ==
"*")
136 return [](std::size_t) {
return true; };
141 "Error parsing list of element ids for local matrix debug "
142 "output. We'll try to proceed anyway, as best as we can.");
144 if (element_ids.empty())
149 return [element_ids = std::move(element_ids)](std::size_t element_id)
150 {
return element_ids.contains(element_id); };
156 auto opt_prefix = getEnvironmentVariable(
"OGS_GLOBAL_MAT_OUT_PREFIX");
158 if (opt_prefix.has_value())
166 "You requested global matrix output (OGS_GLOBAL_MAT_OUT_PREFIX is "
167 "set), which is not yet implemented for PETSc matrices.");
187 process_id,
"M",
"mat");
190 outputGlobalMatrix(M, fh);
195 process_id,
"K",
"mat");
198 outputGlobalMatrix(K, fh);
203 process_id,
"b",
"vec");
206 outputGlobalVector(b, fh);
232 process_id,
"b",
"vec");
235 outputGlobalVector(b, fh);
240 process_id,
"Jac",
"mat");
243 outputGlobalMatrix(Jac, fh);
256 auto const opt_prefix = getEnvironmentVariable(
"OGS_LOCAL_MAT_OUT_PREFIX");
257 auto const opt_elements =
258 getEnvironmentVariable(
"OGS_LOCAL_MAT_OUT_ELEMENTS");
265 "Environment variable OGS_LOCAL_MAT_OUT_ELEMENTS is set, but "
266 "OGS_LOCAL_MAT_OUT_PREFIX is not. Local matrix debug output "
267 "will be disabled.");
275 "Environment variable OGS_LOCAL_MAT_OUT_PREFIX is set, but "
276 "OGS_LOCAL_MAT_OUT_ELEMENTS is not. Local matrix debug output "
277 "will be disabled.");
287 "Environment variable OGS_LOCAL_MAT_OUT_ELEMENTS not set "
288 "properly. Local matrix debug output will be disabled.");
292 auto const outputFilename = localMatrixOutputFilename(*opt_prefix);
298 "File `{}' for local matrix debug output could not be "
303 DBUG(
"Successfully opened local matrix debug output file {}.",
308 std::size_t
const element_id,
309 std::vector<double>
const& local_M_data,
310 std::vector<double>
const& local_K_data,
311 std::vector<double>
const& local_b_data)
318 std::lock_guard lock_guard{
mutex_};
322 DBUG(
"Writing to local matrix debug output file...");
324 fmt::print(fh,
"## t = {:.{}g}, process id = {}, element id = {}\n\n", t,
325 std::numeric_limits<double>::max_digits10, process_id,
328 if (!local_M_data.empty())
331 fmt::print(fh,
"# M\n{}\n\n", toSquareMatrixRowMajor(local_M_data));
334 if (!local_K_data.empty())
337 fmt::print(fh,
"# K\n{}\n\n", toSquareMatrixRowMajor(local_K_data));
340 if (!local_b_data.empty())
348 std::size_t
const element_id,
349 std::vector<double>
const& local_b_data,
350 std::vector<double>
const& local_Jac_data)
357 std::lock_guard lock_guard{
mutex_};
361 DBUG(
"Writing to local matrix debug output file...");
363 fmt::print(fh,
"## t = {:.{}g}, process id = {}, element id = {}\n\n", t,
364 std::numeric_limits<double>::max_digits10, process_id,
367 if (!local_b_data.empty())
373 if (!local_Jac_data.empty())
376 fmt::print(fh,
"# Jac\n{}\n\n\n",
377 toSquareMatrixRowMajor(local_Jac_data));
MathLib::EigenMatrix GlobalMatrix
MathLib::EigenVector GlobalVector
void DBUG(fmt::format_string< Args... > fmt, Args &&... args)
void WARN(fmt::format_string< Args... > fmt, Args &&... args)
IndexType getNumberOfColumns() const
return the number of columns
void write(const std::string &filename) const
printout this matrix for debugging
IndexType getNumberOfRows() const
return the number of rows
IndexType size() const
return a vector length
RawVectorType & getRawVector()
return a raw Eigen vector object
Eigen::Map< const Vector > toVector(std::vector< double > const &data, Eigen::VectorXd::Index size)
Creates an Eigen mapped vector from the given data vector.
std::unordered_set< std::size_t > parseSetOfSizeT(std::string const &str, std::string const &warn_msg)
std::function< bool(std::size_t)> createLocalMatrixOutputElementPredicate(std::string const &element_ids_str)
std::string localMatrixOutputFilename(std::string const &filenamePrefix)
Eigen::Map< const Eigen::Matrix< double, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor > > toSquareMatrixRowMajor(std::vector< double > entries)
static std::optional< std::string > getEnvironmentVariable(std::string const &env_var)
std::string getSeparatorAfterFilenamePrefix(std::string const &filenamePrefix)
std::string filenamePrefix_
void operator()(double const t, int const process_id, GlobalMatrix const &M, GlobalMatrix const &K, GlobalVector const &b)
bool isOutputRequested(std::size_t const element_id) const
std::function< bool(std::size_t)> output_element_predicate_
void operator()(double const t, int const process_id, std::size_t const element_id, std::vector< double > const &local_M_data, std::vector< double > const &local_K_data, std::vector< double > const &local_b_data)
std::ofstream outputFile_