1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514
|
// ------------------------------------------------------------------------
//
// SPDX-License-Identifier: LGPL-2.1-or-later
// Copyright (C) 1998 - 2025 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.
//
// ------------------------------------------------------------------------
#include <deal.II/base/logstream.h>
#include <deal.II/base/memory_consumption.h>
#include <deal.II/base/parameter_handler.h>
#include <deal.II/base/path_search.h>
#include <deal.II/base/utilities.h>
#define BOOST_BIND_GLOBAL_PLACEHOLDERS
#include <boost/algorithm/string.hpp>
#include <boost/io/ios_state.hpp>
#include <boost/property_tree/json_parser.hpp>
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/xml_parser.hpp>
#undef BOOST_BIND_GLOBAL_PLACEHOLDERS
#include <algorithm>
#include <cctype>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <limits>
#include <set>
#include <sstream>
DEAL_II_NAMESPACE_OPEN
ParameterHandler::ParameterHandler()
: entries(new boost::property_tree::ptree())
{}
namespace
{
std::string
mangle(const std::string &s)
{
std::string u;
// reserve the minimum number of characters we will need. it may
// be more but this is the least we can do
u.reserve(s.size());
// see if the name is special and if so mangle the whole thing
const bool mangle_whole_string = (s == "value");
// for all parts of the string, see if it is an allowed character or not
for (const char c : s)
{
static const std::string allowed_characters(
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789");
if ((!mangle_whole_string) &&
(allowed_characters.find(c) != std::string::npos))
u.push_back(c);
else
{
u.push_back('_');
static const char hex[16] = {'0',
'1',
'2',
'3',
'4',
'5',
'6',
'7',
'8',
'9',
'a',
'b',
'c',
'd',
'e',
'f'};
u.push_back(hex[static_cast<unsigned char>(c) / 16]);
u.push_back(hex[static_cast<unsigned char>(c) % 16]);
}
}
return u;
}
std::string
demangle(const std::string &s)
{
std::string u;
u.reserve(s.size());
for (unsigned int i = 0; i < s.size(); ++i)
if (s[i] != '_')
u.push_back(s[i]);
else
{
Assert(i + 2 < s.size(),
ExcMessage("Trying to demangle an invalid string."));
unsigned char c = 0;
switch (s[i + 1])
{
case '0':
c = 0 * 16;
break;
case '1':
c = 1 * 16;
break;
case '2':
c = 2 * 16;
break;
case '3':
c = 3 * 16;
break;
case '4':
c = 4 * 16;
break;
case '5':
c = 5 * 16;
break;
case '6':
c = 6 * 16;
break;
case '7':
c = 7 * 16;
break;
case '8':
c = 8 * 16;
break;
case '9':
c = 9 * 16;
break;
case 'a':
c = 10 * 16;
break;
case 'b':
c = 11 * 16;
break;
case 'c':
c = 12 * 16;
break;
case 'd':
c = 13 * 16;
break;
case 'e':
c = 14 * 16;
break;
case 'f':
c = 15 * 16;
break;
default:
DEAL_II_ASSERT_UNREACHABLE();
}
switch (s[i + 2])
{
case '0':
c += 0;
break;
case '1':
c += 1;
break;
case '2':
c += 2;
break;
case '3':
c += 3;
break;
case '4':
c += 4;
break;
case '5':
c += 5;
break;
case '6':
c += 6;
break;
case '7':
c += 7;
break;
case '8':
c += 8;
break;
case '9':
c += 9;
break;
case 'a':
c += 10;
break;
case 'b':
c += 11;
break;
case 'c':
c += 12;
break;
case 'd':
c += 13;
break;
case 'e':
c += 14;
break;
case 'f':
c += 15;
break;
default:
DEAL_II_ASSERT_UNREACHABLE();
}
u.push_back(static_cast<char>(c));
// skip the two characters
i += 2;
}
return u;
}
/**
* Return whether a given node is a parameter node (as opposed
* to being a subsection or alias node)
*/
bool
is_parameter_node(const boost::property_tree::ptree &p)
{
return static_cast<bool>(p.get_optional<std::string>("value"));
}
/**
* Return whether a given node is a alias node (as opposed
* to being a subsection or parameter node)
*/
bool
is_alias_node(const boost::property_tree::ptree &p)
{
return static_cast<bool>(p.get_optional<std::string>("alias"));
}
/**
* Given a list of directories and subdirectories that identify
* a particular place in the tree, return the string that identifies
* this place in the way the BOOST property tree libraries likes
* to identify things.
*/
std::string
collate_path_string(const char separator,
const std::vector<std::string> &subsection_path)
{
if (subsection_path.size() > 0)
{
std::string p = mangle(subsection_path[0]);
for (unsigned int i = 1; i < subsection_path.size(); ++i)
{
p += separator;
p += mangle(subsection_path[i]);
}
return p;
}
else
return "";
}
/**
* Sort all parameters of the subsection given by the
* @p target_subsection_path argument in alphabetical order,
* as well as all subsections within it recursively.
*/
void
recursively_sort_parameters(
const char separator,
const std::vector<std::string> &target_subsection_path,
boost::property_tree::ptree &tree)
{
boost::property_tree::ptree ¤t_section =
tree.get_child(collate_path_string(separator, target_subsection_path));
// Custom comparator to ensure that the order of sorting is:
// - sorted parameters and aliases;
// - sorted subsections.
static auto compare =
[](const std::pair<std::string, boost::property_tree::ptree> &a,
const std::pair<std::string, boost::property_tree::ptree> &b) {
bool a_is_param =
(is_parameter_node(a.second) || is_alias_node(a.second));
bool b_is_param =
(is_parameter_node(b.second) || is_alias_node(b.second));
// If a is a parameter/alias and b is a subsection,
// a should go first, and vice-versa.
if (a_is_param && !b_is_param)
return true;
if (!a_is_param && b_is_param)
return false;
// Otherwise, compare a and b.
return a.first < b.first;
};
current_section.sort(compare);
// Now transverse subsections tree recursively.
for (const auto &p : current_section)
{
if ((is_parameter_node(p.second) == false) &&
(is_alias_node(p.second) == false))
{
const std::string subsection = demangle(p.first);
std::vector<std::string> subsection_path = target_subsection_path;
subsection_path.emplace_back(subsection);
recursively_sort_parameters(separator, subsection_path, tree);
}
}
}
/**
* 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_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)
{
if (is_parameter_or_alias_node)
{
tree_out.put_child(p.first, p.second);
}
else
{
boost::property_tree::ptree temp;
if (const auto val = p.second.get_value_optional<std::string>())
temp.put_value<std::string>(*val);
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);
}
}
}
/**
* Assert validity of of given output @p style.
*/
void
assert_validity_of_output_style(const ParameterHandler::OutputStyle style)
{
AssertThrow(
(((style & ParameterHandler::XML) != 0) +
((style & ParameterHandler::JSON) != 0) +
((style & ParameterHandler::PRM) != 0) +
((style & ParameterHandler::Description) != 0) +
((style & ParameterHandler::LaTeX) != 0)) == 1,
ExcMessage(
"You have chosen either no or multiple style formats. You can choose "
"between: PRM, Description, LaTeX, XML, JSON."));
}
} // namespace
std::string
ParameterHandler::get_current_path() const
{
return collate_path_string(path_separator, subsection_path);
}
std::string
ParameterHandler::get_current_full_path(const std::string &name) const
{
std::string path = get_current_path();
if (path.empty() == false)
path += path_separator;
path += mangle(name);
return path;
}
std::string
ParameterHandler::get_current_full_path(
const std::vector<std::string> &sub_path,
const std::string &name) const
{
std::string path = get_current_path();
if (path.empty() == false)
path += path_separator;
if (sub_path.empty() == false)
path += collate_path_string(path_separator, sub_path) + path_separator;
path += mangle(name);
return path;
}
void
ParameterHandler::parse_input(std::istream &input,
const std::string &filename,
const std::string &last_line,
const bool skip_undefined)
{
AssertThrow(input.fail() == false, ExcIO());
// store subsections we are currently in
const std::vector<std::string> saved_path = subsection_path;
std::string input_line;
std::string fully_concatenated_line;
bool is_concatenated = false;
// Maintain both the current line number and the current logical line
// number, where the latter refers to the line number where (possibly) the
// current line continuation started.
unsigned int current_line_n = 0;
unsigned int current_logical_line_n = 0;
// Keep a list of deprecation messages if we encounter deprecated entries.
std::string deprecation_messages;
// define an action that tries to scan a line.
//
// if that fails, i.e., if scan_line throws
// an exception either because a parameter doesn't match its
// pattern or because an associated action throws an exception,
// then try to rewind the set of subsections to the same
// point where we were when the current function was called.
// this at least allows to read parameters from a predictable
// state, rather than leave the subsection stack in some
// unknown state.
//
// after unwinding the subsection stack, just re-throw the exception
auto scan_line_or_cleanup =
[this,
&skip_undefined,
&saved_path,
&deprecation_messages](const std::string &line,
const std::string &filename,
const unsigned int line_number) {
try
{
scan_line(line, filename, line_number, skip_undefined);
}
catch (ExcEntryIsDeprecated &e)
{
deprecation_messages += e.what();
}
catch (...)
{
while ((saved_path != subsection_path) &&
(subsection_path.size() > 0))
leave_subsection();
throw;
}
};
while (std::getline(input, input_line))
{
++current_line_n;
if (!is_concatenated)
current_logical_line_n = current_line_n;
// Trim the whitespace at the ends of the line here instead of in
// scan_line. This makes the continuation line logic a lot simpler.
input_line = Utilities::trim(input_line);
// If we see the line which is the same as @p last_line ,
// terminate the parsing.
if (last_line.size() != 0 && input_line == last_line)
break;
// Check whether or not the current line should be joined with the next
// line before calling scan_line.
if (input_line.size() != 0 &&
input_line.find_last_of('\\') == input_line.size() - 1)
{
input_line.erase(input_line.size() - 1); // remove the last '\'
is_concatenated = true;
fully_concatenated_line += input_line;
}
// If the previous line ended in a '\' but the current did not, then we
// should proceed to scan_line.
else if (is_concatenated)
{
fully_concatenated_line += input_line;
is_concatenated = false;
}
// Finally, if neither the previous nor current lines are continuations,
// then the current input line is entirely concatenated.
else
{
fully_concatenated_line = input_line;
}
if (!is_concatenated)
{
scan_line_or_cleanup(fully_concatenated_line,
filename,
current_logical_line_n);
fully_concatenated_line.clear();
}
}
// While it does not make much sense for anyone to actually do this, allow
// the last line to end in a backslash. To do so, we need to parse
// whatever was left in the stash of concatenated lines
if (is_concatenated)
scan_line_or_cleanup(fully_concatenated_line, filename, current_line_n);
if (saved_path != subsection_path)
{
std::stringstream paths_message;
if (saved_path.size() > 0)
{
paths_message << "Path before loading input:\n";
for (unsigned int i = 0; i < subsection_path.size(); ++i)
{
paths_message << std::setw(i * 2 + 4) << " "
<< "subsection " << saved_path[i] << '\n';
}
paths_message << "Current path:\n";
for (unsigned int i = 0; i < subsection_path.size(); ++i)
{
paths_message << std::setw(i * 2 + 4) << " "
<< "subsection " << subsection_path[i]
<< (i == subsection_path.size() - 1 ? "" : "\n");
}
}
// restore subsection we started with before throwing the exception:
subsection_path = saved_path;
AssertThrow(false,
ExcUnbalancedSubsections(filename, paths_message.str()));
}
// if we encountered deprecated entries, throw an exception
// that contains all the deprecation messages
AssertThrow(deprecation_messages.empty(),
ExcEncounteredDeprecatedEntries(deprecation_messages));
}
void
ParameterHandler::parse_input(const std::string &filename,
const std::string &last_line,
const bool skip_undefined,
const bool assert_mandatory_entries_are_found)
{
std::ifstream is(filename);
AssertThrow(is, ExcFileNotOpen(filename));
std::string file_ending = filename.substr(filename.find_last_of('.') + 1);
boost::algorithm::to_lower(file_ending);
if (file_ending == "prm")
parse_input(is, filename, last_line, skip_undefined);
else if (file_ending == "xml")
parse_input_from_xml(is, skip_undefined);
else if (file_ending == "json")
parse_input_from_json(is, skip_undefined);
else
AssertThrow(false,
ExcMessage("The given input file <" + filename +
"> has a file name extension <" + file_ending +
"> that is not recognized. Supported types "
"are .prm, .xml, and .json."));
if (assert_mandatory_entries_are_found)
assert_that_entries_have_been_set();
}
void
ParameterHandler::parse_input_from_string(const std::string &s,
const std::string &last_line,
const bool skip_undefined)
{
std::istringstream input_stream(s);
parse_input(input_stream, "input string", last_line, skip_undefined);
}
namespace
{
// Recursively go through the 'source' tree and see if we can find
// corresponding entries in the 'destination' tree. If not, error out
// (i.e. we have just read an XML file that has entries that weren't
// declared in the ParameterHandler object); if so, copy the value of these
// nodes into the destination object
void
read_xml_recursively(
const boost::property_tree::ptree &source,
const std::string ¤t_path,
const char path_separator,
const std::vector<std::unique_ptr<const Patterns::PatternBase>> &patterns,
const bool skip_undefined,
ParameterHandler &prm)
{
for (const auto &p : source)
{
// a sub-tree must either be a parameter node or a subsection
if (p.second.empty())
{
// set the found parameter in the destination argument
try
{
prm.set(demangle(p.first), p.second.data());
}
catch (const ParameterHandler::ExcEntryUndeclared &entry_string)
{
// ignore undeclared entry assert
AssertThrow(skip_undefined,
ParameterHandler::ExcEntryUndeclared(entry_string));
}
}
else if (p.second.get_optional<std::string>("value"))
{
// set the found parameter in the destination argument
try
{
prm.set(demangle(p.first), p.second.get<std::string>("value"));
}
catch (const ParameterHandler::ExcEntryUndeclared &entry_string)
{
// ignore undeclared entry assert
AssertThrow(skip_undefined,
ParameterHandler::ExcEntryUndeclared(entry_string));
}
// this node might have sub-nodes in addition to "value", such as
// "default_value", "documentation", etc. we might at some point
// in the future want to make sure that if they exist that they
// match the ones in the 'destination' tree
}
else if (p.second.get_optional<std::string>("alias"))
{
// it is an alias node. alias nodes are static and there is
// nothing to do here (but the same applies as mentioned in the
// comment above about the static nodes inside parameter nodes
}
else
{
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 &entry_string)
{
// ignore undeclared entry assert
AssertThrow(skip_undefined,
ParameterHandler::ExcEntryUndeclared(entry_string));
}
}
}
}
// Recursively go through the @p source tree and collapse the nodes of the
// format:
//
//"key":
// {
// "value" : "val",
// "default_value" : "...",
// "documentation" : "...",
// "pattern" : "...",
// "pattern_description": "..."
// },
//
// to
//
// "key" : "val";
//
// As an example a JSON file is shown. However, this function also works for
// XML since both formats are build around the same BOOST data structures.
//
// This function is strongly based on read_xml_recursively().
void
recursively_compress_tree(boost::property_tree::ptree &source)
{
for (auto &p : source)
{
if (p.second.get_optional<std::string>("value"))
{
// save the value in a temporal variable
const auto temp = p.second.get<std::string>("value");
// clear node (children and value)
p.second.clear();
// set the correct value
p.second.put_value<std::string>(temp);
}
else if (p.second.get_optional<std::string>("alias"))
{
}
else
{
// it must be a subsection
recursively_compress_tree(p.second);
}
}
}
void
recursively_keep_non_default(const boost::property_tree::ptree &tree_in,
boost::property_tree::ptree &tree_out)
{
for (const auto &p : tree_in)
{
if (is_parameter_node(p.second))
{
const std::string value = p.second.get<std::string>("value");
if (value != p.second.get<std::string>("default_value"))
tree_out.put_child(p.first, p.second);
}
else if (is_alias_node(p.second))
{
// nothing to do
}
else
{
boost::property_tree::ptree temp;
if (const auto val = p.second.get_value_optional<std::string>())
temp.put_value<std::string>(*val);
recursively_keep_non_default(p.second, temp);
if (temp.size() > 0)
tree_out.put_child(p.first, temp);
}
}
}
} // namespace
void
ParameterHandler::parse_input_from_xml(std::istream &in,
const bool skip_undefined)
{
AssertThrow(in.fail() == false, ExcIO());
// read the XML tree assuming that (as we
// do in print_parameters(XML) it has only
// a single top-level node called
// "ParameterHandler"
boost::property_tree::ptree single_node_tree;
// This boost function will raise an exception if this is not a valid XML
// file.
read_xml(in, single_node_tree);
// make sure there is a single top-level element
// called "ParameterHandler"
AssertThrow(single_node_tree.get_optional<std::string>("ParameterHandler"),
ExcInvalidXMLParameterFile("There is no top-level XML element "
"called \"ParameterHandler\"."));
const std::size_t n_top_level_elements =
std::distance(single_node_tree.begin(), single_node_tree.end());
if (n_top_level_elements != 1)
{
std::ostringstream top_level_message;
top_level_message << "The ParameterHandler input parser found "
<< n_top_level_elements
<< " top level elements while reading\n "
<< " an XML format input file, but there should be"
<< " exactly one top level element.\n"
<< " The top level elements are:\n";
unsigned int entry_n = 0;
for (boost::property_tree::ptree::iterator it = single_node_tree.begin();
it != single_node_tree.end();
++it, ++entry_n)
{
top_level_message
<< " " << it->first
<< (entry_n != n_top_level_elements - 1 ? "\n" : "");
}
// repeat assertion condition to make the printed version easier to read
AssertThrow(n_top_level_elements == 1,
ExcInvalidXMLParameterFile(top_level_message.str()));
}
// read the child elements recursively
const boost::property_tree::ptree &my_entries =
single_node_tree.get_child("ParameterHandler");
read_xml_recursively(
my_entries, "", path_separator, patterns, skip_undefined, *this);
}
void
ParameterHandler::parse_input_from_json(std::istream &in,
const bool skip_undefined)
{
AssertThrow(in.fail() == false, ExcIO());
boost::property_tree::ptree node_tree;
// This boost function will raise an exception if this is not a valid JSON
// file.
try
{
read_json(in, node_tree);
}
catch (const std::exception &e)
{
AssertThrow(
false,
ExcMessage(
"The provided JSON file is not valid. Boost aborted with the "
"following assert message: \n\n" +
std::string(e.what())));
}
// The xml function is reused to read in the xml into the parameter file.
// This function can only read mangled files. Therefore, we create a mangled
// 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_mangled, "", path_separator, patterns, skip_undefined, *this);
}
void
ParameterHandler::clear()
{
entries = std::make_unique<boost::property_tree::ptree>();
entries_set_status.clear();
}
void
ParameterHandler::declare_entry(const std::string &entry,
const std::string &default_value,
const Patterns::PatternBase &pattern,
const std::string &documentation,
const bool has_to_be_set)
{
entries->put(get_current_full_path(entry) + path_separator + "value",
default_value);
entries->put(get_current_full_path(entry) + path_separator + "default_value",
default_value);
entries->put(get_current_full_path(entry) + path_separator + "documentation",
documentation);
// initialize with false
const std::pair<bool, bool> set_status =
std::pair<bool, bool>(has_to_be_set, false);
entries_set_status.insert(
std::pair<std::string, std::pair<bool, bool>>(get_current_full_path(entry),
set_status));
patterns.reserve(patterns.size() + 1);
patterns.emplace_back(pattern.clone());
entries->put(get_current_full_path(entry) + path_separator + "pattern",
static_cast<unsigned int>(patterns.size() - 1));
// also store the description of
// the pattern. we do so because we
// may wish to export the whole
// thing as XML or any other format
// so that external tools can work
// on the parameter file; in that
// case, they will have to be able
// to re-create the patterns as far
// as possible
entries->put(get_current_full_path(entry) + path_separator +
"pattern_description",
patterns.back()->description());
// as documented, do the default value checking at the very end
AssertThrow(pattern.match(default_value),
ExcValueDoesNotMatchPattern(default_value,
pattern.description()));
}
void
ParameterHandler::add_action(
const std::string &entry,
const std::function<void(const std::string &)> &action,
const bool execute_action)
{
actions.push_back(action);
// get the current list of actions, if any
boost::optional<std::string> current_actions =
entries->get_optional<std::string>(get_current_full_path(entry) +
path_separator + "actions");
// if there were actions already associated with this parameter, add
// the current one to it; otherwise, create a one-item list and use
// that
if (current_actions)
{
const std::string all_actions =
current_actions.get() + "," +
Utilities::int_to_string(actions.size() - 1);
entries->put(get_current_full_path(entry) + path_separator + "actions",
all_actions);
}
else
entries->put(get_current_full_path(entry) + path_separator + "actions",
Utilities::int_to_string(actions.size() - 1));
if (execute_action)
{
const std::string default_value = entries->get<std::string>(
get_current_full_path(entry) + path_separator + "default_value");
action(default_value);
}
}
void
ParameterHandler::declare_alias(const std::string &existing_entry_name,
const std::string &alias_name,
const bool alias_is_deprecated)
{
// see if there is anything to refer to already
Assert(entries->get_optional<std::string>(
get_current_full_path(existing_entry_name)),
ExcMessage("You are trying to declare an alias entry <" + alias_name +
"> that references an entry <" + existing_entry_name +
">, but the latter does not exist."));
// then also make sure that what is being referred to is in
// fact a parameter (not an alias or subsection)
Assert(entries->get_optional<std::string>(
get_current_full_path(existing_entry_name) + path_separator +
"value"),
ExcMessage("You are trying to declare an alias entry <" + alias_name +
"> that references an entry <" + existing_entry_name +
">, but the latter does not seem to be a "
"parameter declaration."));
// now also make sure that if the alias has already been
// declared, that it is also an alias and refers to the same
// entry
if (entries->get_optional<std::string>(get_current_full_path(alias_name)))
{
Assert(entries->get_optional<std::string>(
get_current_full_path(alias_name) + path_separator + "alias"),
ExcMessage("You are trying to declare an alias entry <" +
alias_name +
"> but a non-alias entry already exists in this "
"subsection (i.e., there is either a preexisting "
"further subsection, or a parameter entry, with "
"the same name as the alias)."));
Assert(entries->get<std::string>(get_current_full_path(alias_name) +
path_separator + "alias") ==
existing_entry_name,
ExcMessage(
"You are trying to declare an alias entry <" + alias_name +
"> but an alias entry already exists in this "
"subsection and this existing alias references a "
"different parameter entry. Specifically, "
"you are trying to reference the entry <" +
existing_entry_name +
"> whereas the existing alias references "
"the entry <" +
entries->get<std::string>(get_current_full_path(alias_name) +
path_separator + "alias") +
">."));
}
entries->put(get_current_full_path(alias_name) + path_separator + "alias",
existing_entry_name);
if (alias_is_deprecated)
mark_as_deprecated(alias_name);
else
mark_as_deprecated(alias_name, false);
}
void
ParameterHandler::mark_as_deprecated(const std::string &existing_entry_name,
const bool is_deprecated)
{
// assert that the entry exists
Assert(entries->get_optional<std::string>(
get_current_full_path(existing_entry_name)),
ExcMessage("You are trying to mark the entry <" + existing_entry_name +
"> as deprecated, but the entry does not exist."));
// then also make sure that what is being referred to is in
// fact a parameter or alias (not a subsection)
Assert(
entries->get_optional<std::string>(
get_current_full_path(existing_entry_name) + path_separator + "value") ||
entries->get_optional<std::string>(
get_current_full_path(existing_entry_name) + path_separator + "alias"),
ExcMessage("You are trying to mark the entry <" + existing_entry_name +
"> as deprecated, but the entry does not seem to be a "
"parameter declaration or a parameter alias."));
entries->put(get_current_full_path(existing_entry_name) + path_separator +
"deprecation_status",
is_deprecated ? "true" : "false");
}
void
ParameterHandler::enter_subsection(const std::string &subsection,
const bool create_path_if_needed)
{
// if necessary create 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());
// then enter it
subsection_path.push_back(subsection);
}
void
ParameterHandler::leave_subsection()
{
// assert there is a subsection that
// we may leave
Assert(subsection_path.size() != 0, ExcAlreadyAtTopLevel());
if (subsection_path.size() > 0)
subsection_path.pop_back();
}
bool
ParameterHandler::subsection_path_exists(
const std::vector<std::string> &sub_path) const
{
// Get full path to sub_path (i.e. prepend subsection_path to sub_path).
std::vector<std::string> full_path(subsection_path);
full_path.insert(full_path.end(), sub_path.begin(), sub_path.end());
boost::optional<const boost::property_tree::ptree &> subsection(
entries->get_child_optional(
collate_path_string(path_separator, full_path)));
// If subsection is boost::null (i.e. it does not exist)
// or it exists as a parameter/alias node, return false.
// Otherwise (i.e. it exists as a subsection node), return true.
return !(!subsection || is_parameter_node(subsection.get()) ||
is_alias_node(subsection.get()));
}
std::string
ParameterHandler::get(const std::string &entry_string) const
{
// assert that the entry is indeed
// declared
if (boost::optional<std::string> value = entries->get_optional<std::string>(
get_current_full_path(entry_string) + path_separator + "value"))
return value.get();
else
{
Assert(false, ExcEntryUndeclared(entry_string));
return "";
}
}
std::string
ParameterHandler::get(const std::vector<std::string> &entry_subsection_path,
const std::string &entry_string) const
{
// assert that the entry is indeed
// declared
if (boost::optional<std::string> value = entries->get_optional<std::string>(
get_current_full_path(entry_subsection_path, entry_string) +
path_separator + "value"))
return value.get();
else
{
Assert(false,
ExcEntryUndeclared(demangle(
get_current_full_path(entry_subsection_path, entry_string))));
return "";
}
}
long int
ParameterHandler::get_integer(const std::string &entry_string) const
{
try
{
return Utilities::string_to_int(get(entry_string));
}
catch (...)
{
AssertThrow(false,
ExcMessage("Can't convert the parameter value <" +
get(entry_string) + "> for entry <" +
entry_string + "> to an integer."));
return 0;
}
}
long int
ParameterHandler::get_integer(
const std::vector<std::string> &entry_subsection_path,
const std::string &entry_string) const
{
try
{
return Utilities::string_to_int(get(entry_subsection_path, entry_string));
}
catch (...)
{
AssertThrow(false,
ExcMessage(
"Can't convert the parameter value <" +
get(entry_subsection_path, entry_string) + "> for entry <" +
demangle(get_current_full_path(entry_subsection_path,
entry_string)) +
"> to an integer."));
return 0;
}
}
double
ParameterHandler::get_double(const std::string &entry_string) const
{
try
{
return Utilities::string_to_double(get(entry_string));
}
catch (...)
{
AssertThrow(false,
ExcMessage("Can't convert the parameter value <" +
get(entry_string) + "> for entry <" +
entry_string +
"> to a double precision variable."));
return 0;
}
}
double
ParameterHandler::get_double(
const std::vector<std::string> &entry_subsection_path,
const std::string &entry_string) const
{
try
{
return Utilities::string_to_double(
get(entry_subsection_path, entry_string));
}
catch (...)
{
AssertThrow(false,
ExcMessage(
"Can't convert the parameter value <" +
get(entry_subsection_path, entry_string) + "> for entry <" +
demangle(get_current_full_path(entry_subsection_path,
entry_string)) +
"> to a double precision variable."));
return 0;
}
}
bool
ParameterHandler::get_bool(const std::string &entry_string) const
{
const std::string s = get(entry_string);
AssertThrow((s == "true") || (s == "false") || (s == "yes") || (s == "no"),
ExcMessage("Can't convert the parameter value <" +
get(entry_string) + "> for entry <" + entry_string +
"> to a boolean."));
if (s == "true" || s == "yes")
return true;
else
return false;
}
bool
ParameterHandler::get_bool(
const std::vector<std::string> &entry_subsection_path,
const std::string &entry_string) const
{
const std::string s = get(entry_subsection_path, entry_string);
AssertThrow((s == "true") || (s == "false") || (s == "yes") || (s == "no"),
ExcMessage("Can't convert the parameter value <" +
get(entry_subsection_path, entry_string) +
"> for entry <" +
demangle(get_current_full_path(entry_subsection_path,
entry_string)) +
"> to a boolean."));
if (s == "true" || s == "yes")
return true;
else
return false;
}
void
ParameterHandler::set(const std::string &entry_string,
const std::string &new_value)
{
// resolve aliases before looking up the correct entry
std::string path = get_current_full_path(entry_string);
if (entries->get_optional<std::string>(path + path_separator + "alias"))
path = get_current_full_path(
entries->get<std::string>(path + path_separator + "alias"));
// get the node for the entry. if it doesn't exist, then we end up
// in the else-branch below, which asserts that the entry is indeed
// declared
if (entries->get_optional<std::string>(path + path_separator + "value"))
{
// verify that the new value satisfies the provided pattern
const unsigned int pattern_index =
entries->get<unsigned int>(path + path_separator + "pattern");
AssertThrow(patterns[pattern_index]->match(new_value),
ExcValueDoesNotMatchPattern(new_value,
entries->get<std::string>(
path + path_separator +
"pattern_description")));
// then also execute the actions associated with this
// parameter (if any have been provided)
const boost::optional<std::string> action_indices_as_string =
entries->get_optional<std::string>(path + path_separator + "actions");
if (action_indices_as_string)
{
std::vector<int> action_indices = Utilities::string_to_int(
Utilities::split_string_list(action_indices_as_string.get()));
for (const unsigned int index : action_indices)
if (actions.size() >= index + 1)
actions[index](new_value);
}
// finally write the new value into the database
entries->put(path + path_separator + "value", new_value);
auto map_iter = entries_set_status.find(path);
if (map_iter != entries_set_status.end())
map_iter->second = std::pair<bool, bool>(map_iter->second.first, true);
else
AssertThrow(false,
ExcMessage("Could not find parameter " + path +
" in map entries_set_status."));
}
else
AssertThrow(false, ExcEntryUndeclared(entry_string));
}
void
ParameterHandler::set(const std::string &entry_string, const char *new_value)
{
// simply forward
set(entry_string, std::string(new_value));
}
void
ParameterHandler::set(const std::string &entry_string, const double new_value)
{
std::ostringstream s;
s << std::setprecision(16);
s << new_value;
// hand this off to the function that
// actually sets the value as a string
set(entry_string, s.str());
}
void
ParameterHandler::set(const std::string &entry_string, const long int new_value)
{
std::ostringstream s;
s << new_value;
// hand this off to the function that
// actually sets the value as a string
set(entry_string, s.str());
}
void
ParameterHandler::set(const std::string &entry_string, const bool new_value)
{
// hand this off to the function that
// actually sets the value as a string
set(entry_string, (new_value ? "true" : "false"));
}
std::ostream &
ParameterHandler::print_parameters(std::ostream &out,
const OutputStyle style) const
{
AssertThrow(out.fail() == false, ExcIO());
assert_validity_of_output_style(style);
// Create entries copy and sort it, if needed.
// In this way we ensure that the class state is never
// modified by this function.
boost::property_tree::ptree current_entries = *entries.get();
// Sort parameters alphabetically, if needed.
if ((style & KeepDeclarationOrder) == 0)
{
// Dive recursively into the subsections,
// starting from the top level.
recursively_sort_parameters(path_separator,
std::vector<std::string>(),
current_entries);
}
if ((style & KeepOnlyChanged) != 0)
{
boost::property_tree::ptree current_entries_without_default;
recursively_keep_non_default(current_entries,
current_entries_without_default);
current_entries = current_entries_without_default;
}
// we'll have to print some text that is padded with spaces;
// set the appropriate fill character, but also make sure that
// we will restore the previous setting (and all other stream
// flags) when we exit this function
boost::io::ios_flags_saver restore_flags(out);
boost::io::basic_ios_fill_saver<char> restore_fill_state(out);
out.fill(' ');
// we treat XML and JSON is one step via BOOST, whereas all of the others are
// done recursively in our own code. take care of the two special formats
// first
// explicitly compress the tree if requested
if (((style & Short) != 0) && ((style & (XML | JSON)) != 0))
{
// modify the copy of the tree
recursively_compress_tree(current_entries);
}
if ((style & XML) != 0)
{
// call the writer function and exit as there is nothing
// further to do down in this function
//
// XML has a requirement that there can only be one
// single top-level entry, but we may have multiple
// entries and sections. we work around this by
// creating a tree just for this purpose with the
// single top-level node "ParameterHandler" and
// assign the existing tree under it
boost::property_tree::ptree single_node_tree;
single_node_tree.add_child("ParameterHandler", current_entries);
// set indentation character and indentation length
boost::property_tree::xml_writer_settings<
boost::property_tree::ptree::key_type>
settings(' ', 2);
write_xml(out, single_node_tree, settings);
return out;
}
if ((style & JSON) != 0)
{
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;
}
// for all of the other formats, print a preamble:
if (((style & Short) != 0) && ((style & PRM) != 0))
{
// nothing to do
}
else if ((style & PRM) != 0)
{
out << "# Listing of Parameters" << std::endl
<< "# ---------------------" << std::endl;
}
else if ((style & LaTeX) != 0)
{
out << "\\subsection{Global parameters}" << std::endl;
out << "\\label{parameters:global}" << std::endl;
out << std::endl << std::endl;
}
else if ((style & Description) != 0)
{
out << "Listing of Parameters:" << std::endl << std::endl;
}
else
{
DEAL_II_NOT_IMPLEMENTED();
}
// dive recursively into the subsections
recursively_print_parameters(
current_entries,
std::vector<std::string>(), // start at the top level
style,
0,
out);
return out;
}
void
ParameterHandler::print_parameters(
const std::string &filename,
const ParameterHandler::OutputStyle style) const
{
std::string extension = filename.substr(filename.find_last_of('.') + 1);
boost::algorithm::to_lower(extension);
ParameterHandler::OutputStyle output_style = style;
if (extension == "prm")
output_style = style | PRM;
else if (extension == "xml")
output_style = style | XML;
else if (extension == "json")
output_style = style | JSON;
else if (extension == "tex")
output_style = style | LaTeX;
std::ofstream out(filename);
AssertThrow(out.fail() == false, ExcIO());
print_parameters(out, output_style);
}
void
ParameterHandler::recursively_print_parameters(
const boost::property_tree::ptree &tree,
const std::vector<std::string> &target_subsection_path,
const OutputStyle style,
const unsigned int indent_level,
std::ostream &out) const
{
AssertThrow(out.fail() == false, ExcIO());
// this function should not be necessary for XML or JSON output...
Assert(!(style & (XML | JSON)), ExcInternalError());
const boost::property_tree::ptree ¤t_section =
tree.get_child(collate_path_string(path_separator, target_subsection_path));
unsigned int overall_indent_level = indent_level;
const bool is_short = (style & Short) != 0;
if ((style & PRM) != 0)
{
// first find out the longest entry name to be able to align the
// equal signs to do this loop over all nodes of the current
// tree, select the parameter nodes (and discard sub-tree nodes)
// and take the maximum of their lengths
//
// likewise find the longest actual value string to make sure we
// can align the default and documentation strings
std::size_t longest_name = 0;
std::size_t longest_value = 0;
for (const auto &p : current_section)
if (is_parameter_node(p.second) == true)
{
longest_name = std::max(longest_name, demangle(p.first).size());
longest_value = std::max(longest_value,
p.second.get<std::string>("value").size());
}
// print entries one by one
bool first_entry = true;
for (const auto &p : current_section)
if (is_parameter_node(p.second) == true)
{
const std::string value = p.second.get<std::string>("value");
// if there is documentation, then add an empty line
// (unless this is the first entry in a subsection), print
// the documentation, and then the actual entry; break the
// documentation into readable chunks such that the whole
// thing is at most 78 characters wide
if (!is_short &&
!p.second.get<std::string>("documentation").empty())
{
if (first_entry == false)
out << '\n';
else
first_entry = false;
const std::vector<std::string> doc_lines =
Utilities::break_text_into_lines(
p.second.get<std::string>("documentation"),
78 - overall_indent_level * 2 - 2);
for (const auto &doc_line : doc_lines)
{
// Start with the comment start ('#'), padded with
// overall_indent_level*2 spaces at the front.
out << std::setw(overall_indent_level * 2) << "" << '#';
if (!doc_line.empty())
out << ' ' << doc_line;
out << '\n';
}
}
// Print the name and (if set) value of this entry. Ensure proper
// padding with overall_indent_level*2 spaces at the front.
out << std::setw(overall_indent_level * 2) << ""
<< "set " << demangle(p.first)
<< std::setw(longest_name - demangle(p.first).size() + 1) << ' '
<< '=';
if (!value.empty())
out << ' ' << value;
// finally print the default value, but only if it differs
// from the actual value
if (!is_short &&
value != p.second.get<std::string>("default_value"))
{
out << std::setw(longest_value - value.size() + 1) << ' '
<< "# ";
out << "default: "
<< p.second.get<std::string>("default_value");
}
out << '\n';
}
}
else if ((style & LaTeX) != 0)
{
auto escape = [](const std::string &input) {
return Patterns::internal::escape(input, Patterns::PatternBase::LaTeX);
};
// if there are any parameters in this section then print them
// as an itemized list
const bool parameters_exist_here =
std::any_of(current_section.begin(),
current_section.end(),
[](const boost::property_tree::ptree::value_type &p) {
return is_parameter_node(p.second) ||
is_alias_node(p.second);
});
if (parameters_exist_here)
{
out << "\\begin{itemize}" << '\n';
// print entries one by one
for (const auto &p : current_section)
if (is_parameter_node(p.second) == true)
{
const std::string value = p.second.get<std::string>("value");
// print name
out << "\\item {\\it Parameter name:} {\\tt "
<< escape(demangle(p.first)) << "}\n"
<< "\\phantomsection";
{
// create label: labels are not to be escaped but
// mangled
std::string label = "parameters:";
for (const auto &path : target_subsection_path)
{
label.append(mangle(path));
label.append("/");
}
label.append(p.first);
// Backwards-compatibility. Output the label with and
// without escaping whitespace:
if (label.find("_20") != std::string::npos)
out << "\\label{"
<< Utilities::replace_in_string(label, "_20", " ")
<< "}\n";
out << "\\label{" << label << "}\n";
}
out << "\n\n";
out << "\\index[prmindex]{" << escape(demangle(p.first))
<< "}\n";
out << "\\index[prmindexfull]{";
for (const auto &path : target_subsection_path)
out << escape(path) << "!";
out << escape(demangle(p.first)) << "}\n";
// finally print value and default
out << "{\\it Value:} " << escape(value) << "\n\n"
<< '\n'
<< "{\\it Default:} "
<< escape(p.second.get<std::string>("default_value"))
<< "\n\n"
<< '\n';
// if there is a documenting string, print it as well but
// don't escape to allow formatting/formulas
if (!is_short &&
!p.second.get<std::string>("documentation").empty())
out << "{\\it Description:} "
<< p.second.get<std::string>("documentation")
<< ((p.second.get_optional<std::string>(
"deprecation_status") &&
p.second.get<std::string>("deprecation_status") ==
"true") ?
" This parameter is deprecated.\n\n" :
"\n\n")
<< '\n';
if (!is_short)
{
// also output possible values, do not escape because the
// description internally will use LaTeX formatting
const unsigned int pattern_index =
p.second.get<unsigned int>("pattern");
const std::string desc_str =
patterns[pattern_index]->description(
Patterns::PatternBase::LaTeX);
out << "{\\it Possible values:} " << desc_str << '\n';
}
}
else if (is_alias_node(p.second) == true)
{
const std::string alias = p.second.get<std::string>("alias");
// print name
out << "\\item {\\it Parameter name:} {\\tt "
<< escape(demangle(p.first)) << "}\n"
<< "\\phantomsection";
{
// create label: labels are not to be escaped but
// mangled
std::string label = "parameters:";
for (const auto &path : target_subsection_path)
{
label.append(mangle(path));
label.append("/");
}
label.append(p.first);
// Backwards-compatibility. Output the label with and
// without escaping whitespace:
if (label.find("_20") != std::string::npos)
out << "\\label{"
<< Utilities::replace_in_string(label, "_20", " ")
<< "}\n";
out << "\\label{" << label << "}\n";
}
out << "\n\n";
out << "\\index[prmindex]{" << escape(demangle(p.first))
<< "}\n";
out << "\\index[prmindexfull]{";
for (const auto &path : target_subsection_path)
out << escape(path) << "!";
out << escape(demangle(p.first)) << "}\n";
// finally print alias and indicate if it is deprecated
out
<< "This parameter is an alias for the parameter ``\\texttt{"
<< escape(alias) << "}''."
<< (p.second.get<std::string>("deprecation_status") ==
"true" ?
" Its use is deprecated." :
"")
<< "\n\n"
<< '\n';
}
out << "\\end{itemize}" << '\n';
}
}
else if ((style & Description) != 0)
{
// first find out the longest entry name to be able to align the
// equal signs
std::size_t longest_name = 0;
for (const auto &p : current_section)
if (is_parameter_node(p.second) == true)
longest_name = std::max(longest_name, demangle(p.first).size());
// print entries one by one
for (const auto &p : current_section)
if (is_parameter_node(p.second) == true)
{
// print name and value
out << std::setw(overall_indent_level * 2) << ""
<< "set " << demangle(p.first)
<< std::setw(longest_name - demangle(p.first).size() + 1) << " "
<< " = ";
// print possible values:
const unsigned int pattern_index =
p.second.get<unsigned int>("pattern");
const std::string full_desc_str =
patterns[pattern_index]->description(Patterns::PatternBase::Text);
const std::vector<std::string> description_str =
Utilities::break_text_into_lines(
full_desc_str, 78 - overall_indent_level * 2 - 2, '|');
if (description_str.size() > 1)
{
out << '\n';
for (const auto &description : description_str)
out << std::setw(overall_indent_level * 2 + 6) << ""
<< description << '\n';
}
else if (description_str.empty() == false)
out << " " << description_str[0] << '\n';
else
out << '\n';
// if there is a documenting string, print it as well
if (!is_short &&
p.second.get<std::string>("documentation").size() != 0)
out << std::setw(overall_indent_level * 2 + longest_name + 10)
<< ""
<< "(" << p.second.get<std::string>("documentation") << ")"
<< '\n';
}
}
else
{
DEAL_II_NOT_IMPLEMENTED();
}
// if there was text before and there are sections to come, put two
// newlines between the last entry and the first subsection
{
unsigned int n_parameters = 0;
unsigned int n_sections = 0;
for (const auto &p : current_section)
if (is_parameter_node(p.second) == true)
++n_parameters;
else if (is_alias_node(p.second) == false)
++n_sections;
if (((style & Description) == 0) && (!(((style & PRM) != 0) && is_short)) &&
(n_parameters != 0) && (n_sections != 0))
out << "\n\n";
}
// now transverse subsections tree
for (const auto &p : current_section)
if ((is_parameter_node(p.second) == false) &&
(is_alias_node(p.second) == false))
{
// first print the subsection header
if (((style & PRM) != 0) || ((style & Description) != 0))
{
out << std::setw(overall_indent_level * 2) << ""
<< "subsection " << demangle(p.first) << '\n';
}
else if ((style & LaTeX) != 0)
{
auto escape = [](const std::string &input) {
return Patterns::internal::escape(input,
Patterns::PatternBase::LaTeX);
};
out << '\n' << "\\subsection{Parameters in section \\tt ";
// find the path to the current section so that we can
// print it in the \subsection{...} heading
for (const auto &path : target_subsection_path)
out << escape(path) << "/";
out << escape(demangle(p.first));
out << "}" << '\n';
out << "\\label{parameters:";
for (const auto &path : target_subsection_path)
out << mangle(path) << "/";
out << p.first << "}";
out << '\n';
out << '\n';
}
else
{
DEAL_II_NOT_IMPLEMENTED();
}
// then the contents of the subsection
const std::string subsection = demangle(p.first);
std::vector<std::string> directory_path = target_subsection_path;
directory_path.emplace_back(subsection);
recursively_print_parameters(
tree, directory_path, style, overall_indent_level + 1, out);
if (is_short && ((style & PRM) != 0))
{
// write end of subsection.
out << std::setw(overall_indent_level * 2) << ""
<< "end" << '\n';
}
else if ((style & PRM) != 0)
{
// write end of subsection. one blank line after each
// subsection
out << std::setw(overall_indent_level * 2) << ""
<< "end" << '\n'
<< '\n';
// if this is a toplevel subsection, then have two
// newlines
if (overall_indent_level == 0)
out << '\n';
}
else if ((style & Description) != 0)
{
// nothing to do
}
else if ((style & LaTeX) != 0)
{
// nothing to do
}
else
{
DEAL_II_NOT_IMPLEMENTED();
}
}
}
void
ParameterHandler::log_parameters(LogStream &out, const OutputStyle style)
{
out.push("parameters");
// dive recursively into the subsections
log_parameters_section(out, style);
out.pop();
}
void
ParameterHandler::log_parameters_section(LogStream &out,
const OutputStyle style)
{
// Create entries copy and sort it, if needed.
// In this way we ensure that the class state is never
// modified by this function.
boost::property_tree::ptree sorted_entries;
boost::property_tree::ptree *current_entries = entries.get();
// Sort parameters alphabetically, if needed.
if ((style & KeepDeclarationOrder) == 0)
{
sorted_entries = *entries;
current_entries = &sorted_entries;
// Dive recursively into the subsections,
// starting from the current level.
recursively_sort_parameters(path_separator,
subsection_path,
sorted_entries);
}
const boost::property_tree::ptree ¤t_section =
current_entries->get_child(get_current_path());
// print entries one by one
for (const auto &p : current_section)
if (is_parameter_node(p.second) == true)
out << demangle(p.first) << ": " << p.second.get<std::string>("value")
<< std::endl;
// now transverse subsections tree
for (const auto &p : current_section)
if (is_parameter_node(p.second) == false)
{
out.push(demangle(p.first));
enter_subsection(demangle(p.first));
log_parameters_section(out, style);
leave_subsection();
out.pop();
}
}
void
ParameterHandler::scan_line(std::string line,
const std::string &input_filename,
const unsigned int current_line_n,
const bool skip_undefined)
{
// save a copy for some error messages
const std::string original_line = line;
// if there is a comment, delete it
if (line.find('#') != std::string::npos)
line.erase(line.find('#'), std::string::npos);
// replace \t by space:
while (line.find('\t') != std::string::npos)
line.replace(line.find('\t'), 1, " ");
// trim start and end:
line = Utilities::trim(line);
// if line is now empty: leave
if (line.empty())
{
return;
}
// enter subsection
else if (Utilities::match_at_string_start(line, "SUBSECTION ") ||
Utilities::match_at_string_start(line, "subsection "))
{
// delete this prefix
line.erase(0, std::string("subsection").size() + 1);
const std::string subsection = Utilities::trim(line);
// check whether subsection exists
AssertThrow(skip_undefined || entries->get_child_optional(
get_current_full_path(subsection)),
ExcNoSubsection(current_line_n,
input_filename,
demangle(get_current_full_path(subsection))));
// subsection exists
subsection_path.push_back(subsection);
}
// exit subsection
else if (Utilities::match_at_string_start(line, "END") ||
Utilities::match_at_string_start(line, "end"))
{
line.erase(0, 3);
while ((line.size() > 0) && ((std::isspace(line[0])) != 0))
line.erase(0, 1);
AssertThrow(
line.empty(),
ExcCannotParseLine(current_line_n,
input_filename,
"Invalid content after 'end' or 'END' statement."));
AssertThrow(subsection_path.size() != 0,
ExcCannotParseLine(current_line_n,
input_filename,
"There is no subsection to leave here."));
leave_subsection();
}
// regular entry
else if (Utilities::match_at_string_start(line, "SET ") ||
Utilities::match_at_string_start(line, "set "))
{
// erase "set" statement
line.erase(0, 4);
std::string::size_type pos = line.find('=');
AssertThrow(
pos != std::string::npos,
ExcCannotParseLine(current_line_n,
input_filename,
"Invalid format of 'set' or 'SET' statement."));
// extract entry name and value and trim
std::string entry_name = Utilities::trim(std::string(line, 0, pos));
std::string entry_value =
Utilities::trim(std::string(line, pos + 1, std::string::npos));
// resolve aliases before we look up the entry. if necessary, print
// a warning that the alias is deprecated
std::string path = get_current_full_path(entry_name);
if (entries->get_optional<std::string>(path + path_separator + "alias"))
{
if (entries->get<std::string>(path + path_separator +
"deprecation_status") == "true")
{
std::cerr << "Warning in line <" << current_line_n
<< "> of file <" << input_filename
<< ">: You are using the deprecated spelling <"
<< entry_name << "> of the parameter <"
<< entries->get<std::string>(path + path_separator +
"alias")
<< ">." << std::endl;
}
path = get_current_full_path(
entries->get<std::string>(path + path_separator + "alias"));
}
// get the node for the entry. if it doesn't exist, then we end up
// in the else-branch below, which asserts that the entry is indeed
// declared
if (entries->get_optional<std::string>(path + path_separator + "value"))
{
// if entry was declared: does it match the regex? if not, don't enter
// it into the database exception: if it contains characters which
// specify it as a multiple loop entry, then ignore content
if (entry_value.find('{') == std::string::npos)
{
// verify that the new value satisfies the provided pattern
const unsigned int pattern_index =
entries->get<unsigned int>(path + path_separator + "pattern");
AssertThrow(patterns[pattern_index]->match(entry_value),
ExcInvalidEntryForPattern(
current_line_n,
input_filename,
entry_value,
entry_name,
patterns[pattern_index]->description()));
// then also execute the actions associated with this
// parameter (if any have been provided)
const boost::optional<std::string> action_indices_as_string =
entries->get_optional<std::string>(path + path_separator +
"actions");
if (action_indices_as_string)
{
std::vector<int> action_indices =
Utilities::string_to_int(Utilities::split_string_list(
action_indices_as_string.get()));
for (const unsigned int index : action_indices)
if (actions.size() >= index + 1)
actions[index](entry_value);
}
}
// finally write the new value into the database
entries->put(path + path_separator + "value", entry_value);
// record that the entry has been set manually
auto map_iter = entries_set_status.find(path);
if (map_iter != entries_set_status.end())
map_iter->second =
std::pair<bool, bool>(map_iter->second.first, true);
else
AssertThrow(false,
ExcMessage("Could not find parameter " + path +
" in map entries_set_status."));
// Check if the entry (or the resolved alias) is deprecated,
// throw an exception if it is
if (entries->get_optional<std::string>(path + path_separator +
"deprecation_status") &&
entries->get<std::string>(path + path_separator +
"deprecation_status") == "true")
{
AssertThrow(false,
ExcEntryIsDeprecated(current_line_n,
input_filename,
entry_name));
}
}
else
{
AssertThrow(
skip_undefined,
ExcCannotParseLine(current_line_n,
input_filename,
("No entry with name <" + entry_name +
"> was declared in the current subsection.")));
}
}
// an include statement?
else if (Utilities::match_at_string_start(line, "include ") ||
Utilities::match_at_string_start(line, "INCLUDE "))
{
// erase "include " statement and eliminate spaces
line.erase(0, 7);
while ((line.size() > 0) && (line[0] == ' '))
line.erase(0, 1);
// the remainder must then be a filename
AssertThrow(line.size() != 0,
ExcCannotParseLine(current_line_n,
input_filename,
"The current line is an 'include' or "
"'INCLUDE' statement, but it does not "
"name a file for inclusion."));
std::ifstream input(line);
AssertThrow(input,
ExcCannotOpenIncludeStatementFile(current_line_n,
input_filename,
line));
parse_input(input, line, "", skip_undefined);
}
else
{
AssertThrow(
false,
ExcCannotParseLine(current_line_n,
input_filename,
"The line\n\n"
" <" +
original_line +
">\n\n"
"could not be parsed: please check to "
"make sure that the file is not missing a "
"'set', 'include', 'subsection', or 'end' "
"statement."));
}
}
std::size_t
ParameterHandler::memory_consumption() const
{
// TODO: add to this an estimate of the memory in the property_tree
return (MemoryConsumption::memory_consumption(subsection_path));
}
bool
ParameterHandler::operator==(const ParameterHandler &prm2) const
{
if (patterns.size() != prm2.patterns.size())
return false;
for (unsigned int j = 0; j < patterns.size(); ++j)
if (patterns[j]->description() != prm2.patterns[j]->description())
return false;
// instead of walking through all
// the nodes of the two trees
// entries and prm2.entries and
// comparing them for equality,
// simply dump the content of the
// entire structure into a string
// and compare those for equality
std::ostringstream o1, o2;
write_json(o1, *entries);
write_json(o2, *prm2.entries);
return (o1.str() == o2.str());
}
std::set<std::string>
ParameterHandler::get_entries_wrongly_not_set() const
{
std::set<std::string> entries_wrongly_not_set;
for (const auto &it : entries_set_status)
if (it.second.first == true && it.second.second == false)
entries_wrongly_not_set.insert(it.first);
return entries_wrongly_not_set;
}
void
ParameterHandler::assert_that_entries_have_been_set() const
{
const std::set<std::string> entries_wrongly_not_set =
this->get_entries_wrongly_not_set();
if (entries_wrongly_not_set.size() > 0)
{
std::string list_of_missing_parameters = "\n\n";
for (const auto &it : entries_wrongly_not_set)
list_of_missing_parameters += " " + it + "\n";
list_of_missing_parameters += "\n";
AssertThrow(
entries_wrongly_not_set.empty(),
ExcMessage(
"Not all entries of the parameter handler that were declared with "
"`has_to_be_set = true` have been set. The following parameters " +
list_of_missing_parameters +
" have not been set. "
"A possible reason might be that you did not add these parameter to "
"the input file or that their spelling is not correct."));
}
}
MultipleParameterLoop::MultipleParameterLoop()
: n_branches(0)
{}
void
MultipleParameterLoop::parse_input(std::istream &input,
const std::string &filename,
const std::string &last_line,
const bool skip_undefined)
{
AssertThrow(input.fail() == false, ExcIO());
// Note that (to avoid infinite recursion) we have to explicitly call the
// base class version of parse_input and *not* a wrapper (which may be
// virtual and lead us back here)
ParameterHandler::parse_input(input, filename, last_line, skip_undefined);
init_branches();
}
void
MultipleParameterLoop::loop(MultipleParameterLoop::UserClass &uc)
{
for (unsigned int run_no = 0; run_no < n_branches; ++run_no)
{
// give create_new one-based numbers
uc.create_new(run_no + 1);
fill_entry_values(run_no);
uc.run(*this);
}
}
void
MultipleParameterLoop::init_branches()
{
multiple_choices.clear();
init_branches_current_section();
// split up different values
for (auto &multiple_choice : multiple_choices)
multiple_choice.split_different_values();
// finally calculate number of branches
n_branches = 1;
for (const auto &multiple_choice : multiple_choices)
if (multiple_choice.type == Entry::variant)
n_branches *= multiple_choice.different_values.size();
// check whether array entries have the correct
// number of entries
for (const auto &multiple_choice : multiple_choices)
if (multiple_choice.type == Entry::array)
if (multiple_choice.different_values.size() != n_branches)
std::cerr << " The entry value" << std::endl
<< " " << multiple_choice.entry_value << std::endl
<< " for the entry named" << std::endl
<< " " << multiple_choice.entry_name << std::endl
<< " does not have the right number of entries for the "
<< std::endl
<< " " << n_branches
<< " variant runs that will be performed." << std::endl;
// do a first run on filling the values to
// check for the conformance with the regexp
// (later on, this will be lost in the whole
// other output)
for (unsigned int i = 0; i < n_branches; ++i)
fill_entry_values(i);
}
void
MultipleParameterLoop::init_branches_current_section()
{
const boost::property_tree::ptree ¤t_section =
entries->get_child(get_current_path());
// check all entries in the present
// subsection whether they are
// multiple entries
for (const auto &p : current_section)
if (is_parameter_node(p.second) == true)
{
const std::string value = p.second.get<std::string>("value");
if (value.find('{') != std::string::npos)
multiple_choices.emplace_back(subsection_path,
demangle(p.first),
value);
}
// then loop over all subsections
for (const auto &p : current_section)
if (is_parameter_node(p.second) == false)
{
enter_subsection(demangle(p.first));
init_branches_current_section();
leave_subsection();
}
}
void
MultipleParameterLoop::fill_entry_values(const unsigned int run_no)
{
unsigned int possibilities = 1;
std::vector<Entry>::iterator choice;
for (choice = multiple_choices.begin(); choice != multiple_choices.end();
++choice)
{
const unsigned int selection =
(run_no / possibilities) % choice->different_values.size();
std::string entry_value;
if (choice->type == Entry::variant)
entry_value = choice->different_values[selection];
else
{
if (run_no >= choice->different_values.size())
{
std::cerr
<< "The given array for entry <" << choice->entry_name
<< "> does not contain enough elements! Taking empty string instead."
<< std::endl;
entry_value = "";
}
else
entry_value = choice->different_values[run_no];
}
// temporarily enter the
// subsection tree of this
// multiple entry, set the
// value, and get out
// again. the set() operation
// also tests for the
// correctness of the value
// with regard to the pattern
subsection_path.swap(choice->subsection_path);
set(choice->entry_name, entry_value);
subsection_path.swap(choice->subsection_path);
// move ahead if it was a variant entry
if (choice->type == Entry::variant)
possibilities *= choice->different_values.size();
}
}
std::size_t
MultipleParameterLoop::memory_consumption() const
{
std::size_t mem = ParameterHandler::memory_consumption();
for (const auto &multiple_choice : multiple_choices)
mem += multiple_choice.memory_consumption();
return mem;
}
MultipleParameterLoop::Entry::Entry(const std::vector<std::string> &ssp,
const std::string &Name,
const std::string &Value)
: subsection_path(ssp)
, entry_name(Name)
, entry_value(Value)
, type(Entry::array)
{}
void
MultipleParameterLoop::Entry::split_different_values()
{
// split string into three parts:
// part before the opening "{",
// the selection itself, final
// part after "}"
std::string prefix(entry_value, 0, entry_value.find('{'));
std::string multiple(entry_value,
entry_value.find('{') + 1,
entry_value.rfind('}') - entry_value.find('{') - 1);
std::string postfix(entry_value,
entry_value.rfind('}') + 1,
std::string::npos);
// if array entry {{..}}: delete inner
// pair of braces
if (multiple[0] == '{')
multiple.erase(0, 1);
if (multiple.back() == '}')
multiple.erase(multiple.size() - 1, 1);
// erase leading and trailing spaces
// in multiple
while (std::isspace(multiple[0]) != 0)
multiple.erase(0, 1);
while (std::isspace(multiple.back()) != 0)
multiple.erase(multiple.size() - 1, 1);
// delete spaces around '|'
while (multiple.find(" |") != std::string::npos)
multiple.replace(multiple.find(" |"), 2, "|");
while (multiple.find("| ") != std::string::npos)
multiple.replace(multiple.find("| "), 2, "|");
while (multiple.find('|') != std::string::npos)
{
different_values.push_back(
prefix + std::string(multiple, 0, multiple.find('|')) + postfix);
multiple.erase(0, multiple.find('|') + 1);
}
// make up the last selection ("while" broke
// because there was no '|' any more
different_values.push_back(prefix + multiple + postfix);
// finally check whether this was a variant
// entry ({...}) or an array ({{...}})
if ((entry_value.find("{{") != std::string::npos) &&
(entry_value.find("}}") != std::string::npos))
type = Entry::array;
else
type = Entry::variant;
}
std::size_t
MultipleParameterLoop::Entry::memory_consumption() const
{
return (MemoryConsumption::memory_consumption(subsection_path) +
MemoryConsumption::memory_consumption(entry_name) +
MemoryConsumption::memory_consumption(entry_value) +
MemoryConsumption::memory_consumption(different_values) +
sizeof(type));
}
DEAL_II_NAMESPACE_CLOSE
|