22#define BOOST_BIND_GLOBAL_PLACEHOLDERS
23#include <boost/algorithm/string.hpp>
24#include <boost/io/ios_state.hpp>
25#include <boost/property_tree/json_parser.hpp>
26#include <boost/property_tree/ptree.hpp>
27#include <boost/property_tree/xml_parser.hpp>
28#undef BOOST_BIND_GLOBAL_PLACEHOLDERS
45 : entries(new
boost::property_tree::ptree())
52 mangle(
const std::string &s)
61 const bool mangle_whole_string = (s ==
"value");
64 for (
const char c : s)
66 static const std::string allowed_characters(
67 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789");
69 if ((!mangle_whole_string) &&
70 (allowed_characters.find(c) != std::string::npos))
75 static const char hex[16] = {
'0',
91 u.push_back(hex[
static_cast<unsigned char>(c) / 16]);
92 u.push_back(hex[
static_cast<unsigned char>(c) % 16]);
102 demangle(
const std::string &s)
107 for (
unsigned int i = 0; i < s.size(); ++i)
113 ExcMessage(
"Trying to demangle an invalid string."));
223 u.push_back(
static_cast<char>(c));
237 is_parameter_node(
const boost::property_tree::ptree &p)
239 return static_cast<bool>(
p.get_optional<std::string>(
"value"));
248 is_alias_node(
const boost::property_tree::ptree &p)
250 return static_cast<bool>(
p.get_optional<std::string>(
"alias"));
260 collate_path_string(
const char separator,
261 const std::vector<std::string> &subsection_path)
263 if (subsection_path.size() > 0)
265 std::string
p = mangle(subsection_path[0]);
266 for (
unsigned int i = 1; i < subsection_path.size(); ++i)
269 p += mangle(subsection_path[i]);
283 recursively_sort_parameters(
284 const char separator,
285 const std::vector<std::string> &target_subsection_path,
286 boost::property_tree::ptree &tree)
288 boost::property_tree::ptree ¤t_section =
289 tree.get_child(collate_path_string(separator, target_subsection_path));
294 static auto compare =
295 [](
const std::pair<std::string, boost::property_tree::ptree> &a,
296 const std::pair<std::string, boost::property_tree::ptree> &
b) {
298 (is_parameter_node(a.second) || is_alias_node(a.second));
301 (is_parameter_node(
b.second) || is_alias_node(
b.second));
305 if (a_is_param && !b_is_param)
308 if (!a_is_param && b_is_param)
312 return a.first <
b.first;
315 current_section.sort(compare);
318 for (
auto &p : current_section)
320 if ((is_parameter_node(
p.second) ==
false) &&
321 (is_alias_node(
p.second) ==
false))
323 const std::string subsection = demangle(
p.first);
325 std::vector<std::string> subsection_path = target_subsection_path;
326 subsection_path.emplace_back(subsection);
328 recursively_sort_parameters(separator, subsection_path, tree);
341 recursively_mangle_or_demangle(
const boost::property_tree::ptree &tree_in,
342 boost::property_tree::ptree &tree_out,
343 const bool do_mangle,
344 const bool is_parameter_or_alias_node =
false)
346 for (
const auto &p : tree_in)
348 if (is_parameter_or_alias_node)
350 tree_out.put_child(
p.first,
p.second);
354 boost::property_tree::ptree temp;
356 if (
const auto val =
p.second.get_value_optional<std::string>())
357 temp.put_value<std::string>(*val);
359 recursively_mangle_or_demangle(
p.second,
362 is_parameter_node(
p.second) ||
363 is_alias_node(
p.second));
364 tree_out.put_child(do_mangle ? mangle(
p.first) : demangle(
p.
first),
383 "You have chosen either no or multiple style formats. You can choose "
384 "between: PRM, Description, LaTeX, XML, JSON."));
403 if (path.empty() ==
false)
406 path += mangle(name);
415 const std::vector<std::string> &sub_path,
416 const std::string &name)
const
419 if (path.empty() ==
false)
422 if (sub_path.empty() ==
false)
425 path += mangle(name);
434 const std::string &filename,
435 const std::string &last_line,
436 const bool skip_undefined)
443 std::string input_line;
444 std::string fully_concatenated_line;
445 bool is_concatenated =
false;
449 unsigned int current_line_n = 0;
450 unsigned int current_logical_line_n = 0;
464 auto scan_line_or_cleanup = [
this,
466 &saved_path](
const std::string &line,
467 const std::string &filename,
468 const unsigned int line_number) {
471 scan_line(line, filename, line_number, skip_undefined);
483 while (std::getline(input, input_line))
486 if (!is_concatenated)
487 current_logical_line_n = current_line_n;
494 if (last_line.size() != 0 && input_line == last_line)
499 if (input_line.size() != 0 &&
500 input_line.find_last_of(
'\\') == input_line.size() - 1)
502 input_line.erase(input_line.size() - 1);
503 is_concatenated =
true;
505 fully_concatenated_line += input_line;
509 else if (is_concatenated)
511 fully_concatenated_line += input_line;
512 is_concatenated =
false;
518 fully_concatenated_line = input_line;
521 if (!is_concatenated)
523 scan_line_or_cleanup(fully_concatenated_line,
525 current_logical_line_n);
527 fully_concatenated_line.clear();
535 scan_line_or_cleanup(fully_concatenated_line, filename, current_line_n);
539 std::stringstream paths_message;
540 if (saved_path.size() > 0)
542 paths_message <<
"Path before loading input:\n";
545 paths_message << std::setw(i * 2 + 4) <<
" "
546 <<
"subsection " << saved_path[i] <<
'\n';
548 paths_message <<
"Current path:\n";
551 paths_message << std::setw(i * 2 + 4) <<
" "
567 const std::string &last_line,
568 const bool skip_undefined,
569 const bool assert_mandatory_entries_are_found)
571 std::ifstream is(filename);
574 std::string file_ending = filename.substr(filename.find_last_of(
'.') + 1);
575 boost::algorithm::to_lower(file_ending);
576 if (file_ending ==
"prm")
577 parse_input(is, filename, last_line, skip_undefined);
578 else if (file_ending ==
"xml")
580 else if (file_ending ==
"json")
584 ExcMessage(
"Unknown input file name extension. Supported types "
585 "are .prm, .xml, and .json."));
587 if (assert_mandatory_entries_are_found)
595 const std::string &last_line,
596 const bool skip_undefined)
598 std::istringstream input_stream(s);
599 parse_input(input_stream,
"input string", last_line, skip_undefined);
612 read_xml_recursively(
613 const boost::property_tree::ptree &source,
614 const std::string ¤t_path,
615 const char path_separator,
616 const std::vector<std::unique_ptr<const Patterns::PatternBase>> &patterns,
617 const bool skip_undefined,
620 for (
const auto &p : source)
623 if (p.second.empty())
630 prm.
set(demangle(p.first), p.second.data());
638 prm.
set(demangle(
p.first),
p.second.data());
640 else if (
p.second.get_optional<std::string>(
"value"))
647 prm.
set(demangle(
p.first),
648 p.second.get<std::string>(
"value"));
656 prm.
set(demangle(
p.first),
p.second.get<std::string>(
"value"));
663 else if (
p.second.get_optional<std::string>(
"alias"))
675 read_xml_recursively(
p.second,
676 (current_path.empty() ?
678 current_path + path_separator +
715 recursively_compress_tree(boost::property_tree::ptree &source)
717 for (
auto &p : source)
719 if (
p.second.get_optional<std::string>(
"value"))
722 const auto temp =
p.second.get<std::string>(
"value");
726 p.second.put_value<std::string>(temp);
728 else if (
p.second.get_optional<std::string>(
"alias"))
734 recursively_compress_tree(
p.second);
744 const bool skip_undefined)
751 boost::property_tree::ptree single_node_tree;
754 read_xml(in, single_node_tree);
758 AssertThrow(single_node_tree.get_optional<std::string>(
"ParameterHandler"),
760 "called \"ParameterHandler\"."));
762 const std::size_t n_top_level_elements =
763 std::distance(single_node_tree.begin(), single_node_tree.end());
764 if (n_top_level_elements != 1)
766 std::ostringstream top_level_message;
767 top_level_message <<
"The ParameterHandler input parser found "
768 << n_top_level_elements
769 <<
" top level elements while reading\n "
770 <<
" an XML format input file, but there should be"
771 <<
" exactly one top level element.\n"
772 <<
" The top level elements are:\n";
774 unsigned int entry_n = 0;
775 for (boost::property_tree::ptree::iterator it = single_node_tree.begin();
776 it != single_node_tree.end();
781 << (entry_n != n_top_level_elements - 1 ?
"\n" :
"");
790 const boost::property_tree::ptree &my_entries =
791 single_node_tree.get_child(
"ParameterHandler");
793 read_xml_recursively(
800 const bool skip_undefined)
804 boost::property_tree::ptree node_tree;
809 read_json(in, node_tree);
811 catch (
const std::exception &e)
816 "The provided JSON file is not valid. Boost aborted with the "
817 "following assert message: \n\n" +
818 std::string(e.what())));
824 boost::property_tree::ptree node_tree_mangled;
825 recursively_mangle_or_demangle(node_tree,
828 read_xml_recursively(
837 entries = std::make_unique<boost::property_tree::ptree>();
845 const std::string &default_value,
847 const std::string &documentation,
848 const bool has_to_be_set)
858 const std::pair<bool, bool> set_status =
859 std::pair<bool, bool>(has_to_be_set,
false);
867 static_cast<unsigned int>(
patterns.size() - 1));
878 "pattern_description",
891 const std::string &entry,
892 const std::function<
void(
const std::string &)> &action,
893 const bool execute_action)
898 boost::optional<std::string> current_actions =
907 const std::string all_actions =
908 current_actions.get() +
"," +
919 const std::string default_value =
entries->get<std::string>(
921 action(default_value);
929 const std::string &alias_name,
930 const bool alias_is_deprecated)
935 ExcMessage(
"You are trying to declare an alias entry <" + alias_name +
936 "> that references an entry <" + existing_entry_name +
937 ">, but the latter does not exist."));
943 ExcMessage(
"You are trying to declare an alias entry <" + alias_name +
944 "> that references an entry <" + existing_entry_name +
945 ">, but the latter does not seem to be a "
946 "parameter declaration."));
956 ExcMessage(
"You are trying to declare an alias entry <" +
958 "> but a non-alias entry already exists in this "
959 "subsection (i.e., there is either a preexisting "
960 "further subsection, or a parameter entry, with "
961 "the same name as the alias)."));
966 "You are trying to declare an alias entry <" + alias_name +
967 "> but an alias entry already exists in this "
968 "subsection and this existing alias references a "
969 "different parameter entry. Specifically, "
970 "you are trying to reference the entry <" +
971 existing_entry_name +
972 "> whereas the existing alias references "
980 existing_entry_name);
982 "deprecation_status",
983 (alias_is_deprecated ?
"true" :
"false"));
990 const bool create_path_if_needed)
993 const auto path_exists =
996 if (!create_path_if_needed)
1004 boost::property_tree::ptree());
1027 const std::vector<std::string> &sub_path)
const
1031 full_path.insert(full_path.end(), sub_path.begin(), sub_path.end());
1033 boost::optional<const boost::property_tree::ptree &> subsection(
1040 return !(!subsection || is_parameter_node(subsection.get()) ||
1041 is_alias_node(subsection.get()));
1051 if (boost::optional<std::string> value =
entries->get_optional<std::string>(
1065 const std::string &entry_string)
const
1069 if (boost::optional<std::string> value =
entries->get_optional<std::string>(
1094 ExcMessage(
"Can't convert the parameter value <" +
1095 get(entry_string) +
"> for entry <" +
1096 entry_string +
"> to an integer."));
1105 const std::vector<std::string> &entry_subsection_path,
1106 const std::string &entry_string)
const
1116 "Can't convert the parameter value <" +
1117 get(entry_subsection_path, entry_string) +
"> for entry <" +
1120 "> to an integer."));
1137 ExcMessage(
"Can't convert the parameter value <" +
1138 get(entry_string) +
"> for entry <" +
1140 "> to a double precision variable."));
1149 const std::vector<std::string> &entry_subsection_path,
1150 const std::string &entry_string)
const
1155 get(entry_subsection_path, entry_string));
1161 "Can't convert the parameter value <" +
1162 get(entry_subsection_path, entry_string) +
"> for entry <" +
1165 "> to a double precision variable."));
1175 const std::string s =
get(entry_string);
1177 AssertThrow((s ==
"true") || (s ==
"false") || (s ==
"yes") || (s ==
"no"),
1178 ExcMessage(
"Can't convert the parameter value <" +
1179 get(entry_string) +
"> for entry <" + entry_string +
1180 "> to a boolean."));
1181 if (s ==
"true" || s ==
"yes")
1191 const std::vector<std::string> &entry_subsection_path,
1192 const std::string &entry_string)
const
1194 const std::string s =
get(entry_subsection_path, entry_string);
1196 AssertThrow((s ==
"true") || (s ==
"false") || (s ==
"yes") || (s ==
"no"),
1197 ExcMessage(
"Can't convert the parameter value <" +
1198 get(entry_subsection_path, entry_string) +
1202 "> to a boolean."));
1203 if (s ==
"true" || s ==
"yes")
1213 const std::string &new_value)
1227 const unsigned int pattern_index =
1233 "pattern_description")));
1237 const boost::optional<std::string> action_indices_as_string =
1239 if (action_indices_as_string)
1243 for (
const unsigned int index : action_indices)
1244 if (
actions.size() >= index + 1)
1253 map_iter->second = std::pair<bool, bool>(map_iter->second.first,
true);
1256 ExcMessage(
"Could not find parameter " + path +
1257 " in map entries_set_status."));
1268 set(entry_string, std::string(new_value));
1275 std::ostringstream s;
1276 s << std::setprecision(16);
1281 set(entry_string, s.str());
1289 std::ostringstream s;
1294 set(entry_string, s.str());
1304 set(entry_string, (new_value ?
"true" :
"false"));
1315 assert_validity_of_output_style(style);
1320 boost::property_tree::ptree current_entries = *
entries.get();
1328 std::vector<std::string>(),
1336 boost::io::ios_flags_saver restore_flags(out);
1337 boost::io::basic_ios_fill_saver<char> restore_fill_state(out);
1345 if (((style &
Short) != 0) && ((style & (
XML |
JSON)) != 0))
1348 recursively_compress_tree(current_entries);
1351 if ((style &
XML) != 0)
1362 boost::property_tree::ptree single_node_tree;
1363 single_node_tree.add_child(
"ParameterHandler", current_entries);
1365 write_xml(out, single_node_tree);
1369 if ((style &
JSON) != 0)
1371 boost::property_tree::ptree current_entries_demangled;
1372 recursively_mangle_or_demangle(current_entries,
1373 current_entries_demangled,
1375 write_json(out, current_entries_demangled);
1380 if (((style &
Short) != 0) && ((style &
Text) != 0))
1384 else if ((style &
Text) != 0)
1386 out <<
"# Listing of Parameters" << std::endl
1387 <<
"# ---------------------" << std::endl;
1389 else if ((style &
LaTeX) != 0)
1391 out <<
"\\subsection{Global parameters}" << std::endl;
1392 out <<
"\\label{parameters:global}" << std::endl;
1393 out << std::endl << std::endl;
1397 out <<
"Listing of Parameters:" << std::endl << std::endl;
1407 std::vector<std::string>(),
1419 const std::string &filename,
1422 std::string extension = filename.substr(filename.find_last_of(
'.') + 1);
1423 boost::algorithm::to_lower(extension);
1426 if (extension ==
"prm")
1427 output_style = style |
PRM;
1428 else if (extension ==
"xml")
1429 output_style = style |
XML;
1430 else if (extension ==
"json")
1431 output_style = style |
JSON;
1432 else if (extension ==
"tex")
1433 output_style = style |
LaTeX;
1435 std::ofstream out(filename);
1444 const boost::property_tree::ptree &tree,
1445 const std::vector<std::string> &target_subsection_path,
1447 const unsigned int indent_level,
1448 std::ostream &out)
const
1455 const boost::property_tree::ptree ¤t_section =
1456 tree.get_child(collate_path_string(
path_separator, target_subsection_path));
1458 unsigned int overall_indent_level = indent_level;
1460 const bool is_short = (style &
Short) != 0;
1462 if ((style &
Text) != 0)
1471 std::size_t longest_name = 0;
1472 std::size_t longest_value = 0;
1473 for (
const auto &p : current_section)
1474 if (is_parameter_node(p.second) ==
true)
1476 longest_name =
std::max(longest_name, demangle(p.first).size());
1477 longest_value =
std::max(longest_value,
1478 p.second.get<std::string>(
"value").size());
1482 bool first_entry =
true;
1483 for (
const auto &p : current_section)
1484 if (is_parameter_node(p.second) ==
true)
1486 const std::string value = p.second.get<std::string>(
"value");
1494 !p.second.get<std::string>(
"documentation").empty())
1496 if (first_entry ==
false)
1499 first_entry =
false;
1501 const std::vector<std::string> doc_lines =
1503 p.second.get<std::string>(
"documentation"),
1504 78 - overall_indent_level * 2 - 2);
1506 for (
const auto &doc_line : doc_lines)
1507 out << std::setw(overall_indent_level * 2) <<
""
1508 <<
"# " << doc_line <<
'\n';
1512 out << std::setw(overall_indent_level * 2) <<
""
1513 <<
"set " << demangle(p.first)
1514 << std::setw(longest_name - demangle(p.first).size() + 1) <<
" "
1520 value != p.second.get<std::string>(
"default_value"))
1522 out << std::setw(longest_value - value.size() + 1) <<
' '
1525 << p.second.get<std::string>(
"default_value");
1531 else if ((style &
LaTeX) != 0)
1533 auto escape = [](
const std::string &input) {
1539 const bool parameters_exist_here =
1540 std::any_of(current_section.begin(),
1541 current_section.end(),
1542 [](
const boost::property_tree::ptree::value_type &p) {
1543 return is_parameter_node(p.second) ||
1544 is_alias_node(p.second);
1546 if (parameters_exist_here)
1548 out <<
"\\begin{itemize}" <<
'\n';
1551 for (
const auto &p : current_section)
1552 if (is_parameter_node(p.second) ==
true)
1554 const std::string value = p.second.get<std::string>(
"value");
1557 out <<
"\\item {\\it Parameter name:} {\\tt "
1558 << escape(demangle(p.first)) <<
"}\n"
1559 <<
"\\phantomsection";
1563 std::string label =
"parameters:";
1564 for (
const auto &path : target_subsection_path)
1566 label.append(mangle(path));
1569 label.append(p.first);
1572 if (label.find(
"_20") != std::string::npos)
1576 out <<
"\\label{" << label <<
"}\n";
1580 out <<
"\\index[prmindex]{" << escape(demangle(p.first))
1582 out <<
"\\index[prmindexfull]{";
1583 for (
const auto &path : target_subsection_path)
1584 out << escape(path) <<
"!";
1585 out << escape(demangle(p.first)) <<
"}\n";
1588 out <<
"{\\it Value:} " << escape(value) <<
"\n\n"
1590 <<
"{\\it Default:} "
1591 << escape(p.second.get<std::string>(
"default_value"))
1598 !p.second.get<std::string>(
"documentation").empty())
1599 out <<
"{\\it Description:} "
1600 << p.second.get<std::string>(
"documentation") <<
"\n\n"
1606 const unsigned int pattern_index =
1607 p.second.get<
unsigned int>(
"pattern");
1608 const std::string desc_str =
1609 patterns[pattern_index]->description(
1611 out <<
"{\\it Possible values:} " << desc_str <<
'\n';
1614 else if (is_alias_node(p.second) ==
true)
1616 const std::string alias = p.second.get<std::string>(
"alias");
1619 out <<
"\\item {\\it Parameter name:} {\\tt "
1620 << escape(demangle(p.first)) <<
"}\n"
1621 <<
"\\phantomsection";
1625 std::string label =
"parameters:";
1626 for (
const auto &path : target_subsection_path)
1628 label.append(mangle(path));
1631 label.append(p.first);
1634 if (label.find(
"_20") != std::string::npos)
1638 out <<
"\\label{" << label <<
"}\n";
1642 out <<
"\\index[prmindex]{" << escape(demangle(p.first))
1644 out <<
"\\index[prmindexfull]{";
1645 for (
const auto &path : target_subsection_path)
1646 out << escape(path) <<
"!";
1647 out << escape(demangle(p.first)) <<
"}\n";
1651 <<
"This parameter is an alias for the parameter ``\\texttt{"
1652 << escape(alias) <<
"}''."
1653 << (p.second.get<std::string>(
"deprecation_status") ==
1655 " Its use is deprecated." :
1660 out <<
"\\end{itemize}" <<
'\n';
1667 std::size_t longest_name = 0;
1668 for (
const auto &p : current_section)
1669 if (is_parameter_node(p.second) ==
true)
1670 longest_name =
std::max(longest_name, demangle(p.first).size());
1673 for (
const auto &p : current_section)
1674 if (is_parameter_node(p.second) ==
true)
1677 out << std::setw(overall_indent_level * 2) <<
""
1678 <<
"set " << demangle(p.first)
1679 << std::setw(longest_name - demangle(p.first).size() + 1) <<
" "
1683 const unsigned int pattern_index =
1684 p.second.get<
unsigned int>(
"pattern");
1685 const std::string full_desc_str =
1687 const std::vector<std::string> description_str =
1689 full_desc_str, 78 - overall_indent_level * 2 - 2,
'|');
1690 if (description_str.size() > 1)
1693 for (
const auto &description : description_str)
1694 out << std::setw(overall_indent_level * 2 + 6) <<
""
1695 << description <<
'\n';
1697 else if (description_str.empty() ==
false)
1698 out <<
" " << description_str[0] <<
'\n';
1704 p.second.get<std::string>(
"documentation").size() != 0)
1705 out << std::setw(overall_indent_level * 2 + longest_name + 10)
1707 <<
"(" << p.second.get<std::string>(
"documentation") <<
")"
1720 unsigned int n_parameters = 0;
1721 unsigned int n_sections = 0;
1722 for (
const auto &p : current_section)
1723 if (is_parameter_node(p.second) ==
true)
1725 else if (is_alias_node(p.second) ==
false)
1729 (!(((style &
Text) != 0) && is_short)) && (n_parameters != 0) &&
1735 for (
const auto &p : current_section)
1736 if ((is_parameter_node(p.second) ==
false) &&
1737 (is_alias_node(p.second) ==
false))
1742 out << std::setw(overall_indent_level * 2) <<
""
1743 <<
"subsection " << demangle(p.first) <<
'\n';
1745 else if ((style &
LaTeX) != 0)
1747 auto escape = [](
const std::string &input) {
1752 out <<
'\n' <<
"\\subsection{Parameters in section \\tt ";
1756 for (
const auto &path : target_subsection_path)
1757 out << escape(path) <<
"/";
1758 out << escape(demangle(p.first));
1761 out <<
"\\label{parameters:";
1762 for (
const auto &path : target_subsection_path)
1763 out << mangle(path) <<
"/";
1764 out << p.first <<
"}";
1775 const std::string subsection = demangle(p.first);
1776 std::vector<std::string> directory_path = target_subsection_path;
1777 directory_path.emplace_back(subsection);
1780 tree, directory_path, style, overall_indent_level + 1, out);
1782 if (is_short && ((style &
Text) != 0))
1785 out << std::setw(overall_indent_level * 2) <<
""
1788 else if ((style &
Text) != 0)
1792 out << std::setw(overall_indent_level * 2) <<
""
1798 if (overall_indent_level == 0)
1805 else if ((style &
LaTeX) != 0)
1821 out.push(
"parameters");
1836 boost::property_tree::ptree sorted_entries;
1837 boost::property_tree::ptree *current_entries =
entries.get();
1843 current_entries = &sorted_entries;
1852 const boost::property_tree::ptree ¤t_section =
1856 for (
const auto &p : current_section)
1857 if (is_parameter_node(p.second) ==
true)
1858 out << demangle(p.first) <<
": " << p.second.get<std::string>(
"value")
1862 for (
const auto &p : current_section)
1863 if (is_parameter_node(p.second) ==
false)
1865 out.push(demangle(p.first));
1877 const std::string &input_filename,
1878 const unsigned int current_line_n,
1879 const bool skip_undefined)
1882 const std::string original_line = line;
1885 if (line.find(
'#') != std::string::npos)
1886 line.erase(line.find(
'#'), std::string::npos);
1889 while (line.find(
'\t') != std::string::npos)
1890 line.replace(line.find(
'\t'), 1,
" ");
1905 line.erase(0, std::string(
"subsection").size() + 1);
1924 while ((line.size() > 0) && ((std::isspace(line[0])) != 0))
1931 "Invalid content after 'end' or 'END' statement."));
1935 "There is no subsection to leave here."));
1945 std::string::size_type pos = line.find(
'=');
1947 pos != std::string::npos,
1950 "Invalid format of 'set' or 'SET' statement."));
1954 std::string entry_value =
1963 "deprecation_status") ==
"true")
1965 std::cerr <<
"Warning in line <" << current_line_n
1966 <<
"> of file <" << input_filename
1967 <<
">: You are using the deprecated spelling <"
1968 << entry_name <<
"> of the parameter <"
1971 <<
">." << std::endl;
1985 if (entry_value.find(
'{') == std::string::npos)
1988 const unsigned int pattern_index =
1996 patterns[pattern_index]->description()));
2000 const boost::optional<std::string> action_indices_as_string =
2003 if (action_indices_as_string)
2005 std::vector<int> action_indices =
2007 action_indices_as_string.get()));
2008 for (
const unsigned int index : action_indices)
2009 if (
actions.size() >= index + 1)
2020 std::pair<bool, bool>(map_iter->second.first,
true);
2023 ExcMessage(
"Could not find parameter " + path +
2024 " in map entries_set_status."));
2032 (
"No entry with name <" + entry_name +
2033 "> was declared in the current subsection.")));
2042 while ((line.size() > 0) && (line[0] ==
' '))
2049 "The current line is an 'include' or "
2050 "'INCLUDE' statement, but it does not "
2051 "name a file for inclusion."));
2053 std::ifstream input(line);
2070 "could not be parsed: please check to "
2071 "make sure that the file is not missing a "
2072 "'set', 'include', 'subsection', or 'end' "
2094 for (
unsigned int j = 0; j <
patterns.size(); ++j)
2105 std::ostringstream o1, o2;
2107 write_json(o2, *prm2.
entries);
2108 return (o1.str() == o2.str());
2113std::set<std::string>
2116 std::set<std::string> entries_wrongly_not_set;
2119 if (it.second.first ==
true && it.second.second ==
false)
2120 entries_wrongly_not_set.insert(it.first);
2122 return entries_wrongly_not_set;
2130 const std::set<std::string> entries_wrongly_not_set =
2133 if (entries_wrongly_not_set.size() > 0)
2135 std::string list_of_missing_parameters =
"\n\n";
2136 for (
const auto &it : entries_wrongly_not_set)
2137 list_of_missing_parameters +=
" " + it +
"\n";
2138 list_of_missing_parameters +=
"\n";
2141 entries_wrongly_not_set.empty(),
2143 "Not all entries of the parameter handler that were declared with "
2144 "`has_to_be_set = true` have been set. The following parameters " +
2145 list_of_missing_parameters +
2146 " have not been set. "
2147 "A possible reason might be that you did not add these parameter to "
2148 "the input file or that their spelling is not correct."));
2162 const std::string &filename,
2163 const std::string &last_line,
2164 const bool skip_undefined)
2180 for (
unsigned int run_no = 0; run_no <
n_branches; ++run_no)
2199 multiple_choice.split_different_values();
2205 n_branches *= multiple_choice.different_values.size();
2211 if (multiple_choice.different_values.size() !=
n_branches)
2212 std::cerr <<
" The entry value" << std::endl
2213 <<
" " << multiple_choice.entry_value << std::endl
2214 <<
" for the entry named" << std::endl
2215 <<
" " << multiple_choice.entry_name << std::endl
2216 <<
" does not have the right number of entries for the "
2219 <<
" variant runs that will be performed." << std::endl;
2226 for (
unsigned int i = 0; i <
n_branches; ++i)
2235 const boost::property_tree::ptree ¤t_section =
2241 for (
const auto &p : current_section)
2242 if (is_parameter_node(p.second) ==
true)
2244 const std::string value = p.second.get<std::string>(
"value");
2245 if (value.find(
'{') != std::string::npos)
2252 for (
const auto &p : current_section)
2253 if (is_parameter_node(p.second) ==
false)
2266 unsigned int possibilities = 1;
2268 std::vector<Entry>::iterator choice;
2272 const unsigned int selection =
2273 (run_no / possibilities) % choice->different_values.size();
2274 std::string entry_value;
2276 entry_value = choice->different_values[selection];
2279 if (run_no >= choice->different_values.size())
2282 <<
"The given array for entry <" << choice->entry_name
2283 <<
"> does not contain enough elements! Taking empty string instead."
2288 entry_value = choice->different_values[run_no];
2300 set(choice->entry_name, entry_value);
2305 possibilities *= choice->different_values.size();
2316 mem += multiple_choice.memory_consumption();
2324 const std::string &Name,
2325 const std::string &Value)
2326 : subsection_path(ssp)
2328 , entry_value(Value)
2329 , type(
Entry::array)
2341 std::string prefix(entry_value, 0, entry_value.find(
'{'));
2342 std::string multiple(entry_value,
2343 entry_value.find(
'{') + 1,
2344 entry_value.rfind(
'}') - entry_value.find(
'{') - 1);
2345 std::string postfix(entry_value,
2346 entry_value.rfind(
'}') + 1,
2350 if (multiple[0] ==
'{')
2351 multiple.erase(0, 1);
2352 if (multiple.back() ==
'}')
2353 multiple.erase(multiple.size() - 1, 1);
2356 while (std::isspace(multiple[0]) != 0)
2357 multiple.erase(0, 1);
2358 while (std::isspace(multiple.back()) != 0)
2359 multiple.erase(multiple.size() - 1, 1);
2362 while (multiple.find(
" |") != std::string::npos)
2363 multiple.replace(multiple.find(
" |"), 2,
"|");
2364 while (multiple.find(
"| ") != std::string::npos)
2365 multiple.replace(multiple.find(
"| "), 2,
"|");
2367 while (multiple.find(
'|') != std::string::npos)
2369 different_values.push_back(
2370 prefix + std::string(multiple, 0, multiple.find(
'|')) + postfix);
2371 multiple.erase(0, multiple.find(
'|') + 1);
2375 different_values.push_back(prefix + multiple + postfix);
2378 if ((entry_value.find(
"{{") != std::string::npos) &&
2379 (entry_value.find(
"}}") != std::string::npos))
void split_different_values()
std::size_t memory_consumption() const
virtual void create_new(const unsigned int run_no)=0
virtual void run(ParameterHandler &prm)=0
void init_branches_current_section()
void fill_entry_values(const unsigned int run_no)
virtual void parse_input(std::istream &input, const std::string &filename="input file", const std::string &last_line="", const bool skip_undefined=false) override
std::size_t memory_consumption() const
std::vector< Entry > multiple_choices
static const char path_separator
std::size_t memory_consumption() const
virtual void parse_input(std::istream &input, const std::string &filename="input file", const std::string &last_line="", const bool skip_undefined=false)
virtual void parse_input_from_xml(std::istream &input, const bool skip_undefined=false)
std::string get_current_path() const
std::vector< std::unique_ptr< const Patterns::PatternBase > > patterns
void enter_subsection(const std::string &subsection, const bool create_path_if_needed=true)
std::ostream & print_parameters(std::ostream &out, const OutputStyle style) const
void recursively_print_parameters(const boost::property_tree::ptree &tree, const std::vector< std::string > &target_subsection_path, const ParameterHandler::OutputStyle style, const unsigned int indent_level, std::ostream &out) const
void add_action(const std::string &entry, const std::function< void(const std::string &value)> &action, const bool execute_action=true)
long int get_integer(const std::string &entry_string) const
bool get_bool(const std::string &entry_name) const
void declare_entry(const std::string &entry, const std::string &default_value, const Patterns::PatternBase &pattern=Patterns::Anything(), const std::string &documentation="", const bool has_to_be_set=false)
virtual void parse_input_from_string(const std::string &s, const std::string &last_line="", const bool skip_undefined=false)
std::string get(const std::string &entry_string) const
std::set< std::string > get_entries_wrongly_not_set() const
void set(const std::string &entry_name, const std::string &new_value)
void log_parameters(LogStream &out, const OutputStyle style=DefaultStyle)
std::map< std::string, std::pair< bool, bool > > entries_set_status
std::string get_current_full_path(const std::string &name) const
std::vector< std::function< void(const std::string &)> > actions
void log_parameters_section(LogStream &out, const OutputStyle style=DefaultStyle)
std::unique_ptr< boost::property_tree::ptree > entries
bool subsection_path_exists(const std::vector< std::string > &sub_path) const
void scan_line(std::string line, const std::string &input_filename, const unsigned int current_line_n, const bool skip_undefined)
double get_double(const std::string &entry_name) const
void declare_alias(const std::string &existing_entry_name, const std::string &alias_name, const bool alias_is_deprecated=false)
bool operator==(const ParameterHandler &prm2) const
std::vector< std::string > subsection_path
void assert_that_entries_have_been_set() const
virtual void parse_input_from_json(std::istream &input, const bool skip_undefined=false)
virtual std::unique_ptr< PatternBase > clone() const =0
virtual std::string description(const OutputStyle style=Machine) const =0
virtual bool match(const std::string &test_string) const =0
#define DEAL_II_NAMESPACE_OPEN
#define DEAL_II_NAMESPACE_CLOSE
static ::ExceptionBase & ExcNoSubsection(int arg1, std::string arg2, std::string arg3)
static ::ExceptionBase & ExcCannotOpenIncludeStatementFile(int arg1, std::string arg2, std::string arg3)
static ::ExceptionBase & ExcFileNotFound(std::string arg1, std::string arg2)
static ::ExceptionBase & ExcIO()
#define Assert(cond, exc)
static ::ExceptionBase & ExcAlreadyAtTopLevel()
static ::ExceptionBase & ExcInvalidEntryForPattern(int arg1, std::string arg2, std::string arg3, std::string arg4, std::string arg5)
static ::ExceptionBase & ExcEntryUndeclared(std::string arg1)
static ::ExceptionBase & ExcInternalError()
static ::ExceptionBase & ExcValueDoesNotMatchPattern(std::string arg1, std::string arg2)
static ::ExceptionBase & ExcUnbalancedSubsections(std::string arg1, std::string arg2)
static ::ExceptionBase & ExcCannotParseLine(int arg1, std::string arg2, std::string arg3)
static ::ExceptionBase & ExcInvalidXMLParameterFile()
static ::ExceptionBase & ExcMessage(std::string arg1)
#define AssertThrow(cond, exc)
#define DEAL_II_ASSERT_UNREACHABLE()
#define DEAL_II_NOT_IMPLEMENTED()
std::enable_if_t< std::is_fundamental_v< T >, std::size_t > memory_consumption(const T &t)
std::string escape(const std::string &input, const PatternBase::OutputStyle style)
SymmetricTensor< 2, dim, Number > b(const Tensor< 2, dim, Number > &F)
std::vector< std::string > split_string_list(const std::string &s, const std::string &delimiter=",")
std::string replace_in_string(const std::string &input, const std::string &from, const std::string &to)
std::vector< std::string > break_text_into_lines(const std::string &original_text, const unsigned int width, const char delimiter=' ')
std::string int_to_string(const unsigned int value, const unsigned int digits=numbers::invalid_unsigned_int)
bool match_at_string_start(const std::string &name, const std::string &pattern)
double string_to_double(const std::string &s)
std::string trim(const std::string &input)
int string_to_int(const std::string &s)
::VectorizedArray< Number, width > max(const ::VectorizedArray< Number, width > &, const ::VectorizedArray< Number, width > &)