From: Denis Davydov Date: Thu, 20 Sep 2018 15:28:11 +0000 (+0200) Subject: add a flag to ParameterHandler to skip undefined fields and subsections X-Git-Tag: v9.1.0-rc1~688^2~2 X-Git-Url: https://gitweb.dealii.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=7814b352c9d6a1dfe2e6a31032e248790210148c;p=dealii.git add a flag to ParameterHandler to skip undefined fields and subsections --- diff --git a/doc/news/changes/minor/20180920DenisDavydov b/doc/news/changes/minor/20180920DenisDavydov new file mode 100644 index 0000000000..38f78916f1 --- /dev/null +++ b/doc/news/changes/minor/20180920DenisDavydov @@ -0,0 +1,4 @@ +New: Add optional flag to ParameterHandler to skip undefined +sections and parameters. +
+(Denis Davydov 2018/09/20) diff --git a/include/deal.II/base/parameter_handler.h b/include/deal.II/base/parameter_handler.h index a6fc80fdf7..f6ca4506ce 100644 --- a/include/deal.II/base/parameter_handler.h +++ b/include/deal.II/base/parameter_handler.h @@ -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 true, 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 */ diff --git a/source/base/parameter_handler.cc b/source/base/parameter_handler.cc index 246efe92b4..9c25aabeff 100644 --- a/source/base/parameter_handler.cc +++ b/source/base/parameter_handler.cc @@ -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(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 index 0000000000..f81f144afb --- /dev/null +++ b/tests/parameter_handler/parameter_handler_24.cc @@ -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 + +#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 index 0000000000..4d0c45c144 --- /dev/null +++ b/tests/parameter_handler/parameter_handler_24.output @@ -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 index 0000000000..867dffaf38 --- /dev/null +++ b/tests/parameter_handler/parameter_handler_24.prm @@ -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