From f9a83e3ee8619172fca88dbe5d6a3a7ceec79e06 Mon Sep 17 00:00:00 2001 From: Peter Munch Date: Thu, 29 Feb 2024 08:49:12 +0100 Subject: [PATCH] Don't create new sections during parsing --- doc/news/changes/minor/20240229Munch | 6 +++ include/deal.II/base/parameter_handler.h | 5 +- source/base/parameter_handler.cc | 44 ++++++++++++----- .../parameter_handler_read_json_04.cc | 49 +++++++++++++++++++ .../parameter_handler_read_json_04.output | 5 ++ .../prm/parameter_handler_read_json_04.json | 8 +++ 6 files changed, 102 insertions(+), 15 deletions(-) create mode 100644 doc/news/changes/minor/20240229Munch create mode 100644 tests/parameter_handler/parameter_handler_read_json_04.cc create mode 100644 tests/parameter_handler/parameter_handler_read_json_04.output create mode 100644 tests/parameter_handler/prm/parameter_handler_read_json_04.json diff --git a/doc/news/changes/minor/20240229Munch b/doc/news/changes/minor/20240229Munch new file mode 100644 index 0000000000..fe2fef30da --- /dev/null +++ b/doc/news/changes/minor/20240229Munch @@ -0,0 +1,6 @@ +Improved: The parse functions of ParameterHandler used to add subsections +if subsections/parameters have not been defined. This led to an output +of ParameterHandler::print_parameters() that also contained these subsections. +This is not the case anymore. +
+(Peter Munch, Magdalena Schreter-Fleischhacker, 2024/02/12) diff --git a/include/deal.II/base/parameter_handler.h b/include/deal.II/base/parameter_handler.h index 22e69594ce..e4db023b97 100644 --- a/include/deal.II/base/parameter_handler.h +++ b/include/deal.II/base/parameter_handler.h @@ -1252,10 +1252,11 @@ public: const bool alias_is_deprecated = false); /** - * Enter a subsection. If it does not yet exist, create it. + * Enter a subsection. If it does not yet exist, create it if requested. */ void - enter_subsection(const std::string &subsection); + enter_subsection(const std::string &subsection, + const bool create_path_if_needed = true); /** * Leave present subsection. diff --git a/source/base/parameter_handler.cc b/source/base/parameter_handler.cc index 7179be4c31..d713ee9310 100644 --- a/source/base/parameter_handler.cc +++ b/source/base/parameter_handler.cc @@ -664,17 +664,25 @@ namespace } else { - // it must be a subsection - prm.enter_subsection(demangle(p.first)); - read_xml_recursively(p.second, - (current_path.empty() ? - p.first : - current_path + path_separator + p.first), - path_separator, - patterns, - skip_undefined, - prm); - prm.leave_subsection(); + try + { + // it must be a subsection + prm.enter_subsection(demangle(p.first), !skip_undefined); + read_xml_recursively(p.second, + (current_path.empty() ? + p.first : + current_path + path_separator + + p.first), + path_separator, + patterns, + skip_undefined, + prm); + prm.leave_subsection(); + } + catch (const ParameterHandler::ExcEntryUndeclared &) + { + // ignore undeclared entry assert + } } } } @@ -969,10 +977,20 @@ ParameterHandler::declare_alias(const std::string &existing_entry_name, void -ParameterHandler::enter_subsection(const std::string &subsection) +ParameterHandler::enter_subsection(const std::string &subsection, + const bool create_path_if_needed) { // if necessary create subsection - if (!entries->get_child_optional(get_current_full_path(subsection))) + const auto path_exists = + entries->get_child_optional(get_current_full_path(subsection)); + + if (!create_path_if_needed) + { + AssertThrow(path_exists, + ExcEntryUndeclared(get_current_full_path(subsection))); + } + + if (!path_exists) entries->add_child(get_current_full_path(subsection), boost::property_tree::ptree()); diff --git a/tests/parameter_handler/parameter_handler_read_json_04.cc b/tests/parameter_handler/parameter_handler_read_json_04.cc new file mode 100644 index 0000000000..01d76e88a1 --- /dev/null +++ b/tests/parameter_handler/parameter_handler_read_json_04.cc @@ -0,0 +1,49 @@ +// ------------------------------------------------------------------------ +// +// SPDX-License-Identifier: LGPL-2.1-or-later +// Copyright (C) 2020 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. +// +// ------------------------------------------------------------------------ + + + +// check ParameterHandler::parse_input() for json file + +#include + +#include "../tests.h" + + +int +main() +{ + initlog(); + + ParameterHandler prm; + + double test_0 = 0; + double test_1 = 0; + + // 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); + + return 0; +} diff --git a/tests/parameter_handler/parameter_handler_read_json_04.output b/tests/parameter_handler/parameter_handler_read_json_04.output new file mode 100644 index 0000000000..0b64620c7a --- /dev/null +++ b/tests/parameter_handler/parameter_handler_read_json_04.output @@ -0,0 +1,5 @@ + +{ + "test 0": "1", + "test 1": "1" +} diff --git a/tests/parameter_handler/prm/parameter_handler_read_json_04.json b/tests/parameter_handler/prm/parameter_handler_read_json_04.json new file mode 100644 index 0000000000..9a2dbe65f4 --- /dev/null +++ b/tests/parameter_handler/prm/parameter_handler_read_json_04.json @@ -0,0 +1,8 @@ +{ + "test 0": "1", + "test 1": "1", + "test 2": "1", + "test 3": { + "test 4" : "1" + } +} -- 2.39.5