From 75272ad78f5872a82a1756526f72b64c3d509cfa Mon Sep 17 00:00:00 2001 From: Hrvoje Jasak Date: Wed, 11 May 2016 21:55:22 +0100 Subject: [PATCH] Moved cacheing control to solution for consistency --- .../finiteVolume/fvSchemes/fvSchemes.C | 34 +---------- .../finiteVolume/fvSchemes/fvSchemes.H | 4 -- .../gradSchemes/gradScheme/gradScheme.C | 61 ++++--------------- src/foam/matrices/solution/solution.C | 26 ++++++++ src/foam/matrices/solution/solution.H | 33 +++++++++- .../simpleFoam/motorBike/system/fvSchemes | 9 --- .../simpleFoam/motorBike/system/fvSolution | 19 +++--- 7 files changed, 82 insertions(+), 104 deletions(-) diff --git a/src/finiteVolume/finiteVolume/fvSchemes/fvSchemes.C b/src/finiteVolume/finiteVolume/fvSchemes/fvSchemes.C index 63afcda3e..6a8da9ea4 100644 --- a/src/finiteVolume/finiteVolume/fvSchemes/fvSchemes.C +++ b/src/finiteVolume/finiteVolume/fvSchemes/fvSchemes.C @@ -56,7 +56,6 @@ void Foam::fvSchemes::clear() defaultLaplacianScheme_.clear(); fluxRequired_.clear(); defaultFluxRequired_ = false; - cacheFields_.clear(); } // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // @@ -174,15 +173,7 @@ Foam::fvSchemes::fvSchemes(const objectRegistry& obr) tokenList() )() ), - defaultFluxRequired_(false), - cacheFields_ - ( - ITstream - ( - objectPath() + "::cacheFields", - tokenList() - )() - ) + defaultFluxRequired_(false) { if (!headerOk()) { @@ -408,11 +399,6 @@ bool Foam::fvSchemes::read() } } - if (dict.found("cacheFields")) - { - cacheFields_ = dict.subDict("cacheFields"); - } - return true; } else @@ -590,22 +576,4 @@ bool Foam::fvSchemes::fluxRequired(const word& name) const } -bool Foam::fvSchemes::cache(const word& name) const -{ - if (debug) - { - Info<< "Lookup cache for " << name << endl; - } - - if (cacheFields_.found(name)) - { - return true; - } - else - { - return false; - } -} - - // ************************************************************************* // diff --git a/src/finiteVolume/finiteVolume/fvSchemes/fvSchemes.H b/src/finiteVolume/finiteVolume/fvSchemes/fvSchemes.H index 2ad2cde02..8322d0b57 100644 --- a/src/finiteVolume/finiteVolume/fvSchemes/fvSchemes.H +++ b/src/finiteVolume/finiteVolume/fvSchemes/fvSchemes.H @@ -79,8 +79,6 @@ class fvSchemes dictionary fluxRequired_; bool defaultFluxRequired_; - dictionary cacheFields_; - // Private Member Functions @@ -128,8 +126,6 @@ public: bool fluxRequired(const word& name) const; - bool cache(const word& name) const; - // Edit diff --git a/src/finiteVolume/finiteVolume/gradSchemes/gradScheme/gradScheme.C b/src/finiteVolume/finiteVolume/gradSchemes/gradScheme/gradScheme.C index 0dfe7e70e..0501e5d94 100644 --- a/src/finiteVolume/finiteVolume/gradSchemes/gradScheme/gradScheme.C +++ b/src/finiteVolume/finiteVolume/gradSchemes/gradScheme/gradScheme.C @@ -118,28 +118,16 @@ Foam::fv::gradScheme::grad typedef typename outerProduct::type GradType; typedef GeometricField GradFieldType; - if (!this->mesh().changing() && this->mesh().schemesDict().cache(name)) + if (!this->mesh().changing() && this->mesh().solutionDict().cache(name)) { if (!mesh().objectRegistry::template foundObject(name)) { - if (fvSchemes::debug) - { - Info<< "Cache: Calculating and caching " << name - << " originating from " << vsf.name() - << " event No. " << vsf.eventNo() - << endl; - } + solution::cachePrintMessage("Calculating and caching", name, vsf); tmp tgGrad = calcGrad(vsf, name); regIOobject::store(tgGrad.ptr()); } - if (fvSchemes::debug) - { - Info<< "Cache: Retrieving " << name - << " originating from " << vsf.name() - << " event No. " << vsf.eventNo(); - } - + solution::cachePrintMessage("Retrieving", name, vsf); GradFieldType& gGrad = const_cast ( mesh().objectRegistry::template lookupObject(name) @@ -147,7 +135,7 @@ Foam::fv::gradScheme::grad if (gGrad.upToDate(vsf.name())) { - if (fvSchemes::debug) + if (solution::debug) { Info<< ": up-to-date." << endl; } @@ -156,24 +144,15 @@ Foam::fv::gradScheme::grad } else { - if (fvSchemes::debug) + if (solution::debug) { - Info<< ": not up-to-date." << nl - << "Cache: Deleting " << name - << " originating from " << vsf.name() - << " event No. " << vsf.eventNo() - << endl; + Info<< ": not up-to-date." << endl; + solution::cachePrintMessage("Deleting", name, vsf); } gGrad.release(); delete &gGrad; - if (fvSchemes::debug) - { - Info<< "Cache: Recalculating " << name - << " originating from " << vsf.name() - << " event No. " << vsf.eventNo() - << endl; - } + solution::cachePrintMessage("Recalculating", name, vsf); tmp tgGrad = calcGrad(vsf, name); // Note: in order for the patchNeighbourField to be @@ -182,13 +161,7 @@ Foam::fv::gradScheme::grad // shall be moved into the library fvc operators tgGrad().correctBoundaryConditions(); - if (fvSchemes::debug) - { - Info<< "Cache: Storing " << name - << " originating from " << vsf.name() - << " event No. " << vsf.eventNo() - << endl; - } + solution::cachePrintMessage("Storing", name, vsf); regIOobject::store(tgGrad.ptr()); GradFieldType& gGrad = const_cast ( @@ -215,25 +188,13 @@ Foam::fv::gradScheme::grad if (gGrad.ownedByRegistry()) { - if (fvSchemes::debug) - { - Info<< "Cache: Deleting " << name - << " originating from " << vsf.name() - << " event No. " << vsf.eventNo() - << endl; - } + solution::cachePrintMessage("Deleting", name, vsf); gGrad.release(); delete &gGrad; } } - if (fvSchemes::debug) - { - Info<< "Cache: Calculating " << name - << " originating from " << vsf.name() - << " event No. " << vsf.eventNo() - << endl; - } + solution::cachePrintMessage("Calculating", name, vsf); return calcGrad(vsf, name); } } diff --git a/src/foam/matrices/solution/solution.C b/src/foam/matrices/solution/solution.C index 1d63c7ce8..f78a54fc4 100644 --- a/src/foam/matrices/solution/solution.C +++ b/src/foam/matrices/solution/solution.C @@ -64,6 +64,8 @@ Foam::solution::solution(const objectRegistry& obr, const fileName& dictName) IOobject::NO_WRITE ) ), + cache_(dictionary::null), + caching_(false), relaxationFactors_ ( ITstream("relaxationFactors", tokenList())() @@ -179,6 +181,12 @@ bool Foam::solution::read() if (readOk) { + if (found("cache")) + { + cache_ = subDict("cache"); + caching_ = cache_.lookupOrDefault("active", true); + } + if (found("relaxationFactors")) { relaxationFactors_ = subDict("relaxationFactors"); @@ -199,6 +207,24 @@ bool Foam::solution::read() } +bool Foam::solution::cache(const word& name) const +{ + if (caching_) + { + if (debug) + { + Info<< "Cache: find entry for " << name << endl; + } + + return cache_.found(name); + } + else + { + return false; + } +} + + bool Foam::solution::relax(const word& name) const { if (debug) diff --git a/src/foam/matrices/solution/solution.H b/src/foam/matrices/solution/solution.H index fe92b0156..00e8e1fb3 100644 --- a/src/foam/matrices/solution/solution.H +++ b/src/foam/matrices/solution/solution.H @@ -53,6 +53,12 @@ class solution { // Private data + //- Dictionary of temporary fields to cache + dictionary cache_; + + //- Switch for the caching mechanism + bool caching_; + //- Dictionary of relaxation factors for all the fields dictionary relaxationFactors_; @@ -65,6 +71,9 @@ class solution // Private Member Functions + //- Read settings from the dictionary + void read(const dictionary&); + //- Disallow default bitwise copy construct and assignment solution(const solution&); void operator=(const solution&); @@ -84,13 +93,29 @@ public: // Constructors //- Construct for given objectRegistry and dictionary - solution(const objectRegistry& obr, const fileName& dictName); + solution + ( + const objectRegistry& obr, + const fileName& dictName + ); // Member Functions // Access + //- Return true if the given field should be cached + bool cache(const word& name) const; + + //- Helper for printing cache message + template + static void cachePrintMessage + ( + const char* message, + const word& name, + const FieldType& vf + ); + //- Return true if the relaxation factor is given for the field bool relax(const word& name) const; @@ -138,6 +163,12 @@ public: // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // +#ifdef NoRepository +# include "solutionTemplates.C" +#endif + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + #endif // ************************************************************************* // diff --git a/tutorials/incompressible/simpleFoam/motorBike/system/fvSchemes b/tutorials/incompressible/simpleFoam/motorBike/system/fvSchemes index b48fa437e..85c243cd4 100644 --- a/tutorials/incompressible/simpleFoam/motorBike/system/fvSchemes +++ b/tutorials/incompressible/simpleFoam/motorBike/system/fvSchemes @@ -40,8 +40,6 @@ divSchemes laplacianSchemes { default Gauss linear corrected; -// default Gauss linear limited 0.5; -// default Gauss linear limited 0.333; } interpolationSchemes @@ -61,12 +59,5 @@ fluxRequired p; } -cacheFields -{ - grad(U); - grad(p); - grad(k); - grad(omega); -} // ************************************************************************* // diff --git a/tutorials/incompressible/simpleFoam/motorBike/system/fvSolution b/tutorials/incompressible/simpleFoam/motorBike/system/fvSolution index 61742eba9..f4e0c6dd4 100644 --- a/tutorials/incompressible/simpleFoam/motorBike/system/fvSolution +++ b/tutorials/incompressible/simpleFoam/motorBike/system/fvSolution @@ -28,8 +28,7 @@ solvers agglomerator faceAreaPair; nCellsInCoarsestLevel 10; mergeLevels 1; - }; - + } U { solver smoothSolver; @@ -37,8 +36,7 @@ solvers tolerance 1e-8; relTol 0.1; nSweeps 1; - }; - + } k { solver smoothSolver; @@ -46,8 +44,7 @@ solvers tolerance 1e-8; relTol 0.1; nSweeps 1; - }; - + } omega { solver smoothSolver; @@ -55,7 +52,7 @@ solvers tolerance 1e-8; relTol 0.1; nSweeps 1; - }; + } } SIMPLE @@ -71,4 +68,12 @@ relaxationFactors omega 0.7; } +cache +{ +// grad(U); +// grad(p); +// grad(k); +// grad(omega); +} + // ************************************************************************* //