/usr/include/deal.II/lac/chunk_sparse_matrix.templates.h is in libdeal.ii-dev 6.3.1-1.1.
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 | //---------------------------------------------------------------------------
// $Id: chunk_sparse_matrix.templates.h 18764 2009-04-28 13:34:47Z bangerth $
// Version: $Name$
//
// Copyright (C) 2008, 2009 by the deal.II authors
//
// This file is subject to QPL and may not be distributed
// without copyright and license information. Please refer
// to the file deal.II/doc/license.html for the text and
// further information on this license.
//
//---------------------------------------------------------------------------
#ifndef __deal2__chunk_sparse_matrix_templates_h
#define __deal2__chunk_sparse_matrix_templates_h
#include <base/template_constraints.h>
#include <lac/chunk_sparse_matrix.h>
#include <lac/vector.h>
#include <lac/full_matrix.h>
// we only need output streams, but older compilers did not provide
// them in a separate include file
#ifdef HAVE_STD_OSTREAM_HEADER
# include <ostream>
#else
# include <iostream>
#endif
#include <iomanip>
#include <algorithm>
#include <functional>
#include <cmath>
#include <vector>
#include <numeric>
#include <base/thread_management.h>
#include <base/multithread_info.h>
DEAL_II_NAMESPACE_OPEN
namespace internal
{
//TODO: the goal of the ChunkSparseMatrix class is to stream data and use
// the vectorization features of modern processors. to make this happen,
// we will have to vectorize the functions in the following namespace, either
// by hand or by using, for example, optimized BLAS versions for them.
namespace ChunkSparseMatrix
{
/**
* Add the result of multiplying a chunk
* of size chunk_size times chunk_size by
* a source vector fragment of size
* chunk_size to the destination vector
* fragment.
*/
template <typename MatrixIterator,
typename SrcIterator,
typename DstIterator>
inline
void
chunk_vmult_add (const unsigned int chunk_size,
const MatrixIterator matrix,
const SrcIterator src,
DstIterator dst)
{
MatrixIterator matrix_row = matrix;
for (unsigned int i=0; i<chunk_size;
++i, matrix_row += chunk_size)
{
typename std::iterator_traits<DstIterator>::value_type
sum = 0;
for (unsigned int j=0; j<chunk_size; ++j)
sum += matrix_row[j] * src[j];
dst[i] += sum;
}
}
/**
* Like the previous function, but
* subtract. We need this for computing
* the residual.
*/
template <typename MatrixIterator,
typename SrcIterator,
typename DstIterator>
inline
void
chunk_vmult_subtract (const unsigned int chunk_size,
const MatrixIterator matrix,
const SrcIterator src,
DstIterator dst)
{
MatrixIterator matrix_row = matrix;
for (unsigned int i=0; i<chunk_size;
++i, matrix_row += chunk_size)
{
typename std::iterator_traits<DstIterator>::value_type
sum = 0;
for (unsigned int j=0; j<chunk_size; ++j)
sum += matrix_row[j] * src[j];
dst[i] -= sum;
}
}
/**
* Add the result of multiplying the
* transpose of a chunk of size
* chunk_size times chunk_size by a
* source vector fragment of size
* chunk_size to the destination vector
* fragment.
*/
template <typename MatrixIterator,
typename SrcIterator,
typename DstIterator>
inline
void
chunk_Tvmult_add (const unsigned int chunk_size,
const MatrixIterator matrix,
const SrcIterator src,
DstIterator dst)
{
for (unsigned int i=0; i<chunk_size; ++i)
{
typename std::iterator_traits<DstIterator>::value_type
sum = 0;
for (unsigned int j=0; j<chunk_size; ++j)
sum += matrix[j*chunk_size+i] * src[j];
dst[i] += sum;
}
}
/**
* Produce the result of the matrix
* scalar product $u^TMv$ for an
* individual chunk.
*/
template <typename result_type,
typename MatrixIterator,
typename SrcIterator1,
typename SrcIterator2>
inline
result_type
chunk_matrix_scalar_product (const unsigned int chunk_size,
const MatrixIterator matrix,
const SrcIterator1 u,
const SrcIterator2 v)
{
result_type result = 0;
MatrixIterator matrix_row = matrix;
for (unsigned int i=0; i<chunk_size;
++i, matrix_row += chunk_size)
{
typename std::iterator_traits<SrcIterator2>::value_type
sum = 0;
for (unsigned int j=0; j<chunk_size; ++j)
sum += matrix_row[j] * v[j];
result += u[i] * sum;
}
return result;
}
}
}
template <typename number>
ChunkSparseMatrix<number>::ChunkSparseMatrix ()
:
cols(0, "ChunkSparseMatrix"),
val(0),
max_len(0)
{}
template <typename number>
ChunkSparseMatrix<number>::ChunkSparseMatrix (const ChunkSparseMatrix &m)
:
Subscriptor (m),
cols(0, "ChunkSparseMatrix"),
val(0),
max_len(0)
{
Assert (m.cols==0, ExcInvalidConstructorCall());
Assert (m.val==0, ExcInvalidConstructorCall());
Assert (m.max_len==0, ExcInvalidConstructorCall());
}
template <typename number>
ChunkSparseMatrix<number>&
ChunkSparseMatrix<number>::operator = (const ChunkSparseMatrix<number> &m)
{
Assert (m.cols==0, ExcInvalidConstructorCall());
Assert (m.val==0, ExcInvalidConstructorCall());
Assert (m.max_len==0, ExcInvalidConstructorCall());
return *this;
}
template <typename number>
ChunkSparseMatrix<number>::ChunkSparseMatrix (const ChunkSparsityPattern &c)
:
cols(0, "ChunkSparseMatrix"),
val(0),
max_len(0)
{
reinit (c);
}
template <typename number>
ChunkSparseMatrix<number>::ChunkSparseMatrix (const ChunkSparsityPattern &c,
const IdentityMatrix &id)
:
cols(0, "ChunkSparseMatrix"),
val(0),
max_len(0)
{
Assert (c.n_rows() == id.m(), ExcDimensionMismatch (c.n_rows(), id.m()));
Assert (c.n_cols() == id.n(), ExcDimensionMismatch (c.n_cols(), id.n()));
reinit (c);
for (unsigned int i=0; i<n(); ++i)
this->set(i,i,1.);
}
template <typename number>
ChunkSparseMatrix<number>::~ChunkSparseMatrix ()
{
cols = 0;
if (val != 0)
delete[] val;
}
template <typename number>
ChunkSparseMatrix<number> &
ChunkSparseMatrix<number>::operator = (const double d)
{
Assert (d==0, ExcScalarAssignmentOnlyForZeroValue());
Assert (cols != 0, ExcNotInitialized());
Assert (cols->sparsity_pattern.compressed || cols->empty(),
ChunkSparsityPattern::ExcNotCompressed());
if (val)
{
const unsigned int chunk_size = cols->get_chunk_size();
std::fill_n (val,
cols->sparsity_pattern.n_nonzero_elements() *
chunk_size * chunk_size,
0.);
}
return *this;
}
template <typename number>
ChunkSparseMatrix<number> &
ChunkSparseMatrix<number>::operator= (const IdentityMatrix &id)
{
Assert (cols->n_rows() == id.m(),
ExcDimensionMismatch (cols->n_rows(), id.m()));
Assert (cols->n_cols() == id.n(),
ExcDimensionMismatch (cols->n_cols(), id.n()));
*this = 0;
for (unsigned int i=0; i<n(); ++i)
this->set(i,i,1.);
return *this;
}
template <typename number>
void
ChunkSparseMatrix<number>::reinit (const ChunkSparsityPattern &sparsity)
{
cols = &sparsity;
if (cols->empty())
{
if (val != 0)
delete[] val;
val = 0;
max_len = 0;
return;
}
// allocate not just m() * n() elements but
// enough so that we can store full
// chunks. this entails some padding
// elements
const unsigned int chunk_size = cols->get_chunk_size();
const unsigned int N = cols->sparsity_pattern.n_nonzero_elements() *
chunk_size * chunk_size;
if (N > max_len || max_len == 0)
{
if (val != 0)
delete[] val;
val = new number[N];
max_len = N;
}
// fill with zeros. do not just fill N
// elements but all that we allocated to
// ensure that also the padding elements
// are zero and not left at previous values
if (val != 0)
std::fill_n (&val[0], max_len, 0);
}
template <typename number>
void
ChunkSparseMatrix<number>::clear ()
{
cols = 0;
if (val) delete[] val;
val = 0;
max_len = 0;
}
template <typename number>
bool
ChunkSparseMatrix<number>::empty () const
{
if (cols == 0)
return true;
else
return cols->empty();
}
template <typename number>
unsigned int
ChunkSparseMatrix<number>::n_nonzero_elements () const
{
Assert (cols != 0, ExcNotInitialized());
return cols->n_nonzero_elements ();
}
template <typename number>
unsigned int
ChunkSparseMatrix<number>::n_actually_nonzero_elements () const
{
Assert (cols != 0, ExcNotInitialized());
// count those elements that are nonzero,
// even if they lie in the padding around
// the matrix. since we have the invariant
// that padding elements are zero, nothing
// bad can happen here
const unsigned int chunk_size = cols->get_chunk_size();
return std::count_if(&val[0],
&val[cols->sparsity_pattern.n_nonzero_elements () *
chunk_size * chunk_size],
std::bind2nd(std::not_equal_to<double>(), 0));
}
template <typename number>
void
ChunkSparseMatrix<number>::symmetrize ()
{
Assert (cols != 0, ExcNotInitialized());
Assert (cols->rows == cols->cols, ExcNotQuadratic());
Assert (false, ExcNotImplemented());
}
template <typename number>
template <typename somenumber>
ChunkSparseMatrix<number> &
ChunkSparseMatrix<number>::copy_from (const ChunkSparseMatrix<somenumber> &matrix)
{
Assert (cols != 0, ExcNotInitialized());
Assert (val != 0, ExcNotInitialized());
Assert (cols == matrix.cols, ExcDifferentChunkSparsityPatterns());
// copy everything, including padding
// elements
const unsigned int chunk_size = cols->get_chunk_size();
std::copy (&matrix.val[0],
&matrix.val[cols->sparsity_pattern.n_nonzero_elements()
* chunk_size * chunk_size],
&val[0]);
return *this;
}
template <typename number>
template <typename somenumber>
void
ChunkSparseMatrix<number>::copy_from (const FullMatrix<somenumber> &matrix)
{
// first delete previous content
*this = 0;
// then copy old matrix
for (unsigned int row=0; row<matrix.m(); ++row)
for (unsigned int col=0; col<matrix.n(); ++col)
if (matrix(row,col) != 0)
set (row, col, matrix(row,col));
}
template <typename number>
template <typename somenumber>
void
ChunkSparseMatrix<number>::add (const number factor,
const ChunkSparseMatrix<somenumber> &matrix)
{
Assert (cols != 0, ExcNotInitialized());
Assert (val != 0, ExcNotInitialized());
Assert (cols == matrix.cols, ExcDifferentChunkSparsityPatterns());
// add everything, including padding
// elements
const unsigned int chunk_size = cols->get_chunk_size();
number *val_ptr = &val[0];
const somenumber *matrix_ptr = &matrix.val[0];
const number *const end_ptr = &val[cols->sparsity_pattern.n_nonzero_elements()
* chunk_size * chunk_size];
while (val_ptr != end_ptr)
*val_ptr++ += factor * *matrix_ptr++;
}
template <typename number>
template <class OutVector, class InVector>
void
ChunkSparseMatrix<number>::vmult (OutVector& dst,
const InVector& src) const
{
Assert (cols != 0, ExcNotInitialized());
Assert (val != 0, ExcNotInitialized());
Assert(m() == dst.size(), ExcDimensionMismatch(m(),dst.size()));
Assert(n() == src.size(), ExcDimensionMismatch(n(),src.size()));
Assert (!PointerComparison::equal(&src, &dst), ExcSourceEqualsDestination());
// set the output vector to zero and then
// add to it the contributions of vmults
// from individual chunks. this is what
// vmult_add does
dst = 0;
vmult_add (dst, src);
}
template <typename number>
template <class OutVector, class InVector>
void
ChunkSparseMatrix<number>::Tvmult (OutVector& dst,
const InVector& src) const
{
Assert (val != 0, ExcNotInitialized());
Assert (cols != 0, ExcNotInitialized());
Assert(n() == dst.size(), ExcDimensionMismatch(n(),dst.size()));
Assert(m() == src.size(), ExcDimensionMismatch(m(),src.size()));
Assert (!PointerComparison::equal(&src, &dst), ExcSourceEqualsDestination());
Assert (cols != 0, ExcNotInitialized());
Assert (val != 0, ExcNotInitialized());
Assert(m() == dst.size(), ExcDimensionMismatch(m(),dst.size()));
Assert(n() == src.size(), ExcDimensionMismatch(n(),src.size()));
Assert (!PointerComparison::equal(&src, &dst), ExcSourceEqualsDestination());
// set the output vector to zero and then
// add to it the contributions of vmults
// from individual chunks. this is what
// vmult_add does
dst = 0;
Tvmult_add (dst, src);
}
template <typename number>
template <class OutVector, class InVector>
void
ChunkSparseMatrix<number>::vmult_add (OutVector& dst,
const InVector& src) const
{
Assert (cols != 0, ExcNotInitialized());
Assert (val != 0, ExcNotInitialized());
Assert(m() == dst.size(), ExcDimensionMismatch(m(),dst.size()));
Assert(n() == src.size(), ExcDimensionMismatch(n(),src.size()));
Assert (!PointerComparison::equal(&src, &dst), ExcSourceEqualsDestination());
const unsigned int n_chunk_rows = cols->sparsity_pattern.n_rows();
// loop over all chunks. note that we need
// to treat the last chunk row and column
// differently if they have padding
// elements
const bool rows_have_padding = (m() % cols->chunk_size != 0),
cols_have_padding = (n() % cols->chunk_size != 0);
const unsigned int n_regular_chunk_rows
= (rows_have_padding ?
n_chunk_rows-1 :
n_chunk_rows);
const number *val_ptr = val;
const unsigned int *colnum_ptr = cols->sparsity_pattern.colnums;
typename OutVector::iterator dst_ptr = dst.begin();
for (unsigned int chunk_row=0; chunk_row<n_regular_chunk_rows; ++chunk_row)
{
const number *const val_end_of_row = &val[cols->sparsity_pattern.rowstart[chunk_row+1]
* cols->chunk_size
* cols->chunk_size];
while (val_ptr != val_end_of_row)
{
if ((cols_have_padding == false)
||
(*colnum_ptr != cols->sparsity_pattern.n_cols()-1))
internal::ChunkSparseMatrix::chunk_vmult_add
(cols->chunk_size,
val_ptr,
src.begin() + *colnum_ptr * cols->chunk_size,
dst_ptr);
else
// we're at a chunk column that
// has padding
for (unsigned int r=0; r<cols->chunk_size; ++r)
for (unsigned int c=0; c<n() % cols->chunk_size; ++c)
dst(chunk_row * cols->chunk_size + r)
+= (val_ptr[r*cols->chunk_size + c] *
src(*colnum_ptr * cols->chunk_size + c));
++colnum_ptr;
val_ptr += cols->chunk_size * cols->chunk_size;
}
dst_ptr += cols->chunk_size;
}
// now deal with last chunk row if
// necessary
if (rows_have_padding)
{
const unsigned int chunk_row = n_chunk_rows - 1;
const number *const val_end_of_row = &val[cols->sparsity_pattern.rowstart[chunk_row+1]
* cols->chunk_size
* cols->chunk_size];
while (val_ptr != val_end_of_row)
{
if ((cols_have_padding == false)
||
(*colnum_ptr != cols->sparsity_pattern.n_cols()-1))
{
// we're at a chunk row but not
// column that has padding
for (unsigned int r=0; r<m() % cols->chunk_size; ++r)
for (unsigned int c=0; c<cols->chunk_size; ++c)
dst(chunk_row * cols->chunk_size + r)
+= (val_ptr[r*cols->chunk_size + c] *
src(*colnum_ptr * cols->chunk_size + c));
}
else
// we're at a chunk row and
// column that has padding
for (unsigned int r=0; r<m() % cols->chunk_size; ++r)
for (unsigned int c=0; c<n() % cols->chunk_size; ++c)
dst(chunk_row * cols->chunk_size + r)
+= (val_ptr[r*cols->chunk_size + c] *
src(*colnum_ptr * cols->chunk_size + c));
++colnum_ptr;
val_ptr += cols->chunk_size * cols->chunk_size;
}
}
}
template <typename number>
template <class OutVector, class InVector>
void
ChunkSparseMatrix<number>::Tvmult_add (OutVector& dst,
const InVector& src) const
{
Assert (cols != 0, ExcNotInitialized());
Assert (val != 0, ExcNotInitialized());
Assert(m() == dst.size(), ExcDimensionMismatch(m(),dst.size()));
Assert(n() == src.size(), ExcDimensionMismatch(n(),src.size()));
Assert (!PointerComparison::equal(&src, &dst), ExcSourceEqualsDestination());
const unsigned int n_chunk_rows = cols->sparsity_pattern.n_rows();
// loop over all chunks. note that we need
// to treat the last chunk row and column
// differently if they have padding
// elements
const bool rows_have_padding = (m() % cols->chunk_size != 0),
cols_have_padding = (n() % cols->chunk_size != 0);
const unsigned int n_regular_chunk_rows
= (rows_have_padding ?
n_chunk_rows-1 :
n_chunk_rows);
// like in vmult_add, but don't keep an
// iterator into dst around since we're not
// traversing it sequentially this time
const number *val_ptr = val;
const unsigned int *colnum_ptr = cols->sparsity_pattern.colnums;
for (unsigned int chunk_row=0; chunk_row<n_regular_chunk_rows; ++chunk_row)
{
const number *const val_end_of_row = &val[cols->sparsity_pattern.rowstart[chunk_row+1]
* cols->chunk_size
* cols->chunk_size];
while (val_ptr != val_end_of_row)
{
if ((cols_have_padding == false)
||
(*colnum_ptr != cols->sparsity_pattern.n_cols()-1))
internal::ChunkSparseMatrix::chunk_Tvmult_add
(cols->chunk_size,
val_ptr,
src.begin() + chunk_row * cols->chunk_size,
dst.begin() + *colnum_ptr * cols->chunk_size);
else
// we're at a chunk column that
// has padding
for (unsigned int r=0; r<cols->chunk_size; ++r)
for (unsigned int c=0; c<n() % cols->chunk_size; ++c)
dst(*colnum_ptr * cols->chunk_size + c)
+= (val_ptr[r*cols->chunk_size + c] *
src(chunk_row * cols->chunk_size + r));
++colnum_ptr;
val_ptr += cols->chunk_size * cols->chunk_size;
}
}
// now deal with last chunk row if
// necessary
if (rows_have_padding)
{
const unsigned int chunk_row = n_chunk_rows - 1;
const number *const val_end_of_row = &val[cols->sparsity_pattern.rowstart[chunk_row+1]
* cols->chunk_size
* cols->chunk_size];
while (val_ptr != val_end_of_row)
{
if ((cols_have_padding == false)
||
(*colnum_ptr != cols->sparsity_pattern.n_cols()-1))
{
// we're at a chunk row but not
// column that has padding
for (unsigned int r=0; r<m() % cols->chunk_size; ++r)
for (unsigned int c=0; c<cols->chunk_size; ++c)
dst(*colnum_ptr * cols->chunk_size + c)
+= (val_ptr[r*cols->chunk_size + c] *
src(chunk_row * cols->chunk_size + r));
}
else
// we're at a chunk row and
// column that has padding
for (unsigned int r=0; r<m() % cols->chunk_size; ++r)
for (unsigned int c=0; c<n() % cols->chunk_size; ++c)
dst(*colnum_ptr * cols->chunk_size + c)
+= (val_ptr[r*cols->chunk_size + c] *
src(chunk_row * cols->chunk_size + r));
++colnum_ptr;
val_ptr += cols->chunk_size * cols->chunk_size;
}
}
}
template <typename number>
template <typename somenumber>
somenumber
ChunkSparseMatrix<number>::matrix_norm_square (const Vector<somenumber>& v) const
{
Assert (cols != 0, ExcNotInitialized());
Assert (val != 0, ExcNotInitialized());
Assert(m() == v.size(), ExcDimensionMismatch(m(),v.size()));
Assert(n() == v.size(), ExcDimensionMismatch(n(),v.size()));
somenumber result = 0;
////////////////
// like matrix_scalar_product, except that
// the two vectors are now the same
const unsigned int n_chunk_rows = cols->sparsity_pattern.n_rows();
// loop over all chunks. note that we need
// to treat the last chunk row and column
// differently if they have padding
// elements
const bool rows_have_padding = (m() % cols->chunk_size != 0),
cols_have_padding = (n() % cols->chunk_size != 0);
const unsigned int n_regular_chunk_rows
= (rows_have_padding ?
n_chunk_rows-1 :
n_chunk_rows);
const number *val_ptr = val;
const unsigned int *colnum_ptr = cols->sparsity_pattern.colnums;
typename Vector<somenumber>::const_iterator v_ptr = v.begin();
for (unsigned int chunk_row=0; chunk_row<n_regular_chunk_rows; ++chunk_row)
{
const number *const val_end_of_row = &val[cols->sparsity_pattern.rowstart[chunk_row+1]
* cols->chunk_size
* cols->chunk_size];
while (val_ptr != val_end_of_row)
{
if ((cols_have_padding == false)
||
(*colnum_ptr != cols->sparsity_pattern.n_cols()-1))
result +=
internal::ChunkSparseMatrix::
chunk_matrix_scalar_product<somenumber>
(cols->chunk_size,
val_ptr,
v_ptr,
v.begin() + *colnum_ptr * cols->chunk_size);
else
// we're at a chunk column that
// has padding
for (unsigned int r=0; r<cols->chunk_size; ++r)
for (unsigned int c=0; c<n() % cols->chunk_size; ++c)
result
+=
v(chunk_row * cols->chunk_size + r)
* (val_ptr[r*cols->chunk_size + c] *
v(*colnum_ptr * cols->chunk_size + c));
++colnum_ptr;
val_ptr += cols->chunk_size * cols->chunk_size;
}
v_ptr += cols->chunk_size;
}
// now deal with last chunk row if
// necessary
if (rows_have_padding)
{
const unsigned int chunk_row = n_chunk_rows - 1;
const number *const val_end_of_row = &val[cols->sparsity_pattern.rowstart[chunk_row+1]
* cols->chunk_size
* cols->chunk_size];
while (val_ptr != val_end_of_row)
{
if ((cols_have_padding == false)
||
(*colnum_ptr != cols->sparsity_pattern.n_cols()-1))
{
// we're at a chunk row but not
// column that has padding
for (unsigned int r=0; r<m() % cols->chunk_size; ++r)
for (unsigned int c=0; c<cols->chunk_size; ++c)
result
+=
v(chunk_row * cols->chunk_size + r)
* (val_ptr[r*cols->chunk_size + c] *
v(*colnum_ptr * cols->chunk_size + c));
}
else
// we're at a chunk row and
// column that has padding
for (unsigned int r=0; r<m() % cols->chunk_size; ++r)
for (unsigned int c=0; c<n() % cols->chunk_size; ++c)
result
+=
v(chunk_row * cols->chunk_size + r)
* (val_ptr[r*cols->chunk_size + c] *
v(*colnum_ptr * cols->chunk_size + c));
++colnum_ptr;
val_ptr += cols->chunk_size * cols->chunk_size;
}
}
return result;
}
template <typename number>
template <typename somenumber>
somenumber
ChunkSparseMatrix<number>::matrix_scalar_product (const Vector<somenumber>& u,
const Vector<somenumber>& v) const
{
Assert (cols != 0, ExcNotInitialized());
Assert (val != 0, ExcNotInitialized());
Assert(m() == u.size(), ExcDimensionMismatch(m(),u.size()));
Assert(n() == v.size(), ExcDimensionMismatch(n(),v.size()));
// the following works like the vmult_add
// function
somenumber result = 0;
const unsigned int n_chunk_rows = cols->sparsity_pattern.n_rows();
// loop over all chunks. note that we need
// to treat the last chunk row and column
// differently if they have padding
// elements
const bool rows_have_padding = (m() % cols->chunk_size != 0),
cols_have_padding = (n() % cols->chunk_size != 0);
const unsigned int n_regular_chunk_rows
= (rows_have_padding ?
n_chunk_rows-1 :
n_chunk_rows);
const number *val_ptr = val;
const unsigned int *colnum_ptr = cols->sparsity_pattern.colnums;
typename Vector<somenumber>::const_iterator u_ptr = u.begin();
for (unsigned int chunk_row=0; chunk_row<n_regular_chunk_rows; ++chunk_row)
{
const number *const val_end_of_row = &val[cols->sparsity_pattern.rowstart[chunk_row+1]
* cols->chunk_size
* cols->chunk_size];
while (val_ptr != val_end_of_row)
{
if ((cols_have_padding == false)
||
(*colnum_ptr != cols->sparsity_pattern.n_cols()-1))
result +=
internal::ChunkSparseMatrix::
chunk_matrix_scalar_product<somenumber>
(cols->chunk_size,
val_ptr,
u_ptr,
v.begin() + *colnum_ptr * cols->chunk_size);
else
// we're at a chunk column that
// has padding
for (unsigned int r=0; r<cols->chunk_size; ++r)
for (unsigned int c=0; c<n() % cols->chunk_size; ++c)
result
+=
u(chunk_row * cols->chunk_size + r)
* (val_ptr[r*cols->chunk_size + c] *
v(*colnum_ptr * cols->chunk_size + c));
++colnum_ptr;
val_ptr += cols->chunk_size * cols->chunk_size;
}
u_ptr += cols->chunk_size;
}
// now deal with last chunk row if
// necessary
if (rows_have_padding)
{
const unsigned int chunk_row = n_chunk_rows - 1;
const number *const val_end_of_row = &val[cols->sparsity_pattern.rowstart[chunk_row+1]
* cols->chunk_size
* cols->chunk_size];
while (val_ptr != val_end_of_row)
{
if ((cols_have_padding == false)
||
(*colnum_ptr != cols->sparsity_pattern.n_cols()-1))
{
// we're at a chunk row but not
// column that has padding
for (unsigned int r=0; r<m() % cols->chunk_size; ++r)
for (unsigned int c=0; c<cols->chunk_size; ++c)
result
+=
u(chunk_row * cols->chunk_size + r)
* (val_ptr[r*cols->chunk_size + c] *
v(*colnum_ptr * cols->chunk_size + c));
}
else
// we're at a chunk row and
// column that has padding
for (unsigned int r=0; r<m() % cols->chunk_size; ++r)
for (unsigned int c=0; c<n() % cols->chunk_size; ++c)
result
+=
u(chunk_row * cols->chunk_size + r)
* (val_ptr[r*cols->chunk_size + c] *
v(*colnum_ptr * cols->chunk_size + c));
++colnum_ptr;
val_ptr += cols->chunk_size * cols->chunk_size;
}
}
return result;
}
template <typename number>
typename ChunkSparseMatrix<number>::real_type
ChunkSparseMatrix<number>::l1_norm () const
{
Assert (cols != 0, ExcNotInitialized());
Assert (val != 0, ExcNotInitialized());
const unsigned int n_chunk_rows = cols->sparsity_pattern.n_rows();
// loop over all rows and columns; it is
// safe to also loop over the padding
// elements (they are zero) if we make sure
// that the vector into which we sum column
// sums is large enough
Vector<real_type> column_sums(cols->sparsity_pattern.n_cols() *
cols->chunk_size);
for (unsigned int chunk_row=0; chunk_row<n_chunk_rows; ++chunk_row)
for (unsigned int j=cols->sparsity_pattern.rowstart[chunk_row];
j<cols->sparsity_pattern.rowstart[chunk_row+1] ; ++j)
for (unsigned int r=0; r<cols->chunk_size; ++r)
for (unsigned int s=0; s<cols->chunk_size; ++s)
column_sums(cols->sparsity_pattern.colnums[j] *
cols->chunk_size + s) +=
numbers::NumberTraits<number>::abs(val[j * cols->chunk_size *
cols->chunk_size +
r * cols->chunk_size +
s]);
return column_sums.linfty_norm();
}
template <typename number>
typename ChunkSparseMatrix<number>::real_type
ChunkSparseMatrix<number>::linfty_norm () const
{
Assert (cols != 0, ExcNotInitialized());
Assert (val != 0, ExcNotInitialized());
// this function works like l1_norm(). it
// can be made more efficient (without
// allocating a temporary vector) as is
// done in the SparseMatrix class but since
// it is rarely called in time critical
// places it is probably not worth it
const unsigned int n_chunk_rows = cols->sparsity_pattern.n_rows();
// loop over all rows and columns; it is
// safe to also loop over the padding
// elements (they are zero) if we make sure
// that the vector into which we sum column
// sums is large enough
Vector<real_type> row_sums(cols->sparsity_pattern.n_rows() *
cols->chunk_size);
for (unsigned int chunk_row=0; chunk_row<n_chunk_rows; ++chunk_row)
for (unsigned int j=cols->sparsity_pattern.rowstart[chunk_row];
j<cols->sparsity_pattern.rowstart[chunk_row+1] ; ++j)
for (unsigned int r=0; r<cols->chunk_size; ++r)
for (unsigned int s=0; s<cols->chunk_size; ++s)
row_sums(chunk_row * cols->chunk_size + r) +=
numbers::NumberTraits<number>::abs(val[j * cols->chunk_size *
cols->chunk_size +
r * cols->chunk_size +
s]);
return row_sums.linfty_norm();
}
template <typename number>
typename ChunkSparseMatrix<number>::real_type
ChunkSparseMatrix<number>::frobenius_norm () const
{
// simply add up all entries in the
// sparsity pattern, without taking any
// reference to rows or columns
//
// padding elements are zero, so we can add
// them up as well
real_type norm_sqr = 0;
for (const number *ptr = &val[0]; ptr != &val[max_len]; ++ptr)
norm_sqr += numbers::NumberTraits<number>::abs_square(*ptr);
return std::sqrt (norm_sqr);
}
template <typename number>
template <typename somenumber>
somenumber
ChunkSparseMatrix<number>::residual (Vector<somenumber> &dst,
const Vector<somenumber> &u,
const Vector<somenumber> &b) const
{
Assert (cols != 0, ExcNotInitialized());
Assert (val != 0, ExcNotInitialized());
Assert(m() == dst.size(), ExcDimensionMismatch(m(),dst.size()));
Assert(m() == b.size(), ExcDimensionMismatch(m(),b.size()));
Assert(n() == u.size(), ExcDimensionMismatch(n(),u.size()));
Assert (&u != &dst, ExcSourceEqualsDestination());
// set dst=b, then subtract the result of
// A*u from it. since the purpose of the
// current class is to promote streaming of
// data rather than more random access
// patterns, breaking things up into two
// loops may be reasonable
dst = b;
/////////
// the rest of this function is like
// vmult_add, except that we subtract
// rather than add A*u
/////////
const unsigned int n_chunk_rows = cols->sparsity_pattern.n_rows();
// loop over all chunks. note that we need
// to treat the last chunk row and column
// differently if they have padding
// elements
const bool rows_have_padding = (m() % cols->chunk_size != 0),
cols_have_padding = (n() % cols->chunk_size != 0);
const unsigned int n_regular_chunk_rows
= (rows_have_padding ?
n_chunk_rows-1 :
n_chunk_rows);
const number *val_ptr = val;
const unsigned int *colnum_ptr = cols->sparsity_pattern.colnums;
typename Vector<somenumber>::iterator dst_ptr = dst.begin();
for (unsigned int chunk_row=0; chunk_row<n_regular_chunk_rows; ++chunk_row)
{
const number *const val_end_of_row = &val[cols->sparsity_pattern.rowstart[chunk_row+1]
* cols->chunk_size
* cols->chunk_size];
while (val_ptr != val_end_of_row)
{
if ((cols_have_padding == false)
||
(*colnum_ptr != cols->sparsity_pattern.n_cols()-1))
internal::ChunkSparseMatrix::chunk_vmult_subtract
(cols->chunk_size,
val_ptr,
u.begin() + *colnum_ptr * cols->chunk_size,
dst_ptr);
else
// we're at a chunk column that
// has padding
for (unsigned int r=0; r<cols->chunk_size; ++r)
for (unsigned int c=0; c<n() % cols->chunk_size; ++c)
dst(chunk_row * cols->chunk_size + r)
-= (val_ptr[r*cols->chunk_size + c] *
u(*colnum_ptr * cols->chunk_size + c));
++colnum_ptr;
val_ptr += cols->chunk_size * cols->chunk_size;
}
dst_ptr += cols->chunk_size;
}
// now deal with last chunk row if
// necessary
if (rows_have_padding)
{
const unsigned int chunk_row = n_chunk_rows - 1;
const number *const val_end_of_row = &val[cols->sparsity_pattern.rowstart[chunk_row+1]
* cols->chunk_size
* cols->chunk_size];
while (val_ptr != val_end_of_row)
{
if ((cols_have_padding == false)
||
(*colnum_ptr != cols->sparsity_pattern.n_cols()-1))
{
// we're at a chunk row but not
// column that has padding
for (unsigned int r=0; r<m() % cols->chunk_size; ++r)
for (unsigned int c=0; c<cols->chunk_size; ++c)
dst(chunk_row * cols->chunk_size + r)
-= (val_ptr[r*cols->chunk_size + c] *
u(*colnum_ptr * cols->chunk_size + c));
}
else
// we're at a chunk row and
// column that has padding
for (unsigned int r=0; r<m() % cols->chunk_size; ++r)
for (unsigned int c=0; c<n() % cols->chunk_size; ++c)
dst(chunk_row * cols->chunk_size + r)
-= (val_ptr[r*cols->chunk_size + c] *
u(*colnum_ptr * cols->chunk_size + c));
++colnum_ptr;
val_ptr += cols->chunk_size * cols->chunk_size;
}
dst_ptr += cols->chunk_size;
}
// finally compute the norm
return dst.l2_norm();
}
template <typename number>
template <typename somenumber>
void
ChunkSparseMatrix<number>::precondition_Jacobi (Vector<somenumber> &dst,
const Vector<somenumber> &src,
const number /*om*/) const
{
Assert (cols != 0, ExcNotInitialized());
Assert (val != 0, ExcNotInitialized());
Assert (cols->optimize_diagonal(),
typename ChunkSparsityPattern::ExcDiagonalNotOptimized());
Assert (dst.size() == n(), ExcDimensionMismatch (dst.size(), n()));
Assert (src.size() == n(), ExcDimensionMismatch (src.size(), n()));
Assert (false, ExcNotImplemented());
}
template <typename number>
template <typename somenumber>
void
ChunkSparseMatrix<number>::precondition_SSOR (Vector<somenumber> &dst,
const Vector<somenumber> &src,
const number /*om*/) const
{
// to understand how this function works
// you may want to take a look at the CVS
// archives to see the original version
// which is much clearer...
Assert (cols != 0, ExcNotInitialized());
Assert (val != 0, ExcNotInitialized());
Assert (cols->optimize_diagonal(),
typename ChunkSparsityPattern::ExcDiagonalNotOptimized());
Assert (dst.size() == n(), ExcDimensionMismatch (dst.size(), n()));
Assert (src.size() == n(), ExcDimensionMismatch (src.size(), n()));
Assert (false, ExcNotImplemented());
}
template <typename number>
template <typename somenumber>
void
ChunkSparseMatrix<number>::precondition_SOR (Vector<somenumber>& dst,
const Vector<somenumber>& src,
const number om) const
{
Assert (cols != 0, ExcNotInitialized());
Assert (val != 0, ExcNotInitialized());
Assert (cols->optimize_diagonal(),
typename ChunkSparsityPattern::ExcDiagonalNotOptimized());
dst = src;
SOR(dst,om);
}
template <typename number>
template <typename somenumber>
void
ChunkSparseMatrix<number>::precondition_TSOR (Vector<somenumber>& dst,
const Vector<somenumber>& src,
const number om) const
{
Assert (cols != 0, ExcNotInitialized());
Assert (val != 0, ExcNotInitialized());
Assert (cols->optimize_diagonal(),
typename ChunkSparsityPattern::ExcDiagonalNotOptimized());
dst = src;
TSOR(dst,om);
}
template <typename number>
template <typename somenumber>
void
ChunkSparseMatrix<number>::SOR (Vector<somenumber>& dst,
const number /*om*/) const
{
Assert (cols != 0, ExcNotInitialized());
Assert (val != 0, ExcNotInitialized());
Assert (cols->optimize_diagonal(),
typename ChunkSparsityPattern::ExcDiagonalNotOptimized());
Assert (m() == dst.size(), ExcDimensionMismatch(m(),dst.size()));
Assert (false, ExcNotImplemented());
}
template <typename number>
template <typename somenumber>
void
ChunkSparseMatrix<number>::TSOR (Vector<somenumber>& dst,
const number /*om*/) const
{
Assert (cols != 0, ExcNotInitialized());
Assert (val != 0, ExcNotInitialized());
Assert (cols->optimize_diagonal(),
typename ChunkSparsityPattern::ExcDiagonalNotOptimized());
Assert (m() == dst.size(), ExcDimensionMismatch(m(),dst.size()));
Assert (false, ExcNotImplemented());
}
template <typename number>
template <typename somenumber>
void
ChunkSparseMatrix<number>::PSOR (Vector<somenumber>& dst,
const std::vector<unsigned int>& permutation,
const std::vector<unsigned int>& inverse_permutation,
const number /*om*/) const
{
Assert (cols != 0, ExcNotInitialized());
Assert (val != 0, ExcNotInitialized());
Assert (cols->optimize_diagonal(),
typename ChunkSparsityPattern::ExcDiagonalNotOptimized());
Assert (m() == dst.size(), ExcDimensionMismatch(m(),dst.size()));
Assert (m() == permutation.size(),
ExcDimensionMismatch(m(), permutation.size()));
Assert (m() == inverse_permutation.size(),
ExcDimensionMismatch(m(), inverse_permutation.size()));
Assert (false, ExcNotImplemented());
}
template <typename number>
template <typename somenumber>
void
ChunkSparseMatrix<number>::TPSOR (Vector<somenumber>& dst,
const std::vector<unsigned int>& permutation,
const std::vector<unsigned int>& inverse_permutation,
const number /*om*/) const
{
Assert (cols != 0, ExcNotInitialized());
Assert (val != 0, ExcNotInitialized());
Assert (cols->optimize_diagonal(),
typename ChunkSparsityPattern::ExcDiagonalNotOptimized());
Assert (m() == dst.size(), ExcDimensionMismatch(m(),dst.size()));
Assert (m() == permutation.size(),
ExcDimensionMismatch(m(), permutation.size()));
Assert (m() == inverse_permutation.size(),
ExcDimensionMismatch(m(), inverse_permutation.size()));
Assert (false, ExcNotImplemented());
}
template <typename number>
template <typename somenumber>
void
ChunkSparseMatrix<number>::SOR_step (Vector<somenumber> &v,
const Vector<somenumber> &b,
const number /*om*/) const
{
Assert (cols != 0, ExcNotInitialized());
Assert (val != 0, ExcNotInitialized());
Assert (cols->optimize_diagonal(),
typename ChunkSparsityPattern::ExcDiagonalNotOptimized());
Assert (m() == v.size(), ExcDimensionMismatch(m(),v.size()));
Assert (m() == b.size(), ExcDimensionMismatch(m(),b.size()));
Assert (false, ExcNotImplemented());
}
template <typename number>
template <typename somenumber>
void
ChunkSparseMatrix<number>::TSOR_step (Vector<somenumber> &v,
const Vector<somenumber> &b,
const number /*om*/) const
{
Assert (cols != 0, ExcNotInitialized());
Assert (val != 0, ExcNotInitialized());
Assert (cols->optimize_diagonal(),
typename ChunkSparsityPattern::ExcDiagonalNotOptimized());
Assert (m() == v.size(), ExcDimensionMismatch(m(),v.size()));
Assert (m() == b.size(), ExcDimensionMismatch(m(),b.size()));
Assert (false, ExcNotImplemented());
}
template <typename number>
template <typename somenumber>
void
ChunkSparseMatrix<number>::SSOR_step (Vector<somenumber> &v,
const Vector<somenumber> &b,
const number om) const
{
SOR_step(v,b,om);
TSOR_step(v,b,om);
}
template <typename number>
template <typename somenumber>
void
ChunkSparseMatrix<number>::SSOR (Vector<somenumber>& dst,
const number /*om*/) const
{
Assert (cols != 0, ExcNotInitialized());
Assert (val != 0, ExcNotInitialized());
Assert (cols->optimize_diagonal(),
typename ChunkSparsityPattern::ExcDiagonalNotOptimized());
Assert (m() == dst.size(), ExcDimensionMismatch(m(),dst.size()));
Assert (false, ExcNotImplemented());
}
template <typename number>
const ChunkSparsityPattern &
ChunkSparseMatrix<number>::get_sparsity_pattern () const
{
Assert (cols != 0, ExcNotInitialized());
return *cols;
}
template <typename number>
void ChunkSparseMatrix<number>::print (std::ostream &out) const
{
AssertThrow (out, ExcIO());
Assert (cols != 0, ExcNotInitialized());
Assert (val != 0, ExcNotInitialized());
Assert (false, ExcNotImplemented());
AssertThrow (out, ExcIO());
}
template <typename number>
void ChunkSparseMatrix<number>::print_formatted (std::ostream &out,
const unsigned int precision,
const bool scientific,
const unsigned int width_,
const char* zero_string,
const double denominator) const
{
AssertThrow (out, ExcIO());
Assert (cols != 0, ExcNotInitialized());
Assert (val != 0, ExcNotInitialized());
unsigned int width = width_;
Assert (false, ExcNotImplemented());
std::ios::fmtflags old_flags = out.flags();
unsigned int old_precision = out.precision (precision);
if (scientific)
{
out.setf (std::ios::scientific, std::ios::floatfield);
if (!width)
width = precision+7;
} else {
out.setf (std::ios::fixed, std::ios::floatfield);
if (!width)
width = precision+2;
}
for (unsigned int i=0; i<m(); ++i)
{
for (unsigned int j=0; j<n(); ++j)
if (cols->sparsity_pattern(i,j) != SparsityPattern::invalid_entry)
out << std::setw(width)
<< val[cols->sparsity_pattern(i,j)] * denominator << ' ';
else
out << std::setw(width) << zero_string << ' ';
out << std::endl;
};
AssertThrow (out, ExcIO());
// reset output format
out.precision(old_precision);
out.flags (old_flags);
}
template <typename number>
void ChunkSparseMatrix<number>::print_pattern (std::ostream &out,
const double threshold) const
{
AssertThrow (out, ExcIO());
Assert (cols != 0, ExcNotInitialized());
Assert (val != 0, ExcNotInitialized());
const unsigned int chunk_size = cols->get_chunk_size();
// loop over all chunk rows and columns,
// and each time we find something repeat
// it chunk_size times in both directions
for (unsigned int i=0; i<cols->sparsity_pattern.n_rows(); ++i)
{
for (unsigned int d=0; d<chunk_size; ++d)
for (unsigned int j=0; j<cols->sparsity_pattern.n_cols(); ++j)
if (cols->sparsity_pattern(i,j) == SparsityPattern::invalid_entry)
{
for (unsigned int e=0; e<chunk_size; ++e)
out << '.';
}
else
if (std::fabs(val[cols->sparsity_pattern(i,j)]) > threshold)
{
for (unsigned int e=0; e<chunk_size; ++e)
out << '*';
}
else
{
for (unsigned int e=0; e<chunk_size; ++e)
out << ':';
}
out << std::endl;
}
AssertThrow (out, ExcIO());
}
template <typename number>
void
ChunkSparseMatrix<number>::block_write (std::ostream &out) const
{
AssertThrow (out, ExcIO());
// first the simple objects,
// bracketed in [...]
out << '[' << max_len << "][";
// then write out real data
out.write (reinterpret_cast<const char*>(&val[0]),
reinterpret_cast<const char*>(&val[max_len])
- reinterpret_cast<const char*>(&val[0]));
out << ']';
AssertThrow (out, ExcIO());
}
template <typename number>
void
ChunkSparseMatrix<number>::block_read (std::istream &in)
{
AssertThrow (in, ExcIO());
char c;
// first read in simple data
in >> c;
AssertThrow (c == '[', ExcIO());
in >> max_len;
in >> c;
AssertThrow (c == ']', ExcIO());
in >> c;
AssertThrow (c == '[', ExcIO());
// reallocate space
delete[] val;
val = new number[max_len];
// then read data
in.read (reinterpret_cast<char*>(&val[0]),
reinterpret_cast<char*>(&val[max_len])
- reinterpret_cast<char*>(&val[0]));
in >> c;
AssertThrow (c == ']', ExcIO());
}
template <typename number>
unsigned int
ChunkSparseMatrix<number>::memory_consumption () const
{
return sizeof(*this) + max_len*sizeof(number);
}
DEAL_II_NAMESPACE_CLOSE
#endif
|