Merge branch 'feature-fvOptions-writeFile' into 'develop'

ENH: fvOptions: add writeFile functionality

See merge request Development/openfoam!577
This commit is contained in:
Andrew Heather
2022-12-08 11:37:56 +00:00
8 changed files with 280 additions and 101 deletions

View File

@ -291,9 +291,22 @@ bool Foam::functionObjects::writeFile::writeToFile() const
}
bool Foam::functionObjects::writeFile::canWriteToFile() const
{
return (Pstream::master() && writeToFile_ && filePtr_);
}
bool Foam::functionObjects::writeFile::canResetFile() const
{
return (Pstream::master() && writeToFile_ && !filePtr_);
}
bool Foam::functionObjects::writeFile::canWriteHeader() const
{
return writeToFile_ && (updateHeader_ || !writtenHeader_);
return
Pstream::master() && writeToFile_ && (updateHeader_ || !writtenHeader_);
}

View File

@ -31,23 +31,23 @@ Description
Base class for writing single files from the function objects.
Usage
\verbatim
<userDefinedSubDictName1>
<dictName>
{
// Mandatory and other optional entries
// Inherited entries
...
// Optional (inherited) entries (runtime modifiable)
writePrecision 8;
writeToFile true;
useUserTime true;
// Optional entries
writePrecision <int>;
writeToFile <bool>;
useUserTime <bool>;
updateHeader <bool>;
}
\endverbatim
where the entries mean:
\table
Property | Description | Type | Reqd | Dflt
Property | Description | Type | Reqd | Deflt
writePrecision | Number of decimal points | int | no | \<system dflt\>
writeToFile | Produce text file output? | bool | no | true
useUserTime | Use user time (e.g. degrees)? | bool | no | true
@ -60,12 +60,9 @@ Note
unaffected by these changes.
Use the \c updateHeader flag to override the default behaviour.
See also
- Foam::functionObject
- Foam::functionObjects::logFiles
SourceFiles
writeFile.C
writeFileTemplates.C
\*---------------------------------------------------------------------------*/
@ -91,7 +88,7 @@ class writeFile
{
protected:
// Protected data
// Protected Data
//- Reference to the region objectRegistry
const objectRegistry& fileObr_;
@ -244,6 +241,12 @@ public:
//- Flag to allow writing to file
virtual bool writeToFile() const;
//- Flag to allow writing to the file
virtual bool canWriteToFile() const;
//- Flag to allow resetting the file
virtual bool canResetFile() const;
//- Flag to allow writing the header
virtual bool canWriteHeader() const;

View File

@ -49,7 +49,7 @@ namespace fv
}
// * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
// * * * * * * * * * * * * Protected Member Functions * * * * * * * * * * * //
void Foam::fv::velocityDampingConstraint::addDamping(fvMatrix<vector>& eqn)
{
@ -89,10 +89,35 @@ void Foam::fv::velocityDampingConstraint::addDamping(fvMatrix<vector>& eqn)
return (denom ? 1e-2*round(1e4*num/denom) : 0);
};
const scalar nDampedPercent = percent(nDamped, nTotCells);
Info<< type() << ' ' << name_ << " damped "
<< nDamped << " ("
<< percent(nDamped, nTotCells)
<< nDampedPercent
<< "%) of cells, with max limit " << UMax_ << endl;
if (canWriteToFile())
{
file()
<< mesh_.time().timeOutputValue() << token::TAB
<< nDamped << token::TAB
<< nDampedPercent
<< endl;
}
}
void Foam::fv::velocityDampingConstraint::writeFileHeader(Ostream& os)
{
writeHeaderValue(os, "UMax", Foam::name(UMax_));
writeCommented(os, "Time");
writeTabbed(os, "nDamped_[count]");
writeTabbed(os, "nDamped_[%]");
os << endl;
writtenHeader_ = true;
}
@ -107,6 +132,8 @@ Foam::fv::velocityDampingConstraint::velocityDampingConstraint
)
:
fv::cellSetOption(name, modelType, dict, mesh),
writeFile(mesh, name, typeName, dict, false),
UMax_(GREAT), // overwritten later
C_(1)
{
read(dict);
@ -133,24 +160,35 @@ void Foam::fv::velocityDampingConstraint::writeData(Ostream& os) const
bool Foam::fv::velocityDampingConstraint::read(const dictionary& dict)
{
if (fv::cellSetOption::read(dict))
if (!(fv::cellSetOption::read(dict) && writeFile::read(dict)))
{
coeffs_.readEntry("UMax", UMax_);
coeffs_.readIfPresent("C", C_);
if (!coeffs_.readIfPresent("UNames", fieldNames_))
{
fieldNames_.resize(1);
fieldNames_.first() = coeffs_.getOrDefault<word>("U", "U");
}
fv::option::resetApplied();
return true;
return false;
}
return false;
coeffs_.readEntry("UMax", UMax_);
coeffs_.readIfPresent("C", C_);
if (!coeffs_.readIfPresent("UNames", fieldNames_))
{
fieldNames_.resize(1);
fieldNames_.first() = coeffs_.getOrDefault<word>("U", "U");
}
fv::option::resetApplied();
if (canResetFile())
{
resetFile(typeName);
}
if (canWriteHeader())
{
writeFileHeader(file());
}
return true;
}

View File

@ -85,8 +85,9 @@ Usage
\endtable
The inherited entries are elaborated in:
- \link fvOption.H \endlink
- \link cellSetOption.H \endlink
- \link fvOption.H \endlink
- \link cellSetOption.H \endlink
- \link writeFile.H \endlink
Note
- When active, this constraint manipulates the system of equations.
@ -102,10 +103,11 @@ SourceFiles
\*---------------------------------------------------------------------------*/
#ifndef velocityDampingConstraint_H
#define velocityDampingConstraint_H
#ifndef fv_velocityDampingConstraint_H
#define fv_velocityDampingConstraint_H
#include "cellSetOption.H"
#include "writeFile.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
@ -120,7 +122,8 @@ namespace fv
class velocityDampingConstraint
:
public fv::cellSetOption
public fv::cellSetOption,
public functionObjects::writeFile
{
protected:
@ -138,6 +141,9 @@ protected:
//- Constrain the given velocity fields by a given maximum value
void addDamping(fvMatrix<vector>& eqn);
//- Write file header information
void writeFileHeader(Ostream& os);
public:

View File

@ -43,6 +43,24 @@ namespace fv
}
// * * * * * * * * * * * * Protected Member Functions * * * * * * * * * * * //
void Foam::fv::limitTemperature::writeFileHeader(Ostream& os)
{
writeHeaderValue(os, "Tmin", Foam::name(Tmin_));
writeHeaderValue(os, "Tmax", Foam::name(Tmax_));
writeCommented(os, "Time");
writeTabbed(os, "nDampedCellsMin_[count]");
writeTabbed(os, "nDampedCellsMin_[%]");
writeTabbed(os, "nDampedCellsMax_[count]");
writeTabbed(os, "nDampedCellsMax_[%]");
os << endl;
writtenHeader_ = true;
}
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
Foam::fv::limitTemperature::limitTemperature
@ -54,23 +72,14 @@ Foam::fv::limitTemperature::limitTemperature
)
:
fv::cellSetOption(name, modelType, dict, mesh),
Tmin_(coeffs_.get<scalar>("min")),
Tmax_(coeffs_.get<scalar>("max")),
phase_(coeffs_.getOrDefault<word>("phase", word::null))
writeFile(mesh, name, typeName, dict, false),
Tmin_(0),
Tmax_(0),
phase_(word::null)
{
if (isActive())
{
// Set the field name to that of the energy
// field from which the temperature is obtained
const auto& thermo =
mesh_.lookupObject<basicThermo>
(
IOobject::groupName(basicThermo::dictName, phase_)
);
fieldNames_.resize(1, thermo.he().name());
fv::option::resetApplied();
read(dict);
}
}
@ -79,32 +88,57 @@ Foam::fv::limitTemperature::limitTemperature
bool Foam::fv::limitTemperature::read(const dictionary& dict)
{
if (fv::cellSetOption::read(dict))
if (!(fv::cellSetOption::read(dict) && writeFile::read(dict)))
{
coeffs_.readEntry("min", Tmin_);
coeffs_.readEntry("max", Tmax_);
if (Tmax_ < Tmin_)
{
FatalIOErrorInFunction(dict)
<< "Minimum temperature limit cannot exceed maximum limit" << nl
<< "min = " << Tmin_ << nl
<< "max = " << Tmax_
<< exit(FatalIOError);
}
if (Tmin_ < 0)
{
FatalIOErrorInFunction(dict)
<< "Minimum temperature limit cannot be negative" << nl
<< "min = " << Tmin_
<< exit(FatalIOError);
}
return true;
return false;
}
return false;
coeffs_.readEntry("min", Tmin_);
coeffs_.readEntry("max", Tmax_);
coeffs_.readIfPresent("phase", phase_);
if (Tmax_ < Tmin_)
{
FatalIOErrorInFunction(dict)
<< "Minimum temperature limit cannot exceed maximum limit" << nl
<< "min = " << Tmin_ << nl
<< "max = " << Tmax_
<< exit(FatalIOError);
}
if (Tmin_ < 0)
{
FatalIOErrorInFunction(dict)
<< "Minimum temperature limit cannot be negative" << nl
<< "min = " << Tmin_
<< exit(FatalIOError);
}
// Set the field name to that of the energy
// field from which the temperature is obtained
const auto& thermo =
mesh_.lookupObject<basicThermo>
(
IOobject::groupName(basicThermo::dictName, phase_)
);
fieldNames_.resize(1, thermo.he().name());
fv::option::resetApplied();
if (canResetFile())
{
resetFile(typeName);
}
if (canWriteHeader())
{
writeFileHeader(file());
}
return true;
}
@ -162,18 +196,33 @@ void Foam::fv::limitTemperature::correct(volScalarField& he)
return (denom ? 1e-2*round(1e4*num/denom) : 0);
};
const scalar nBelowMinPercent = percent(nBelowMin, nTotCells);
const scalar nAboveMaxPercent = percent(nAboveMax, nTotCells);
Info<< type() << ' ' << name_ << " Lower limited " << nBelowMin << " ("
<< percent(nBelowMin, nTotCells)
<< nBelowMinPercent
<< "%) of cells, with min limit " << Tmin_ << endl;
Info<< type() << ' ' << name_ << " Upper limited " << nAboveMax << " ("
<< percent(nAboveMax, nTotCells)
<< nAboveMaxPercent
<< "%) of cells, with max limit " << Tmax_ << endl;
Info<< type() << ' ' << name_ << " Unlimited Tmin " << Tmin0 << endl;
Info<< type() << ' ' << name_ << " Unlimited Tmax " << Tmax0 << endl;
if (canWriteToFile())
{
file()
<< mesh_.time().timeOutputValue() << token::TAB
<< nBelowMin << token::TAB
<< nBelowMinPercent << token::TAB
<< nAboveMax << token::TAB
<< nAboveMaxPercent
<< endl;
}
// Handle boundaries in the case of 'all'
bool changedValues = (nBelowMin || nAboveMax);
if (!cellSetOption::useSubMesh())

View File

@ -6,7 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2012-2017 OpenFOAM Foundation
Copyright (C) 2020 OpenCFD Ltd.
Copyright (C) 2020-2022 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -75,8 +75,9 @@ Usage
\endtable
The inherited entries are elaborated in:
- \link fvOption.H \endlink
- \link cellSetOption.H \endlink
- \link fvOption.H \endlink
- \link cellSetOption.H \endlink
- \link writeFile.H \endlink
See also
- Foam::fv::fixedTemperatureConstraint
@ -86,10 +87,11 @@ SourceFiles
\*---------------------------------------------------------------------------*/
#ifndef limitTemperature_H
#define limitTemperature_H
#ifndef fv_limitTemperature_H
#define fv_limitTemperature_H
#include "cellSetOption.H"
#include "writeFile.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
@ -99,12 +101,13 @@ namespace fv
{
/*---------------------------------------------------------------------------*\
Class limitTemperature Declaration
Class limitTemperature Declaration
\*---------------------------------------------------------------------------*/
class limitTemperature
:
public fv::cellSetOption
public fv::cellSetOption,
public functionObjects::writeFile
{
protected:
@ -116,10 +119,16 @@ protected:
//- Maximum temperature limit [K]
scalar Tmax_;
//- Optional phase name [K]
//- Optional phase name
word phase_;
// Protected Member Functions
//- Write file header information
void writeFileHeader(Ostream& os);
public:
//- Runtime type information

View File

@ -42,6 +42,23 @@ namespace fv
}
// * * * * * * * * * * * * Protected Member Functions * * * * * * * * * * * //
void Foam::fv::limitVelocity::writeFileHeader(Ostream& os)
{
writeHeaderValue(os, "UMax", Foam::name(max_));
writeCommented(os, "Time");
writeTabbed(os, "nDampedCells_[count]");
writeTabbed(os, "nDampedCells_[%]");
writeTabbed(os, "nDampedFaces_[count]");
writeTabbed(os, "nDampedFaces_[%]");
os << endl;
writtenHeader_ = true;
}
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
Foam::fv::limitVelocity::limitVelocity
@ -53,11 +70,11 @@ Foam::fv::limitVelocity::limitVelocity
)
:
fv::cellSetOption(name, modelType, dict, mesh),
UName_(coeffs_.getOrDefault<word>("U", "U")),
max_(coeffs_.get<scalar>("max"))
writeFile(mesh, name, typeName, dict, false),
UName_(word::null),
max_(0)
{
fieldNames_.resize(1, UName_);
fv::option::resetApplied();
read(dict);
}
@ -65,14 +82,32 @@ Foam::fv::limitVelocity::limitVelocity
bool Foam::fv::limitVelocity::read(const dictionary& dict)
{
if (fv::cellSetOption::read(dict))
if (!(fv::cellSetOption::read(dict) && writeFile::read(dict)))
{
coeffs_.readEntry("max", max_);
return true;
return false;
}
return false;
coeffs_.readEntry("max", max_);
coeffs_.readIfPresent("U", UName_);
fieldNames_.resize(1, UName_);
fv::option::resetApplied();
if (canResetFile())
{
resetFile(typeName);
}
if (canWriteHeader())
{
writeFileHeader(file());
}
return true;
}
@ -137,19 +172,24 @@ void Foam::fv::limitVelocity::correct(volVectorField& U)
reduce(nCellsAbove, sumOp<label>());
const scalar nCellsAbovePercent = percent(nCellsAbove, nTotCells);
// Report total numbers and percent
Info<< type() << ' ' << name_ << " Limited ";
Info<< nCellsAbove << " ("
<< percent(nCellsAbove, nTotCells)
<< nCellsAbovePercent
<< "%) of cells";
reduce(nTotFaces, sumOp<label>());
reduce(nFacesAbove, sumOp<label>());
scalar nFacesAbovePercent(0);
if (nTotFaces)
{
nFacesAbovePercent = percent(nFacesAbove, nTotFaces);
Info<< ", " << nFacesAbove << " ("
<< percent(nFacesAbove, nTotFaces)
<< nFacesAbovePercent
<< "%) of faces";
}
Info<< ", with max limit " << max_ << endl;
@ -160,6 +200,18 @@ void Foam::fv::limitVelocity::correct(volVectorField& U)
// boundary conditions opportunity to correct
U.correctBoundaryConditions();
}
if (canWriteToFile())
{
file()
<< mesh_.time().timeOutputValue() << token::TAB
<< nCellsAbove << token::TAB
<< nCellsAbovePercent << token::TAB
<< nFacesAbove << token::TAB
<< nFacesAbovePercent
<< endl;
}
}

View File

@ -6,7 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2016-2017 OpenFOAM Foundation
Copyright (C) 2020 OpenCFD Ltd.
Copyright (C) 2020-2022 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -69,8 +69,9 @@ Usage
\endtable
The inherited entries are elaborated in:
- \link fvOption.H \endlink
- \link cellSetOption.H \endlink
- \link fvOption.H \endlink
- \link cellSetOption.H \endlink
- \link writeFile.H \endlink
See also
- Foam::fv::velocityDampingConstraint
@ -80,10 +81,11 @@ SourceFiles
\*---------------------------------------------------------------------------*/
#ifndef limitVelocity_H
#define limitVelocity_H
#ifndef fv_limitVelocity_H
#define fv_limitVelocity_H
#include "cellSetOption.H"
#include "writeFile.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
@ -93,12 +95,13 @@ namespace fv
{
/*---------------------------------------------------------------------------*\
Class limitVelocity Declaration
Class limitVelocity Declaration
\*---------------------------------------------------------------------------*/
class limitVelocity
:
public fv::cellSetOption
public fv::cellSetOption,
public functionObjects::writeFile
{
protected:
@ -111,6 +114,12 @@ protected:
scalar max_;
// Protected Member Functions
//- Write file header information
void writeFileHeader(Ostream& os);
public:
//- Runtime type information