From: Peter Munch Date: Wed, 21 Aug 2024 21:10:12 +0000 (+0200) Subject: ParameterHandler: Only keep non-default parameters during printing X-Git-Url: https://gitweb.dealii.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=bbb0f5fbd5fe9edfa614aa2d97648e014e5c3784;p=dealii.git ParameterHandler: Only keep non-default parameters during printing --- diff --git a/doc/news/changes/minor/20240822Munch b/doc/news/changes/minor/20240822Munch new file mode 100644 index 0000000000..bc803506a1 --- /dev/null +++ b/doc/news/changes/minor/20240822Munch @@ -0,0 +1,5 @@ +New: The new setting OutputStyle::KeepOnlyChanged allows +to print only changed parameters with +ParameterHandler::print_parameters(). +
+(Peter Munch, 2024/08/22) diff --git a/include/deal.II/base/parameter_handler.h b/include/deal.II/base/parameter_handler.h index 32f14ef0e9..cf622002b9 100644 --- a/include/deal.II/base/parameter_handler.h +++ b/include/deal.II/base/parameter_handler.h @@ -961,6 +961,11 @@ public: * values as a LaTeX file. */ ShortLaTeX = LaTeX | Short, + + /** + * Write out only parameters with changed values. + */ + KeepOnlyChanged = 0x0200, }; diff --git a/source/base/parameter_handler.cc b/source/base/parameter_handler.cc index 189910f9fe..9b4cf71788 100644 --- a/source/base/parameter_handler.cc +++ b/source/base/parameter_handler.cc @@ -737,6 +737,38 @@ namespace } } } + + void + recursively_keep_non_default(const boost::property_tree::ptree &tree_in, + boost::property_tree::ptree &tree_out) + { + for (const auto &p : tree_in) + { + if (is_parameter_node(p.second)) + { + const std::string value = p.second.get("value"); + + if (value != p.second.get("default_value")) + tree_out.put_child(p.first, p.second); + } + else if (is_alias_node(p.second)) + { + // nothing to do + } + else + { + boost::property_tree::ptree temp; + + if (const auto val = p.second.get_value_optional()) + temp.put_value(*val); + + recursively_keep_non_default(p.second, temp); + + if (temp.size() > 0) + tree_out.put_child(p.first, temp); + } + } + } } // namespace @@ -1331,6 +1363,14 @@ ParameterHandler::print_parameters(std::ostream &out, current_entries); } + if ((style & KeepOnlyChanged) != 0) + { + boost::property_tree::ptree current_entries_without_default; + recursively_keep_non_default(current_entries, + current_entries_without_default); + current_entries = current_entries_without_default; + } + // we'll have to print some text that is padded with spaces; // set the appropriate fill character, but also make sure that // we will restore the previous setting (and all other stream diff --git a/tests/parameter_handler/parameter_handler_read_json_05.cc b/tests/parameter_handler/parameter_handler_read_json_05.cc new file mode 100644 index 0000000000..08552fa70d --- /dev/null +++ b/tests/parameter_handler/parameter_handler_read_json_05.cc @@ -0,0 +1,54 @@ +// ------------------------------------------------------------------------ +// +// SPDX-License-Identifier: LGPL-2.1-or-later +// Copyright (C) 2020 - 2024 by the deal.II authors +// +// This file is part of the deal.II library. +// +// Part of the source code is dual licensed under Apache-2.0 WITH +// LLVM-exception OR LGPL-2.1-or-later. Detailed license information +// governing the source code and code contributions can be found in +// LICENSE.md and CONTRIBUTING.md at the top level directory of deal.II. +// +// ------------------------------------------------------------------------ + + + +// Like parameter_handler_read_json_05 but output with +// ParameterHandler::OutputStyle::KeepOnlyChanged. This is achieved +// by adding two parameters with default values, altering one of +// the value of the parameters and printing the parameters +// using the ParameterHandler::OutputStyle::KeepOnlyChanged. + +#include + +#include "../tests.h" + + +int +main() +{ + initlog(); + + ParameterHandler prm; + + double test_0 = 0; + double test_1 = 1; + + // test if underscore can be parsed + prm.add_parameter("test 0", test_0); + prm.add_parameter("test 1", test_1); + + std::string source = SOURCE_DIR; + std::string filename = source + "/prm/parameter_handler_read_json_04.json"; + + std::ifstream file; + file.open(filename); + prm.parse_input_from_json(file, true); + + prm.print_parameters(deallog.get_file_stream(), + ParameterHandler::OutputStyle::ShortJSON | + ParameterHandler::OutputStyle::KeepOnlyChanged); + + return 0; +} diff --git a/tests/parameter_handler/parameter_handler_read_json_05.output b/tests/parameter_handler/parameter_handler_read_json_05.output new file mode 100644 index 0000000000..dff4b820d0 --- /dev/null +++ b/tests/parameter_handler/parameter_handler_read_json_05.output @@ -0,0 +1,4 @@ + +{ + "test 0": "1" +}