Processing math: 100%
OGS
MathLib::LinAlg Namespace Reference

Detailed Description

Some general linear algebra functionality.

By using the provided functions linear algebra capabilities can be used for different matrix and vector types in a way that is agnostic towards the specific type used.

For documentation, refer to that of the templated method. All specializations or overload must behave in the same way.

Functions

void setLocalAccessibleVector (PETScVector const &x)
 
void set (PETScVector &x, double const a)
 
void copy (PETScVector const &x, PETScVector &y)
 
void scale (PETScVector &x, double const a)
 
void aypx (PETScVector &y, double const a, PETScVector const &x)
 
void axpy (PETScVector &y, double const a, PETScVector const &x)
 
void axpby (PETScVector &y, double const a, double const b, PETScVector const &x)
 
template<>
void componentwiseDivide (PETScVector &w, PETScVector const &x, PETScVector const &y)
 
template<>
double norm1 (PETScVector const &x)
 
template<>
double norm2 (PETScVector const &x)
 
template<>
double normMax (PETScVector const &x)
 
void copy (PETScMatrix const &A, PETScMatrix &B)
 
void scale (PETScMatrix &A, double const a)
 
void aypx (PETScMatrix &Y, double const a, PETScMatrix const &X)
 
void axpy (PETScMatrix &Y, double const a, PETScMatrix const &X)
 
void matMult (PETScMatrix const &A, PETScVector const &x, PETScVector &y)
 
void matMultAdd (PETScMatrix const &A, PETScVector const &v1, PETScVector const &v2, PETScVector &v3)
 
void finalizeAssembly (PETScMatrix &A)
 
void finalizeAssembly (PETScVector &x)
 
template<typename MatrixOrVector >
void copy (MatrixOrVector const &x, MatrixOrVector &y)
 Copies x to y. More...
 
template<typename MatrixOrVector >
void scale (MatrixOrVector &x, double const a)
 Scales x by a. More...
 
template<typename MatrixOrVector >
void aypx (MatrixOrVector &y, double const a, MatrixOrVector const &x)
 Computes y = a \cdot y + x . More...
 
template<typename MatrixOrVector >
void axpy (MatrixOrVector &y, double const a, MatrixOrVector const &x)
 Computes y = a \cdot x + y . More...
 
template<typename MatrixOrVector >
void axpby (MatrixOrVector &y, double const a, double const b, MatrixOrVector const &x)
 Computes y = a \cdot x + b \cdot y . More...
 
template<typename MatrixOrVector >
void componentwiseDivide (MatrixOrVector &w, MatrixOrVector const &x, MatrixOrVector const &y)
 Computes w = x/y componentwise. More...
 
template<typename MatrixOrVector >
double norm1 (MatrixOrVector const &x)
 Computes the Manhattan norm of x. More...
 
template<typename MatrixOrVector >
double norm2 (MatrixOrVector const &x)
 Computes the Euclidean norm of x. More...
 
template<typename MatrixOrVector >
double normMax (MatrixOrVector const &x)
 Computes the maximum norm of x. More...
 
template<typename MatrixOrVector >
double norm (MatrixOrVector const &x, MathLib::VecNormType type)
 
template<typename Matrix >
void finalizeAssembly (Matrix &)
 
template<typename Matrix , typename Vector >
void matMult (Matrix const &A, Vector const &x, Vector &y)
 
template<typename Matrix , typename Vector >
void matMultAdd (Matrix const &A, Vector const &v1, Vector const &v2, Vector &v3)
 

Function Documentation

◆ axpby() [1/2]

template<typename MatrixOrVector >
void MathLib::LinAlg::axpby ( MatrixOrVector &  y,
double const  a,
double const  b,
MatrixOrVector const &  x 
)

Computes y = a \cdot x + b \cdot y .

Definition at line 65 of file LinAlg.h.

66 {
67  y = a*x + b*y;
68 }

◆ axpby() [2/2]

void MathLib::LinAlg::axpby ( PETScVector y,
double const  a,
double const  b,
PETScVector const &  x 
)

Definition at line 64 of file LinAlg.cpp.

65 {
66  // TODO check sizes
67  VecAXPBY(y.getRawVector(), a, b, x.getRawVector());
68 }

References MathLib::PETScVector::getRawVector().

Referenced by NumLib::TimeDiscretization::getXdot().

◆ axpy() [1/3]

template<typename MatrixOrVector >
void MathLib::LinAlg::axpy ( MatrixOrVector &  y,
double const  a,
MatrixOrVector const &  x 
)

Computes y = a \cdot x + y .

Definition at line 58 of file LinAlg.h.

59 {
60  y += a*x;
61 }

◆ axpy() [2/3]

void MathLib::LinAlg::axpy ( PETScMatrix Y,
double const  a,
PETScMatrix const &  X 
)

Definition at line 131 of file LinAlg.cpp.

132 {
133  // TODO check sizes
134  // TODO sparsity pattern, currently they are assumed to be different (slow)
135  MatAXPY(Y.getRawMatrix(), a, X.getRawMatrix(), DIFFERENT_NONZERO_PATTERN);
136 }

References MathLib::PETScMatrix::getRawMatrix().

◆ axpy() [3/3]

◆ aypx() [1/3]

template<typename MatrixOrVector >
void MathLib::LinAlg::aypx ( MatrixOrVector &  y,
double const  a,
MatrixOrVector const &  x 
)

Computes y = a \cdot y + x .

Definition at line 51 of file LinAlg.h.

52 {
53  y = a*y + x;
54 }

◆ aypx() [2/3]

void MathLib::LinAlg::aypx ( PETScMatrix Y,
double const  a,
PETScMatrix const &  X 
)

Definition at line 123 of file LinAlg.cpp.

124 {
125  // TODO check sizes
126  // TODO sparsity pattern, currently they are assumed to be different (slow)
127  MatAYPX(Y.getRawMatrix(), a, X.getRawMatrix(), DIFFERENT_NONZERO_PATTERN);
128 }

References MathLib::PETScMatrix::getRawMatrix().

◆ aypx() [3/3]

void MathLib::LinAlg::aypx ( PETScVector y,
double const  a,
PETScVector const &  x 
)

Definition at line 50 of file LinAlg.cpp.

51 {
52  // TODO check sizes
53  VecAYPX(y.getRawVector(), a, x.getRawVector());
54 }

References MathLib::PETScVector::getRawVector().

Referenced by NumLib::MatrixTranslatorGeneral< ODESystemTag::FirstOrderImplicitQuasilinear >::computeA(), and ProcessLib::ComponentTransport::ComponentTransportProcess::solveReactionEquation().

◆ componentwiseDivide() [1/2]

template<typename MatrixOrVector >
void MathLib::LinAlg::componentwiseDivide ( MatrixOrVector &  w,
MatrixOrVector const &  x,
MatrixOrVector const &  y 
)

Computes w = x/y componentwise.

◆ componentwiseDivide() [2/2]

template<>
void MathLib::LinAlg::componentwiseDivide ( PETScVector w,
PETScVector const &  x,
PETScVector const &  y 
)

Definition at line 73 of file LinAlg.cpp.

75 {
76  VecPointwiseDivide(w.getRawVector(), x.getRawVector(), y.getRawVector());
77 }

References MathLib::PETScVector::getRawVector().

Referenced by NumLib::LocalLinearLeastSquaresExtrapolator::extrapolate().

◆ copy() [1/3]

template<typename MatrixOrVector >
void MathLib::LinAlg::copy ( MatrixOrVector const &  x,
MatrixOrVector &  y 
)

Copies x to y.

Definition at line 37 of file LinAlg.h.

38 {
39  y = x;
40 }

◆ copy() [2/3]

void MathLib::LinAlg::copy ( PETScMatrix const &  A,
PETScMatrix B 
)

Definition at line 111 of file LinAlg.cpp.

112 {
113  B = A;
114 }

◆ copy() [3/3]

void MathLib::LinAlg::copy ( PETScVector const &  x,
PETScVector y 
)

Definition at line 37 of file LinAlg.cpp.

38 {
39  if (!y.getRawVector())
40  y.shallowCopy(x);
41  VecCopy(x.getRawVector(), y.getRawVector());
42 }

References MathLib::PETScVector::getRawVector(), and MathLib::PETScVector::shallowCopy().

Referenced by GMSHPrefsDialog::GMSHPrefsDialog(), FileIO::Gocad::GocadSGridReader::GocadSGridReader(), GeoLib::Raster::Raster(), FileIO::Gocad::GocadSGridReader::addFaceSetQuad(), FileIO::Gocad::GocadSGridReader::addGocadPropertiesToMesh(), addIntegrationPointData(), addIntegrationPointMetaData(), MeshLib::MeshLayerMapper::addLayerToMesh(), MeshLib::addLayerToMesh(), MeshLib::addPropertyToMesh(), FileIO::SwmmInterface::addResultsToMesh(), MeshGeoToolsLib::appendLinesAlongPolylines(), ProcessLib::LIE::PostProcessTool::calculateTotalDisplacement(), NumLib::MatrixTranslatorGeneral< ODESystemTag::FirstOrderImplicitQuasilinear >::computeA(), NumLib::MatrixTranslatorGeneral< ODESystemTag::FirstOrderImplicitQuasilinear >::computeJacobian(), NumLib::TimeDiscretization::computeRelativeChangeFromPreviousTimestep(), ProcessLib::TimeLoop::computeTimeStepping(), MeshLib::VtkMeshConverter::convertTypedArray(), ProcessLib::LIE::PostProcessTool::copyPropertyValues(), MeshLib::createQuadraticOrderMesh(), ChemistryLib::createReactionRates(), BaseLib::excludeObjectCopy(), ProcessLib::ComponentTransport::ComponentTransportProcess::extrapolateIntegrationPointValuesToNodes(), flipRaster(), MeshLib::getBaseNodes(), ProcessLib::PhaseFieldIrreversibleDamageOracleBoundaryCondition::getEssentialBCValues(), ProcessLib::LIE::getFractureMatrixDataInMesh(), LayeredMeshGenerator::getMesh(), NumLib::SimpleMatrixVectorProvider::getVector(), NumLib::BackwardEuler::getWeightedOldX(), MaterialLib::Solids::MFront::MFront< DisplacementDim >::integrateStress(), main(), BaseLib::operator<<(), operator<<(), FileIO::Gocad::operator<<(), ChemistryLib::PhreeqcIOData::anonymous_namespace{PhreeqcIO.cpp}::operator<<(), ApplicationUtils::NodeWiseMeshPartitioner::partitionOtherMesh(), ProcessLib::HeatTransportBHE::HeatTransportBHEProcess::postTimestepConcreteProcess(), MeshLib::PropertyVector< T * >::print(), ApplicationUtils::NodeWiseMeshPartitioner::processPartition(), MathLib::TemplatePoint< T, DIM >::read(), FileIO::Gocad::GocadSGridReader::readElementPropertiesBinary(), FileIO::GMSInterface::readGMS3DMMesh(), FileIO::SwmmInterface::readSwmmInputToLineMesh(), FileIO::TetGenInterface::readTetGenMesh(), GeoLib::MinimalBoundingSphere::recurseCalculation(), ProcessLib::TimeLoop::setCoupledSolutions(), ProcessLib::Process::setInitialConditions(), DiagramList::setList(), ProcessLib::Deformation::solidMaterialInternalToSecondaryVariables(), ProcessLib::Deformation::solidMaterialInternalVariablesToIntegrationPointWriter(), NumLib::NonlinearSolver< NonlinearSolverTag::Newton >::solve(), NumLib::NonlinearSolver< NonlinearSolverTag::Picard >::solve(), NumLib::PETScNonlinearSolver::solve(), ProcessLib::TimeLoop::solveCoupledEquationSystemsByStaggeredScheme(), ProcessLib::ComponentTransport::ComponentTransportProcess::solveReactionEquation(), BaseLib::splitString(), ProcessLib::PhaseField::PhaseFieldProcess< DisplacementDim >::updateConstraints(), anonymous_namespace{IdentifySubdomainMesh.cpp}::updateOrCheckExistingSubdomainProperty(), MathLib::TemplatePoint< T, DIM >::write(), and writeDataToMesh().

◆ finalizeAssembly() [1/3]

template<typename Matrix >
void MathLib::LinAlg::finalizeAssembly ( Matrix &  )

◆ finalizeAssembly() [2/3]

◆ finalizeAssembly() [3/3]

void MathLib::LinAlg::finalizeAssembly ( PETScVector x)

Definition at line 167 of file LinAlg.cpp.

168 {
169  x.finalizeAssembly();
170 }

References MathLib::PETScVector::finalizeAssembly().

◆ matMult() [1/2]

template<typename Matrix , typename Vector >
void MathLib::LinAlg::matMult ( Matrix const &  A,
Vector const &  x,
Vector &  y 
)

Computes y = A \cdot x .

Note
x must not be the same object as y. This restirction has been chosen in order to fulfill the requirements of the respective PETSc function.

Definition at line 114 of file LinAlg.h.

115 {
116  assert(&x != &y);
117  y = A*x;
118 }

◆ matMult() [2/2]

◆ matMultAdd() [1/2]

template<typename Matrix , typename Vector >
void MathLib::LinAlg::matMultAdd ( Matrix const &  A,
Vector const &  v1,
Vector const &  v2,
Vector &  v3 
)

Computes v_3 = A \cdot v_1 + v_2 .

Note
x must not be the same object as y. This restirction has been chosen in order to fulfill the requirements of the respective PETSc function.

Definition at line 127 of file LinAlg.h.

128 {
129  assert(&v1 != &v3);
130  v3 = v2 + A*v1;
131 }

◆ matMultAdd() [2/2]

void MathLib::LinAlg::matMultAdd ( PETScMatrix const &  A,
PETScVector const &  v1,
PETScVector const &  v2,
PETScVector v3 
)

Definition at line 151 of file LinAlg.cpp.

153 {
154  // TODO check sizes
155  assert(&v1 != &v3);
156  if (!v3.getRawVector())
157  v3.shallowCopy(v1);
158  MatMultAdd(A.getRawMatrix(), v1.getRawVector(), v2.getRawVector(),
159  v3.getRawVector());
160 }

References MathLib::PETScMatrix::getRawMatrix(), MathLib::PETScVector::getRawVector(), and MathLib::PETScVector::shallowCopy().

Referenced by NumLib::MatrixTranslatorGeneral< ODESystemTag::FirstOrderImplicitQuasilinear >::computeResidual(), ProcessLib::computeResiduum(), and NumLib::MatrixTranslatorGeneral< ODESystemTag::FirstOrderImplicitQuasilinear >::computeRhs().

◆ norm()

template<typename MatrixOrVector >
double MathLib::LinAlg::norm ( MatrixOrVector const &  x,
MathLib::VecNormType  type 
)

Definition at line 88 of file LinAlg.h.

89 {
90  switch (type) {
92  return norm1(x);
94  return norm2(x);
96  return normMax(x);
97  default:
98  OGS_FATAL("Invalid norm type given.");
99  }
100 }
#define OGS_FATAL(...)
Definition: Error.h:26
double normMax(MatrixOrVector const &x)
Computes the maximum norm of x.
double norm1(MatrixOrVector const &x)
Computes the Manhattan norm of x.
double norm2(MatrixOrVector const &x)
Computes the Euclidean norm of x.

References MathLib::INFINITY_N, norm1(), MathLib::NORM1, norm2(), MathLib::NORM2, normMax(), and OGS_FATAL.

Referenced by LayeredVolume::addLayerBoundaries(), MathLib::calcProjPntToLineAndDists(), NumLib::ConvergenceCriterionDeltaX::checkDeltaX(), NumLib::ConvergenceCriterionResidual::checkDeltaX(), NumLib::ConvergenceCriterionResidual::checkResidual(), NumLib::TimeDiscretization::computeRelativeChangeFromPreviousTimestep(), MathLib::KelvinVector::Invariants< KelvinVectorSize >::equivalentStress(), GeoLib::Polyline::getDistanceAlongPolyline(), GeoLib::Grid< POINT >::getNearestPoint(), MathLib::KelvinVector::Invariants< KelvinVectorSize >::J2(), MathLib::KelvinVector::Invariants< KelvinVectorSize >::J3(), norm1(), norm2(), normMax(), and GeoLib::PointVec::resetInternalDataStructures().

◆ norm1() [1/2]

template<typename MatrixOrVector >
double MathLib::LinAlg::norm1 ( MatrixOrVector const &  x)

Computes the Manhattan norm of x.

◆ norm1() [2/2]

template<>
double MathLib::LinAlg::norm1 ( PETScVector const &  x)

Definition at line 82 of file LinAlg.cpp.

83 {
84  PetscScalar norm = 0.;
85  VecNorm(x.getRawVector(), NORM_1, &norm);
86  return norm;
87 }
double norm(MatrixOrVector const &x, MathLib::VecNormType type)
Definition: LinAlg.h:88

References MathLib::PETScVector::getRawVector(), and norm().

Referenced by norm().

◆ norm2() [1/2]

template<typename MatrixOrVector >
double MathLib::LinAlg::norm2 ( MatrixOrVector const &  x)

Computes the Euclidean norm of x.

◆ norm2() [2/2]

template<>
double MathLib::LinAlg::norm2 ( PETScVector const &  x)

Definition at line 92 of file LinAlg.cpp.

93 {
94  PetscScalar norm = 0.;
95  VecNorm(x.getRawVector(), NORM_2, &norm);
96  return norm;
97 }

References MathLib::PETScVector::getRawVector(), and norm().

Referenced by norm().

◆ normMax() [1/2]

template<typename MatrixOrVector >
double MathLib::LinAlg::normMax ( MatrixOrVector const &  x)

Computes the maximum norm of x.

◆ normMax() [2/2]

template<>
double MathLib::LinAlg::normMax ( PETScVector const &  x)

Definition at line 102 of file LinAlg.cpp.

103 {
104  PetscScalar norm = 0.;
105  VecNorm(x.getRawVector(), NORM_INFINITY, &norm);
106  return norm;
107 }

References MathLib::PETScVector::getRawVector(), and norm().

Referenced by norm().

◆ scale() [1/3]

template<typename MatrixOrVector >
void MathLib::LinAlg::scale ( MatrixOrVector &  x,
double const  a 
)

Scales x by a.

Definition at line 44 of file LinAlg.h.

45 {
46  x *= a;
47 }

◆ scale() [2/3]

void MathLib::LinAlg::scale ( PETScMatrix A,
double const  a 
)

Definition at line 117 of file LinAlg.cpp.

118 {
119  MatScale(A.getRawMatrix(), a);
120 }

References MathLib::PETScMatrix::getRawMatrix().

◆ scale() [3/3]

◆ set()

void MathLib::LinAlg::set ( PETScVector x,
double const  a 
)

Definition at line 32 of file LinAlg.cpp.

33 {
34  VecSet(x.getRawVector(), a);
35 }

References MathLib::PETScVector::getRawVector().

Referenced by ProcessLib::ComponentTransport::intersection(), and ProcessLib::SmallDeformation::writeMaterialForces().

◆ setLocalAccessibleVector()