Nektar++
Public Member Functions | Private Member Functions | Private Attributes | List of all members
Nektar::LinearSystem Class Reference

#include <NekLinSys.hpp>

Public Member Functions

template<typename MatrixType >
 LinearSystem (const std::shared_ptr< MatrixType > &theA, PointerWrapper wrapperType=eCopy)
 
template<typename MatrixType >
 LinearSystem (const MatrixType &theA, PointerWrapper wrapperType=eCopy)
 
 LinearSystem (const LinearSystem &rhs)
 
LinearSystemoperator= (const LinearSystem &rhs)
 
template<typename VectorType >
RawType_t< VectorType > Solve (const VectorType &b)
 
template<typename BType , typename XType >
void Solve (const BType &b, XType &x) const
 
template<typename VectorType >
RawType_t< VectorType > SolveTranspose (const VectorType &b)
 
template<typename BType , typename XType >
void SolveTranspose (const BType &b, XType &x) const
 
unsigned int GetRows () const
 
unsigned int GetColumns () const
 

Private Member Functions

template<typename MatrixType >
void FactorMatrix (const MatrixType &theA)
 
void swap (LinearSystem &rhs)
 

Private Attributes

unsigned int n
 
Array< OneD, double > A
 
Array< OneD, int > m_ipivot
 
unsigned int m_numberOfSubDiagonals
 
unsigned int m_numberOfSuperDiagonals
 
MatrixStorage m_matrixType
 
char m_transposeFlag
 

Detailed Description

Definition at line 376 of file NekLinSys.hpp.

Constructor & Destructor Documentation

◆ LinearSystem() [1/3]

template<typename MatrixType >
Nektar::LinearSystem::LinearSystem ( const std::shared_ptr< MatrixType > &  theA,
PointerWrapper  wrapperType = eCopy 
)
inlineexplicit

Definition at line 380 of file NekLinSys.hpp.

382 : n(theA->GetRows()), A(theA->GetPtr()), m_ipivot(),
383 m_numberOfSubDiagonals(theA->GetNumberOfSubDiagonals()),
384 m_numberOfSuperDiagonals(theA->GetNumberOfSuperDiagonals()),
385 m_matrixType(theA->GetType()),
386 m_transposeFlag(theA->GetTransposeFlag())
387 {
388 // At some point we should fix this. We should upate the copy of
389 // A to be transposd for this to work.
390 ASSERTL0(theA->GetTransposeFlag() == 'N',
391 "LinearSystem requires a non-transposed matrix.");
392 ASSERTL0((wrapperType == eWrapper && theA->GetType() != eBANDED) ||
393 wrapperType == eCopy,
394 "Banded matrices can't be wrapped");
395
396 if (wrapperType == eCopy)
397 {
398 A = Array<OneD, double>(theA->GetPtr().size());
399 CopyArray(theA->GetPtr(), A);
400 }
401
402 FactorMatrix(*theA);
403 }
#define ASSERTL0(condition, msg)
Definition: ErrorUtil.hpp:208
Array< OneD, double > A
Definition: NekLinSys.hpp:692
Array< OneD, int > m_ipivot
Definition: NekLinSys.hpp:693
void FactorMatrix(const MatrixType &theA)
Definition: NekLinSys.hpp:502
unsigned int m_numberOfSuperDiagonals
Definition: NekLinSys.hpp:695
unsigned int m_numberOfSubDiagonals
Definition: NekLinSys.hpp:694
MatrixStorage m_matrixType
Definition: NekLinSys.hpp:696
void CopyArray(const Array< OneD, ConstDataType > &source, Array< OneD, DataType > &dest)

References ASSERTL0, Nektar::CopyArray(), Nektar::eBANDED, Nektar::eCopy, Nektar::eWrapper, and FactorMatrix().

◆ LinearSystem() [2/3]

template<typename MatrixType >
Nektar::LinearSystem::LinearSystem ( const MatrixType &  theA,
PointerWrapper  wrapperType = eCopy 
)
inlineexplicit

Definition at line 406 of file NekLinSys.hpp.

408 : n(theA.GetRows()), A(theA.GetPtr()), m_ipivot(),
409 m_numberOfSubDiagonals(theA.GetNumberOfSubDiagonals()),
410 m_numberOfSuperDiagonals(theA.GetNumberOfSuperDiagonals()),
411 m_matrixType(theA.GetType()), m_transposeFlag(theA.GetTransposeFlag())
412 {
413 // At some point we should fix this. We should upate the copy of
414 // A to be transposd for this to work.
415 ASSERTL0(theA.GetTransposeFlag() == 'N',
416 "LinearSystem requires a non-transposed matrix.");
417 ASSERTL0((wrapperType == eWrapper && theA.GetType() != eBANDED) ||
418 wrapperType == eCopy,
419 "Banded matrices can't be wrapped");
420
421 if (wrapperType == eCopy)
422 {
423 A = Array<OneD, double>(theA.GetPtr().size());
424 CopyArray(theA.GetPtr(), A);
425 }
426
427 FactorMatrix(theA);
428 }

References ASSERTL0, Nektar::CopyArray(), Nektar::eBANDED, Nektar::eCopy, Nektar::eWrapper, and FactorMatrix().

◆ LinearSystem() [3/3]

Nektar::LinearSystem::LinearSystem ( const LinearSystem rhs)
inline

Definition at line 430 of file NekLinSys.hpp.

431 : n(rhs.n), A(rhs.A), m_ipivot(rhs.m_ipivot),
432 m_numberOfSubDiagonals(rhs.m_numberOfSubDiagonals),
433 m_numberOfSuperDiagonals(rhs.m_numberOfSuperDiagonals),
434 m_matrixType(rhs.m_matrixType), m_transposeFlag(rhs.m_transposeFlag)
435 {
436 }

Member Function Documentation

◆ FactorMatrix()

template<typename MatrixType >
void Nektar::LinearSystem::FactorMatrix ( const MatrixType &  theA)
inlineprivate

Definition at line 502 of file NekLinSys.hpp.

503 {
504 switch (m_matrixType)
505 {
506 case eFULL:
507 {
508 int m = theA.GetRows();
509 int n = theA.GetColumns();
510
511 int pivotSize = std::max(1, std::min(m, n));
512 int info = 0;
513 m_ipivot = Array<OneD, int>(pivotSize);
514
515 Lapack::Dgetrf(m, n, A.get(), m, m_ipivot.get(), info);
516
517 if (info < 0)
518 {
519 std::string message =
520 "ERROR: The " + std::to_string(-info) +
521 "th parameter had an illegal parameter for dgetrf";
522 ASSERTL0(false, message.c_str());
523 }
524 else if (info > 0)
525 {
526 std::string message =
527 "ERROR: Element u_" + std::to_string(info) +
528 std::to_string(info) + " is 0 from dgetrf";
529 ASSERTL0(false, message.c_str());
530 }
531 }
532 break;
533 case eDIAGONAL:
534 for (unsigned int i = 0; i < theA.GetColumns(); ++i)
535 {
536 A[i] = 1.0 / theA(i, i);
537 }
538 break;
541 break;
542 case eSYMMETRIC:
543 {
544 int info = 0;
545 int pivotSize = theA.GetRows();
546 m_ipivot = Array<OneD, int>(pivotSize);
547
548 Lapack::Dsptrf('U', theA.GetRows(), A.get(), m_ipivot.get(),
549 info);
550
551 if (info < 0)
552 {
553 std::string message =
554 "ERROR: The " + std::to_string(-info) +
555 "th parameter had an illegal parameter for dsptrf";
556 NEKERROR(ErrorUtil::efatal, message.c_str());
557 }
558 else if (info > 0)
559 {
560 std::string message =
561 "ERROR: Element u_" + std::to_string(info) +
562 std::to_string(info) + " is 0 from dsptrf";
563 NEKERROR(ErrorUtil::efatal, message.c_str());
564 }
565 }
566 break;
568 {
569 int info = 0;
570 Lapack::Dpptrf('U', theA.GetRows(), A.get(), info);
571
572 if (info < 0)
573 {
574 std::string message =
575 "ERROR: The " + std::to_string(-info) +
576 "th parameter had an illegal parameter for dpptrf";
577 NEKERROR(ErrorUtil::efatal, message.c_str());
578 }
579 else if (info > 0)
580 {
581 std::string message =
582 "ERROR: The leading minor of order " +
583 std::to_string(info) +
584 " is not positive definite from dpptrf";
585 NEKERROR(ErrorUtil::efatal, message.c_str());
586 }
587 }
588 break;
589 case eBANDED:
590 {
591 int M = n;
592 int N = n;
593 int KL = m_numberOfSubDiagonals;
595
596 // The array we pass in to dgbtrf must have enough space for KL
597 // subdiagonals and KL+KU superdiagonals (see lapack users
598 // guide, in the section discussing band storage.
599 unsigned int requiredStorageSize =
601 KL + KU);
602
603 unsigned int rawRows = KL + KU + 1;
604 A = Array<OneD, double>(requiredStorageSize);
605
606 // Put the extra elements up front.
607 for (unsigned int i = 0; i < theA.GetColumns(); ++i)
608 {
609 std::copy(theA.GetRawPtr() + i * rawRows,
610 theA.GetRawPtr() + (i + 1) * rawRows,
611 A.get() + (i + 1) * KL + i * rawRows);
612 }
613
614 int info = 0;
615 int pivotSize = theA.GetRows();
616 m_ipivot = Array<OneD, int>(pivotSize);
617
618 Lapack::Dgbtrf(M, N, KL, KU, A.get(), 2 * KL + KU + 1,
619 m_ipivot.get(), info);
620
621 if (info < 0)
622 {
623 std::string message =
624 "ERROR: The " + std::to_string(-info) +
625 "th parameter had an illegal parameter for dgbtrf";
626 NEKERROR(ErrorUtil::efatal, message.c_str());
627 }
628 else if (info > 0)
629 {
630 std::string message =
631 "ERROR: Element u_" + std::to_string(info) +
632 std::to_string(info) + " is 0 from dgbtrf";
633 NEKERROR(ErrorUtil::efatal, message.c_str());
634 }
635 }
636 break;
638 {
639 ASSERTL1(
641 std::string("Number of sub- and superdiagonals should ") +
642 std::string("be equal for a symmetric banded matrix"));
643
645 int info = 0;
646 Lapack::Dpbtrf('U', theA.GetRows(), KU, A.get(), KU + 1, info);
647
648 if (info < 0)
649 {
650 std::string message =
651 "ERROR: The " + std::to_string(-info) +
652 "th parameter had an illegal parameter for dpbtrf";
653 NEKERROR(ErrorUtil::efatal, message.c_str());
654 }
655 else if (info > 0)
656 {
657 std::string message =
658 "ERROR: The leading minor of order " +
659 std::to_string(info) +
660 " is not positive definite from dpbtrf";
661 NEKERROR(ErrorUtil::efatal, message.c_str());
662 }
663 }
664 break;
666 NEKERROR(ErrorUtil::efatal, "Unhandled matrix type");
667 break;
669 NEKERROR(ErrorUtil::efatal, "Unhandled matrix type");
670 break;
672 NEKERROR(ErrorUtil::efatal, "Unhandled matrix type");
673 break;
674
675 default:
676 NEKERROR(ErrorUtil::efatal, "Unhandled matrix type");
677 }
678 }
#define NEKERROR(type, msg)
Assert Level 0 – Fundamental assert which is used whether in FULLDEBUG, DEBUG or OPT compilation mode...
Definition: ErrorUtil.hpp:202
#define ASSERTL1(condition, msg)
Assert Level 1 – Debugging which is used whether in FULLDEBUG or DEBUG compilation mode....
Definition: ErrorUtil.hpp:242
def copy(self)
Definition: pycml.py:2663
static void Dgbtrf(const int &m, const int &n, const int &kl, const int &ku, double *a, const int &lda, int *ipiv, int &info)
General banded matrix LU factorisation.
Definition: Lapack.hpp:231
static void Dgetrf(const int &m, const int &n, double *a, const int &lda, int *ipiv, int &info)
General matrix LU factorisation.
Definition: Lapack.hpp:262
static void Dpptrf(const char &uplo, const int &n, double *ap, int &info)
Cholesky factor a real positive definite packed-symmetric matrix.
Definition: Lapack.hpp:199
static void Dsptrf(const char &uplo, const int &n, double *ap, int *ipiv, int &info)
factor a real packed-symmetric matrix using Bunch-Kaufman pivoting.
Definition: Lapack.hpp:128
static void Dpbtrf(const char &uplo, const int &n, const int &kd, double *ab, const int &ldab, int &info)
Cholesky factorize a real positive definite banded-symmetric matrix.
Definition: Lapack.hpp:215
@ eLOWER_TRIANGULAR_BANDED
@ ePOSITIVE_DEFINITE_SYMMETRIC_BANDED
@ ePOSITIVE_DEFINITE_SYMMETRIC
@ eUPPER_TRIANGULAR_BANDED
static unsigned int GetRequiredStorageSize(unsigned int totalRows, unsigned int totalColumns, unsigned int subDiags, unsigned int superDiags)
Calculates and returns the storage size required.
Definition: MatrixFuncs.cpp:42

References ASSERTL0, ASSERTL1, CellMLToNektar.pycml::copy(), Lapack::Dgbtrf(), Lapack::Dgetrf(), Lapack::Dpbtrf(), Lapack::Dpptrf(), Lapack::Dsptrf(), Nektar::eBANDED, Nektar::eDIAGONAL, Nektar::ErrorUtil::efatal, Nektar::eFULL, Nektar::eLOWER_TRIANGULAR, Nektar::eLOWER_TRIANGULAR_BANDED, Nektar::ePOSITIVE_DEFINITE_SYMMETRIC, Nektar::ePOSITIVE_DEFINITE_SYMMETRIC_BANDED, Nektar::eSYMMETRIC, Nektar::eSYMMETRIC_BANDED, Nektar::eUPPER_TRIANGULAR, Nektar::eUPPER_TRIANGULAR_BANDED, Nektar::BandedMatrixFuncs::GetRequiredStorageSize(), m_ipivot, m_matrixType, m_numberOfSubDiagonals, m_numberOfSuperDiagonals, n, and NEKERROR.

Referenced by LinearSystem().

◆ GetColumns()

unsigned int Nektar::LinearSystem::GetColumns ( ) const
inline

Definition at line 496 of file NekLinSys.hpp.

497 {
498 return n;
499 }

References n.

◆ GetRows()

unsigned int Nektar::LinearSystem::GetRows ( ) const
inline

Definition at line 492 of file NekLinSys.hpp.

493 {
494 return n;
495 }

References n.

Referenced by Solve(), and SolveTranspose().

◆ operator=()

LinearSystem & Nektar::LinearSystem::operator= ( const LinearSystem rhs)
inline

Definition at line 438 of file NekLinSys.hpp.

439 {
440 LinearSystem temp(rhs);
441 swap(temp);
442 return *this;
443 }
void swap(LinearSystem &rhs)
Definition: NekLinSys.hpp:680
LinearSystem(const std::shared_ptr< MatrixType > &theA, PointerWrapper wrapperType=eCopy)
Definition: NekLinSys.hpp:380

References swap().

◆ Solve() [1/2]

template<typename BType , typename XType >
void Nektar::LinearSystem::Solve ( const BType &  b,
XType &  x 
) const
inline

Definition at line 460 of file NekLinSys.hpp.

461 {
467 }
static DataType & reference(DataType &o)
static const DataType & const_reference(const DataType &o)
static void Solve(const BVectorType &b, XVectorType &x, MatrixStorage m_matrixType, const Array< OneD, const int > &m_ipivot, unsigned int n, const Array< OneD, const double > &A, char m_transposeFlag, unsigned int m_numberOfSubDiagonals, unsigned int m_numberOfSuperDiagonals)
Definition: NekLinSys.hpp:75

References m_ipivot, m_matrixType, m_numberOfSubDiagonals, m_numberOfSuperDiagonals, m_transposeFlag, n, and Nektar::LinearSystemSolver::Solve().

◆ Solve() [2/2]

template<typename VectorType >
RawType_t< VectorType > Nektar::LinearSystem::Solve ( const VectorType &  b)
inline

◆ SolveTranspose() [1/2]

template<typename BType , typename XType >
void Nektar::LinearSystem::SolveTranspose ( const BType &  b,
XType &  x 
) const
inline

Definition at line 483 of file NekLinSys.hpp.

484 {
490 }
static void SolveTranspose(const BVectorType &b, XVectorType &x, MatrixStorage m_matrixType, const Array< OneD, const int > &m_ipivot, unsigned int n, const Array< OneD, const double > &A, char m_transposeFlag, unsigned int m_numberOfSubDiagonals, unsigned int m_numberOfSuperDiagonals)
Definition: NekLinSys.hpp:235

References m_ipivot, m_matrixType, m_numberOfSubDiagonals, m_numberOfSuperDiagonals, m_transposeFlag, n, and Nektar::LinearSystemSolver::SolveTranspose().

◆ SolveTranspose() [2/2]

template<typename VectorType >
RawType_t< VectorType > Nektar::LinearSystem::SolveTranspose ( const VectorType &  b)
inline

◆ swap()

void Nektar::LinearSystem::swap ( LinearSystem rhs)
inlineprivate

Definition at line 680 of file NekLinSys.hpp.

681 {
682 std::swap(n, rhs.n);
683 std::swap(A, rhs.A);
684 std::swap(m_ipivot, rhs.m_ipivot);
685 std::swap(m_numberOfSubDiagonals, rhs.m_numberOfSubDiagonals);
686 std::swap(m_numberOfSuperDiagonals, rhs.m_numberOfSuperDiagonals);
687 std::swap(m_matrixType, rhs.m_matrixType);
688 std::swap(m_transposeFlag, rhs.m_transposeFlag);
689 }

References A, m_ipivot, m_matrixType, m_numberOfSubDiagonals, m_numberOfSuperDiagonals, m_transposeFlag, and n.

Referenced by operator=().

Member Data Documentation

◆ A

Array<OneD, double> Nektar::LinearSystem::A
private

Definition at line 692 of file NekLinSys.hpp.

Referenced by swap().

◆ m_ipivot

Array<OneD, int> Nektar::LinearSystem::m_ipivot
private

Definition at line 693 of file NekLinSys.hpp.

Referenced by FactorMatrix(), Solve(), SolveTranspose(), and swap().

◆ m_matrixType

MatrixStorage Nektar::LinearSystem::m_matrixType
private

Definition at line 696 of file NekLinSys.hpp.

Referenced by FactorMatrix(), Solve(), SolveTranspose(), and swap().

◆ m_numberOfSubDiagonals

unsigned int Nektar::LinearSystem::m_numberOfSubDiagonals
private

Definition at line 694 of file NekLinSys.hpp.

Referenced by FactorMatrix(), Solve(), SolveTranspose(), and swap().

◆ m_numberOfSuperDiagonals

unsigned int Nektar::LinearSystem::m_numberOfSuperDiagonals
private

Definition at line 695 of file NekLinSys.hpp.

Referenced by FactorMatrix(), Solve(), SolveTranspose(), and swap().

◆ m_transposeFlag

char Nektar::LinearSystem::m_transposeFlag
private

Definition at line 697 of file NekLinSys.hpp.

Referenced by Solve(), SolveTranspose(), and swap().

◆ n

unsigned int Nektar::LinearSystem::n
private

Definition at line 691 of file NekLinSys.hpp.

Referenced by FactorMatrix(), GetColumns(), GetRows(), Solve(), SolveTranspose(), and swap().