Skip to content
Snippets Groups Projects
Commit bc6f35ef authored by Dmitri Naumov's avatar Dmitri Naumov
Browse files

[BL] Add {:converged} formatter to file name ctor

The output file name can now include converged/not-converged
information. If the true, nothing is added. If false, "not_converged"
is added. See the last two unit-tests.
parent 6a97e56d
No related branches found
No related tags found
No related merge requests found
......@@ -100,6 +100,16 @@ bool substituteKeyword(std::string& result,
return false;
}
if constexpr (std::is_same_v<std::remove_cvref_t<T>, bool>)
{
if (keyword == "converged")
{
std::string r = data ? "" : "_not_converged";
result.replace(begin, end - begin + 1, r);
return true;
}
}
std::unordered_map<std::type_index, char> type_specification;
type_specification[std::type_index(typeid(int))] = 'd';
type_specification[std::type_index(typeid(double))] = 'f'; // default
......@@ -127,7 +137,8 @@ std::string constructFormattedFileName(std::string const& format_specification,
std::string const& mesh_name,
int const timestep,
double const t,
int const iteration)
int const iteration,
bool const converged)
{
char const open_char = '{';
char const close_char = '}';
......@@ -144,7 +155,9 @@ std::string constructFormattedFileName(std::string const& format_specification,
getParenthesizedString(result, open_char, close_char, begin);
if (!substituteKeyword(result, str, begin, end, "timestep", timestep) &&
!substituteKeyword(result, str, begin, end, "time", t) &&
!substituteKeyword(result, str, begin, end, "iteration", iteration))
!substituteKeyword(result, str, begin, end, "iteration",
iteration) &&
!substituteKeyword(result, str, begin, end, "converged", converged))
{
substituteKeyword(result, str, begin, end, "meshname", mesh_name);
}
......
......@@ -49,9 +49,9 @@ getParenthesizedString(std::string const& in,
std::string constructFormattedFileName(std::string const& format_specification,
std::string const& mesh_name,
int const timestep,
double const t,
int const iteration);
int const timestep, double const t,
int const iteration,
bool const converged);
std::vector<double> readDoublesFromBinaryFile(const std::string& filename);
......
Template for the prefix which will be prepended to the output files. Allowed
template expressions are: `{:meshname}`, `{:timestep}`, `{:time}`,
`{:iteration}`.
`{:iteration}`, `{:converged}`.
Default value is `{:meshname}`.
......@@ -11,3 +11,6 @@ Furthermore, it is possible to specify the format of the expressions above. For
instance {:0>3timestep} results in a 3-digit output, if necessary with preceding
zeros. Also the time output can be formatted using the typical floating
point precision (for instance 0.4) and type (e, E, f, F, g, G) modifiers.
If the non-linear solver norms are not met the `{:converged}` expression is replaced with
`_not_converged` and removed otherwise.
Template for the suffix which will be appended to the output files. Allowed
template expressions are: `{:meshname}`, `{:timestep}`, `{:time}`,
`{:iteration}`.
`{:iteration}`, `{:converged}`.
Default value is `_ts_{:timestep}_t_{:time}`.
......@@ -11,3 +11,6 @@ Furthermore, it is possible to specify the format of the expressions above. For
instance {:0>3timestep} results in a 3-digit output, if necessary with preceding
zeros. Also the time output can be formatted using the typical floating
point precision (for instance 0.4) and type (e, E, f, F, g, G) modifiers.
If the non-linear solver norms are not met the `{:converged}` expression is replaced with
`_not_converged` and removed otherwise.
......@@ -395,7 +395,7 @@ void Output::doOutputNonlinearIteration(
}
std::string const output_file_name = _output_format->constructFilename(
process.getMesh().getName(), timestep, t(), iteration);
process.getMesh().getName(), timestep, t(), iteration, converged);
std::string const output_file_path =
BaseLib::joinPaths(_output_format->directory, output_file_name);
......
......@@ -76,7 +76,7 @@ std::string OutputVTKFormat::constructPVDName(
{
return BaseLib::joinPaths(
directory,
BaseLib::constructFormattedFileName(prefix, mesh_name, 0, 0, 0) +
BaseLib::constructFormattedFileName(prefix, mesh_name, 0, 0, 0, true) +
".pvd");
}
......@@ -92,21 +92,22 @@ OutputFormat::OutputFormat(std::string const& directory, std::string prefix,
std::string OutputVTKFormat::constructFilename(std::string const& mesh_name,
int const timestep,
double const t,
int const iteration) const
int const iteration,
bool const converged) const
{
return BaseLib::constructFormattedFileName(prefix, mesh_name, timestep, t,
iteration) +
iteration, converged) +
BaseLib::constructFormattedFileName(suffix, mesh_name, timestep, t,
iteration) +
iteration, converged) +
".vtu";
}
std::string OutputXDMFHDF5Format::constructFilename(
std::string const& mesh_name, int const timestep, double const t,
int const iteration) const
int const iteration, bool const converged) const
{
return BaseLib::constructFormattedFileName(prefix, mesh_name, timestep, t,
iteration) +
iteration, converged) +
".xdmf";
}
......
......@@ -42,9 +42,9 @@ struct OutputFormat
std::vector<std::reference_wrapper<const MeshLib::Mesh>> const& meshes,
std::set<std::string> const& output_variables) const = 0;
virtual std::string constructFilename(std::string const& mesh_name,
int const timestep,
double const t,
int const iteration) const = 0;
int const timestep, double const t,
int const iteration,
bool const converged) const = 0;
};
inline std::ostream& operator<<(std::ostream& os, OutputFormat const& of)
......@@ -86,7 +86,8 @@ struct OutputVTKFormat final : public OutputFormat
std::string constructFilename(std::string const& mesh_name,
int const timestep, double const t,
int const iteration) const override;
int const iteration,
bool const converged) const override;
std::string constructPVDName(std::string const& mesh_name) const;
};
......@@ -114,7 +115,8 @@ struct OutputXDMFHDF5Format final : public OutputFormat
std::string constructFilename(std::string const& mesh_name,
int const timestep, double const t,
int const iteration) const override;
int const iteration,
bool const converged) const override;
mutable std::unique_ptr<MeshLib::IO::XdmfHdfWriter> mesh_xdmf_hdf_writer;
//! Specifies the number of hdf5 output files.
......
......@@ -23,56 +23,66 @@ TEST(BaseLib, constructFormattedFileName)
{
{
auto const formatted_filename = BaseLib::constructFormattedFileName(
"test_{:timestep}", "mesh_name", 2, 0.2, 3);
"test_{:timestep}", "mesh_name", 2, 0.2, 3, true);
ASSERT_EQ("test_2", formatted_filename);
}
{
auto const formatted_filename_time =
BaseLib::constructFormattedFileName("test_{:0.5time}", "mesh_name",
2, 0.2, 3);
2, 0.2, 3, true);
ASSERT_EQ("test_0.20000", formatted_filename_time);
}
{
auto const formatted_filename = BaseLib::constructFormattedFileName(
"test_{:iteration}", "mesh_name", 2, 0.2, 3);
"test_{:iteration}", "mesh_name", 2, 0.2, 3, true);
ASSERT_EQ("test_3", formatted_filename);
}
{
auto const formatted_filename_timestep_time =
BaseLib::constructFormattedFileName("test_{:timestep}_{:0.5time}",
"mesh_name", 2, 0.2, 3);
"mesh_name", 2, 0.2, 3, true);
ASSERT_EQ("test_2_0.20000", formatted_filename_timestep_time);
}
{
auto const formatted_filename_time_timestep =
BaseLib::constructFormattedFileName("test_{:.4time}_{:timestep}",
"mesh_name", 2, 0.2, 3);
"mesh_name", 2, 0.2, 3, true);
ASSERT_EQ("test_0.2000_2", formatted_filename_time_timestep);
}
{
auto const formatted_filename = BaseLib::constructFormattedFileName(
"test_{:.4time}_{:timestep}", "mesh_name", 2, 0.2, 3);
"test_{:.4time}_{:timestep}", "mesh_name", 2, 0.2, 3, true);
ASSERT_EQ("test_0.2000_2", formatted_filename);
}
{
auto const formatted_filename = BaseLib::constructFormattedFileName(
"_ts_{:timestep}_t_{:.4time}", "mesh_name", 2, 0.2, 3);
"_ts_{:timestep}_t_{:.4time}", "mesh_name", 2, 0.2, 3, true);
ASSERT_EQ("_ts_2_t_0.2000", formatted_filename);
}
{
auto const formatted_filename = BaseLib::constructFormattedFileName(
"_ts_{:0>3timestep}_t_{:.4time}", "mesh_name", 2, 0.2, 3);
"_ts_{:0>3timestep}_t_{:.4time}", "mesh_name", 2, 0.2, 3, true);
ASSERT_EQ("_ts_002_t_0.2000", formatted_filename);
}
{
auto const formatted_filename = BaseLib::constructFormattedFileName(
"_ts_{:0>3timestep}_t_{:.4etime}", "mesh_name", 2, 0.2, 3);
"_ts_{:0>3timestep}_t_{:.4etime}", "mesh_name", 2, 0.2, 3, true);
ASSERT_EQ("_ts_002_t_2.0000e-01", formatted_filename);
}
{
auto const formatted_filename = BaseLib::constructFormattedFileName(
"{:meshname}_ts_{:0>3timestep}_t_{:.4etime}_iter_{:0>2iteration}",
"mesh_name", 2, 0.2, 3);
"mesh_name", 2, 0.2, 3, true);
ASSERT_EQ("mesh_name_ts_002_t_2.0000e-01_iter_03", formatted_filename);
}
{
auto const formatted_filename = BaseLib::constructFormattedFileName(
"{:meshname}{:converged}", "mesh_name", 2, 0.2, 3, true);
ASSERT_EQ("mesh_name", formatted_filename);
}
{
auto const formatted_filename = BaseLib::constructFormattedFileName(
"{:meshname}{:converged}", "mesh_name", 2, 0.2, 3, false);
ASSERT_EQ("mesh_name_not_converged", formatted_filename);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment