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)
 
 ~LinearSystem ()
 
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(), eVECTOR_WRAPPER), 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:215
Array< OneD, double > A
Definition: NekLinSys.hpp:696
Array< OneD, int > m_ipivot
Definition: NekLinSys.hpp:697
void FactorMatrix(const MatrixType &theA)
Definition: NekLinSys.hpp:506
unsigned int m_numberOfSuperDiagonals
Definition: NekLinSys.hpp:699
unsigned int m_numberOfSubDiagonals
Definition: NekLinSys.hpp:698
MatrixStorage m_matrixType
Definition: NekLinSys.hpp:700
@ eVECTOR_WRAPPER
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(), eVECTOR_WRAPPER), 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 }

◆ ~LinearSystem()

Nektar::LinearSystem::~LinearSystem ( )
inline

Definition at line 445 of file NekLinSys.hpp.

446 {
447 }

Member Function Documentation

◆ FactorMatrix()

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

Definition at line 506 of file NekLinSys.hpp.

507 {
508 switch (m_matrixType)
509 {
510 case eFULL:
511 {
512 int m = theA.GetRows();
513 int n = theA.GetColumns();
514
515 int pivotSize = std::max(1, std::min(m, n));
516 int info = 0;
517 m_ipivot = Array<OneD, int>(pivotSize);
518
519 Lapack::Dgetrf(m, n, A.get(), m, m_ipivot.get(), info);
520
521 if (info < 0)
522 {
523 std::string message =
524 "ERROR: The " + std::to_string(-info) +
525 "th parameter had an illegal parameter for dgetrf";
526 ASSERTL0(false, message.c_str());
527 }
528 else if (info > 0)
529 {
530 std::string message =
531 "ERROR: Element u_" + std::to_string(info) +
532 std::to_string(info) + " is 0 from dgetrf";
533 ASSERTL0(false, message.c_str());
534 }
535 }
536 break;
537 case eDIAGONAL:
538 for (unsigned int i = 0; i < theA.GetColumns(); ++i)
539 {
540 A[i] = 1.0 / theA(i, i);
541 }
542 break;
545 break;
546 case eSYMMETRIC:
547 {
548 int info = 0;
549 int pivotSize = theA.GetRows();
550 m_ipivot = Array<OneD, int>(pivotSize);
551
552 Lapack::Dsptrf('U', theA.GetRows(), A.get(), m_ipivot.get(),
553 info);
554
555 if (info < 0)
556 {
557 std::string message =
558 "ERROR: The " + std::to_string(-info) +
559 "th parameter had an illegal parameter for dsptrf";
560 NEKERROR(ErrorUtil::efatal, message.c_str());
561 }
562 else if (info > 0)
563 {
564 std::string message =
565 "ERROR: Element u_" + std::to_string(info) +
566 std::to_string(info) + " is 0 from dsptrf";
567 NEKERROR(ErrorUtil::efatal, message.c_str());
568 }
569 }
570 break;
572 {
573 int info = 0;
574 Lapack::Dpptrf('U', theA.GetRows(), A.get(), info);
575
576 if (info < 0)
577 {
578 std::string message =
579 "ERROR: The " + std::to_string(-info) +
580 "th parameter had an illegal parameter for dpptrf";
581 NEKERROR(ErrorUtil::efatal, message.c_str());
582 }
583 else if (info > 0)
584 {
585 std::string message =
586 "ERROR: The leading minor of order " +
587 std::to_string(info) +
588 " is not positive definite from dpptrf";
589 NEKERROR(ErrorUtil::efatal, message.c_str());
590 }
591 }
592 break;
593 case eBANDED:
594 {
595 int M = n;
596 int N = n;
597 int KL = m_numberOfSubDiagonals;
599
600 // The array we pass in to dgbtrf must have enough space for KL
601 // subdiagonals and KL+KU superdiagonals (see lapack users
602 // guide, in the section discussing band storage.
603 unsigned int requiredStorageSize =
605 KL + KU);
606
607 unsigned int rawRows = KL + KU + 1;
608 A = Array<OneD, double>(requiredStorageSize);
609
610 // Put the extra elements up front.
611 for (unsigned int i = 0; i < theA.GetColumns(); ++i)
612 {
613 std::copy(theA.GetRawPtr() + i * rawRows,
614 theA.GetRawPtr() + (i + 1) * rawRows,
615 A.get() + (i + 1) * KL + i * rawRows);
616 }
617
618 int info = 0;
619 int pivotSize = theA.GetRows();
620 m_ipivot = Array<OneD, int>(pivotSize);
621
622 Lapack::Dgbtrf(M, N, KL, KU, A.get(), 2 * KL + KU + 1,
623 m_ipivot.get(), info);
624
625 if (info < 0)
626 {
627 std::string message =
628 "ERROR: The " + std::to_string(-info) +
629 "th parameter had an illegal parameter for dgbtrf";
630 NEKERROR(ErrorUtil::efatal, message.c_str());
631 }
632 else if (info > 0)
633 {
634 std::string message =
635 "ERROR: Element u_" + std::to_string(info) +
636 std::to_string(info) + " is 0 from dgbtrf";
637 NEKERROR(ErrorUtil::efatal, message.c_str());
638 }
639 }
640 break;
642 {
643 ASSERTL1(
645 std::string("Number of sub- and superdiagonals should ") +
646 std::string("be equal for a symmetric banded matrix"));
647
649 int info = 0;
650 Lapack::Dpbtrf('U', theA.GetRows(), KU, A.get(), KU + 1, info);
651
652 if (info < 0)
653 {
654 std::string message =
655 "ERROR: The " + std::to_string(-info) +
656 "th parameter had an illegal parameter for dpbtrf";
657 NEKERROR(ErrorUtil::efatal, message.c_str());
658 }
659 else if (info > 0)
660 {
661 std::string message =
662 "ERROR: The leading minor of order " +
663 std::to_string(info) +
664 " is not positive definite from dpbtrf";
665 NEKERROR(ErrorUtil::efatal, message.c_str());
666 }
667 }
668 break;
670 NEKERROR(ErrorUtil::efatal, "Unhandled matrix type");
671 break;
673 NEKERROR(ErrorUtil::efatal, "Unhandled matrix type");
674 break;
676 NEKERROR(ErrorUtil::efatal, "Unhandled matrix type");
677 break;
678
679 default:
680 NEKERROR(ErrorUtil::efatal, "Unhandled matrix type");
681 }
682 }
#define NEKERROR(type, msg)
Assert Level 0 – Fundamental assert which is used whether in FULLDEBUG, DEBUG or OPT compilation mode...
Definition: ErrorUtil.hpp:209
#define ASSERTL1(condition, msg)
Assert Level 1 – Debugging which is used whether in FULLDEBUG or DEBUG compilation mode....
Definition: ErrorUtil.hpp:249
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:44

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 500 of file NekLinSys.hpp.

501 {
502 return n;
503 }

References n.

◆ GetRows()

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

Definition at line 496 of file NekLinSys.hpp.

497 {
498 return n;
499 }

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:684
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 464 of file NekLinSys.hpp.

465 {
471 }
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 487 of file NekLinSys.hpp.

488 {
494 }
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 684 of file NekLinSys.hpp.

685 {
686 std::swap(n, rhs.n);
687 std::swap(A, rhs.A);
688 std::swap(m_ipivot, rhs.m_ipivot);
689 std::swap(m_numberOfSubDiagonals, rhs.m_numberOfSubDiagonals);
690 std::swap(m_numberOfSuperDiagonals, rhs.m_numberOfSuperDiagonals);
691 std::swap(m_matrixType, rhs.m_matrixType);
692 std::swap(m_transposeFlag, rhs.m_transposeFlag);
693 }

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 696 of file NekLinSys.hpp.

Referenced by swap().

◆ m_ipivot

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

Definition at line 697 of file NekLinSys.hpp.

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

◆ m_matrixType

MatrixStorage Nektar::LinearSystem::m_matrixType
private

Definition at line 700 of file NekLinSys.hpp.

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

◆ m_numberOfSubDiagonals

unsigned int Nektar::LinearSystem::m_numberOfSubDiagonals
private

Definition at line 698 of file NekLinSys.hpp.

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

◆ m_numberOfSuperDiagonals

unsigned int Nektar::LinearSystem::m_numberOfSuperDiagonals
private

Definition at line 699 of file NekLinSys.hpp.

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

◆ m_transposeFlag

char Nektar::LinearSystem::m_transposeFlag
private

Definition at line 701 of file NekLinSys.hpp.

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

◆ n

unsigned int Nektar::LinearSystem::n
private

Definition at line 695 of file NekLinSys.hpp.

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