Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't create new sections during parsing #16704

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions doc/news/changes/minor/20240229Munch
Original file line number Diff line number Diff line change
@@ -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.
<br>
(Peter Munch, Magdalena Schreter-Fleischhacker, 2024/02/12)
5 changes: 3 additions & 2 deletions include/deal.II/base/parameter_handler.h
Original file line number Diff line number Diff line change
Expand Up @@ -1252,10 +1252,11 @@ class ParameterHandler : public Subscriptor
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.
Expand Down
44 changes: 31 additions & 13 deletions source/base/parameter_handler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
}
}
Expand Down Expand Up @@ -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());

Expand Down
49 changes: 49 additions & 0 deletions tests/parameter_handler/parameter_handler_read_json_04.cc
Original file line number Diff line number Diff line change
@@ -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 <deal.II/base/parameter_handler.h>

#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;
}
5 changes: 5 additions & 0 deletions tests/parameter_handler/parameter_handler_read_json_04.output
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@

{
"test 0": "1",
"test 1": "1"
}
Comment on lines +2 to +5
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The output looked like this:

{
    "test 0": "1",
    "test 1": "1",
    "test 3": ""
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"test 0": "1",
"test 1": "1",
"test 2": "1",
"test 3": {
"test 4" : "1"
}
Comment on lines +2 to +7
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand what is happening with this test. You only declare parameters test 0 and test 1. How come we accept this as a valid input file? Is it because we read the whole file and then go through our own parameters to see which ones have been read, ignoring everything that has been read but does not correspond to our own parameters?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Exactly. The ignoring works well for the parameters but not for the sections with the result that new sections are created.

}