diff --git a/README.md b/README.md index 188f272984ad6dd7ee24049da2fb2b037d8d13be..bab143f80e0bfc932677d43a9337c03f9273e24a 100644 --- a/README.md +++ b/README.md @@ -56,5 +56,5 @@ with the same terms as OpenFOAM itself. [Hypre]: https://computing.llnl.gov/projects/hypre-scalable-linear-solvers-multigrid-methods [Petsc]: https://www.mcs.anl.gov/petsc/ -[Petsc-Installation]: etc/README.md +[Petsc-Installation]: doc/README.md [Petsc-Ext-Installation]: https://www.mcs.anl.gov/petsc/documentation/installation.html diff --git a/etc/README.md b/doc/README.md similarity index 100% rename from etc/README.md rename to doc/README.md diff --git a/src/Allwclean b/src/Allwclean index 734938b0f5c6c54bbfcaea78cb958c369bd02d28..004019adfb06cd1a659c431e7fedefc5cb4b59aa 100755 --- a/src/Allwclean +++ b/src/Allwclean @@ -2,6 +2,6 @@ cd "${0%/*}" || exit # Run from this directory #------------------------------------------------------------------------------ -petsc4Foam/Allwclean +petsc4Foam/Allwclean $* #------------------------------------------------------------------------------ diff --git a/src/Allwmake b/src/Allwmake index 56da38c38e5e9d086a66ad470c7e155cdc2139a0..20d18c3a24358f330db815ef65b0df34636285ea 100755 --- a/src/Allwmake +++ b/src/Allwmake @@ -2,6 +2,6 @@ cd "${0%/*}" || exit # Run from this directory #------------------------------------------------------------------------------ -petsc4Foam/Allwmake +petsc4Foam/Allwmake $* #------------------------------------------------------------------------------ diff --git a/src/petsc4Foam/Make/files b/src/petsc4Foam/Make/files index eee14d3027cce414336328916f72458d78c3e0e4..bd45c6fef2656c3b43f9e4f05c68560af7eaafe8 100644 --- a/src/petsc4Foam/Make/files +++ b/src/petsc4Foam/Make/files @@ -1,4 +1,4 @@ -csrMatrix/solvers/petscSolver.C +solvers/petscSolver.C utils/petscUtils.C utils/petscControls.C utils/petscLinearSolverContexts.C diff --git a/src/petsc4Foam/csrMatrix/solvers/petscSolver.C b/src/petsc4Foam/solvers/petscSolver.C similarity index 93% rename from src/petsc4Foam/csrMatrix/solvers/petscSolver.C rename to src/petsc4Foam/solvers/petscSolver.C index c1102054cb10068ede517d2a0e9f419061aa8036..0c0e09b608b7e1cceb8a0da58215b9e28d9c9320 100644 --- a/src/petsc4Foam/csrMatrix/solvers/petscSolver.C +++ b/src/petsc4Foam/solvers/petscSolver.C @@ -38,6 +38,7 @@ License #include "petscLinearSolverContexts.H" #include "petscUtils.H" #include "petscWrappedVector.H" +#include <cstring> // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * // @@ -125,7 +126,7 @@ Foam::solverPerformance Foam::petscSolver::solve const bool verbose ( - petscDict_.lookupOrDefault("verbose", false) + petscDict_.getOrDefault("verbose", false) ); dictionary petscDictOptions = petscDict_.subOrEmptyDict("options"); @@ -157,43 +158,31 @@ Foam::solverPerformance Foam::petscSolver::solve if (!ctx.initialized()) { - if (debug) - { - Info<< "Initializing PETSc Linear Solver " << eqName_ << nl; - } + DebugInfo<< "Initializing PETSc Linear Solver " << eqName_ << nl; ctx.initialized() = true; needsCacheUpdate = false; - computeMatAllocation(Amat, - lowNonZero, - maxLowNonZeroPerRow); - buildMat(Amat, - lowNonZero, - maxLowNonZeroPerRow); + computeMatAllocation(Amat, lowNonZero, maxLowNonZeroPerRow); + buildMat(Amat, lowNonZero, maxLowNonZeroPerRow); buildKsp(Amat, ksp); } if (ctx.caching.needsMatrixUpdate() && needsCacheUpdate) { - buildMat(Amat, - lowNonZero, - maxLowNonZeroPerRow); + buildMat(Amat, lowNonZero, maxLowNonZeroPerRow); updateKsp(ksp, Amat, !ctx.caching.needsPrecondUpdate()); } // Solver name from petsc KSPType ksptype; KSPGetType(ksp, &ksptype); - const word solverName - ( - ksptype - ); + const word solverName(ksptype); // Use built-in residual norm computation const bool usePetscResidualNorm ( - petscDict_.lookupOrDefault("use_petsc_residual_norm", false) + petscDict_.getOrDefault("use_petsc_residual_norm", false) ); // Setup class containing solver performance data @@ -259,10 +248,7 @@ void Foam::petscSolver::updateKsp const bool reuse ) const { - if (debug) - { - Info<< "Cache-Hit: updating PETSc Amat " << eqName_ << nl; - } + DebugInfo<< "Cache-Hit: updating PETSc Amat " << eqName_ << nl; if (reuse) { @@ -270,10 +256,7 @@ void Foam::petscSolver::updateKsp } else { - if (debug) - { - Info<< "Cache-Hit: updating PETSc Pmat " << eqName_ << nl; - } + DebugInfo<< "Cache-Hit: updating PETSc Pmat " << eqName_ << nl; KSPSetReusePreconditioner(ksp, PETSC_FALSE); } @@ -304,7 +287,7 @@ void Foam::petscSolver::buildKsp const bool verbose ( - petscDict_.lookupOrDefault("verbose", false) + petscDict_.getOrDefault("verbose", false) ); // Solver name from petsc dictionary @@ -388,7 +371,7 @@ void Foam::petscSolver::buildKsp maxIter_ ); - // Use the solution from the previous timestep if the solver is not preonly + // Use solution from the previous timestep if the solver is not 'preonly' if (solverName != "preonly") { KSPSetInitialGuessNonzero(ksp, PETSC_TRUE); @@ -498,16 +481,16 @@ void Foam::petscSolver::computeMatAllocation ( Amat, 1, - nonZero_.data(), // on-processor - nonZeroNeigh_.data(), // off-processor + nonZero_.data(), // on-processor + nonZeroNeigh_.data(), // off-processor nonZero_.data(), nonZeroNeigh_.data() ); MatMPISELLSetPreallocation ( Amat, - 0, nonZero_.data(), // on-processor - 0, nonZeroNeigh_.data() // off-processor + 0, nonZero_.data(), // on-processor + 0, nonZeroNeigh_.data() // off-processor ); // set the matrix options diff --git a/src/petsc4Foam/csrMatrix/solvers/petscSolver.H b/src/petsc4Foam/solvers/petscSolver.H similarity index 98% rename from src/petsc4Foam/csrMatrix/solvers/petscSolver.H rename to src/petsc4Foam/solvers/petscSolver.H index 8f2e07aaddc51ef8c944e61df09868060bb24fd6..a15f28177e132206501b7ad83f738e079edd15b5 100644 --- a/src/petsc4Foam/csrMatrix/solvers/petscSolver.H +++ b/src/petsc4Foam/solvers/petscSolver.H @@ -58,10 +58,12 @@ SourceFiles \*---------------------------------------------------------------------------*/ -#ifndef petscSolver_H -#define petscSolver_H +#ifndef petscFoamSolver_H +#define petscFoamSolver_H #include "lduMatrix.H" + +// PETSc #include "petscmat.h" #include "petscksp.h" diff --git a/src/petsc4Foam/utils/petscCacheManager.H b/src/petsc4Foam/utils/petscCacheManager.H index d05fdc2a67511141db97623e1e77d2545faadda0..4a4b35008a71bc274b2c26135bf91f78502021d0 100644 --- a/src/petsc4Foam/utils/petscCacheManager.H +++ b/src/petsc4Foam/utils/petscCacheManager.H @@ -61,8 +61,8 @@ SourceFiles \*---------------------------------------------------------------------------*/ -#ifndef petscCacheManager_H -#define petscCacheManager_H +#ifndef petscFoamCacheManager_H +#define petscFoamCacheManager_H #include "Enum.H" #include "clockValue.H" @@ -72,7 +72,6 @@ SourceFiles namespace Foam { - namespace PetscUtils { @@ -82,29 +81,52 @@ namespace PetscUtils struct Caching { - //- Supported caching types - enum cachingTypes - { - None, - Always, - Periodic, - Adaptive - }; + // Public Data Types + + //- Supported caching types + enum cachingTypes + { + None, + Always, + Periodic, + Adaptive + }; + + + //- Names for selectable caching types + static const Enum<cachingTypes> cachingTypeNames_; + + + // Member Data + + cachingTypes updateType_; + + int updateFreq_; + + //- Elapsed time (s) for current iteration + double timeIter; + + //- Elapsed time (s) for first iteration + double time0; + + //- Elapsed time (s) for second iteration + double time1; + //- Timer value + clockValue timer_; - //- Names for selectable caching types - static const Enum<cachingTypes> cachingTypeNames_; + // Constructors - //- Default construct - Caching() - : - updateType_{cachingTypes::None}, - updateFreq_{1}, - time{0}, - time0{0}, - time1{0} - {} + //- Default construct. No caching + Caching() + : + updateType_{cachingTypes::None}, + updateFreq_{1}, + timeIter{0}, + time0{0}, + time1{0} + {} void init(const word& key, const dictionary& dict) @@ -124,24 +146,6 @@ struct Caching updateFreq_ = coeffsDict.getOrDefault<int>("frequency", 1); } } - - - // Member Data - - cachingTypes updateType_; - - int updateFreq_; - - //- Elapsed time (s) for current iteration - double time; - - //- Elapsed time (s) for first iteration - double time0; - - //- Elapsed time (s) for second iteration - double time1; - - clockValue timer_; }; } // End namespace PetscUtils @@ -210,15 +214,15 @@ public: if (precondCaching.updateType_ == PetscUtils::Caching::Adaptive) { // Elapsed timing interval (s) - precondCaching.time = precondCaching.timer_.elapsed(); + precondCaching.timeIter = precondCaching.timer_.elapsed(); if (iter == 0) { - precondCaching.time0 = precondCaching.time; + precondCaching.time0 = precondCaching.timeIter; } else if (iter == 1) { - precondCaching.time1 = precondCaching.time; + precondCaching.time1 = precondCaching.timeIter; } } @@ -269,8 +273,12 @@ private: { if (iter > 3) // we need at least three times { - double ratio0 = precondCaching.time0 / precondCaching.time; - double ratio1 = precondCaching.time1 / precondCaching.time; + const double ratio0 = + precondCaching.time0 / precondCaching.timeIter; + + const double ratio1 = + precondCaching.time1 / precondCaching.timeIter; + int nsteps = min(1e5, ratio0 * (1. / mag(1. - ratio1 + 1e-6))); diff --git a/src/petsc4Foam/utils/petscControls.H b/src/petsc4Foam/utils/petscControls.H index 9ba47f3756fb83f71f0db47da5a3f1feeb4a1104..ea0891d3bda28c0fa3bc067e5050fdf3d164baf8 100644 --- a/src/petsc4Foam/utils/petscControls.H +++ b/src/petsc4Foam/utils/petscControls.H @@ -36,8 +36,8 @@ SourceFiles \*---------------------------------------------------------------------------*/ -#ifndef petscControls_H -#define petscControls_H +#ifndef petscFoamControls_H +#define petscFoamControls_H #include "IOdictionary.H" #include "MeshObject.H" @@ -50,7 +50,6 @@ namespace Foam // Forward Declarations class Time; - /*---------------------------------------------------------------------------*\ Class petscControls Declaration \*---------------------------------------------------------------------------*/ @@ -68,7 +67,7 @@ class petscControls public: - // Declare name of the class and its debug switch + //- Runtime information TypeName("petscControlDict"); @@ -95,7 +94,7 @@ public: static void stop(); //- Test if loaded - inline bool valid() const + bool valid() const { return loaded_; } diff --git a/src/petsc4Foam/utils/petscLinearSolverContext.H b/src/petsc4Foam/utils/petscLinearSolverContext.H index ed65c0b7a3e9c7be21bdee859197bbd2e06bbf7e..75b1047ade61ebfecaaa4cd3d718809180dd5769 100644 --- a/src/petsc4Foam/utils/petscLinearSolverContext.H +++ b/src/petsc4Foam/utils/petscLinearSolverContext.H @@ -34,15 +34,17 @@ SourceFiles \*---------------------------------------------------------------------------*/ -#ifndef petscLinearSolverContext_H -#define petscLinearSolverContext_H +#ifndef petscFoamLinearSolverContext_H +#define petscFoamLinearSolverContext_H #include "solverPerformance.H" #include "petscCacheManager.H" +// PETSc #include "petscvec.h" #include "petscmat.h" #include "petscksp.h" + // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // namespace Foam @@ -82,7 +84,7 @@ public: // Constructors - //- Construct null + //- Default construct petscLinearSolverContext() : init_(false), diff --git a/src/petsc4Foam/utils/petscLinearSolverContexts.H b/src/petsc4Foam/utils/petscLinearSolverContexts.H index e9942dc3b0097a76c6facba9037553a4914ab9fc..04bb86b46ab482c6f39f57a1d35579a73aeb6d41 100644 --- a/src/petsc4Foam/utils/petscLinearSolverContexts.H +++ b/src/petsc4Foam/utils/petscLinearSolverContexts.H @@ -34,13 +34,13 @@ SourceFiles \*---------------------------------------------------------------------------*/ -#ifndef petscLinearSolverContexts_H -#define petscLinearSolverContexts_H +#ifndef petscFoamLinearSolverContexts_H +#define petscFoamLinearSolverContexts_H #include "fvMesh.H" #include "MeshObject.H" -#include "petscLinearSolverContext.H" #include "HashPtrTable.H" +#include "petscLinearSolverContext.H" // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // @@ -63,18 +63,19 @@ class petscLinearSolverContexts // Private Data //- Linear solver contexts - HashPtrTable<petscLinearSolverContext> contexts_; + mutable HashPtrTable<petscLinearSolverContext> contexts_; public: + //- Runtime information TypeName("petscLinearSolverContext"); // Constructors //- Construct from mesh for given field - petscLinearSolverContexts(const fvMesh& mesh) + explicit petscLinearSolverContexts(const fvMesh& mesh) : MeshObject < @@ -95,11 +96,7 @@ public: //- Linear solver contexts (mutable) HashPtrTable<petscLinearSolverContext>& contexts() const { - return - const_cast - < - HashPtrTable<petscLinearSolverContext>& - >(contexts_); + return contexts_; } @@ -110,7 +107,7 @@ public: } - //- Get existing or a new context + //- Get existing or a new context by name petscLinearSolverContext& getContext(const word& name) const { auto iter = contexts().find(name); @@ -119,23 +116,20 @@ public: if (!iter.found()) { - contexts().set(name, new petscLinearSolverContext()); + contexts().set(name, new petscLinearSolverContext{}); iter = contexts().find(name); } - if (debug) - { - Info - << "cache-" << (cached ? "hit" : "miss") - << " PETSc linear solver context " - << name << " mesh: " << mesh().name() << nl; - } + DebugInfo + << "cache-" << (cached ? "hit" : "miss") + << " PETSc linear solver context " + << name << " mesh: " << mesh().name() << nl; return *(*iter); } - //- Remove context + //- Remove context by name bool removeContext(const word& name) const { return contexts().erase(name); diff --git a/src/petsc4Foam/utils/petscUtils.C b/src/petsc4Foam/utils/petscUtils.C index ab8710b0f2b294f96c2d8be09818532982c2f2f2..670e73adc43adb99361d458f6b901e6484383a18 100644 --- a/src/petsc4Foam/utils/petscUtils.C +++ b/src/petsc4Foam/utils/petscUtils.C @@ -26,13 +26,12 @@ License \*---------------------------------------------------------------------------*/ -#include <algorithm> -#include <cstring> - #include "lduMatrix.H" #include "error.H" #include "petscUtils.H" #include "petscLinearSolverContext.H" +#include <algorithm> + // * * * * * * * * * * * * * * * Global Functions * * * * * * * * * * * * * // Foam::scalar Foam::gAverage(Vec input) @@ -294,7 +293,7 @@ void Foam::PetscUtils::setFlag Info<< key << ' ' << val << nl; } - PetscOptionsSetValue(NULL, key.c_str(), val.c_str()); + PetscOptionsSetValue(nullptr, key.c_str(), val.c_str()); } @@ -315,7 +314,7 @@ void Foam::PetscUtils::setFlags Info<< key << ' ' << val << nl; } - PetscOptionsSetValue(NULL, key.c_str(), val.c_str()); + PetscOptionsSetValue(nullptr, key.c_str(), val.c_str()); } } diff --git a/src/petsc4Foam/utils/petscUtils.H b/src/petsc4Foam/utils/petscUtils.H index 02095bfaedda39f3d731d6b8cb9c7ac0ab886ed5..8ed301035b2250a36a1756a0811efb8e1dab6540 100644 --- a/src/petsc4Foam/utils/petscUtils.H +++ b/src/petsc4Foam/utils/petscUtils.H @@ -31,12 +31,12 @@ Description Utilities for OpenFOAM / PETSc interaction SourceFiles - PetscUtils.C + petscUtils.C \*---------------------------------------------------------------------------*/ -#ifndef PetscUtils_H -#define PetscUtils_H +#ifndef petscFoamUtils_H +#define petscFoamUtils_H // OpenFOAM #include "scalarField.H" diff --git a/src/petsc4Foam/utils/petscWrappedVector.H b/src/petsc4Foam/utils/petscWrappedVector.H index 75cc4c3447872a4aca58be8aca26a6816f82510f..3189108d397c42320eb072c6690e4834ccdc56de 100644 --- a/src/petsc4Foam/utils/petscWrappedVector.H +++ b/src/petsc4Foam/utils/petscWrappedVector.H @@ -30,12 +30,11 @@ Description Wrap an OpenFOAM list as a PETSc vector SourceFiles - PetscWrappedVector.H \*---------------------------------------------------------------------------*/ -#ifndef PetscWrappedVector_H -#define PetscWrappedVector_H +#ifndef petscFoamWrappedVector_H +#define petscFoamWrappedVector_H // OpenFOAM #include "List.H" @@ -63,6 +62,7 @@ class PetscWrappedVector // Private Member Functions + //- Wrap an OpenFOAM list of values as a PETSc vector template<class T> void createImpl @@ -72,7 +72,11 @@ class PetscWrappedVector ) { MatCreateVecs(mat, &vec_, PETSC_NULL); - VecPlaceArray(vec_, reinterpret_cast<scalar*>(const_cast<T*>(list.cdata()))); + VecPlaceArray + ( + vec_, + reinterpret_cast<scalar*>(const_cast<T*>(list.cdata())) + ); } //- Wrap an OpenFOAM list of values as a PETSc vector @@ -100,6 +104,7 @@ class PetscWrappedVector //- No copy assignment void operator=(const PetscWrappedVector&) = delete; + public: // Constructors @@ -140,6 +145,7 @@ public: createImpl(list, comm); } + //- Destructor ~PetscWrappedVector() {