/usr/include/deal.II/lac/block_matrix_base.h is in libdeal.ii-dev 8.4.2-2+b1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 | // ---------------------------------------------------------------------
//
// Copyright (C) 2004 - 2016 by the deal.II authors
//
// This file is part of the deal.II library.
//
// The deal.II library is free software; you can use it, redistribute
// it, and/or modify it under the terms of the GNU Lesser General
// Public License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
// The full text of the license can be found in the file LICENSE at
// the top level of the deal.II distribution.
//
// ---------------------------------------------------------------------
#ifndef dealii__block_matrix_base_h
#define dealii__block_matrix_base_h
#include <deal.II/base/config.h>
#include <deal.II/base/table.h>
#include <deal.II/base/thread_management.h>
#include <deal.II/base/utilities.h>
#include <deal.II/base/smartpointer.h>
#include <deal.II/base/memory_consumption.h>
#include <deal.II/lac/block_indices.h>
#include <deal.II/lac/exceptions.h>
#include <deal.II/lac/full_matrix.h>
#include <deal.II/lac/matrix_iterator.h>
#include <deal.II/lac/vector.h>
#include <cmath>
DEAL_II_NAMESPACE_OPEN
template <typename> class MatrixIterator;
/*! @addtogroup Matrix1
*@{
*/
/**
* Namespace in which iterators in block matrices are implemented.
*
* @author Wolfgang Bangerth, 2004
*/
namespace BlockMatrixIterators
{
/**
* Base class for block matrix accessors, implementing the stepping through
* a matrix.
*/
template <class BlockMatrixType>
class AccessorBase
{
public:
/**
* Declare type for container size.
*/
typedef types::global_dof_index size_type;
/**
* Typedef the value type of the matrix we point into.
*/
typedef typename BlockMatrixType::value_type value_type;
/**
* Initialize data fields to default values.
*/
AccessorBase ();
/**
* Block row of the element represented by this object.
*/
unsigned int block_row() const;
/**
* Block column of the element represented by this object.
*/
unsigned int block_column() const;
protected:
/**
* Block row into which we presently point.
*/
unsigned int row_block;
/**
* Block column into which we presently point.
*/
unsigned int col_block;
/**
* Let the iterator class be a friend.
*/
template <typename>
friend class MatrixIterator;
};
/**
* Accessor classes in block matrices.
*/
template <class BlockMatrixType, bool Constness>
class Accessor;
/**
* Block matrix accessor for non const matrices.
*/
template <class BlockMatrixType>
class Accessor<BlockMatrixType, false>
:
public AccessorBase<BlockMatrixType>
{
public:
/**
* Declare type for container size.
*/
typedef types::global_dof_index size_type;
/**
* Type of the matrix used in this accessor.
*/
typedef BlockMatrixType MatrixType;
/**
* Typedef the value type of the matrix we point into.
*/
typedef typename BlockMatrixType::value_type value_type;
/**
* Constructor. Since we use accessors only for read access, a const
* matrix pointer is sufficient.
*
* Place the iterator at the beginning of the given row of the matrix, or
* create the end pointer if @p row equals the total number of rows in the
* matrix.
*/
Accessor (BlockMatrixType *m,
const size_type row,
const size_type col);
/**
* Row number of the element represented by this object.
*/
size_type row() const;
/**
* Column number of the element represented by this object.
*/
size_type column() const;
/**
* Value of the entry at the current position.
*/
value_type value() const;
/**
* Set new value.
*/
void set_value(value_type newval) const;
protected:
/**
* The matrix accessed.
*/
BlockMatrixType *matrix;
/**
* Iterator of the underlying matrix class.
*/
typename BlockMatrixType::BlockType::iterator base_iterator;
/**
* Move ahead one element.
*/
void advance ();
/**
* Compare this accessor with another one for equality.
*/
bool operator == (const Accessor &a) const;
template <typename> friend class MatrixIterator;
friend class Accessor<BlockMatrixType, true>;
};
/**
* Block matrix accessor for constant matrices, implementing the stepping
* through a matrix.
*/
template <class BlockMatrixType>
class Accessor<BlockMatrixType, true>
:
public AccessorBase<BlockMatrixType>
{
public:
/**
* Declare type for container size.
*/
typedef types::global_dof_index size_type;
/**
* Type of the matrix used in this accessor.
*/
typedef const BlockMatrixType MatrixType;
/**
* Typedef the value type of the matrix we point into.
*/
typedef typename BlockMatrixType::value_type value_type;
/**
* Constructor. Since we use accessors only for read access, a const
* matrix pointer is sufficient.
*
* Place the iterator at the beginning of the given row of the matrix, or
* create the end pointer if @p row equals the total number of rows in the
* matrix.
*/
Accessor (const BlockMatrixType *m,
const size_type row,
const size_type col);
/**
* Initialize const accessor from non const accessor.
*/
Accessor(const Accessor<BlockMatrixType, false> &);
/**
* Row number of the element represented by this object.
*/
size_type row() const;
/**
* Column number of the element represented by this object.
*/
size_type column() const;
/**
* Value of the entry at the current position.
*/
value_type value() const;
protected:
/**
* The matrix accessed.
*/
const BlockMatrixType *matrix;
/**
* Iterator of the underlying matrix class.
*/
typename BlockMatrixType::BlockType::const_iterator base_iterator;
/**
* Move ahead one element.
*/
void advance ();
/**
* Compare this accessor with another one for equality.
*/
bool operator == (const Accessor &a) const;
/**
* Let the iterator class be a friend.
*/
template <typename>
friend class dealii::MatrixIterator;
};
}
/**
* Blocked matrix class. The behaviour of objects of this type is almost as
* for the usual matrix objects, with most of the functions being implemented
* in both classes. The main difference is that the matrix represented by this
* object is composed of an array of matrices (e.g. of type
* SparseMatrix<number>) and all accesses to the elements of this object are
* relayed to accesses of the base matrices. The actual type of the individual
* blocks of this matrix is the type of the template argument, and can, for
* example be the usual SparseMatrix or PETScWrappers::SparseMatrix.
*
* In addition to the usual matrix access and linear algebra functions, there
* are functions block() which allow access to the different blocks of the
* matrix. This may, for example, be of help when you want to implement Schur
* complement methods, or block preconditioners, where each block belongs to a
* specific component of the equation you are presently discretizing.
*
* Note that the numbers of blocks and rows are implicitly determined by the
* sparsity pattern objects used.
*
* Objects of this type are frequently used when a system of differential
* equations has solutions with variables that fall into different classes.
* For example, solutions of the Stokes or Navier-Stokes equations have @p dim
* velocity components and one pressure component. In this case, it may make
* sense to consider the linear system of equations as a system of 2x2 blocks,
* and one can construct preconditioners or solvers based on this 2x2 block
* structure. This class can help you in these cases, as it allows to view the
* matrix alternatively as one big matrix, or as a number of individual
* blocks.
*
*
* <h3>Inheriting from this class</h3>
*
* Since this class simply forwards its calls to the subobjects (if necessary
* after adjusting indices denoting which subobject is meant), this class is
* completely independent of the actual type of the subobject. The functions
* that set up block matrices and destroy them, however, have to be
* implemented in derived classes. These functions also have to fill the data
* members provided by this base class, as they are only used passively in
* this class.
*
*
* Most of the functions take a vector or block vector argument. These
* functions can, in general, only successfully be compiled if the individual
* blocks of this matrix implement the respective functions operating on the
* vector type in question. For example, if you have a block sparse matrix
* over deal.II SparseMatrix objects, then you will likely not be able to form
* the matrix-vector multiplication with a block vector over
* PETScWrappers::SparseMatrix objects. If you attempt anyway, you will likely
* get a number of compiler errors.
*
* @note Instantiations for this template are provided for <tt>@<float@> and
* @<double@></tt>; others can be generated in application programs (see the
* section on
* @ref Instantiations
* in the manual).
*
* @see
* @ref GlossBlockLA "Block (linear algebra)"
* @author Wolfgang Bangerth, 2000, 2004
*/
template <typename MatrixType>
class BlockMatrixBase : public Subscriptor
{
public:
/**
* Typedef the type of the underlying matrix.
*/
typedef MatrixType BlockType;
/**
* Type of matrix entries. These are analogous to typedefs in the standard
* library containers.
*/
typedef typename BlockType::value_type value_type;
typedef value_type *pointer;
typedef const value_type *const_pointer;
typedef value_type &reference;
typedef const value_type &const_reference;
typedef types::global_dof_index size_type;
typedef
MatrixIterator<BlockMatrixIterators::Accessor<BlockMatrixBase, false> >
iterator;
typedef
MatrixIterator<BlockMatrixIterators::Accessor<BlockMatrixBase, true> >
const_iterator;
/**
* Default constructor.
*/
BlockMatrixBase ();
/**
* Destructor.
*/
~BlockMatrixBase ();
/**
* Copy the matrix given as argument into the current object.
*
* Copying matrices is an expensive operation that we do not want to happen
* by accident through compiler generated code for <code>operator=</code>.
* (This would happen, for example, if one accidentally declared a function
* argument of the current type <i>by value</i> rather than <i>by
* reference</i>.) The functionality of copying matrices is implemented in
* this member function instead. All copy operations of objects of this type
* therefore require an explicit function call.
*
* The source matrix may be a matrix of arbitrary type, as long as its data
* type is convertible to the data type of this matrix.
*
* The function returns a reference to <tt>this</tt>.
*/
template <class BlockMatrixType>
BlockMatrixBase &
copy_from (const BlockMatrixType &source);
/**
* Access the block with the given coordinates.
*/
BlockType &
block (const unsigned int row,
const unsigned int column);
/**
* Access the block with the given coordinates. Version for constant
* objects.
*/
const BlockType &
block (const unsigned int row,
const unsigned int column) const;
/**
* Return the dimension of the codomain (or range) space. To remember: the
* matrix is of dimension $m \times n$.
*/
size_type m () const;
/**
* Return the dimension of the domain space. To remember: the matrix is of
* dimension $m \times n$.
*/
size_type n () const;
/**
* Return the number of blocks in a column. Returns zero if no sparsity
* pattern is presently associated to this matrix.
*/
unsigned int n_block_rows () const;
/**
* Return the number of blocks in a row. Returns zero if no sparsity pattern
* is presently associated to this matrix.
*/
unsigned int n_block_cols () const;
/**
* Set the element <tt>(i,j)</tt> to <tt>value</tt>. Throws an error if the
* entry does not exist or if <tt>value</tt> is not a finite number. Still,
* it is allowed to store zero values in non-existent fields.
*/
void set (const size_type i,
const size_type j,
const value_type value);
/**
* Set all elements given in a FullMatrix into the sparse matrix locations
* given by <tt>indices</tt>. In other words, this function writes the
* elements in <tt>full_matrix</tt> into the calling matrix, using the
* local-to-global indexing specified by <tt>indices</tt> for both the rows
* and the columns of the matrix. This function assumes a quadratic sparse
* matrix and a quadratic full_matrix, the usual situation in FE
* calculations.
*
* The optional parameter <tt>elide_zero_values</tt> can be used to specify
* whether zero values should be set anyway or they should be filtered away
* (and not change the previous content in the respective element if it
* exists). The default value is <tt>false</tt>, i.e., even zero values are
* treated.
*/
template <typename number>
void set (const std::vector<size_type> &indices,
const FullMatrix<number> &full_matrix,
const bool elide_zero_values = false);
/**
* Same function as before, but now including the possibility to use
* rectangular full_matrices and different local-to-global indexing on rows
* and columns, respectively.
*/
template <typename number>
void set (const std::vector<size_type> &row_indices,
const std::vector<size_type> &col_indices,
const FullMatrix<number> &full_matrix,
const bool elide_zero_values = false);
/**
* Set several elements in the specified row of the matrix with column
* indices as given by <tt>col_indices</tt> to the respective value.
*
* The optional parameter <tt>elide_zero_values</tt> can be used to specify
* whether zero values should be set anyway or they should be filtered away
* (and not change the previous content in the respective element if it
* exists). The default value is <tt>false</tt>, i.e., even zero values are
* treated.
*/
template <typename number>
void set (const size_type row,
const std::vector<size_type> &col_indices,
const std::vector<number> &values,
const bool elide_zero_values = false);
/**
* Set several elements to values given by <tt>values</tt> in a given row in
* columns given by col_indices into the sparse matrix.
*
* The optional parameter <tt>elide_zero_values</tt> can be used to specify
* whether zero values should be inserted anyway or they should be filtered
* away. The default value is <tt>false</tt>, i.e., even zero values are
* inserted/replaced.
*/
template <typename number>
void set (const size_type row,
const size_type n_cols,
const size_type *col_indices,
const number *values,
const bool elide_zero_values = false);
/**
* Add <tt>value</tt> to the element (<i>i,j</i>). Throws an error if the
* entry does not exist or if <tt>value</tt> is not a finite number. Still,
* it is allowed to store zero values in non-existent fields.
*/
void add (const size_type i,
const size_type j,
const value_type value);
/**
* Add all elements given in a FullMatrix<double> into sparse matrix
* locations given by <tt>indices</tt>. In other words, this function adds
* the elements in <tt>full_matrix</tt> to the respective entries in calling
* matrix, using the local-to-global indexing specified by <tt>indices</tt>
* for both the rows and the columns of the matrix. This function assumes a
* quadratic sparse matrix and a quadratic full_matrix, the usual situation
* in FE calculations.
*
* The optional parameter <tt>elide_zero_values</tt> can be used to specify
* whether zero values should be added anyway or these should be filtered
* away and only non-zero data is added. The default value is <tt>true</tt>,
* i.e., zero values won't be added into the matrix.
*/
template <typename number>
void add (const std::vector<size_type> &indices,
const FullMatrix<number> &full_matrix,
const bool elide_zero_values = true);
/**
* Same function as before, but now including the possibility to use
* rectangular full_matrices and different local-to-global indexing on rows
* and columns, respectively.
*/
template <typename number>
void add (const std::vector<size_type> &row_indices,
const std::vector<size_type> &col_indices,
const FullMatrix<number> &full_matrix,
const bool elide_zero_values = true);
/**
* Set several elements in the specified row of the matrix with column
* indices as given by <tt>col_indices</tt> to the respective value.
*
* The optional parameter <tt>elide_zero_values</tt> can be used to specify
* whether zero values should be added anyway or these should be filtered
* away and only non-zero data is added. The default value is <tt>true</tt>,
* i.e., zero values won't be added into the matrix.
*/
template <typename number>
void add (const size_type row,
const std::vector<size_type> &col_indices,
const std::vector<number> &values,
const bool elide_zero_values = true);
/**
* Add an array of values given by <tt>values</tt> in the given global
* matrix row at columns specified by col_indices in the sparse matrix.
*
* The optional parameter <tt>elide_zero_values</tt> can be used to specify
* whether zero values should be added anyway or these should be filtered
* away and only non-zero data is added. The default value is <tt>true</tt>,
* i.e., zero values won't be added into the matrix.
*/
template <typename number>
void add (const size_type row,
const size_type n_cols,
const size_type *col_indices,
const number *values,
const bool elide_zero_values = true,
const bool col_indices_are_sorted = false);
/**
* Add <tt>matrix</tt> scaled by <tt>factor</tt> to this matrix, i.e. the
* matrix <tt>factor*matrix</tt> is added to <tt>this</tt>. If the sparsity
* pattern of the calling matrix does not contain all the elements in the
* sparsity pattern of the input matrix, this function will throw an
* exception.
*
* Depending on MatrixType, however, additional restrictions might arise.
* Some sparse matrix formats require <tt>matrix</tt> to be based on the
* same sparsity pattern as the calling matrix.
*/
void add (const value_type factor,
const BlockMatrixBase<MatrixType> &matrix);
/**
* Return the value of the entry (i,j). This may be an expensive operation
* and you should always take care where to call this function. In order to
* avoid abuse, this function throws an exception if the wanted element does
* not exist in the matrix.
*/
value_type operator () (const size_type i,
const size_type j) const;
/**
* This function is mostly like operator()() in that it returns the value of
* the matrix entry <tt>(i,j)</tt>. The only difference is that if this
* entry does not exist in the sparsity pattern, then instead of raising an
* exception, zero is returned. While this may be convenient in some cases,
* note that it is simple to write algorithms that are slow compared to an
* optimal solution, since the sparsity of the matrix is not used.
*/
value_type el (const size_type i,
const size_type j) const;
/**
* Return the main diagonal element in the <i>i</i>th row. This function
* throws an error if the matrix is not quadratic and also if the diagonal
* blocks of the matrix are not quadratic.
*
* This function is considerably faster than the operator()(), since for
* quadratic matrices, the diagonal entry may be the first to be stored in
* each row and access therefore does not involve searching for the right
* column number.
*/
value_type diag_element (const size_type i) const;
/**
* Call the compress() function on all the subblocks of the matrix.
*
*
* See
* @ref GlossCompress "Compressing distributed objects"
* for more information.
*/
void compress (::dealii::VectorOperation::values operation);
/**
* Multiply the entire matrix by a fixed factor.
*/
BlockMatrixBase &operator *= (const value_type factor);
/**
* Divide the entire matrix by a fixed factor.
*/
BlockMatrixBase &operator /= (const value_type factor);
/**
* Adding Matrix-vector multiplication. Add $M*src$ on $dst$ with $M$ being
* this matrix.
*/
template <class BlockVectorType>
void vmult_add (BlockVectorType &dst,
const BlockVectorType &src) const;
/**
* Adding Matrix-vector multiplication. Add <i>M<sup>T</sup>src</i> to
* <i>dst</i> with <i>M</i> being this matrix. This function does the same
* as vmult_add() but takes the transposed matrix.
*/
template <class BlockVectorType>
void Tvmult_add (BlockVectorType &dst,
const BlockVectorType &src) const;
/**
* Return the norm of the vector <i>v</i> with respect to the norm induced
* by this matrix, i.e. <i>v<sup>T</sup>Mv)</i>. This is useful, e.g. in the
* finite element context, where the <i>L<sup>T</sup></i>-norm of a function
* equals the matrix norm with respect to the mass matrix of the vector
* representing the nodal values of the finite element function. Note that
* even though the function's name might suggest something different, for
* historic reasons not the norm but its square is returned, as defined
* above by the scalar product.
*
* Obviously, the matrix needs to be square for this operation.
*/
template <class BlockVectorType>
value_type
matrix_norm_square (const BlockVectorType &v) const;
/**
* Compute the matrix scalar product $\left(u,Mv\right)$.
*/
template <class BlockVectorType>
value_type
matrix_scalar_product (const BlockVectorType &u,
const BlockVectorType &v) const;
/**
* Compute the residual <i>r=b-Ax</i>. Write the residual into <tt>dst</tt>.
*/
template <class BlockVectorType>
value_type residual (BlockVectorType &dst,
const BlockVectorType &x,
const BlockVectorType &b) const;
/**
* Print the matrix to the given stream, using the format <tt>(line,col)
* value</tt>, i.e. one nonzero entry of the matrix per line. The optional
* flag outputs the sparsity pattern in a different style according to the
* underlying sparse matrix type.
*/
void print (std::ostream &out,
const bool alternative_output = false) const;
/**
* Iterator starting at the first entry.
*/
iterator begin ();
/**
* Final iterator.
*/
iterator end ();
/**
* Iterator starting at the first entry of row <tt>r</tt>.
*/
iterator begin (const size_type r);
/**
* Final iterator of row <tt>r</tt>.
*/
iterator end (const size_type r);
/**
* Iterator starting at the first entry.
*/
const_iterator begin () const;
/**
* Final iterator.
*/
const_iterator end () const;
/**
* Iterator starting at the first entry of row <tt>r</tt>.
*/
const_iterator begin (const size_type r) const;
/**
* Final iterator of row <tt>r</tt>.
*/
const_iterator end (const size_type r) const;
/**
* Return a reference to the underlying BlockIndices data of the rows.
*/
const BlockIndices &get_row_indices () const;
/**
* Return a reference to the underlying BlockIndices data of the columns.
*/
const BlockIndices &get_column_indices () const;
/**
* Determine an estimate for the memory consumption (in bytes) of this
* object. Note that only the memory reserved on the current processor is
* returned in case this is called in an MPI-based program.
*/
std::size_t memory_consumption () const;
/**
* @addtogroup Exceptions
* @{
*/
/**
* Exception
*/
DeclException4 (ExcIncompatibleRowNumbers,
int, int, int, int,
<< "The blocks [" << arg1 << ',' << arg2 << "] and ["
<< arg3 << ',' << arg4 << "] have differing row numbers.");
/**
* Exception
*/
DeclException4 (ExcIncompatibleColNumbers,
int, int, int, int,
<< "The blocks [" << arg1 << ',' << arg2 << "] and ["
<< arg3 << ',' << arg4 << "] have differing column numbers.");
//@}
protected:
/**
* Release all memory and return to a state just like after having called
* the default constructor. It also forgets the sparsity pattern it was
* previously tied to.
*
* This calls clear for all sub-matrices and then resets this object to have
* no blocks at all.
*
* This function is protected since it may be necessary to release
* additional structures. A derived class can make it public again, if it is
* sufficient.
*/
void clear ();
/**
* Index arrays for rows and columns.
*/
BlockIndices row_block_indices;
BlockIndices column_block_indices;
/**
* Array of sub-matrices.
*/
Table<2,SmartPointer<BlockType, BlockMatrixBase<MatrixType> > > sub_objects;
/**
* This function collects the sizes of the sub-objects and stores them in
* internal arrays, in order to be able to relay global indices into the
* matrix to indices into the subobjects. You *must* call this function each
* time after you have changed the size of the sub-objects.
*
* Derived classes should call this function whenever the size of the sub-
* objects has changed and the @p X_block_indices arrays need to be updated.
*
* Note that this function is not public since not all derived classes need
* to export its interface. For example, for the usual deal.II SparseMatrix
* class, the sizes are implicitly determined whenever reinit() is called,
* and individual blocks cannot be resized. For that class, this function
* therefore does not have to be public. On the other hand, for the PETSc
* classes, there is no associated sparsity pattern object that determines
* the block sizes, and for these the function needs to be publicly
* available. These classes therefore export this function.
*/
void collect_sizes ();
/**
* Matrix-vector multiplication: let $dst = M*src$ with $M$ being this
* matrix.
*
* Due to problems with deriving template arguments between the block and
* non-block versions of the vmult/Tvmult functions, the actual functions
* are implemented in derived classes, with implementations forwarding the
* calls to the implementations provided here under a unique name for which
* template arguments can be derived by the compiler.
*/
template <class BlockVectorType>
void vmult_block_block (BlockVectorType &dst,
const BlockVectorType &src) const;
/**
* Matrix-vector multiplication. Just like the previous function, but only
* applicable if the matrix has only one block column.
*
* Due to problems with deriving template arguments between the block and
* non-block versions of the vmult/Tvmult functions, the actual functions
* are implemented in derived classes, with implementations forwarding the
* calls to the implementations provided here under a unique name for which
* template arguments can be derived by the compiler.
*/
template <class BlockVectorType,
class VectorType>
void vmult_block_nonblock (BlockVectorType &dst,
const VectorType &src) const;
/**
* Matrix-vector multiplication. Just like the previous function, but only
* applicable if the matrix has only one block row.
*
* Due to problems with deriving template arguments between the block and
* non-block versions of the vmult/Tvmult functions, the actual functions
* are implemented in derived classes, with implementations forwarding the
* calls to the implementations provided here under a unique name for which
* template arguments can be derived by the compiler.
*/
template <class BlockVectorType,
class VectorType>
void vmult_nonblock_block (VectorType &dst,
const BlockVectorType &src) const;
/**
* Matrix-vector multiplication. Just like the previous function, but only
* applicable if the matrix has only one block.
*
* Due to problems with deriving template arguments between the block and
* non-block versions of the vmult/Tvmult functions, the actual functions
* are implemented in derived classes, with implementations forwarding the
* calls to the implementations provided here under a unique name for which
* template arguments can be derived by the compiler.
*/
template <class VectorType>
void vmult_nonblock_nonblock (VectorType &dst,
const VectorType &src) const;
/**
* Matrix-vector multiplication: let $dst = M^T*src$ with $M$ being this
* matrix. This function does the same as vmult() but takes the transposed
* matrix.
*
* Due to problems with deriving template arguments between the block and
* non-block versions of the vmult/Tvmult functions, the actual functions
* are implemented in derived classes, with implementations forwarding the
* calls to the implementations provided here under a unique name for which
* template arguments can be derived by the compiler.
*/
template <class BlockVectorType>
void Tvmult_block_block (BlockVectorType &dst,
const BlockVectorType &src) const;
/**
* Matrix-vector multiplication. Just like the previous function, but only
* applicable if the matrix has only one block row.
*
* Due to problems with deriving template arguments between the block and
* non-block versions of the vmult/Tvmult functions, the actual functions
* are implemented in derived classes, with implementations forwarding the
* calls to the implementations provided here under a unique name for which
* template arguments can be derived by the compiler.
*/
template <class BlockVectorType,
class VectorType>
void Tvmult_block_nonblock (BlockVectorType &dst,
const VectorType &src) const;
/**
* Matrix-vector multiplication. Just like the previous function, but only
* applicable if the matrix has only one block column.
*
* Due to problems with deriving template arguments between the block and
* non-block versions of the vmult/Tvmult functions, the actual functions
* are implemented in derived classes, with implementations forwarding the
* calls to the implementations provided here under a unique name for which
* template arguments can be derived by the compiler.
*/
template <class BlockVectorType,
class VectorType>
void Tvmult_nonblock_block (VectorType &dst,
const BlockVectorType &src) const;
/**
* Matrix-vector multiplication. Just like the previous function, but only
* applicable if the matrix has only one block.
*
* Due to problems with deriving template arguments between the block and
* non-block versions of the vmult/Tvmult functions, the actual functions
* are implemented in derived classes, with implementations forwarding the
* calls to the implementations provided here under a unique name for which
* template arguments can be derived by the compiler.
*/
template <class VectorType>
void Tvmult_nonblock_nonblock (VectorType &dst,
const VectorType &src) const;
protected:
/**
* Some matrix types, in particular PETSc, need to synchronize set and add
* operations. This has to be done for all matrices in the BlockMatrix. This
* routine prepares adding of elements by notifying all blocks. Called by
* all internal routines before adding elements.
*/
void prepare_add_operation();
/**
* Notifies all blocks to let them prepare for setting elements, see
* prepare_add_operation().
*/
void prepare_set_operation();
private:
/**
* A structure containing some fields used by the set() and add() functions
* that is used to pre-sort the input fields. Since one can reasonably
* expect to call set() and add() from multiple threads at once as long as
* the matrix indices that are touched are disjoint, these temporary data
* fields need to be guarded by a mutex; the structure therefore contains
* such a mutex as a member variable.
*/
struct TemporaryData
{
/**
* Temporary vector for counting the elements written into the individual
* blocks when doing a collective add or set.
*/
std::vector<size_type> counter_within_block;
/**
* Temporary vector for column indices on each block when writing local to
* global data on each sparse matrix.
*/
std::vector<std::vector<size_type> > column_indices;
/**
* Temporary vector for storing the local values (they need to be
* reordered when writing local to global).
*/
std::vector<std::vector<value_type> > column_values;
/**
* A mutex variable used to guard access to the member variables of this
* structure;
*/
Threads::Mutex mutex;
/**
* Copy operator. This is needed because the default copy operator of this
* class is deleted (since Threads::Mutex is not copyable) and hence the
* default copy operator of the enclosing class is also deleted.
*
* The implementation here simply does nothing -- TemporaryData objects
* are just scratch objects that are resized at the beginning of their
* use, so there is no point actually copying anything.
*/
TemporaryData &operator = (const TemporaryData &)
{
return *this;
}
};
/**
* A set of scratch arrays that can be used by the add() and set() functions
* that take pointers to data to pre-sort indices before use. Access from
* multiple threads is synchronized via the mutex variable that is part of
* the structure.
*/
TemporaryData temporary_data;
/**
* Make the iterator class a friend. We have to work around a compiler bug
* here again.
*/
template <typename, bool>
friend class BlockMatrixIterators::Accessor;
template <typename>
friend class MatrixIterator;
};
/*@}*/
#ifndef DOXYGEN
/* ------------------------- Template functions ---------------------- */
namespace BlockMatrixIterators
{
template <class BlockMatrixType>
inline
AccessorBase<BlockMatrixType>::AccessorBase()
:
row_block(0),
col_block(0)
{}
template <class BlockMatrixType>
inline
unsigned int
AccessorBase<BlockMatrixType>::block_row() const
{
Assert (row_block != numbers::invalid_unsigned_int,
ExcIteratorPastEnd());
return row_block;
}
template <class BlockMatrixType>
inline
unsigned int
AccessorBase<BlockMatrixType>::block_column() const
{
Assert (col_block != numbers::invalid_unsigned_int,
ExcIteratorPastEnd());
return col_block;
}
template <class BlockMatrixType>
inline
Accessor<BlockMatrixType, true>::Accessor (
const BlockMatrixType *matrix,
const size_type row,
const size_type col)
:
matrix(matrix),
base_iterator(matrix->block(0,0).begin())
{
(void)col;
Assert(col==0, ExcNotImplemented());
// check if this is a regular row or
// the end of the matrix
if (row < matrix->m())
{
const std::pair<unsigned int,size_type> indices
= matrix->row_block_indices.global_to_local(row);
// find the first block that does
// have an entry in this row
for (unsigned int bc=0; bc<matrix->n_block_cols(); ++bc)
{
base_iterator
= matrix->block(indices.first, bc).begin(indices.second);
if (base_iterator !=
matrix->block(indices.first, bc).end(indices.second))
{
this->row_block = indices.first;
this->col_block = bc;
return;
}
}
// hm, there is no block that has
// an entry in this column. we need
// to take the next entry then,
// which may be the first entry of
// the next row, or recursively the
// next row, or so on
*this = Accessor (matrix, row+1, 0);
}
else
{
// we were asked to create the end
// iterator for this matrix
this->row_block = numbers::invalid_unsigned_int;
this->col_block = numbers::invalid_unsigned_int;
}
}
// template <class BlockMatrixType>
// inline
// Accessor<BlockMatrixType, true>::Accessor (const Accessor<BlockMatrixType, true>& other)
// :
// matrix(other.matrix),
// base_iterator(other.base_iterator)
// {
// this->row_block = other.row_block;
// this->col_block = other.col_block;
// }
template <class BlockMatrixType>
inline
Accessor<BlockMatrixType, true>::Accessor (const Accessor<BlockMatrixType, false> &other)
:
matrix(other.matrix),
base_iterator(other.base_iterator)
{
this->row_block = other.row_block;
this->col_block = other.col_block;
}
template <class BlockMatrixType>
inline
typename Accessor<BlockMatrixType, true>::size_type
Accessor<BlockMatrixType, true>::row() const
{
Assert (this->row_block != numbers::invalid_unsigned_int,
ExcIteratorPastEnd());
return (matrix->row_block_indices.local_to_global(this->row_block, 0) +
base_iterator->row());
}
template <class BlockMatrixType>
inline
typename Accessor<BlockMatrixType, true>::size_type
Accessor<BlockMatrixType, true>::column() const
{
Assert (this->col_block != numbers::invalid_unsigned_int,
ExcIteratorPastEnd());
return (matrix->column_block_indices.local_to_global(this->col_block,0) +
base_iterator->column());
}
template <class BlockMatrixType>
inline
typename Accessor<BlockMatrixType, true>::value_type
Accessor<BlockMatrixType, true>::value () const
{
Assert (this->row_block != numbers::invalid_unsigned_int,
ExcIteratorPastEnd());
Assert (this->col_block != numbers::invalid_unsigned_int,
ExcIteratorPastEnd());
return base_iterator->value();
}
template <class BlockMatrixType>
inline
void
Accessor<BlockMatrixType, true>::advance ()
{
Assert (this->row_block != numbers::invalid_unsigned_int,
ExcIteratorPastEnd());
Assert (this->col_block != numbers::invalid_unsigned_int,
ExcIteratorPastEnd());
// Remember current row inside block
size_type local_row = base_iterator->row();
// Advance one element inside the
// current block
++base_iterator;
// while we hit the end of the row of a
// block (which may happen multiple
// times if rows inside a block are
// empty), we have to jump to the next
// block and take the
while (base_iterator ==
matrix->block(this->row_block, this->col_block).end(local_row))
{
// jump to next block in this block
// row, if possible, otherwise go
// to next row
if (this->col_block < matrix->n_block_cols()-1)
{
++this->col_block;
base_iterator
= matrix->block(this->row_block, this->col_block).begin(local_row);
}
else
{
// jump back to next row in
// first block column
this->col_block = 0;
++local_row;
// see if this has brought us
// past the number of rows in
// this block. if so see
// whether we've just fallen
// off the end of the whole
// matrix
if (local_row == matrix->block(this->row_block, this->col_block).m())
{
local_row = 0;
++this->row_block;
if (this->row_block == matrix->n_block_rows())
{
this->row_block = numbers::invalid_unsigned_int;
this->col_block = numbers::invalid_unsigned_int;
return;
}
}
base_iterator
= matrix->block(this->row_block, this->col_block).begin(local_row);
}
}
}
template <class BlockMatrixType>
inline
bool
Accessor<BlockMatrixType, true>::operator == (const Accessor &a) const
{
if (matrix != a.matrix)
return false;
if (this->row_block == a.row_block
&& this->col_block == a.col_block)
// end iterators do not necessarily
// have to have the same
// base_iterator representation, but
// valid iterators have to
return (((this->row_block == numbers::invalid_unsigned_int)
&&
(this->col_block == numbers::invalid_unsigned_int))
||
(base_iterator == a.base_iterator));
return false;
}
//----------------------------------------------------------------------//
template <class BlockMatrixType>
inline
Accessor<BlockMatrixType, false>::Accessor (
BlockMatrixType *matrix,
const size_type row,
const size_type col)
:
matrix(matrix),
base_iterator(matrix->block(0,0).begin())
{
(void)col;
Assert(col==0, ExcNotImplemented());
// check if this is a regular row or
// the end of the matrix
if (row < matrix->m())
{
const std::pair<unsigned int,size_type> indices
= matrix->row_block_indices.global_to_local(row);
// find the first block that does
// have an entry in this row
for (size_type bc=0; bc<matrix->n_block_cols(); ++bc)
{
base_iterator
= matrix->block(indices.first, bc).begin(indices.second);
if (base_iterator !=
matrix->block(indices.first, bc).end(indices.second))
{
this->row_block = indices.first;
this->col_block = bc;
return;
}
}
// hm, there is no block that has
// an entry in this column. we need
// to take the next entry then,
// which may be the first entry of
// the next row, or recursively the
// next row, or so on
*this = Accessor (matrix, row+1, 0);
}
else
{
// we were asked to create the end
// iterator for this matrix
this->row_block = numbers::invalid_size_type;
this->col_block = numbers::invalid_size_type;
}
}
template <class BlockMatrixType>
inline
typename Accessor<BlockMatrixType, false>::size_type
Accessor<BlockMatrixType, false>::row() const
{
Assert (this->row_block != numbers::invalid_size_type,
ExcIteratorPastEnd());
return (matrix->row_block_indices.local_to_global(this->row_block, 0) +
base_iterator->row());
}
template <class BlockMatrixType>
inline
typename Accessor<BlockMatrixType, false>::size_type
Accessor<BlockMatrixType, false>::column() const
{
Assert (this->col_block != numbers::invalid_size_type,
ExcIteratorPastEnd());
return (matrix->column_block_indices.local_to_global(this->col_block,0) +
base_iterator->column());
}
template <class BlockMatrixType>
inline
typename Accessor<BlockMatrixType, false>::value_type
Accessor<BlockMatrixType, false>::value () const
{
Assert (this->row_block != numbers::invalid_size_type,
ExcIteratorPastEnd());
Assert (this->col_block != numbers::invalid_size_type,
ExcIteratorPastEnd());
return base_iterator->value();
}
template <class BlockMatrixType>
inline
void
Accessor<BlockMatrixType, false>::set_value (typename Accessor<BlockMatrixType, false>::value_type newval) const
{
Assert (this->row_block != numbers::invalid_size_type,
ExcIteratorPastEnd());
Assert (this->col_block != numbers::invalid_size_type,
ExcIteratorPastEnd());
base_iterator->value() = newval;
}
template <class BlockMatrixType>
inline
void
Accessor<BlockMatrixType, false>::advance ()
{
Assert (this->row_block != numbers::invalid_size_type,
ExcIteratorPastEnd());
Assert (this->col_block != numbers::invalid_size_type,
ExcIteratorPastEnd());
// Remember current row inside block
size_type local_row = base_iterator->row();
// Advance one element inside the
// current block
++base_iterator;
// while we hit the end of the row of a
// block (which may happen multiple
// times if rows inside a block are
// empty), we have to jump to the next
// block and take the
while (base_iterator ==
matrix->block(this->row_block, this->col_block).end(local_row))
{
// jump to next block in this block
// row, if possible, otherwise go
// to next row
if (this->col_block < matrix->n_block_cols()-1)
{
++this->col_block;
base_iterator
= matrix->block(this->row_block, this->col_block).begin(local_row);
}
else
{
// jump back to next row in
// first block column
this->col_block = 0;
++local_row;
// see if this has brought us
// past the number of rows in
// this block. if so see
// whether we've just fallen
// off the end of the whole
// matrix
if (local_row == matrix->block(this->row_block, this->col_block).m())
{
local_row = 0;
++this->row_block;
if (this->row_block == matrix->n_block_rows())
{
this->row_block = numbers::invalid_size_type;
this->col_block = numbers::invalid_size_type;
return;
}
}
base_iterator
= matrix->block(this->row_block, this->col_block).begin(local_row);
}
}
}
template <class BlockMatrixType>
inline
bool
Accessor<BlockMatrixType, false>::operator == (const Accessor &a) const
{
if (matrix != a.matrix)
return false;
if (this->row_block == a.row_block
&& this->col_block == a.col_block)
// end iterators do not necessarily
// have to have the same
// base_iterator representation, but
// valid iterators have to
return (((this->row_block == numbers::invalid_size_type)
&&
(this->col_block == numbers::invalid_size_type))
||
(base_iterator == a.base_iterator));
return false;
}
}
//---------------------------------------------------------------------------
template <typename MatrixType>
inline
BlockMatrixBase<MatrixType>::BlockMatrixBase ()
{}
template <typename MatrixType>
inline
BlockMatrixBase<MatrixType>::~BlockMatrixBase ()
{
clear ();
}
template <class MatrixType>
template <class BlockMatrixType>
inline
BlockMatrixBase<MatrixType> &
BlockMatrixBase<MatrixType>::
copy_from (const BlockMatrixType &source)
{
for (unsigned int r=0; r<n_block_rows(); ++r)
for (unsigned int c=0; c<n_block_cols(); ++c)
block(r,c).copy_from (source.block(r,c));
return *this;
}
template <class MatrixType>
std::size_t
BlockMatrixBase<MatrixType>::memory_consumption () const
{
std::size_t mem =
MemoryConsumption::memory_consumption(row_block_indices)+
MemoryConsumption::memory_consumption(column_block_indices)+
MemoryConsumption::memory_consumption(sub_objects)+
MemoryConsumption::memory_consumption(temporary_data.counter_within_block)+
MemoryConsumption::memory_consumption(temporary_data.column_indices)+
MemoryConsumption::memory_consumption(temporary_data.column_values)+
sizeof(temporary_data.mutex);
for (unsigned int r=0; r<n_block_rows(); ++r)
for (unsigned int c=0; c<n_block_cols(); ++c)
{
MatrixType *p = this->sub_objects[r][c];
mem += MemoryConsumption::memory_consumption(*p);
}
return mem;
}
template <class MatrixType>
inline
void
BlockMatrixBase<MatrixType>::clear ()
{
for (unsigned int r=0; r<n_block_rows(); ++r)
for (unsigned int c=0; c<n_block_cols(); ++c)
{
MatrixType *p = this->sub_objects[r][c];
this->sub_objects[r][c] = 0;
delete p;
}
sub_objects.reinit (0,0);
// reset block indices to empty
row_block_indices = column_block_indices = BlockIndices ();
}
template <class MatrixType>
inline
typename BlockMatrixBase<MatrixType>::BlockType &
BlockMatrixBase<MatrixType>::block (const unsigned int row,
const unsigned int column)
{
Assert (row<n_block_rows(),
ExcIndexRange (row, 0, n_block_rows()));
Assert (column<n_block_cols(),
ExcIndexRange (column, 0, n_block_cols()));
return *sub_objects[row][column];
}
template <class MatrixType>
inline
const typename BlockMatrixBase<MatrixType>::BlockType &
BlockMatrixBase<MatrixType>::block (const unsigned int row,
const unsigned int column) const
{
Assert (row<n_block_rows(),
ExcIndexRange (row, 0, n_block_rows()));
Assert (column<n_block_cols(),
ExcIndexRange (column, 0, n_block_cols()));
return *sub_objects[row][column];
}
template <class MatrixType>
inline
typename BlockMatrixBase<MatrixType>::size_type
BlockMatrixBase<MatrixType>::m () const
{
return row_block_indices.total_size();
}
template <class MatrixType>
inline
typename BlockMatrixBase<MatrixType>::size_type
BlockMatrixBase<MatrixType>::n () const
{
return column_block_indices.total_size();
}
template <class MatrixType>
inline
unsigned int
BlockMatrixBase<MatrixType>::n_block_cols () const
{
return column_block_indices.size();
}
template <class MatrixType>
inline
unsigned int
BlockMatrixBase<MatrixType>::n_block_rows () const
{
return row_block_indices.size();
}
// Write the single set manually,
// since the other function has a lot
// of overhead in that case.
template <class MatrixType>
inline
void
BlockMatrixBase<MatrixType>::set (const size_type i,
const size_type j,
const value_type value)
{
prepare_set_operation();
AssertIsFinite(value);
const std::pair<unsigned int,size_type>
row_index = row_block_indices.global_to_local (i),
col_index = column_block_indices.global_to_local (j);
block(row_index.first,col_index.first).set (row_index.second,
col_index.second,
value);
}
template <class MatrixType>
template <typename number>
inline
void
BlockMatrixBase<MatrixType>::set (const std::vector<size_type> &row_indices,
const std::vector<size_type> &col_indices,
const FullMatrix<number> &values,
const bool elide_zero_values)
{
Assert (row_indices.size() == values.m(),
ExcDimensionMismatch(row_indices.size(), values.m()));
Assert (col_indices.size() == values.n(),
ExcDimensionMismatch(col_indices.size(), values.n()));
for (size_type i=0; i<row_indices.size(); ++i)
set (row_indices[i], col_indices.size(), &col_indices[0], &values(i,0),
elide_zero_values);
}
template <class MatrixType>
template <typename number>
inline
void
BlockMatrixBase<MatrixType>::set (const std::vector<size_type> &indices,
const FullMatrix<number> &values,
const bool elide_zero_values)
{
Assert (indices.size() == values.m(),
ExcDimensionMismatch(indices.size(), values.m()));
Assert (values.n() == values.m(), ExcNotQuadratic());
for (size_type i=0; i<indices.size(); ++i)
set (indices[i], indices.size(), &indices[0], &values(i,0),
elide_zero_values);
}
template <class MatrixType>
template <typename number>
inline
void
BlockMatrixBase<MatrixType>::set (const size_type row,
const std::vector<size_type> &col_indices,
const std::vector<number> &values,
const bool elide_zero_values)
{
Assert (col_indices.size() == values.size(),
ExcDimensionMismatch(col_indices.size(), values.size()));
set (row, col_indices.size(), &col_indices[0], &values[0],
elide_zero_values);
}
// This is a very messy function, since
// we need to calculate to each position
// the location in the global array.
template <class MatrixType>
template <typename number>
inline
void
BlockMatrixBase<MatrixType>::set (const size_type row,
const size_type n_cols,
const size_type *col_indices,
const number *values,
const bool elide_zero_values)
{
prepare_set_operation();
// lock access to the temporary data structure to
// allow multiple threads to call this function concurrently
Threads::Mutex::ScopedLock lock (temporary_data.mutex);
// Resize scratch arrays
if (temporary_data.column_indices.size() < this->n_block_cols())
{
temporary_data.column_indices.resize (this->n_block_cols());
temporary_data.column_values.resize (this->n_block_cols());
temporary_data.counter_within_block.resize (this->n_block_cols());
}
// Resize sub-arrays to n_cols. This
// is a bit wasteful, but we resize
// only a few times (then the maximum
// row length won't increase that
// much any more). At least we know
// that all arrays are going to be of
// the same size, so we can check
// whether the size of one is large
// enough before actually going
// through all of them.
if (temporary_data.column_indices[0].size() < n_cols)
{
for (unsigned int i=0; i<this->n_block_cols(); ++i)
{
temporary_data.column_indices[i].resize(n_cols);
temporary_data.column_values[i].resize(n_cols);
}
}
// Reset the number of added elements
// in each block to zero.
for (unsigned int i=0; i<this->n_block_cols(); ++i)
temporary_data.counter_within_block[i] = 0;
// Go through the column indices to
// find out which portions of the
// values should be set in which
// block of the matrix. We need to
// touch all the data, since we can't
// be sure that the data of one block
// is stored contiguously (in fact,
// indices will be intermixed when it
// comes from an element matrix).
for (size_type j=0; j<n_cols; ++j)
{
number value = values[j];
if (value == number() && elide_zero_values == true)
continue;
const std::pair<unsigned int, size_type>
col_index = this->column_block_indices.global_to_local(col_indices[j]);
const size_type local_index = temporary_data.counter_within_block[col_index.first]++;
temporary_data.column_indices[col_index.first][local_index] = col_index.second;
temporary_data.column_values[col_index.first][local_index] = value;
}
#ifdef DEBUG
// If in debug mode, do a check whether
// the right length has been obtained.
size_type length = 0;
for (unsigned int i=0; i<this->n_block_cols(); ++i)
length += temporary_data.counter_within_block[i];
Assert (length <= n_cols, ExcInternalError());
#endif
// Now we found out about where the
// individual columns should start and
// where we should start reading out
// data. Now let's write the data into
// the individual blocks!
const std::pair<unsigned int,size_type>
row_index = this->row_block_indices.global_to_local (row);
for (unsigned int block_col=0; block_col<n_block_cols(); ++block_col)
{
if (temporary_data.counter_within_block[block_col] == 0)
continue;
block(row_index.first, block_col).set
(row_index.second,
temporary_data.counter_within_block[block_col],
&temporary_data.column_indices[block_col][0],
&temporary_data.column_values[block_col][0],
false);
}
}
template <class MatrixType>
inline
void
BlockMatrixBase<MatrixType>::add (const size_type i,
const size_type j,
const value_type value)
{
AssertIsFinite(value);
prepare_add_operation();
// save some cycles for zero additions, but
// only if it is safe for the matrix we are
// working with
typedef typename MatrixType::Traits MatrixTraits;
if ((MatrixTraits::zero_addition_can_be_elided == true)
&&
(value == value_type()))
return;
const std::pair<unsigned int,size_type>
row_index = row_block_indices.global_to_local (i),
col_index = column_block_indices.global_to_local (j);
block(row_index.first,col_index.first).add (row_index.second,
col_index.second,
value);
}
template <class MatrixType>
template <typename number>
inline
void
BlockMatrixBase<MatrixType>::add (const std::vector<size_type> &row_indices,
const std::vector<size_type> &col_indices,
const FullMatrix<number> &values,
const bool elide_zero_values)
{
Assert (row_indices.size() == values.m(),
ExcDimensionMismatch(row_indices.size(), values.m()));
Assert (col_indices.size() == values.n(),
ExcDimensionMismatch(col_indices.size(), values.n()));
for (size_type i=0; i<row_indices.size(); ++i)
add (row_indices[i], col_indices.size(), &col_indices[0], &values(i,0),
elide_zero_values);
}
template <class MatrixType>
template <typename number>
inline
void
BlockMatrixBase<MatrixType>::add (const std::vector<size_type> &indices,
const FullMatrix<number> &values,
const bool elide_zero_values)
{
Assert (indices.size() == values.m(),
ExcDimensionMismatch(indices.size(), values.m()));
Assert (values.n() == values.m(), ExcNotQuadratic());
for (size_type i=0; i<indices.size(); ++i)
add (indices[i], indices.size(), &indices[0], &values(i,0),
elide_zero_values);
}
template <class MatrixType>
template <typename number>
inline
void
BlockMatrixBase<MatrixType>::add (const size_type row,
const std::vector<size_type> &col_indices,
const std::vector<number> &values,
const bool elide_zero_values)
{
Assert (col_indices.size() == values.size(),
ExcDimensionMismatch(col_indices.size(), values.size()));
add (row, col_indices.size(), &col_indices[0], &values[0],
elide_zero_values);
}
// This is a very messy function, since
// we need to calculate to each position
// the location in the global array.
template <class MatrixType>
template <typename number>
inline
void
BlockMatrixBase<MatrixType>::add (const size_type row,
const size_type n_cols,
const size_type *col_indices,
const number *values,
const bool elide_zero_values,
const bool col_indices_are_sorted)
{
prepare_add_operation();
// TODO: Look over this to find out
// whether we can do that more
// efficiently.
if (col_indices_are_sorted == true)
{
#ifdef DEBUG
// check whether indices really are
// sorted.
size_type before = col_indices[0];
for (size_type i=1; i<n_cols; ++i)
if (col_indices[i] <= before)
Assert (false, ExcMessage ("Flag col_indices_are_sorted is set, but "
"indices appear to not be sorted."))
else
before = col_indices[i];
#endif
const std::pair<unsigned int,size_type>
row_index = this->row_block_indices.global_to_local (row);
if (this->n_block_cols() > 1)
{
const size_type *first_block = Utilities::lower_bound (col_indices,
col_indices+n_cols,
this->column_block_indices.block_start(1));
const size_type n_zero_block_indices = first_block - col_indices;
block(row_index.first, 0).add (row_index.second,
n_zero_block_indices,
col_indices,
values,
elide_zero_values,
col_indices_are_sorted);
if (n_zero_block_indices < n_cols)
this->add(row, n_cols - n_zero_block_indices, first_block,
values + n_zero_block_indices, elide_zero_values,
false);
}
else
{
block(row_index.first, 0). add (row_index.second,
n_cols,
col_indices,
values,
elide_zero_values,
col_indices_are_sorted);
}
return;
}
// Lock scratch arrays, then resize them
Threads::Mutex::ScopedLock lock (temporary_data.mutex);
if (temporary_data.column_indices.size() < this->n_block_cols())
{
temporary_data.column_indices.resize (this->n_block_cols());
temporary_data.column_values.resize (this->n_block_cols());
temporary_data.counter_within_block.resize (this->n_block_cols());
}
// Resize sub-arrays to n_cols. This
// is a bit wasteful, but we resize
// only a few times (then the maximum
// row length won't increase that
// much any more). At least we know
// that all arrays are going to be of
// the same size, so we can check
// whether the size of one is large
// enough before actually going
// through all of them.
if (temporary_data.column_indices[0].size() < n_cols)
{
for (unsigned int i=0; i<this->n_block_cols(); ++i)
{
temporary_data.column_indices[i].resize(n_cols);
temporary_data.column_values[i].resize(n_cols);
}
}
// Reset the number of added elements
// in each block to zero.
for (unsigned int i=0; i<this->n_block_cols(); ++i)
temporary_data.counter_within_block[i] = 0;
// Go through the column indices to
// find out which portions of the
// values should be written into
// which block of the matrix. We need
// to touch all the data, since we
// can't be sure that the data of one
// block is stored contiguously (in
// fact, data will be intermixed when
// it comes from an element matrix).
for (size_type j=0; j<n_cols; ++j)
{
number value = values[j];
if (value == number() && elide_zero_values == true)
continue;
const std::pair<unsigned int, size_type>
col_index = this->column_block_indices.global_to_local(col_indices[j]);
const size_type local_index = temporary_data.counter_within_block[col_index.first]++;
temporary_data.column_indices[col_index.first][local_index] = col_index.second;
temporary_data.column_values[col_index.first][local_index] = value;
}
#ifdef DEBUG
// If in debug mode, do a check whether
// the right length has been obtained.
size_type length = 0;
for (unsigned int i=0; i<this->n_block_cols(); ++i)
length += temporary_data.counter_within_block[i];
Assert (length <= n_cols, ExcInternalError());
#endif
// Now we found out about where the
// individual columns should start and
// where we should start reading out
// data. Now let's write the data into
// the individual blocks!
const std::pair<unsigned int,size_type>
row_index = this->row_block_indices.global_to_local (row);
for (unsigned int block_col=0; block_col<n_block_cols(); ++block_col)
{
if (temporary_data.counter_within_block[block_col] == 0)
continue;
block(row_index.first, block_col).add
(row_index.second,
temporary_data.counter_within_block[block_col],
&temporary_data.column_indices[block_col][0],
&temporary_data.column_values[block_col][0],
false,
col_indices_are_sorted);
}
}
template <class MatrixType>
inline
void
BlockMatrixBase<MatrixType>::add (const value_type factor,
const BlockMatrixBase<MatrixType> &matrix)
{
AssertIsFinite(factor);
prepare_add_operation();
// save some cycles for zero additions, but
// only if it is safe for the matrix we are
// working with
typedef typename MatrixType::Traits MatrixTraits;
if ((MatrixTraits::zero_addition_can_be_elided == true)
&&
(factor == 0))
return;
for (unsigned int row=0; row<n_block_rows(); ++row)
for (unsigned int col=0; col<n_block_cols(); ++col)
// This function should throw if the sparsity
// patterns of the two blocks differ
block(row, col).add(factor, matrix.block(row,col));
}
template <class MatrixType>
inline
typename BlockMatrixBase<MatrixType>::value_type
BlockMatrixBase<MatrixType>::operator () (const size_type i,
const size_type j) const
{
const std::pair<unsigned int,size_type>
row_index = row_block_indices.global_to_local (i),
col_index = column_block_indices.global_to_local (j);
return block(row_index.first,col_index.first) (row_index.second,
col_index.second);
}
template <class MatrixType>
inline
typename BlockMatrixBase<MatrixType>::value_type
BlockMatrixBase<MatrixType>::el (const size_type i,
const size_type j) const
{
const std::pair<unsigned int,size_type>
row_index = row_block_indices.global_to_local (i),
col_index = column_block_indices.global_to_local (j);
return block(row_index.first,col_index.first).el (row_index.second,
col_index.second);
}
template <class MatrixType>
inline
typename BlockMatrixBase<MatrixType>::value_type
BlockMatrixBase<MatrixType>::diag_element (const size_type i) const
{
Assert (n_block_rows() == n_block_cols(),
ExcNotQuadratic());
const std::pair<unsigned int,size_type>
index = row_block_indices.global_to_local (i);
return block(index.first,index.first).diag_element(index.second);
}
template <class MatrixType>
inline
void
BlockMatrixBase<MatrixType>::compress (::dealii::VectorOperation::values operation)
{
for (unsigned int r=0; r<n_block_rows(); ++r)
for (unsigned int c=0; c<n_block_cols(); ++c)
block(r,c).compress (operation);
}
template <class MatrixType>
inline
BlockMatrixBase<MatrixType> &
BlockMatrixBase<MatrixType>::operator *= (const value_type factor)
{
Assert (n_block_cols() != 0, ExcNotInitialized());
Assert (n_block_rows() != 0, ExcNotInitialized());
for (unsigned int r=0; r<n_block_rows(); ++r)
for (unsigned int c=0; c<n_block_cols(); ++c)
block(r,c) *= factor;
return *this;
}
template <class MatrixType>
inline
BlockMatrixBase<MatrixType> &
BlockMatrixBase<MatrixType>::operator /= (const value_type factor)
{
Assert (n_block_cols() != 0, ExcNotInitialized());
Assert (n_block_rows() != 0, ExcNotInitialized());
Assert (factor !=0, ExcDivideByZero());
const value_type factor_inv = 1. / factor;
for (unsigned int r=0; r<n_block_rows(); ++r)
for (unsigned int c=0; c<n_block_cols(); ++c)
block(r,c) *= factor_inv;
return *this;
}
template <class MatrixType>
const BlockIndices &
BlockMatrixBase<MatrixType>::get_row_indices () const
{
return this->row_block_indices;
}
template <class MatrixType>
const BlockIndices &
BlockMatrixBase<MatrixType>::get_column_indices () const
{
return this->column_block_indices;
}
template <class MatrixType>
template <class BlockVectorType>
void
BlockMatrixBase<MatrixType>::
vmult_block_block (BlockVectorType &dst,
const BlockVectorType &src) const
{
Assert (dst.n_blocks() == n_block_rows(),
ExcDimensionMismatch(dst.n_blocks(), n_block_rows()));
Assert (src.n_blocks() == n_block_cols(),
ExcDimensionMismatch(src.n_blocks(), n_block_cols()));
for (size_type row=0; row<n_block_rows(); ++row)
{
block(row,0).vmult (dst.block(row),
src.block(0));
for (size_type col=1; col<n_block_cols(); ++col)
block(row,col).vmult_add (dst.block(row),
src.block(col));
};
}
template <class MatrixType>
template <class BlockVectorType,
class VectorType>
void
BlockMatrixBase<MatrixType>::
vmult_nonblock_block (VectorType &dst,
const BlockVectorType &src) const
{
Assert (n_block_rows() == 1,
ExcDimensionMismatch(1, n_block_rows()));
Assert (src.n_blocks() == n_block_cols(),
ExcDimensionMismatch(src.n_blocks(), n_block_cols()));
block(0,0).vmult (dst, src.block(0));
for (size_type col=1; col<n_block_cols(); ++col)
block(0,col).vmult_add (dst, src.block(col));
}
template <class MatrixType>
template <class BlockVectorType,
class VectorType>
void
BlockMatrixBase<MatrixType>::
vmult_block_nonblock (BlockVectorType &dst,
const VectorType &src) const
{
Assert (dst.n_blocks() == n_block_rows(),
ExcDimensionMismatch(dst.n_blocks(), n_block_rows()));
Assert (1 == n_block_cols(),
ExcDimensionMismatch(1, n_block_cols()));
for (size_type row=0; row<n_block_rows(); ++row)
block(row,0).vmult (dst.block(row),
src);
}
template <class MatrixType>
template <class VectorType>
void
BlockMatrixBase<MatrixType>::
vmult_nonblock_nonblock (VectorType &dst,
const VectorType &src) const
{
Assert (1 == n_block_rows(),
ExcDimensionMismatch(1, n_block_rows()));
Assert (1 == n_block_cols(),
ExcDimensionMismatch(1, n_block_cols()));
block(0,0).vmult (dst, src);
}
template <class MatrixType>
template <class BlockVectorType>
void
BlockMatrixBase<MatrixType>::vmult_add (BlockVectorType &dst,
const BlockVectorType &src) const
{
Assert (dst.n_blocks() == n_block_rows(),
ExcDimensionMismatch(dst.n_blocks(), n_block_rows()));
Assert (src.n_blocks() == n_block_cols(),
ExcDimensionMismatch(src.n_blocks(), n_block_cols()));
for (unsigned int row=0; row<n_block_rows(); ++row)
for (unsigned int col=0; col<n_block_cols(); ++col)
block(row,col).vmult_add (dst.block(row),
src.block(col));
}
template <class MatrixType>
template <class BlockVectorType>
void
BlockMatrixBase<MatrixType>::
Tvmult_block_block (BlockVectorType &dst,
const BlockVectorType &src) const
{
Assert (dst.n_blocks() == n_block_cols(),
ExcDimensionMismatch(dst.n_blocks(), n_block_cols()));
Assert (src.n_blocks() == n_block_rows(),
ExcDimensionMismatch(src.n_blocks(), n_block_rows()));
dst = 0.;
for (unsigned int row=0; row<n_block_rows(); ++row)
{
for (unsigned int col=0; col<n_block_cols(); ++col)
block(row,col).Tvmult_add (dst.block(col),
src.block(row));
};
}
template <class MatrixType>
template <class BlockVectorType,
class VectorType>
void
BlockMatrixBase<MatrixType>::
Tvmult_block_nonblock (BlockVectorType &dst,
const VectorType &src) const
{
Assert (dst.n_blocks() == n_block_cols(),
ExcDimensionMismatch(dst.n_blocks(), n_block_cols()));
Assert (1 == n_block_rows(),
ExcDimensionMismatch(1, n_block_rows()));
dst = 0.;
for (unsigned int col=0; col<n_block_cols(); ++col)
block(0,col).Tvmult_add (dst.block(col), src);
}
template <class MatrixType>
template <class BlockVectorType,
class VectorType>
void
BlockMatrixBase<MatrixType>::
Tvmult_nonblock_block (VectorType &dst,
const BlockVectorType &src) const
{
Assert (1 == n_block_cols(),
ExcDimensionMismatch(1, n_block_cols()));
Assert (src.n_blocks() == n_block_rows(),
ExcDimensionMismatch(src.n_blocks(), n_block_rows()));
block(0,0).Tvmult (dst, src.block(0));
for (size_type row=1; row<n_block_rows(); ++row)
block(row,0).Tvmult_add (dst, src.block(row));
}
template <class MatrixType>
template <class VectorType>
void
BlockMatrixBase<MatrixType>::
Tvmult_nonblock_nonblock (VectorType &dst,
const VectorType &src) const
{
Assert (1 == n_block_cols(),
ExcDimensionMismatch(1, n_block_cols()));
Assert (1 == n_block_rows(),
ExcDimensionMismatch(1, n_block_rows()));
block(0,0).Tvmult (dst, src);
}
template <class MatrixType>
template <class BlockVectorType>
void
BlockMatrixBase<MatrixType>::Tvmult_add (BlockVectorType &dst,
const BlockVectorType &src) const
{
Assert (dst.n_blocks() == n_block_cols(),
ExcDimensionMismatch(dst.n_blocks(), n_block_cols()));
Assert (src.n_blocks() == n_block_rows(),
ExcDimensionMismatch(src.n_blocks(), n_block_rows()));
for (unsigned int row=0; row<n_block_rows(); ++row)
for (unsigned int col=0; col<n_block_cols(); ++col)
block(row,col).Tvmult_add (dst.block(col),
src.block(row));
}
template <class MatrixType>
template <class BlockVectorType>
typename BlockMatrixBase<MatrixType>::value_type
BlockMatrixBase<MatrixType>::matrix_norm_square (const BlockVectorType &v) const
{
Assert (n_block_rows() == n_block_cols(), ExcNotQuadratic());
Assert (v.n_blocks() == n_block_rows(),
ExcDimensionMismatch(v.n_blocks(), n_block_rows()));
value_type norm_sqr = 0;
for (unsigned int row=0; row<n_block_rows(); ++row)
for (unsigned int col=0; col<n_block_cols(); ++col)
if (row==col)
norm_sqr += block(row,col).matrix_norm_square (v.block(row));
else
norm_sqr += block(row,col).matrix_scalar_product (v.block(row),
v.block(col));
return norm_sqr;
}
template <class MatrixType>
template <class BlockVectorType>
typename BlockMatrixBase<MatrixType>::value_type
BlockMatrixBase<MatrixType>::
matrix_scalar_product (const BlockVectorType &u,
const BlockVectorType &v) const
{
Assert (u.n_blocks() == n_block_rows(),
ExcDimensionMismatch(u.n_blocks(), n_block_rows()));
Assert (v.n_blocks() == n_block_cols(),
ExcDimensionMismatch(v.n_blocks(), n_block_cols()));
value_type result = 0;
for (unsigned int row=0; row<n_block_rows(); ++row)
for (unsigned int col=0; col<n_block_cols(); ++col)
result += block(row,col).matrix_scalar_product (u.block(row),
v.block(col));
return result;
}
template <class MatrixType>
template <class BlockVectorType>
typename BlockMatrixBase<MatrixType>::value_type
BlockMatrixBase<MatrixType>::
residual (BlockVectorType &dst,
const BlockVectorType &x,
const BlockVectorType &b) const
{
Assert (dst.n_blocks() == n_block_rows(),
ExcDimensionMismatch(dst.n_blocks(), n_block_rows()));
Assert (b.n_blocks() == n_block_rows(),
ExcDimensionMismatch(b.n_blocks(), n_block_rows()));
Assert (x.n_blocks() == n_block_cols(),
ExcDimensionMismatch(x.n_blocks(), n_block_cols()));
// in block notation, the residual is
// r_i = b_i - \sum_j A_ij x_j.
// this can be written as
// r_i = b_i - A_i0 x_0 - \sum_{j>0} A_ij x_j.
//
// for the first two terms, we can
// call the residual function of
// A_i0. for the other terms, we
// use vmult_add. however, we want
// to subtract, so in order to
// avoid a temporary vector, we
// perform a sign change of the
// first two term before, and after
// adding up
for (unsigned int row=0; row<n_block_rows(); ++row)
{
block(row,0).residual (dst.block(row),
x.block(0),
b.block(row));
for (size_type i=0; i<dst.block(row).size(); ++i)
dst.block(row)(i) = -dst.block(row)(i);
for (unsigned int col=1; col<n_block_cols(); ++col)
block(row,col).vmult_add (dst.block(row),
x.block(col));
for (size_type i=0; i<dst.block(row).size(); ++i)
dst.block(row)(i) = -dst.block(row)(i);
};
value_type res = 0;
for (size_type row=0; row<n_block_rows(); ++row)
res += dst.block(row).norm_sqr ();
return std::sqrt(res);
}
template <class MatrixType>
inline
void
BlockMatrixBase<MatrixType>::print (std::ostream &out,
const bool alternative_output) const
{
for (unsigned int row=0; row<n_block_rows(); ++row)
for (unsigned int col=0; col<n_block_cols(); ++col)
{
if (!alternative_output)
out << "Block (" << row << ", " << col << ")" << std::endl;
block(row, col).print(out, alternative_output);
}
}
template <class MatrixType>
inline
typename BlockMatrixBase<MatrixType>::const_iterator
BlockMatrixBase<MatrixType>::begin () const
{
return const_iterator(this, 0);
}
template <class MatrixType>
inline
typename BlockMatrixBase<MatrixType>::const_iterator
BlockMatrixBase<MatrixType>::end () const
{
return const_iterator(this, m());
}
template <class MatrixType>
inline
typename BlockMatrixBase<MatrixType>::const_iterator
BlockMatrixBase<MatrixType>::begin (const size_type r) const
{
Assert (r<m(), ExcIndexRange(r,0,m()));
return const_iterator(this, r);
}
template <class MatrixType>
inline
typename BlockMatrixBase<MatrixType>::const_iterator
BlockMatrixBase<MatrixType>::end (const size_type r) const
{
Assert (r<m(), ExcIndexRange(r,0,m()));
return const_iterator(this, r+1);
}
template <class MatrixType>
inline
typename BlockMatrixBase<MatrixType>::iterator
BlockMatrixBase<MatrixType>::begin ()
{
return iterator(this, 0);
}
template <class MatrixType>
inline
typename BlockMatrixBase<MatrixType>::iterator
BlockMatrixBase<MatrixType>::end ()
{
return iterator(this, m());
}
template <class MatrixType>
inline
typename BlockMatrixBase<MatrixType>::iterator
BlockMatrixBase<MatrixType>::begin (const size_type r)
{
Assert (r<m(), ExcIndexRange(r,0,m()));
return iterator(this, r);
}
template <class MatrixType>
inline
typename BlockMatrixBase<MatrixType>::iterator
BlockMatrixBase<MatrixType>::end (const size_type r)
{
Assert (r<m(), ExcIndexRange(r,0,m()));
return iterator(this, r+1);
}
template <class MatrixType>
void
BlockMatrixBase<MatrixType>::collect_sizes ()
{
std::vector<size_type> row_sizes (this->n_block_rows());
std::vector<size_type> col_sizes (this->n_block_cols());
// first find out the row sizes
// from the first block column
for (unsigned int r=0; r<this->n_block_rows(); ++r)
row_sizes[r] = sub_objects[r][0]->m();
// then check that the following
// block columns have the same
// sizes
for (unsigned int c=1; c<this->n_block_cols(); ++c)
for (unsigned int r=0; r<this->n_block_rows(); ++r)
Assert (row_sizes[r] == sub_objects[r][c]->m(),
ExcIncompatibleRowNumbers (r,0,r,c));
// finally initialize the row
// indices with this array
this->row_block_indices.reinit (row_sizes);
// then do the same with the columns
for (unsigned int c=0; c<this->n_block_cols(); ++c)
col_sizes[c] = sub_objects[0][c]->n();
for (unsigned int r=1; r<this->n_block_rows(); ++r)
for (unsigned int c=0; c<this->n_block_cols(); ++c)
Assert (col_sizes[c] == sub_objects[r][c]->n(),
ExcIncompatibleRowNumbers (0,c,r,c));
// finally initialize the row
// indices with this array
this->column_block_indices.reinit (col_sizes);
}
template <class MatrixType>
void
BlockMatrixBase<MatrixType>::prepare_add_operation ()
{
for (unsigned int row=0; row<n_block_rows(); ++row)
for (unsigned int col=0; col<n_block_cols(); ++col)
block(row, col).prepare_add();
}
template <class MatrixType>
void
BlockMatrixBase<MatrixType>::prepare_set_operation ()
{
for (unsigned int row=0; row<n_block_rows(); ++row)
for (unsigned int col=0; col<n_block_cols(); ++col)
block(row, col).prepare_set();
}
#endif // DOXYGEN
DEAL_II_NAMESPACE_CLOSE
#endif // dealii__block_matrix_base_h
|