ENH: finiteArea: enable cache-gradient mechanism

This commit is contained in:
Kutalmis Bercin 2023-05-05 15:07:33 +01:00 committed by Andrew Heather
parent 528c5ed8eb
commit 188c303a27
12 changed files with 276 additions and 77 deletions

View File

@ -6,6 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2016-2017 Wikki Ltd
Copyright (C) 2023 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -31,6 +32,7 @@ License
#include "facEdgeIntegrate.H"
#include "faMesh.H"
#include "faGradScheme.H"
#include "gaussFaGrad.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
@ -57,20 +59,10 @@ grad
const GeometricField<Type, faePatchField, edgeMesh>& ssf
)
{
const areaVectorField &n = ssf.mesh().faceAreaNormals();
typedef typename outerProduct<vector,Type>::type GradType;
tmp<GeometricField<GradType, faPatchField, areaMesh>> tgGrad =
fac::edgeIntegrate(ssf.mesh().Sf()*ssf);
GeometricField<GradType, faPatchField, areaMesh>& gGrad = tgGrad.ref();
gGrad -= n*(n & gGrad);
gGrad.correctBoundaryConditions();
return tgGrad;
return fa::gaussGrad<Type>::gradf(ssf, "grad(" + ssf.name() + ')');
}
template<class Type>
tmp
<
@ -108,22 +100,11 @@ grad
const word& name
)
{
const areaVectorField &n = vf.mesh().faceAreaNormals();
typedef typename outerProduct<vector,Type>::type GradType;
tmp<GeometricField<GradType, faPatchField, areaMesh>> tgGrad =
fa::gradScheme<Type>::New
(
vf.mesh(),
vf.mesh().gradScheme(name)
).ref().grad(vf);
GeometricField<GradType, faPatchField, areaMesh>& gGrad = tgGrad.ref();
gGrad -= n*(n & gGrad);
gGrad.correctBoundaryConditions();
return tgGrad;
return fa::gradScheme<Type>::New
(
vf.mesh(),
vf.mesh().gradScheme(name)
)().grad(vf, name);
}

View File

@ -6,7 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2016-2017 Wikki Ltd
Copyright (C) 2019-2021 OpenCFD Ltd.
Copyright (C) 2019-2023 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -28,6 +28,8 @@ License
#include "fa.H"
#include "HashTable.H"
#include "objectRegistry.H"
#include "solution.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
@ -83,11 +85,112 @@ tmp<gradScheme<Type>> gradScheme<Type>::New
}
// * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
template<class Type>
gradScheme<Type>::~gradScheme()
{}
tmp
<
GeometricField
<
typename outerProduct<vector, Type>::type,
faPatchField,
areaMesh
>
>
gradScheme<Type>::grad
(
const GeometricField<Type, faPatchField, areaMesh>& vsf,
const word& name
) const
{
typedef typename outerProduct<vector, Type>::type GradType;
typedef GeometricField<GradType, faPatchField, areaMesh> GradFieldType;
GradFieldType* pgGrad =
mesh().thisDb().template getObjectPtr<GradFieldType>(name);
if (!this->mesh().cache(name)) // || this->mesh().changing()
{
// Delete any old occurrences to avoid double registration
if (pgGrad && pgGrad->ownedByRegistry())
{
solution::cachePrintMessage("Deleting", name, vsf);
delete pgGrad;
}
solution::cachePrintMessage("Calculating", name, vsf);
return calcGrad(vsf, name);
}
if (!pgGrad)
{
solution::cachePrintMessage("Calculating and caching", name, vsf);
pgGrad = calcGrad(vsf, name).ptr();
regIOobject::store(pgGrad);
}
else
{
if (pgGrad->upToDate(vsf))
{
solution::cachePrintMessage("Reusing", name, vsf);
}
else
{
solution::cachePrintMessage("Updating", name, vsf);
delete pgGrad;
pgGrad = calcGrad(vsf, name).ptr();
regIOobject::store(pgGrad);
}
}
return *pgGrad;
}
template<class Type>
tmp
<
GeometricField
<
typename outerProduct<vector, Type>::type,
faPatchField,
areaMesh
>
>
gradScheme<Type>::grad
(
const GeometricField<Type, faPatchField, areaMesh>& vsf
) const
{
return grad(vsf, "grad(" + vsf.name() + ')');
}
template<class Type>
tmp
<
GeometricField
<
typename outerProduct<vector, Type>::type,
faPatchField,
areaMesh
>
>
gradScheme<Type>::grad
(
const tmp<GeometricField<Type, faPatchField, areaMesh>>& tvsf
) const
{
typedef typename outerProduct<vector, Type>::type GradType;
typedef GeometricField<GradType, faPatchField, areaMesh> GradFieldType;
tmp<GradFieldType> tgrad = grad(tvsf());
tvsf.clear();
return tgrad;
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //

View File

@ -6,6 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2016-2017 Wikki Ltd
Copyright (C) 2023 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -113,26 +114,62 @@ public:
//- Destructor
virtual ~gradScheme();
virtual ~gradScheme() = default;
// Member Functions
//- Return mesh reference
const faMesh& mesh() const
{
return mesh_;
}
const faMesh& mesh() const { return mesh_; }
//- Calculate and return the grad of the given field.
// Used by grad either to recalculate the cached gradient when it is
// out of date with respect to the field or when it is not cached.
virtual tmp
<
GeometricField
<typename outerProduct<vector, Type>::type, faPatchField, areaMesh>
> calcGrad
(
const GeometricField<Type, faPatchField, areaMesh>&,
const word& name
) const = 0;
//- Calculate and return the grad of the given field
virtual tmp
//- which may have been cached
tmp
<
GeometricField
<typename outerProduct<vector, Type>::type, faPatchField, areaMesh>
> grad
(
const GeometricField<Type, faPatchField, areaMesh>&,
const word& name
) const;
//- Calculate and return the grad of the given field
//- with the default name
//- which may have been cached
tmp
<
GeometricField
<typename outerProduct<vector, Type>::type, faPatchField, areaMesh>
> grad
(
const GeometricField<Type, faPatchField, areaMesh>&
) const = 0;
) const;
//- Calculate and return the grad of the given field
//- with the default name
//- which may have been cached
tmp
<
GeometricField
<typename outerProduct<vector, Type>::type, faPatchField, areaMesh>
> grad
(
const tmp<GeometricField<Type, faPatchField, areaMesh>>&
) const;
};

View File

@ -6,6 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2016-2017 Wikki Ltd
Copyright (C) 2023 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -47,17 +48,52 @@ tmp
<
GeometricField
<
typename outerProduct<vector, Type>::type, faPatchField, areaMesh
typename outerProduct<vector, Type>::type,
faPatchField,
areaMesh
>
>
gaussGrad<Type>::grad
gaussGrad<Type>::gradf
(
const GeometricField<Type, faPatchField, areaMesh>& vsf
const GeometricField<Type, faePatchField, edgeMesh>& ssf,
const word& name
)
{
const areaVectorField &n = ssf.mesh().faceAreaNormals();
typedef typename outerProduct<vector,Type>::type GradType;
tmp<GeometricField<GradType, faPatchField, areaMesh>> tgGrad =
fac::edgeIntegrate(ssf.mesh().Sf()*ssf);
GeometricField<GradType, faPatchField, areaMesh>& gGrad = tgGrad.ref();
gGrad -= n*(n & gGrad);
gGrad.correctBoundaryConditions();
return tgGrad;
}
template<class Type>
tmp
<
GeometricField
<
typename outerProduct<vector, Type>::type,
faPatchField,
areaMesh
>
>
gaussGrad<Type>::calcGrad
(
const GeometricField<Type, faPatchField, areaMesh>& vsf,
const word& name
) const
{
typedef typename outerProduct<vector, Type>::type GradType;
typedef GeometricField<GradType, faPatchField, areaMesh> GradFieldType;
tmp<GeometricField<GradType, faPatchField, areaMesh>> tgGrad
tmp<GradFieldType> tgGrad
(
fac::edgeIntegrate
(
@ -65,12 +101,11 @@ gaussGrad<Type>::grad
*tinterpScheme_().interpolate(vsf)
)
);
GeometricField<GradType, faPatchField, areaMesh>& gGrad = tgGrad.ref();
GradFieldType& gGrad = tgGrad.ref();
gGrad.correctBoundaryConditions();
gGrad.rename(name);
gGrad.rename("grad(" + vsf.name() + ')');
correctBoundaryConditions(vsf, gGrad);
return tgGrad;

View File

@ -6,6 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2016-2017 Wikki Ltd
Copyright (C) 2023 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -62,8 +63,9 @@ class gaussGrad
:
public fa::gradScheme<Type>
{
// Private data
// Private Data
//- Interpolation scheme
tmp<edgeInterpolationScheme<Type>> tinterpScheme_;
@ -118,17 +120,32 @@ public:
// Member Functions
//- Return the gradient of the given field calculated
// using Gauss' theorem on the interpolated field
//- Return the gradient of the given field
//- calculated using Gauss' theorem on the given surface field
static
tmp
<
GeometricField
<typename outerProduct<vector, Type>::type, faPatchField, areaMesh>
> grad
> gradf
(
const GeometricField<Type, faPatchField, areaMesh>&
const GeometricField<Type, faePatchField, edgeMesh>&,
const word& name
);
//- Return the gradient of the given field to the
//- gradScheme::grad for optional caching
virtual tmp
<
GeometricField
<typename outerProduct<vector, Type>::type, faPatchField, areaMesh>
> calcGrad
(
const GeometricField<Type, faPatchField, areaMesh>& vsf,
const word& name
) const;
//- Correct the boundary values of the gradient using the patchField
// snGrad functions
static void correctBoundaryConditions

View File

@ -6,6 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2016-2017 Wikki Ltd
Copyright (C) 2023 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -45,18 +46,20 @@ namespace fa
{
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
template<class Type>
tmp
<
GeometricField
<
typename outerProduct<vector, Type>::type, faPatchField, areaMesh
typename outerProduct<vector, Type>::type,
faPatchField,
areaMesh
>
>
leastSquaresFaGrad<Type>::grad
leastSquaresFaGrad<Type>::calcGrad
(
const GeometricField<Type, faPatchField, areaMesh>& vsf
const GeometricField<Type, faPatchField, areaMesh>& vsf,
const word& name
) const
{
typedef typename outerProduct<vector, Type>::type GradType;
@ -69,7 +72,7 @@ leastSquaresFaGrad<Type>::grad
(
IOobject
(
"grad(" + vsf.name() + ')',
name,
vsf.instance(),
vsf.db(),
IOobject::NO_READ,

View File

@ -6,6 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2016-2017 Wikki Ltd
Copyright (C) 2023 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -93,13 +94,16 @@ public:
// Member Functions
//- Return the gradient of the given field to the
//- gradScheme::grad for optional caching
virtual tmp
<
GeometricField
<typename outerProduct<vector, Type>::type, faPatchField, areaMesh>
> grad
> calcGrad
(
const GeometricField<Type, faPatchField, areaMesh>&
const GeometricField<Type, faPatchField, areaMesh>& vsf,
const word& name
) const;
};

View File

@ -6,6 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2016-2017 Wikki Ltd
Copyright (C) 2023 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -116,13 +117,16 @@ public:
// Member Functions
tmp
//- Return the gradient of the given field to the
//- gradScheme::grad for optional caching
virtual tmp
<
GeometricField
<typename outerProduct<vector, Type>::type, faPatchField, areaMesh>
> grad
> calcGrad
(
const GeometricField<Type, faPatchField, areaMesh>&
const GeometricField<Type, faPatchField, areaMesh>& vsf,
const word& name
) const;
};

View File

@ -6,6 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2016-2017 Wikki Ltd
Copyright (C) 2023 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -72,14 +73,15 @@ inline void edgeLimitedGrad<Type>::limitEdge
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
template<>
tmp<areaVectorField> edgeLimitedGrad<scalar>::grad
tmp<areaVectorField> edgeLimitedGrad<scalar>::calcGrad
(
const areaScalarField& vsf
const areaScalarField& vsf,
const word& name
) const
{
const faMesh& mesh = vsf.mesh();
tmp<areaVectorField> tGrad = basicGradScheme_().grad(vsf);
tmp<areaVectorField> tGrad = basicGradScheme_().calcGrad(vsf, name);
if (k_ < SMALL)
{
@ -206,14 +208,15 @@ tmp<areaVectorField> edgeLimitedGrad<scalar>::grad
template<>
tmp<areaTensorField> edgeLimitedGrad<vector>::grad
tmp<areaTensorField> edgeLimitedGrad<vector>::calcGrad
(
const areaVectorField& vvf
const areaVectorField& vvf,
const word& name
) const
{
const faMesh& mesh = vvf.mesh();
tmp<areaTensorField> tGrad = basicGradScheme_().grad(vvf);
tmp<areaTensorField> tGrad = basicGradScheme_().grad(vvf, name);
if (k_ < SMALL)
{

View File

@ -6,6 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2016-2017 Wikki Ltd
Copyright (C) 2023 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -118,14 +119,16 @@ public:
const Type& extrapolate
);
tmp
//- Return the gradient of the given field to the
//- gradScheme::grad for optional caching
virtual tmp
<
GeometricField
<typename outerProduct<vector, Type>::type, faPatchField, areaMesh>
> grad
> calcGrad
(
const GeometricField<Type, faPatchField, areaMesh>&
const GeometricField<Type, faPatchField, areaMesh>& vsf,
const word& name
) const;
};

View File

@ -6,6 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2016-2017 Wikki Ltd
Copyright (C) 2023 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -94,14 +95,15 @@ inline void faceLimitedGrad<Type>::limitEdge
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
template<>
tmp<areaVectorField> faceLimitedGrad<scalar>::grad
tmp<areaVectorField> faceLimitedGrad<scalar>::calcGrad
(
const areaScalarField& vsf
const areaScalarField& vsf,
const word& name
) const
{
const faMesh& mesh = vsf.mesh();
tmp<areaVectorField> tGrad = basicGradScheme_().grad(vsf);
tmp<areaVectorField> tGrad = basicGradScheme_().calcGrad(vsf, name);
if (k_ < SMALL)
{
@ -243,14 +245,15 @@ tmp<areaVectorField> faceLimitedGrad<scalar>::grad
template<>
tmp<areaTensorField> faceLimitedGrad<vector>::grad
tmp<areaTensorField> faceLimitedGrad<vector>::calcGrad
(
const areaVectorField& vsf
const areaVectorField& vsf,
const word& name
) const
{
const faMesh& mesh = vsf.mesh();
tmp<areaTensorField> tGrad = basicGradScheme_().grad(vsf);
tmp<areaTensorField> tGrad = basicGradScheme_().grad(vsf, name);
if (k_ < SMALL)
{

View File

@ -44,5 +44,11 @@ relaxationFactors
Us 0.5;
}
cache
{
grad(h);
grad(Us);
}
// ************************************************************************* //