]> https://gitweb.dealii.org/ - dealii.git/commitdiff
add a flag to ParameterHandler to skip undefined fields and subsections
authorDenis Davydov <davydden@gmail.com>
Thu, 20 Sep 2018 15:28:11 +0000 (17:28 +0200)
committerDenis Davydov <davydden@gmail.com>
Fri, 21 Sep 2018 08:26:34 +0000 (10:26 +0200)
doc/news/changes/minor/20180920DenisDavydov [new file with mode: 0644]
include/deal.II/base/parameter_handler.h
source/base/parameter_handler.cc
tests/parameter_handler/parameter_handler_24.cc [new file with mode: 0644]
tests/parameter_handler/parameter_handler_24.output [new file with mode: 0644]
tests/parameter_handler/parameter_handler_24.prm [new file with mode: 0644]

diff --git a/doc/news/changes/minor/20180920DenisDavydov b/doc/news/changes/minor/20180920DenisDavydov
new file mode 100644 (file)
index 0000000..38f7891
--- /dev/null
@@ -0,0 +1,4 @@
+New: Add optional flag to ParameterHandler to skip undefined
+sections and parameters.
+<br>
+(Denis Davydov 2018/09/20)
index a6fc80fdf7717a3699ab5e6eaad099630a0d2ff9..f6ca4506ce8fa6ca0f9545cbd18aa5d44d4e142c 100644 (file)
@@ -839,6 +839,7 @@ class MultipleParameterLoop;
  * @author Wolfgang Bangerth, October 1997, revised February 1998, 2010, 2011, 2017
  * @author Alberto Sartori, 2015
  * @author David Wells, 2016
+ * @author Denis Davydov, 2018
  */
 class ParameterHandler : public Subscriptor
 {
@@ -901,8 +902,14 @@ public:
 
   /**
    * Constructor.
+   *
+   * If @p skip_undefined is <code>true</code>, the parameter handler
+   * will skip undefined sections and entries. This is useful for partially
+   * parsing a parameter file, for example to obtain only the spatial dimension
+   * of the problem. By default all entries and subsections are expected to be
+   * declared.
    */
-  ParameterHandler();
+  ParameterHandler(const bool skip_undefined = false);
 
   /**
    * Destructor. Declare this only to have a virtual destructor, which is
@@ -1598,6 +1605,11 @@ private:
    */
   static const char path_separator = '.';
 
+  /**
+   * A flag to skip undefined entries.
+   */
+  const bool skip_undefined;
+
   /**
    * Path of presently selected subsections; empty list means top level
    */
index 246efe92b45bf0b745cd898ddaf00f22695283be..9c25aabeffbaa582878be9038d2cc7339d43f430 100644 (file)
@@ -39,8 +39,9 @@
 DEAL_II_NAMESPACE_OPEN
 
 
-ParameterHandler::ParameterHandler()
-  : entries(new boost::property_tree::ptree())
+ParameterHandler::ParameterHandler(const bool skip_undefined)
+  : skip_undefined(skip_undefined)
+  , entries(new boost::property_tree::ptree())
 {}
 
 
@@ -2109,8 +2110,8 @@ ParameterHandler::scan_line(std::string        line,
       const std::string subsection = Utilities::trim(line);
 
       // check whether subsection exists
-      AssertThrow(entries->get_child_optional(
-                    get_current_full_path(subsection)),
+      AssertThrow(skip_undefined || entries->get_child_optional(
+                                      get_current_full_path(subsection)),
                   ExcNoSubsection(current_line_n,
                                   input_filename,
                                   demangle(get_current_full_path(subsection))));
@@ -2181,12 +2182,9 @@ ParameterHandler::scan_line(std::string        line,
       // declared
       if (entries->get_optional<std::string>(path + path_separator + "value"))
         {
-          // if entry was declared:
-          // does it match the regex? if not,
-          // don't enter it into the database
-          // exception: if it contains characters
-          // which specify it as a multiple loop
-          // entry, then ignore content
+          // if entry was declared: does it match the regex? if not, don't enter
+          // it into the database exception: if it contains characters which
+          // specify it as a multiple loop entry, then ignore content
           if (entry_value.find('{') == std::string::npos)
             {
               // verify that the new value satisfies the provided pattern
@@ -2222,7 +2220,7 @@ ParameterHandler::scan_line(std::string        line,
       else
         {
           AssertThrow(
-            false,
+            skip_undefined,
             ExcCannotParseLine(current_line_n,
                                input_filename,
                                ("No entry with name <" + entry_name +
diff --git a/tests/parameter_handler/parameter_handler_24.cc b/tests/parameter_handler/parameter_handler_24.cc
new file mode 100644 (file)
index 0000000..f81f144
--- /dev/null
@@ -0,0 +1,47 @@
+// ---------------------------------------------------------------------
+//
+// Copyright (C) 2002 - 2017 by the deal.II authors
+//
+// This file is part of the deal.II library.
+//
+// The deal.II library is free software; you can use it, redistribute
+// it, and/or modify it under the terms of the GNU Lesser General
+// Public License as published by the Free Software Foundation; either
+// version 2.1 of the License, or (at your option) any later version.
+// The full text of the license can be found in the file LICENSE.md at
+// the top level directory of deal.II.
+//
+// ---------------------------------------------------------------------
+
+
+
+// check skip_undefined settings in parameter handler.
+
+#include <deal.II/base/parameter_handler.h>
+
+#include "../tests.h"
+
+void
+check()
+{
+  ParameterHandler prm(true);
+  prm.enter_subsection("Geometry");
+  prm.declare_entry("dim", "1", Patterns::Integer(1, 3));
+  prm.leave_subsection();
+
+  prm.declare_entry("Dimension", "1", Patterns::Integer(1, 3));
+
+  std::ifstream in(SOURCE_DIR "/parameter_handler_24.prm");
+  prm.parse_input(in, "input file");
+
+  prm.print_parameters(deallog.get_file_stream(), ParameterHandler::Text);
+}
+
+
+int
+main()
+{
+  initlog();
+  check();
+  return 0;
+}
diff --git a/tests/parameter_handler/parameter_handler_24.output b/tests/parameter_handler/parameter_handler_24.output
new file mode 100644 (file)
index 0000000..4d0c45c
--- /dev/null
@@ -0,0 +1,11 @@
+
+# Listing of Parameters
+# ---------------------
+set Dimension = 3 # default: 1
+
+
+subsection Geometry
+  set dim = 2 # default: 1
+end
+
+
diff --git a/tests/parameter_handler/parameter_handler_24.prm b/tests/parameter_handler/parameter_handler_24.prm
new file mode 100644 (file)
index 0000000..867dffa
--- /dev/null
@@ -0,0 +1,20 @@
+
+subsection Boundary conditions
+  # dimension-dependent
+  set Function expressions = 0:0,0,x*t
+end
+
+set Some other dimension dependent parameter = 1.
+
+subsection Geometry
+  set dim = 2
+  set Some unrelevant field = 22
+end
+
+subsection Linear solver
+  set Tolerance = 1e-9
+end
+
+set Dimension = 3
+
+set That = this

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.