--- /dev/null
+New: Add optional flag to ParameterHandler to skip undefined
+sections and parameters.
+<br>
+(Denis Davydov 2018/09/20)
* @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
{
/**
* 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
*/
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
*/
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())
{}
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))));
// 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
else
{
AssertThrow(
- false,
+ skip_undefined,
ExcCannotParseLine(current_line_n,
input_filename,
("No entry with name <" + entry_name +
--- /dev/null
+// ---------------------------------------------------------------------
+//
+// 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;
+}
--- /dev/null
+
+# Listing of Parameters
+# ---------------------
+set Dimension = 3 # default: 1
+
+
+subsection Geometry
+ set dim = 2 # default: 1
+end
+
+
--- /dev/null
+
+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