Skip to content
Snippets Groups Projects
Commit c9bdf7bd authored by Dmitri Naumov's avatar Dmitri Naumov
Browse files

Merge branch 'NumLibMathLibCleanups' into 'master'

NumLib and MathLib PETSc related cleanups

See merge request ogs/ogs!4072
parents b8dfff8f 2058bb6f
No related branches found
No related tags found
No related merge requests found
...@@ -29,7 +29,7 @@ void setLocalAccessibleVector(PETScVector const& x) ...@@ -29,7 +29,7 @@ void setLocalAccessibleVector(PETScVector const& x)
x.setLocalAccessibleVector(); x.setLocalAccessibleVector();
} }
void set(PETScVector& x, double const a) void set(PETScVector& x, PetscScalar const a)
{ {
VecSet(x.getRawVector(), a); VecSet(x.getRawVector(), a);
} }
...@@ -41,27 +41,28 @@ void copy(PETScVector const& x, PETScVector& y) ...@@ -41,27 +41,28 @@ void copy(PETScVector const& x, PETScVector& y)
VecCopy(x.getRawVector(), y.getRawVector()); VecCopy(x.getRawVector(), y.getRawVector());
} }
void scale(PETScVector& x, double const a) void scale(PETScVector& x, PetscScalar const a)
{ {
VecScale(x.getRawVector(), a); VecScale(x.getRawVector(), a);
} }
// y = a*y + X // y = a*y + X
void aypx(PETScVector& y, double const a, PETScVector const& x) void aypx(PETScVector& y, PetscScalar const a, PETScVector const& x)
{ {
// TODO check sizes // TODO check sizes
VecAYPX(y.getRawVector(), a, x.getRawVector()); VecAYPX(y.getRawVector(), a, x.getRawVector());
} }
// y = a*x + y // y = a*x + y
void axpy(PETScVector& y, double const a, PETScVector const& x) void axpy(PETScVector& y, PetscScalar const a, PETScVector const& x)
{ {
// TODO check sizes // TODO check sizes
VecAXPY(y.getRawVector(), a, x.getRawVector()); VecAXPY(y.getRawVector(), a, x.getRawVector());
} }
// y = a*x + b*y // y = a*x + b*y
void axpby(PETScVector& y, double const a, double const b, PETScVector const& x) void axpby(PETScVector& y, PetscScalar const a, PetscScalar const b,
PETScVector const& x)
{ {
// TODO check sizes // TODO check sizes
VecAXPBY(y.getRawVector(), a, b, x.getRawVector()); VecAXPBY(y.getRawVector(), a, b, x.getRawVector());
...@@ -114,13 +115,13 @@ void copy(PETScMatrix const& A, PETScMatrix& B) ...@@ -114,13 +115,13 @@ void copy(PETScMatrix const& A, PETScMatrix& B)
} }
// A = a*A // A = a*A
void scale(PETScMatrix& A, double const a) void scale(PETScMatrix& A, PetscScalar const a)
{ {
MatScale(A.getRawMatrix(), a); MatScale(A.getRawMatrix(), a);
} }
// Y = a*Y + X // Y = a*Y + X
void aypx(PETScMatrix& Y, double const a, PETScMatrix const& X) void aypx(PETScMatrix& Y, PetscScalar const a, PETScMatrix const& X)
{ {
// TODO check sizes // TODO check sizes
// TODO sparsity pattern, currently they are assumed to be different (slow) // TODO sparsity pattern, currently they are assumed to be different (slow)
...@@ -128,7 +129,7 @@ void aypx(PETScMatrix& Y, double const a, PETScMatrix const& X) ...@@ -128,7 +129,7 @@ void aypx(PETScMatrix& Y, double const a, PETScMatrix const& X)
} }
// Y = a*X + Y // Y = a*X + Y
void axpy(PETScMatrix& Y, double const a, PETScMatrix const& X) void axpy(PETScMatrix& Y, PetscScalar const a, PETScMatrix const& X)
{ {
// TODO check sizes // TODO check sizes
// TODO sparsity pattern, currently they are assumed to be different (slow) // TODO sparsity pattern, currently they are assumed to be different (slow)
......
...@@ -14,6 +14,10 @@ ...@@ -14,6 +14,10 @@
#include "BaseLib/Error.h" #include "BaseLib/Error.h"
#include "LinAlgEnums.h" #include "LinAlgEnums.h"
#ifdef USE_PETSC
#include <petscsystypes.h>
#endif
namespace MathLib namespace MathLib
{ {
...@@ -40,29 +44,30 @@ void copy(MatrixOrVector const& x, MatrixOrVector& y) ...@@ -40,29 +44,30 @@ void copy(MatrixOrVector const& x, MatrixOrVector& y)
} }
//! Scales \c x by \c a //! Scales \c x by \c a
template<typename MatrixOrVector> template <typename MatrixOrVector>
void scale(MatrixOrVector& x, double const a) void scale(MatrixOrVector& x, double const a)
{ {
x *= a; x *= a;
} }
//! Computes \f$ y = a \cdot y + x \f$. //! Computes \f$ y = a \cdot y + x \f$.
template<typename MatrixOrVector> template <typename MatrixOrVector>
void aypx(MatrixOrVector& y, double const a, MatrixOrVector const& x) void aypx(MatrixOrVector& y, double const a, MatrixOrVector const& x)
{ {
y = a*y + x; y = a*y + x;
} }
//! Computes \f$ y = a \cdot x + y \f$. //! Computes \f$ y = a \cdot x + y \f$.
template<typename MatrixOrVector> template <typename MatrixOrVector>
void axpy(MatrixOrVector& y, double const a, MatrixOrVector const& x) void axpy(MatrixOrVector& y, double const a, MatrixOrVector const& x)
{ {
y += a*x; y += a*x;
} }
//! Computes \f$ y = a \cdot x + b \cdot y \f$. //! Computes \f$ y = a \cdot x + b \cdot y \f$.
template<typename MatrixOrVector> template <typename MatrixOrVector>
void axpby(MatrixOrVector& y, double const a, double const b, MatrixOrVector const& x) void axpby(MatrixOrVector& y, double const a, double const b,
MatrixOrVector const& x)
{ {
y = a*x + b*y; y = a*x + b*y;
} }
...@@ -150,35 +155,34 @@ namespace LinAlg ...@@ -150,35 +155,34 @@ namespace LinAlg
/// Call this before call operator[] or get(...) of x. /// Call this before call operator[] or get(...) of x.
void setLocalAccessibleVector(PETScVector const& x); void setLocalAccessibleVector(PETScVector const& x);
void set(PETScVector& x, double const a); void set(PETScVector& x, PetscScalar const a);
void copy(PETScVector const& x, PETScVector& y); void copy(PETScVector const& x, PETScVector& y);
void scale(PETScVector& x, double const a); void scale(PETScVector& x, PetscScalar const a);
// y = a*y + X // y = a*y + X
void aypx(PETScVector& y, double const a, PETScVector const& x); void aypx(PETScVector& y, PetscScalar const a, PETScVector const& x);
// y = a*x + y // y = a*x + y
void axpy(PETScVector& y, double const a, PETScVector const& x); void axpy(PETScVector& y, PetscScalar const a, PETScVector const& x);
// y = a*x + y // y = a*x + y
void axpby(PETScVector& y, double const a, double const b, PETScVector const& x); void axpby(PETScVector& y, PetscScalar const a, PetscScalar const b,
PETScVector const& x);
// Matrix // Matrix
void copy(PETScMatrix const& A, PETScMatrix& B); void copy(PETScMatrix const& A, PETScMatrix& B);
// A = a*A // A = a*A
void scale(PETScMatrix& A, double const a); void scale(PETScMatrix& A, PetscScalar const a);
// Y = a*Y + X // Y = a*Y + X
void aypx(PETScMatrix& Y, double const a, PETScMatrix const& X); void aypx(PETScMatrix& Y, PetscScalar const a, PETScMatrix const& X);
// Y = a*X + Y // Y = a*X + Y
void axpy(PETScMatrix& Y, double const a, PETScMatrix const& X); void axpy(PETScMatrix& Y, PetscScalar const a, PETScMatrix const& X);
// Matrix and Vector // Matrix and Vector
......
...@@ -314,7 +314,7 @@ void PETScVector::shallowCopy(const PETScVector& v) ...@@ -314,7 +314,7 @@ void PETScVector::shallowCopy(const PETScVector& v)
has_ghost_id_ = v.has_ghost_id_; has_ghost_id_ = v.has_ghost_id_;
global_ids2local_ids_ghost_ = v.global_ids2local_ids_ghost_; global_ids2local_ids_ghost_ = v.global_ids2local_ids_ghost_;
VecSetOption(v_, VEC_IGNORE_NEGATIVE_INDICES, PETSC_TRUE); config();
} }
void finalizeVectorAssembly(PETScVector& vec) void finalizeVectorAssembly(PETScVector& vec)
......
...@@ -47,4 +47,28 @@ double computeRelativeChangeFromPreviousTimestep(GlobalVector const& x, ...@@ -47,4 +47,28 @@ double computeRelativeChangeFromPreviousTimestep(GlobalVector const& x,
// Only norm_x is close to zero // Only norm_x is close to zero
return norm_dx / std::numeric_limits<double>::epsilon(); return norm_dx / std::numeric_limits<double>::epsilon();
} }
void TimeDiscretization::getXdot(GlobalVector const& x_at_new_timestep,
GlobalVector const& x_old,
GlobalVector& xdot) const
{
namespace LinAlg = MathLib::LinAlg;
double const dt = getCurrentTimeIncrement();
// xdot = 1/dt * x_at_new_timestep - x_old
getWeightedOldX(xdot, x_old);
LinAlg::axpby(xdot, 1. / dt, -1.0, x_at_new_timestep);
}
void BackwardEuler::getWeightedOldX(GlobalVector& y,
GlobalVector const& x_old) const
{
namespace LinAlg = MathLib::LinAlg;
// y = x_old / delta_t
LinAlg::copy(x_old, y);
LinAlg::scale(y, 1.0 / _delta_t);
}
} // end of namespace NumLib } // end of namespace NumLib
...@@ -132,16 +132,7 @@ public: ...@@ -132,16 +132,7 @@ public:
//! \f$. //! \f$.
void getXdot(GlobalVector const& x_at_new_timestep, void getXdot(GlobalVector const& x_at_new_timestep,
GlobalVector const& x_old, GlobalVector const& x_old,
GlobalVector& xdot) const GlobalVector& xdot) const;
{
namespace LinAlg = MathLib::LinAlg;
double const dt = getCurrentTimeIncrement();
// xdot = 1/dt * x_at_new_timestep - x_old
getWeightedOldX(xdot, x_old);
LinAlg::axpby(xdot, 1. / dt, -1.0, x_at_new_timestep);
}
//! Returns \f$ x_O \f$. //! Returns \f$ x_O \f$.
virtual void getWeightedOldX( virtual void getWeightedOldX(
...@@ -155,7 +146,6 @@ class BackwardEuler final : public TimeDiscretization ...@@ -155,7 +146,6 @@ class BackwardEuler final : public TimeDiscretization
{ {
public: public:
void setInitialState(const double t0) override { _t = t0; } void setInitialState(const double t0) override { _t = t0; }
void nextTimestep(const double t, const double delta_t) override void nextTimestep(const double t, const double delta_t) override
{ {
_t = t; _t = t;
...@@ -165,14 +155,7 @@ public: ...@@ -165,14 +155,7 @@ public:
double getCurrentTime() const override { return _t; } double getCurrentTime() const override { return _t; }
double getCurrentTimeIncrement() const override { return _delta_t; } double getCurrentTimeIncrement() const override { return _delta_t; }
void getWeightedOldX(GlobalVector& y, void getWeightedOldX(GlobalVector& y,
GlobalVector const& x_old) const override GlobalVector const& x_old) const override;
{
namespace LinAlg = MathLib::LinAlg;
// y = x_old / delta_t
LinAlg::copy(x_old, y);
LinAlg::scale(y, 1.0 / _delta_t);
}
private: private:
double _t = std::numeric_limits<double>::quiet_NaN(); //!< \f$ t_C \f$ double _t = std::numeric_limits<double>::quiet_NaN(); //!< \f$ t_C \f$
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment