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 (
const 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(
"The given input file <" + filename +
585 "> has a file name extension <" + file_ending +
586 "> that is not recognized. Supported types "
587 "are .prm, .xml, and .json."));
589 if (assert_mandatory_entries_are_found)
597 const std::string &last_line,
598 const bool skip_undefined)
600 std::istringstream input_stream(s);
601 parse_input(input_stream,
"input string", last_line, skip_undefined);
614 read_xml_recursively(
615 const boost::property_tree::ptree &source,
616 const std::string ¤t_path,
617 const char path_separator,
618 const std::vector<std::unique_ptr<const Patterns::PatternBase>> &patterns,
619 const bool skip_undefined,
622 for (
const auto &p : source)
625 if (p.second.empty())
632 prm.
set(demangle(p.first), p.second.data());
640 prm.
set(demangle(p.first), p.second.data());
642 else if (p.second.get_optional<std::string>(
"value"))
649 prm.
set(demangle(p.first),
650 p.second.get<std::string>(
"value"));
658 prm.
set(demangle(p.first), p.second.get<std::string>(
"value"));
665 else if (p.second.get_optional<std::string>(
"alias"))
677 read_xml_recursively(p.second,
678 (current_path.empty() ?
680 current_path + path_separator +
717 recursively_compress_tree(boost::property_tree::ptree &source)
719 for (
auto &p : source)
721 if (p.second.get_optional<std::string>(
"value"))
724 const auto temp = p.second.get<std::string>(
"value");
728 p.second.put_value<std::string>(temp);
730 else if (p.second.get_optional<std::string>(
"alias"))
736 recursively_compress_tree(p.second);
746 const bool skip_undefined)
753 boost::property_tree::ptree single_node_tree;
756 read_xml(in, single_node_tree);
760 AssertThrow(single_node_tree.get_optional<std::string>(
"ParameterHandler"),
762 "called \"ParameterHandler\"."));
764 const std::size_t n_top_level_elements =
765 std::distance(single_node_tree.begin(), single_node_tree.end());
766 if (n_top_level_elements != 1)
768 std::ostringstream top_level_message;
769 top_level_message <<
"The ParameterHandler input parser found "
770 << n_top_level_elements
771 <<
" top level elements while reading\n "
772 <<
" an XML format input file, but there should be"
773 <<
" exactly one top level element.\n"
774 <<
" The top level elements are:\n";
776 unsigned int entry_n = 0;
777 for (boost::property_tree::ptree::iterator it = single_node_tree.begin();
778 it != single_node_tree.end();
783 << (entry_n != n_top_level_elements - 1 ?
"\n" :
"");
792 const boost::property_tree::ptree &my_entries =
793 single_node_tree.get_child(
"ParameterHandler");
795 read_xml_recursively(
802 const bool skip_undefined)
806 boost::property_tree::ptree node_tree;
811 read_json(in, node_tree);
813 catch (
const std::exception &e)
818 "The provided JSON file is not valid. Boost aborted with the "
819 "following assert message: \n\n" +
820 std::string(e.what())));
826 boost::property_tree::ptree node_tree_mangled;
827 recursively_mangle_or_demangle(node_tree,
830 read_xml_recursively(
839 entries = std::make_unique<boost::property_tree::ptree>();
847 const std::string &default_value,
849 const std::string &documentation,
850 const bool has_to_be_set)
860 const std::pair<bool, bool> set_status =
861 std::pair<bool, bool>(has_to_be_set,
false);
869 static_cast<unsigned int>(
patterns.size() - 1));
880 "pattern_description",
893 const std::string &entry,
894 const std::function<
void(
const std::string &)> &action,
895 const bool execute_action)
900 boost::optional<std::string> current_actions =
909 const std::string all_actions =
910 current_actions.get() +
"," +
921 const std::string default_value =
entries->get<std::string>(
923 action(default_value);
931 const std::string &alias_name,
932 const bool alias_is_deprecated)
937 ExcMessage(
"You are trying to declare an alias entry <" + alias_name +
938 "> that references an entry <" + existing_entry_name +
939 ">, but the latter does not exist."));
945 ExcMessage(
"You are trying to declare an alias entry <" + alias_name +
946 "> that references an entry <" + existing_entry_name +
947 ">, but the latter does not seem to be a "
948 "parameter declaration."));
958 ExcMessage(
"You are trying to declare an alias entry <" +
960 "> but a non-alias entry already exists in this "
961 "subsection (i.e., there is either a preexisting "
962 "further subsection, or a parameter entry, with "
963 "the same name as the alias)."));
968 "You are trying to declare an alias entry <" + alias_name +
969 "> but an alias entry already exists in this "
970 "subsection and this existing alias references a "
971 "different parameter entry. Specifically, "
972 "you are trying to reference the entry <" +
973 existing_entry_name +
974 "> whereas the existing alias references "
982 existing_entry_name);
984 "deprecation_status",
985 (alias_is_deprecated ?
"true" :
"false"));
992 const bool create_path_if_needed)
995 const auto path_exists =
998 if (!create_path_if_needed)
1006 boost::property_tree::ptree());
1029 const std::vector<std::string> &sub_path)
const
1033 full_path.insert(full_path.end(), sub_path.begin(), sub_path.end());
1035 boost::optional<const boost::property_tree::ptree &> subsection(
1042 return !(!subsection || is_parameter_node(subsection.get()) ||
1043 is_alias_node(subsection.get()));
1053 if (boost::optional<std::string> value =
entries->get_optional<std::string>(
1067 const std::string &entry_string)
const
1071 if (boost::optional<std::string> value =
entries->get_optional<std::string>(
1096 ExcMessage(
"Can't convert the parameter value <" +
1097 get(entry_string) +
"> for entry <" +
1098 entry_string +
"> to an integer."));
1107 const std::vector<std::string> &entry_subsection_path,
1108 const std::string &entry_string)
const
1118 "Can't convert the parameter value <" +
1119 get(entry_subsection_path, entry_string) +
"> for entry <" +
1122 "> to an integer."));
1139 ExcMessage(
"Can't convert the parameter value <" +
1140 get(entry_string) +
"> for entry <" +
1142 "> to a double precision variable."));
1151 const std::vector<std::string> &entry_subsection_path,
1152 const std::string &entry_string)
const
1157 get(entry_subsection_path, entry_string));
1163 "Can't convert the parameter value <" +
1164 get(entry_subsection_path, entry_string) +
"> for entry <" +
1167 "> to a double precision variable."));
1177 const std::string s =
get(entry_string);
1179 AssertThrow((s ==
"true") || (s ==
"false") || (s ==
"yes") || (s ==
"no"),
1180 ExcMessage(
"Can't convert the parameter value <" +
1181 get(entry_string) +
"> for entry <" + entry_string +
1182 "> to a boolean."));
1183 if (s ==
"true" || s ==
"yes")
1193 const std::vector<std::string> &entry_subsection_path,
1194 const std::string &entry_string)
const
1196 const std::string s =
get(entry_subsection_path, entry_string);
1198 AssertThrow((s ==
"true") || (s ==
"false") || (s ==
"yes") || (s ==
"no"),
1199 ExcMessage(
"Can't convert the parameter value <" +
1200 get(entry_subsection_path, entry_string) +
1204 "> to a boolean."));
1205 if (s ==
"true" || s ==
"yes")
1215 const std::string &new_value)
1229 const unsigned int pattern_index =
1235 "pattern_description")));
1239 const boost::optional<std::string> action_indices_as_string =
1241 if (action_indices_as_string)
1245 for (
const unsigned int index : action_indices)
1246 if (
actions.size() >= index + 1)
1255 map_iter->second = std::pair<bool, bool>(map_iter->second.first,
true);
1258 ExcMessage(
"Could not find parameter " + path +
1259 " in map entries_set_status."));
1270 set(entry_string, std::string(new_value));
1277 std::ostringstream s;
1278 s << std::setprecision(16);
1283 set(entry_string, s.str());
1291 std::ostringstream s;
1296 set(entry_string, s.str());
1306 set(entry_string, (new_value ?
"true" :
"false"));
1317 assert_validity_of_output_style(style);
1322 boost::property_tree::ptree current_entries = *
entries.get();
1330 std::vector<std::string>(),
1338 boost::io::ios_flags_saver restore_flags(out);
1339 boost::io::basic_ios_fill_saver<char> restore_fill_state(out);
1347 if (((style &
Short) != 0) && ((style & (
XML |
JSON)) != 0))
1350 recursively_compress_tree(current_entries);
1353 if ((style &
XML) != 0)
1364 boost::property_tree::ptree single_node_tree;
1365 single_node_tree.add_child(
"ParameterHandler", current_entries);
1367 write_xml(out, single_node_tree);
1371 if ((style &
JSON) != 0)
1373 boost::property_tree::ptree current_entries_demangled;
1374 recursively_mangle_or_demangle(current_entries,
1375 current_entries_demangled,
1377 write_json(out, current_entries_demangled);
1382 if (((style &
Short) != 0) && ((style &
Text) != 0))
1386 else if ((style &
Text) != 0)
1388 out <<
"# Listing of Parameters" << std::endl
1389 <<
"# ---------------------" << std::endl;
1391 else if ((style &
LaTeX) != 0)
1393 out <<
"\\subsection{Global parameters}" << std::endl;
1394 out <<
"\\label{parameters:global}" << std::endl;
1395 out << std::endl << std::endl;
1399 out <<
"Listing of Parameters:" << std::endl << std::endl;
1409 std::vector<std::string>(),
1421 const std::string &filename,
1424 std::string extension = filename.substr(filename.find_last_of(
'.') + 1);
1425 boost::algorithm::to_lower(extension);
1428 if (extension ==
"prm")
1429 output_style = style |
PRM;
1430 else if (extension ==
"xml")
1431 output_style = style |
XML;
1432 else if (extension ==
"json")
1433 output_style = style |
JSON;
1434 else if (extension ==
"tex")
1435 output_style = style |
LaTeX;
1437 std::ofstream out(filename);
1446 const boost::property_tree::ptree &tree,
1447 const std::vector<std::string> &target_subsection_path,
1449 const unsigned int indent_level,
1450 std::ostream &out)
const
1457 const boost::property_tree::ptree ¤t_section =
1458 tree.get_child(collate_path_string(
path_separator, target_subsection_path));
1460 unsigned int overall_indent_level = indent_level;
1462 const bool is_short = (style &
Short) != 0;
1464 if ((style &
Text) != 0)
1473 std::size_t longest_name = 0;
1474 std::size_t longest_value = 0;
1475 for (
const auto &p : current_section)
1476 if (is_parameter_node(p.second) ==
true)
1478 longest_name =
std::max(longest_name, demangle(p.first).size());
1479 longest_value =
std::max(longest_value,
1480 p.second.get<std::string>(
"value").size());
1484 bool first_entry =
true;
1485 for (
const auto &p : current_section)
1486 if (is_parameter_node(p.second) ==
true)
1488 const std::string value = p.second.get<std::string>(
"value");
1496 !p.second.get<std::string>(
"documentation").empty())
1498 if (first_entry ==
false)
1501 first_entry =
false;
1503 const std::vector<std::string> doc_lines =
1505 p.second.get<std::string>(
"documentation"),
1506 78 - overall_indent_level * 2 - 2);
1508 for (
const auto &doc_line : doc_lines)
1509 out << std::setw(overall_indent_level * 2) <<
""
1510 <<
"# " << doc_line <<
'\n';
1514 out << std::setw(overall_indent_level * 2) <<
""
1515 <<
"set " << demangle(p.first)
1516 << std::setw(longest_name - demangle(p.first).size() + 1) <<
" "
1522 value != p.second.get<std::string>(
"default_value"))
1524 out << std::setw(longest_value - value.size() + 1) <<
' '
1527 << p.second.get<std::string>(
"default_value");
1533 else if ((style &
LaTeX) != 0)
1535 auto escape = [](
const std::string &input) {
1541 const bool parameters_exist_here =
1542 std::any_of(current_section.begin(),
1543 current_section.end(),
1544 [](
const boost::property_tree::ptree::value_type &p) {
1545 return is_parameter_node(p.second) ||
1546 is_alias_node(p.second);
1548 if (parameters_exist_here)
1550 out <<
"\\begin{itemize}" <<
'\n';
1553 for (
const auto &p : current_section)
1554 if (is_parameter_node(p.second) ==
true)
1556 const std::string value = p.second.get<std::string>(
"value");
1559 out <<
"\\item {\\it Parameter name:} {\\tt "
1560 << escape(demangle(p.first)) <<
"}\n"
1561 <<
"\\phantomsection";
1565 std::string label =
"parameters:";
1566 for (
const auto &path : target_subsection_path)
1568 label.append(mangle(path));
1571 label.append(p.first);
1574 if (label.find(
"_20") != std::string::npos)
1578 out <<
"\\label{" << label <<
"}\n";
1582 out <<
"\\index[prmindex]{" << escape(demangle(p.first))
1584 out <<
"\\index[prmindexfull]{";
1585 for (
const auto &path : target_subsection_path)
1586 out << escape(path) <<
"!";
1587 out << escape(demangle(p.first)) <<
"}\n";
1590 out <<
"{\\it Value:} " << escape(value) <<
"\n\n"
1592 <<
"{\\it Default:} "
1593 << escape(p.second.get<std::string>(
"default_value"))
1600 !p.second.get<std::string>(
"documentation").empty())
1601 out <<
"{\\it Description:} "
1602 << p.second.get<std::string>(
"documentation") <<
"\n\n"
1608 const unsigned int pattern_index =
1609 p.second.get<
unsigned int>(
"pattern");
1610 const std::string desc_str =
1611 patterns[pattern_index]->description(
1613 out <<
"{\\it Possible values:} " << desc_str <<
'\n';
1616 else if (is_alias_node(p.second) ==
true)
1618 const std::string alias = p.second.get<std::string>(
"alias");
1621 out <<
"\\item {\\it Parameter name:} {\\tt "
1622 << escape(demangle(p.first)) <<
"}\n"
1623 <<
"\\phantomsection";
1627 std::string label =
"parameters:";
1628 for (
const auto &path : target_subsection_path)
1630 label.append(mangle(path));
1633 label.append(p.first);
1636 if (label.find(
"_20") != std::string::npos)
1640 out <<
"\\label{" << label <<
"}\n";
1644 out <<
"\\index[prmindex]{" << escape(demangle(p.first))
1646 out <<
"\\index[prmindexfull]{";
1647 for (
const auto &path : target_subsection_path)
1648 out << escape(path) <<
"!";
1649 out << escape(demangle(p.first)) <<
"}\n";
1653 <<
"This parameter is an alias for the parameter ``\\texttt{"
1654 << escape(alias) <<
"}''."
1655 << (p.second.get<std::string>(
"deprecation_status") ==
1657 " Its use is deprecated." :
1662 out <<
"\\end{itemize}" <<
'\n';
1669 std::size_t longest_name = 0;
1670 for (
const auto &p : current_section)
1671 if (is_parameter_node(p.second) ==
true)
1672 longest_name =
std::max(longest_name, demangle(p.first).size());
1675 for (
const auto &p : current_section)
1676 if (is_parameter_node(p.second) ==
true)
1679 out << std::setw(overall_indent_level * 2) <<
""
1680 <<
"set " << demangle(p.first)
1681 << std::setw(longest_name - demangle(p.first).size() + 1) <<
" "
1685 const unsigned int pattern_index =
1686 p.second.get<
unsigned int>(
"pattern");
1687 const std::string full_desc_str =
1689 const std::vector<std::string> description_str =
1691 full_desc_str, 78 - overall_indent_level * 2 - 2,
'|');
1692 if (description_str.size() > 1)
1695 for (
const auto &description : description_str)
1696 out << std::setw(overall_indent_level * 2 + 6) <<
""
1697 << description <<
'\n';
1699 else if (description_str.empty() ==
false)
1700 out <<
" " << description_str[0] <<
'\n';
1706 p.second.get<std::string>(
"documentation").size() != 0)
1707 out << std::setw(overall_indent_level * 2 + longest_name + 10)
1709 <<
"(" << p.second.get<std::string>(
"documentation") <<
")"
1722 unsigned int n_parameters = 0;
1723 unsigned int n_sections = 0;
1724 for (
const auto &p : current_section)
1725 if (is_parameter_node(p.second) ==
true)
1727 else if (is_alias_node(p.second) ==
false)
1731 (!(((style &
Text) != 0) && is_short)) && (n_parameters != 0) &&
1737 for (
const auto &p : current_section)
1738 if ((is_parameter_node(p.second) ==
false) &&
1739 (is_alias_node(p.second) ==
false))
1744 out << std::setw(overall_indent_level * 2) <<
""
1745 <<
"subsection " << demangle(p.first) <<
'\n';
1747 else if ((style &
LaTeX) != 0)
1749 auto escape = [](
const std::string &input) {
1754 out <<
'\n' <<
"\\subsection{Parameters in section \\tt ";
1758 for (
const auto &path : target_subsection_path)
1759 out << escape(path) <<
"/";
1760 out << escape(demangle(p.first));
1763 out <<
"\\label{parameters:";
1764 for (
const auto &path : target_subsection_path)
1765 out << mangle(path) <<
"/";
1766 out << p.first <<
"}";
1777 const std::string subsection = demangle(p.first);
1778 std::vector<std::string> directory_path = target_subsection_path;
1779 directory_path.emplace_back(subsection);
1782 tree, directory_path, style, overall_indent_level + 1, out);
1784 if (is_short && ((style &
Text) != 0))
1787 out << std::setw(overall_indent_level * 2) <<
""
1790 else if ((style &
Text) != 0)
1794 out << std::setw(overall_indent_level * 2) <<
""
1800 if (overall_indent_level == 0)
1807 else if ((style &
LaTeX) != 0)
1823 out.
push(
"parameters");
1838 boost::property_tree::ptree sorted_entries;
1839 boost::property_tree::ptree *current_entries =
entries.get();
1845 current_entries = &sorted_entries;
1854 const boost::property_tree::ptree ¤t_section =
1858 for (
const auto &p : current_section)
1859 if (is_parameter_node(p.second) ==
true)
1860 out << demangle(p.first) <<
": " << p.second.get<std::string>(
"value")
1864 for (
const auto &p : current_section)
1865 if (is_parameter_node(p.second) ==
false)
1867 out.
push(demangle(p.first));
1879 const std::string &input_filename,
1880 const unsigned int current_line_n,
1881 const bool skip_undefined)
1884 const std::string original_line = line;
1887 if (line.find(
'#') != std::string::npos)
1888 line.erase(line.find(
'#'), std::string::npos);
1891 while (line.find(
'\t') != std::string::npos)
1892 line.replace(line.find(
'\t'), 1,
" ");
1907 line.erase(0, std::string(
"subsection").size() + 1);
1926 while ((line.size() > 0) && ((std::isspace(line[0])) != 0))
1933 "Invalid content after 'end' or 'END' statement."));
1937 "There is no subsection to leave here."));
1947 std::string::size_type pos = line.find(
'=');
1949 pos != std::string::npos,
1952 "Invalid format of 'set' or 'SET' statement."));
1956 std::string entry_value =
1965 "deprecation_status") ==
"true")
1967 std::cerr <<
"Warning in line <" << current_line_n
1968 <<
"> of file <" << input_filename
1969 <<
">: You are using the deprecated spelling <"
1970 << entry_name <<
"> of the parameter <"
1973 <<
">." << std::endl;
1987 if (entry_value.find(
'{') == std::string::npos)
1990 const unsigned int pattern_index =
1998 patterns[pattern_index]->description()));
2002 const boost::optional<std::string> action_indices_as_string =
2005 if (action_indices_as_string)
2007 std::vector<int> action_indices =
2009 action_indices_as_string.get()));
2010 for (
const unsigned int index : action_indices)
2011 if (
actions.size() >= index + 1)
2022 std::pair<bool, bool>(map_iter->second.first,
true);
2025 ExcMessage(
"Could not find parameter " + path +
2026 " in map entries_set_status."));
2034 (
"No entry with name <" + entry_name +
2035 "> was declared in the current subsection.")));
2044 while ((line.size() > 0) && (line[0] ==
' '))
2051 "The current line is an 'include' or "
2052 "'INCLUDE' statement, but it does not "
2053 "name a file for inclusion."));
2055 std::ifstream input(line);
2072 "could not be parsed: please check to "
2073 "make sure that the file is not missing a "
2074 "'set', 'include', 'subsection', or 'end' "
2096 for (
unsigned int j = 0; j <
patterns.size(); ++j)
2107 std::ostringstream o1, o2;
2109 write_json(o2, *prm2.
entries);
2110 return (o1.str() == o2.str());
2115std::set<std::string>
2118 std::set<std::string> entries_wrongly_not_set;
2121 if (it.second.first ==
true && it.second.second ==
false)
2122 entries_wrongly_not_set.insert(it.first);
2124 return entries_wrongly_not_set;
2132 const std::set<std::string> entries_wrongly_not_set =
2135 if (entries_wrongly_not_set.size() > 0)
2137 std::string list_of_missing_parameters =
"\n\n";
2138 for (
const auto &it : entries_wrongly_not_set)
2139 list_of_missing_parameters +=
" " + it +
"\n";
2140 list_of_missing_parameters +=
"\n";
2143 entries_wrongly_not_set.empty(),
2145 "Not all entries of the parameter handler that were declared with "
2146 "`has_to_be_set = true` have been set. The following parameters " +
2147 list_of_missing_parameters +
2148 " have not been set. "
2149 "A possible reason might be that you did not add these parameter to "
2150 "the input file or that their spelling is not correct."));
2164 const std::string &filename,
2165 const std::string &last_line,
2166 const bool skip_undefined)
2182 for (
unsigned int run_no = 0; run_no <
n_branches; ++run_no)
2201 multiple_choice.split_different_values();
2207 n_branches *= multiple_choice.different_values.size();
2213 if (multiple_choice.different_values.size() !=
n_branches)
2214 std::cerr <<
" The entry value" << std::endl
2215 <<
" " << multiple_choice.entry_value << std::endl
2216 <<
" for the entry named" << std::endl
2217 <<
" " << multiple_choice.entry_name << std::endl
2218 <<
" does not have the right number of entries for the "
2221 <<
" variant runs that will be performed." << std::endl;
2228 for (
unsigned int i = 0; i <
n_branches; ++i)
2237 const boost::property_tree::ptree ¤t_section =
2243 for (
const auto &p : current_section)
2244 if (is_parameter_node(p.second) ==
true)
2246 const std::string value = p.second.get<std::string>(
"value");
2247 if (value.find(
'{') != std::string::npos)
2254 for (
const auto &p : current_section)
2255 if (is_parameter_node(p.second) ==
false)
2268 unsigned int possibilities = 1;
2270 std::vector<Entry>::iterator choice;
2274 const unsigned int selection =
2275 (run_no / possibilities) % choice->different_values.size();
2276 std::string entry_value;
2278 entry_value = choice->different_values[selection];
2281 if (run_no >= choice->different_values.size())
2284 <<
"The given array for entry <" << choice->entry_name
2285 <<
"> does not contain enough elements! Taking empty string instead."
2290 entry_value = choice->different_values[run_no];
2302 set(choice->entry_name, entry_value);
2307 possibilities *= choice->different_values.size();
2318 mem += multiple_choice.memory_consumption();
2326 const std::string &Name,
2327 const std::string &Value)
2328 : subsection_path(ssp)
2330 , entry_value(Value)
2331 , type(
Entry::array)
2343 std::string prefix(entry_value, 0, entry_value.find(
'{'));
2344 std::string multiple(entry_value,
2345 entry_value.find(
'{') + 1,
2346 entry_value.rfind(
'}') - entry_value.find(
'{') - 1);
2347 std::string postfix(entry_value,
2348 entry_value.rfind(
'}') + 1,
2352 if (multiple[0] ==
'{')
2353 multiple.erase(0, 1);
2354 if (multiple.back() ==
'}')
2355 multiple.erase(multiple.size() - 1, 1);
2358 while (std::isspace(multiple[0]) != 0)
2359 multiple.erase(0, 1);
2360 while (std::isspace(multiple.back()) != 0)
2361 multiple.erase(multiple.size() - 1, 1);
2364 while (multiple.find(
" |") != std::string::npos)
2365 multiple.replace(multiple.find(
" |"), 2,
"|");
2366 while (multiple.find(
"| ") != std::string::npos)
2367 multiple.replace(multiple.find(
"| "), 2,
"|");
2369 while (multiple.find(
'|') != std::string::npos)
2371 different_values.push_back(
2372 prefix + std::string(multiple, 0, multiple.find(
'|')) + postfix);
2373 multiple.erase(0, multiple.find(
'|') + 1);
2377 different_values.push_back(prefix + multiple + postfix);
2380 if ((entry_value.find(
"{{") != std::string::npos) &&
2381 (entry_value.find(
"}}") != std::string::npos))
void push(const std::string &text)
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 & ExcIO()
static ::ExceptionBase & ExcFileNotOpen(std::string arg1)
#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 > &)