]> https://gitweb.dealii.org/ - dealii.git/commitdiff
ParameterHandler: Only keep non-default parameters during printing 17593/head
authorPeter Munch <peterrmuench@gmail.com>
Wed, 21 Aug 2024 21:10:12 +0000 (23:10 +0200)
committerPeter Munch <peterrmuench@gmail.com>
Thu, 22 Aug 2024 16:59:01 +0000 (18:59 +0200)
doc/news/changes/minor/20240822Munch [new file with mode: 0644]
include/deal.II/base/parameter_handler.h
source/base/parameter_handler.cc
tests/parameter_handler/parameter_handler_read_json_05.cc [new file with mode: 0644]
tests/parameter_handler/parameter_handler_read_json_05.output [new file with mode: 0644]

diff --git a/doc/news/changes/minor/20240822Munch b/doc/news/changes/minor/20240822Munch
new file mode 100644 (file)
index 0000000..bc80350
--- /dev/null
@@ -0,0 +1,5 @@
+New: The new setting OutputStyle::KeepOnlyChanged allows
+to print only changed parameters with
+ParameterHandler::print_parameters().
+<br>
+(Peter Munch, 2024/08/22)
index 32f14ef0e9ad216f67418dccac941abe958012ee..cf622002b96c0a2e797ad1585f6b3c9cb14bff64 100644 (file)
@@ -961,6 +961,11 @@ public:
      * values as a LaTeX file.
      */
     ShortLaTeX = LaTeX | Short,
+
+    /**
+     * Write out only parameters with changed values.
+     */
+    KeepOnlyChanged = 0x0200,
   };
 
 
index 189910f9fe086a5a3c2f0818f62bbf412b0f2066..9b4cf71788a9d0f187f61a1c8f52457d78c4a80b 100644 (file)
@@ -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<std::string>("value");
+
+            if (value != p.second.get<std::string>("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<std::string>())
+              temp.put_value<std::string>(*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 (file)
index 0000000..08552fa
--- /dev/null
@@ -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 <deal.II/base/parameter_handler.h>
+
+#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 (file)
index 0000000..dff4b82
--- /dev/null
@@ -0,0 +1,4 @@
+
+{
+    "test 0": "1"
+}

In the beginning the Universe was created. This has made a lot of people very angry and has been widely regarded as a bad move.

Douglas Adams


Typeset in Trocchi and Trocchi Bold Sans Serif.