13#include <spdlog/fmt/bundled/ostream.h>
16#include <unordered_set>
27 return filenamePrefix.empty() || filenamePrefix.ends_with(
'/') ||
28 filenamePrefix.ends_with(
'\\')
34static void outputGlobalMatrix(
GlobalMatrix const& mat, std::ostream& os)
41static void outputGlobalVector(
GlobalVector const& vec, std::ostream& os)
43 os << std::setprecision(16) <<
"(" << vec.
size() <<
")\n";
47std::ofstream openGlobalMatrixOutputFile(std::string
const& filenamePrefix,
48 std::size_t
const counter,
49 double const t,
int const process_id,
50 std::string
const& which_matrix,
51 std::string
const& extension)
53 auto const filename = fmt::format(
54 "{}{}ogs_global_matrix_cnt_{:03}_t_{:g}_pcs_{}_{}.{}", filenamePrefix,
56 which_matrix, extension);
58 std::ofstream fh{filename};
62 OGS_FATAL(
"Could not open file `{}' for global matrix debug output",
71 std::string
const& env_var)
73 char const*
const prefix = std::getenv(env_var.c_str());
74 return prefix ? std::make_optional(prefix) : std::nullopt;
79 return fmt::format(
"{}{}ogs_local_matrix.log", filenamePrefix,
83Eigen::Map<
const Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic,
88 static_cast<Eigen::Index
>(std::round(std::sqrt(entries.size())));
90 return {entries.data(), num_r_c, num_r_c};
99 std::string
const& warn_msg)
101 std::istringstream sstr{str};
102 std::unordered_set<std::size_t> result;
103 std::ptrdiff_t value;
105 while (sstr >> value)
107 [[likely]]
if (value >= 0)
109 result.insert(value);
113 if (!warn_msg.empty())
115 WARN(
"{}", warn_msg);
122 if (!sstr.eof() && !warn_msg.empty())
125 WARN(
"{}", warn_msg);
132 std::string
const& element_ids_str)
134 if (element_ids_str.empty())
139 if (element_ids_str ==
"*")
141 return [](std::size_t) {
return true; };
146 "Error parsing list of element ids for local matrix debug "
147 "output. We'll try to proceed anyway, as best as we can.");
149 if (element_ids.empty())
154 return [element_ids = std::move(element_ids)](std::size_t element_id)
155 {
return element_ids.contains(element_id); };
161 auto opt_prefix = getEnvironmentVariable(
"OGS_GLOBAL_MAT_OUT_PREFIX");
163 if (opt_prefix.has_value())
171 "You requested global matrix output (OGS_GLOBAL_MAT_OUT_PREFIX is "
172 "set), which is not yet implemented for PETSc matrices.");
193 process_id,
"M",
"mat");
196 outputGlobalMatrix(M, fh);
201 process_id,
"K",
"mat");
204 outputGlobalMatrix(K, fh);
209 process_id,
"b",
"vec");
212 outputGlobalVector(b, fh);
218 process_id,
"Jac",
"mat");
221 outputGlobalMatrix(*Jac, fh);
236 auto const opt_prefix = getEnvironmentVariable(
"OGS_LOCAL_MAT_OUT_PREFIX");
237 auto const opt_elements =
238 getEnvironmentVariable(
"OGS_LOCAL_MAT_OUT_ELEMENTS");
245 "Environment variable OGS_LOCAL_MAT_OUT_ELEMENTS is set, but "
246 "OGS_LOCAL_MAT_OUT_PREFIX is not. Local matrix debug output "
247 "will be disabled.");
255 "Environment variable OGS_LOCAL_MAT_OUT_PREFIX is set, but "
256 "OGS_LOCAL_MAT_OUT_ELEMENTS is not. Local matrix debug output "
257 "will be disabled.");
267 "Environment variable OGS_LOCAL_MAT_OUT_ELEMENTS not set "
268 "properly. Local matrix debug output will be disabled.");
272 auto const outputFilename = localMatrixOutputFilename(*opt_prefix);
278 "File `{}' for local matrix debug output could not be "
283 DBUG(
"Successfully opened local matrix debug output file {}.",
288 double const t,
int const process_id, std::size_t
const element_id,
289 std::vector<double>
const& local_M_data,
290 std::vector<double>
const& local_K_data,
291 std::vector<double>
const& local_b_data,
292 std::vector<double>
const*
const local_Jac_data)
299 std::lock_guard lock_guard{
mutex_};
303 DBUG(
"Writing to local matrix debug output file...");
305 fmt::print(fh,
"## t = {:.15g}, process id = {}, element id = {}\n\n", t,
306 process_id, element_id);
308 if (!local_M_data.empty())
311 fmt::print(fh,
"# M\n{}\n\n", toSquareMatrixRowMajor(local_M_data));
314 if (!local_K_data.empty())
317 fmt::print(fh,
"# K\n{}\n\n", toSquareMatrixRowMajor(local_K_data));
320 if (!local_b_data.empty())
326 if (local_Jac_data && !local_Jac_data->empty())
329 fmt::print(fh,
"# Jac\n{}\n\n\n",
330 toSquareMatrixRowMajor(*local_Jac_data));
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
Global vector based on Eigen vector.
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, GlobalMatrix const *const Jac=nullptr)
bool isOutputRequested(std::size_t const element_id) const
std::function< bool(std::size_t)> output_element_predicate_
std::ofstream outputFile_
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::vector< double > const *const local_Jac_data=nullptr)