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

ParameterHandler::parse_input_from_json(): do not use mangled parameter names anymore #16544

Merged
merged 1 commit into from
Mar 16, 2024
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
4 changes: 4 additions & 0 deletions doc/news/changes/incompatibilities/20240126SchreterMunch
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Changed:`ParameterHandler::parse_input_from_json()` reads now a json-file
without mangled parameter names.
<br>
(Magdalena Schreter, Peter Munch, 2024/01/26)
39 changes: 25 additions & 14 deletions source/base/parameter_handler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -331,15 +331,17 @@ namespace
}

/**
* Demangle all parameters recursively and attach them to @p tree_out.
* Mangle (@p do_mangle = true) or demangle (@p do_mangle = false) all
* parameters recursively and attach them to @p tree_out.
* @p is_parameter_or_alias_node indicates, whether a given node
* @p tree_in is a parameter node or an alias node (as opposed to being
* a subsection).
*/
void
recursively_demangle(const boost::property_tree::ptree &tree_in,
boost::property_tree::ptree &tree_out,
const bool is_parameter_or_alias_node = false)
recursively_mangle_or_demangle(const boost::property_tree::ptree &tree_in,
boost::property_tree::ptree &tree_out,
const bool do_mangle,
const bool is_parameter_or_alias_node = false)
{
for (const auto &p : tree_in)
{
Expand All @@ -354,11 +356,13 @@ namespace
if (const auto val = p.second.get_value_optional<std::string>())
temp.put_value<std::string>(*val);

recursively_demangle(p.second,
temp,
is_parameter_node(p.second) ||
is_alias_node(p.second));
tree_out.put_child(demangle(p.first), temp);
recursively_mangle_or_demangle(p.second,
temp,
do_mangle,
is_parameter_node(p.second) ||
is_alias_node(p.second));
tree_out.put_child(do_mangle ? mangle(p.first) : demangle(p.first),
temp);
}
}
}
Expand Down Expand Up @@ -815,9 +819,14 @@ ParameterHandler::parse_input_from_json(std::istream &in,
}

// The xml function is reused to read in the xml into the parameter file.
// This means that only mangled files can be read.
// This function can only read mangled files. Therefore, we create a mangeled
// tree first.
boost::property_tree::ptree node_tree_mangled;
recursively_mangle_or_demangle(node_tree,
node_tree_mangled,
true /*do_mangle*/);
read_xml_recursively(
node_tree, "", path_separator, patterns, skip_undefined, *this);
node_tree_mangled, "", path_separator, patterns, skip_undefined, *this);
}


Expand Down Expand Up @@ -1359,9 +1368,11 @@ ParameterHandler::print_parameters(std::ostream &out,

if ((style & JSON) != 0)
{
boost::property_tree::ptree current_entries_damangled;
recursively_demangle(current_entries, current_entries_damangled);
write_json(out, current_entries_damangled);
boost::property_tree::ptree current_entries_demangled;
recursively_mangle_or_demangle(current_entries,
current_entries_demangled,
false /*do_mangle*/);
write_json(out, current_entries_demangled);
return out;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@



// check ParameterHandler::parse_input_from_xml
// check ParameterHandler::parse_input_from_json

#include <deal.II/base/parameter_handler.h>

Expand All @@ -27,13 +27,14 @@ main()
initlog();

// default values
int int1 = 1;
int int2 = 2;
double double1 = 1.234;
double double2 = 4.321;
std::string str = "< & > ; /";
int intint = 2;
double doubledouble = 6.1415926;
int int1 = 1;
int int2 = 2;
double double1 = 1.234;
double double2 = 4.321;
std::string str = "< & > ; /";
int intint = 2;
double doubledouble = 6.1415926;
double double_double = 0;

ParameterHandler prm;
prm.add_parameter("int1", int1, "doc 1");
Expand All @@ -56,11 +57,12 @@ main()
prm.add_parameter("string&list", str, "docs 1");
prm.add_parameter("int*int", intint);
prm.add_parameter("double+double", doubledouble, "docs 3");
prm.add_parameter("double_double", double_double, "docs 4");
}
prm.leave_subsection();

// read from json
std::ifstream in(SOURCE_DIR "/prm/parameter_handler_read_json.prm");
std::ifstream in(SOURCE_DIR "/prm/parameter_handler_read_json_01.json");
prm.parse_input_from_json(in);

Assert(int1 == 2, ExcNotImplemented());
Expand All @@ -70,6 +72,7 @@ main()
Assert(str == "< & > ; /", ExcNotImplemented());
Assert(intint == 2, ExcNotImplemented());
Assert(doubledouble == 7.1415926, ExcNotImplemented());
Assert(double_double == 1.234, ExcNotImplemented());

// write it out again
prm.print_parameters(deallog.get_file_stream(), ParameterHandler::JSON);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,15 @@
"pattern_description": "[Double -MAX_DOUBLE...MAX_DOUBLE (inclusive)]",
"actions": "6"
},
"double_double":
{
"value": "1.234",
"default_value": "0",
"documentation": "docs 4",
"pattern": "7",
"pattern_description": "[Double -MAX_DOUBLE...MAX_DOUBLE (inclusive)]",
"actions": "7"
},
"int*int":
{
"value": "2",
Expand Down
2 changes: 1 addition & 1 deletion tests/parameter_handler/parameter_handler_read_json_02.cc
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ main()
prm.leave_subsection();

// read from json
std::ifstream in(SOURCE_DIR "/prm/parameter_handler_read_json_02.prm");
std::ifstream in(SOURCE_DIR "/prm/parameter_handler_read_json_02.json");
prm.parse_input_from_json(in, true);

AssertDimension(int1, 1);
Expand Down
6 changes: 6 additions & 0 deletions tests/parameter_handler/parameter_handler_read_json_03.cc
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ main()
int int2 = 0;
int int3 = 0;
int int4 = 0;
int int5 = 0;

ParameterHandler prm;
prm.add_parameter("int1", int1);
Expand All @@ -41,6 +42,10 @@ main()
prm.add_parameter("int4", int4);
prm.leave_subsection();

prm.enter_subsection("test_underscore");
prm.add_parameter("int_5", int5);
prm.leave_subsection();

std::string source = SOURCE_DIR;
std::string filename = source + "/prm/parameter_handler_read_json_03.json";
prm.parse_input(filename, "", true, true);
Expand All @@ -49,6 +54,7 @@ main()
AssertDimension(int2, 2);
AssertDimension(int3, 3);
AssertDimension(int4, 4);
AssertDimension(int5, 10);

return 0;
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
},
"ss1":
{
"double_201":
"double 1":
{
"value": "2.234",
"default_value": "1.234",
Expand All @@ -28,7 +28,7 @@
},
"ss2":
{
"double_202":
"double 2":
{
"value": "5.321",
"default_value": "4.321",
Expand All @@ -38,31 +38,39 @@
}
}
},
"Testing_25testing":
"Testing%testing":
{
"string_26list":
"string&list":
{
"value": "< & > ; \/",
"default_value": "< & > ; \/",
"documentation": "docs 1",
"pattern": "4",
"pattern_description": "[Anything]"
},
"int_2aint":
"int*int":
{
"value": "2",
"default_value": "2",
"documentation": "",
"pattern": "5",
"pattern_description": "[Integer range -2147483648...2147483647 (inclusive)]"
},
"double_2bdouble":
"double+double":
{
"value": "7.1415926",
"default_value": "6.1415926",
"documentation": "docs 3",
"pattern": "6",
"pattern_description": "[Double -MAX_DOUBLE...MAX_DOUBLE (inclusive)]"
},
"double_double":
{
"value": "1.234",
"default_value": "0",
"documentation": "docs 4",
"pattern": "7",
"pattern_description": "[Double -MAX_DOUBLE...MAX_DOUBLE (inclusive)]"
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@
"dummy" : {"int3" : 3, "int4" : 4},
"int5": {"value": 5},
"int6": 6,
"dummy2" : {"int7" : 7, "int8" : 8}
}
"dummy2" : {"int7" : 7, "int8" : 8},
"test_underscore": {"int_5": 10}
}