surfaceCourantNumber.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration |
5  \\ / A nd | www.openfoam.com
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8  Copyright (C) 2024 OpenCFD Ltd.
9 -------------------------------------------------------------------------------
10 License
11  This file is part of OpenFOAM.
12 
13  OpenFOAM is free software: you can redistribute it and/or modify it
14  under the terms of the GNU General Public License as published by
15  the Free Software Foundation, either version 3 of the License, or
16  (at your option) any later version.
17 
18  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
19  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
20  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21  for more details.
22 
23  You should have received a copy of the GNU General Public License
24  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
25 
26 \*---------------------------------------------------------------------------*/
27 
28 #include "surfaceCourantNumber.H"
29 #include "fvMesh.H"
30 #include "faMesh.H"
31 #include "areaFields.H"
32 #include "edgeFields.H"
33 #include "facEdgeIntegrate.H"
36 
37 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
38 
39 namespace Foam
40 {
41 namespace functionObjects
42 {
43  defineTypeNameAndDebug(surfaceCourantNumber, 0);
44  addToRunTimeSelectionTable(functionObject, surfaceCourantNumber, dictionary);
45 }
46 }
47 
48 
49 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
50 
51 void Foam::functionObjects::surfaceCourantNumber::writeFileHeader(Ostream& os)
52 {
53  writeHeader(os, "Surface Courant Number");
54 
55  writeCommented(os, "Time");
56  writeTabbed(os, "min");
57  writeTabbed(os, "max");
58  writeTabbed(os, "mean");
59  os << endl;
60 
62 }
63 
64 
65 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
66 
68 (
69  const word& name,
70  const Time& runTime,
71  const dictionary& dict
72 )
73 :
75  writeFile(mesh_, name, typeName, dict),
76  resultName_("surfaceCo"),
77  phisName_("phis"),
78  rhoName_("rho")
79 {
80  read(dict);
81 }
82 
83 
84 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
85 
87 {
89  {
90  return false;
91  }
92 
93  dict.readIfPresent("result", resultName_);
94  dict.readIfPresent("phis", phisName_);
95  dict.readIfPresent("rho", rhoName_);
96 
97  // Registry containing all finite-area meshes on the polyMesh
98  const auto* faRegistryPtr = faMesh::registry(mesh_);
99 
100  if (!faRegistryPtr)
101  {
103  << "No finite-area object registry is available."
104  << abort(FatalIOError);
105  }
106 
107  word areaName;
108 
109  if (!dict.readIfPresent("area", areaName))
110  {
111  wordList available = faRegistryPtr->sortedNames<faMesh>();
112  if (!available.empty())
113  {
114  areaName = available.front();
115  }
116  }
117 
118  if (areaName.empty())
119  {
121  << "No name for finite-area mesh is available."
122  << abort(FatalIOError);
123  }
124 
125  faMeshPtr_ = std::shared_ptr<const faMesh>
126  (
127  faRegistryPtr->cfindObject<faMesh>(areaName),
128  [](const faMesh*) { /* no-op deleter to avoid double deletion */ }
129  );
130 
131  return true;
132 }
133 
134 
136 {
137  if (!faMeshPtr_->foundObject<edgeScalarField>(phisName_))
138  {
140  << "No edge flux field is available. "
141  << "Name of provided edge flux field (phi): " << phisName_
142  << endl;
143 
144  return false;
145  }
146 
147  const auto& phis = faMeshPtr_->lookupObject<edgeScalarField>(phisName_);
148 
149  tmp<areaScalarField::Internal> tCo =
150  (0.5*faMeshPtr_->time().deltaT())
151  *fac::edgeSum(mag(phis))()()
152  /faMeshPtr_->S();
153 
154  areaScalarField::Internal Co = tCo.ref();
155 
156  if (Co.dimensions() == dimDensity)
157  {
158  Co /= faMeshPtr_->lookupObject<areaScalarField>(rhoName_);
159  }
160 
161  auto* resultPtr = faMeshPtr_->getObjectPtr<areaScalarField>(resultName_);
162 
163  if (!resultPtr)
164  {
165  resultPtr = new areaScalarField
166  (
167  IOobject
168  (
169  resultName_,
170  faMeshPtr_->time().timeName(),
171  *faMeshPtr_,
172  IOobjectOption()
173  ),
174  *faMeshPtr_,
177  );
178  regIOobject::store(resultPtr);
179  }
180  auto& result = *resultPtr;
181 
182  result.internalFieldRef() = tCo;
183  result.correctBoundaryConditions();
184 
185 
186  const scalarMinMax limits(gMinMax(result));
187  const scalar mean = gAverage(result);
188 
189  Log << "Surface Courant number: "
190  << "mean: " << mean
191  << " max: " << limits.max()
192  << endl;
193 
194  if (writeToFile())
195  {
196  if (!writtenHeader_) writeFileHeader(file());
197 
198  writeCurrentTime(file());
199  file()
200  << token::TAB << limits.min()
201  << token::TAB << limits.max()
202  << token::TAB << mean
203  << endl;
204  }
205 
206  return true;
207 }
208 
209 
211 {
212  const auto* result = faMeshPtr_->cfindObject<areaScalarField>(resultName_);
213 
214  if (!result)
215  {
216  return false;
217  }
218 
219  Log << type() << " " << name() << " write: " << result->name() << endl;
220 
221  result->write();
222 
223  return true;
224 }
225 
226 
227 // ************************************************************************* //
Finite area mesh (used for 2-D non-Euclidian finite area method) defined using a patch of faces on a ...
Definition: faMesh.H:133
dictionary dict
defineTypeNameAndDebug(ObukhovLength, 0)
MinMax< scalar > scalarMinMax
A scalar min/max range.
Definition: MinMax.H:97
virtual void writeHeader(Ostream &os, const string &str) const
Write a commented header to stream.
Definition: writeFile.C:344
dimensioned< typename typeOfMag< Type >::type > mag(const dimensioned< Type > &dt)
tmp< GeometricField< Type, faPatchField, areaMesh > > edgeSum(const GeometricField< Type, faePatchField, edgeMesh > &ssf)
A list of keyword definitions, which are a keyword followed by a number of values (eg...
Definition: dictionary.H:129
Tab [isspace].
Definition: token.H:129
T & front()
Access first element of the list, position [0].
Definition: UListI.H:230
engineTime & runTime
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:529
bool store()
Register object with its registry and transfer ownership to the registry.
Definition: regIOobjectI.H:36
surfaceCourantNumber(const word &name, const Time &runTime, const dictionary &dict)
Construct from Time and dictionary.
bool writtenHeader_
Flag to identify whether the header has been written.
Definition: writeFile.H:157
const dimensionSet dimless
Dimensionless.
DimensionedField< scalar, areaMesh > Internal
The internal field type from which this GeometricField is derived.
Class to control time during OpenFOAM simulations that is also the top-level objectRegistry.
Definition: Time.H:69
static const word & zeroGradientType() noexcept
The type name for zeroGradient patch fields.
Definition: faPatchField.H:191
Macros for easy insertion into run-time selection tables.
bool read(const char *buf, int32_t &val)
Same as readInt32.
Definition: int32.H:127
fileName::Type type(const fileName &name, const bool followLink=true)
Return the file type: DIRECTORY or FILE, normally following symbolic links.
Definition: POSIX.C:801
auto limits
Definition: setRDeltaT.H:186
virtual void writeCommented(Ostream &os, const string &str) const
Write a commented string to stream.
Definition: writeFile.C:318
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for expressions::valueTypeCode::INVALID.
Definition: exprTraits.C:127
A class for handling words, derived from Foam::string.
Definition: word.H:63
MinMax< Type > gMinMax(const FieldField< Field, Type > &f)
virtual bool write(const token &tok)=0
Write token to stream or otherwise handle it.
GeometricField< scalar, faePatchField, edgeMesh > edgeScalarField
Definition: edgeFieldsFwd.H:43
errorManip< error > abort(error &err)
Definition: errorManip.H:139
edgeScalarField phis(IOobject("phis", runTime.timeName(), aMesh.thisDb(), IOobject::NO_READ, IOobject::NO_WRITE), linearEdgeInterpolate(Us) &aMesh.Le())
Type gAverage(const FieldField< Field, Type > &f, const label comm)
The global arithmetic average of a FieldField.
OBJstream os(runTime.globalPath()/outputName)
addToRunTimeSelectionTable(functionObject, ObukhovLength, dictionary)
virtual bool read(const dictionary &dict)
Read.
Definition: writeFile.C:240
const dimensionSet dimDensity
virtual bool write()
Write the result field.
List< word > wordList
List of word.
Definition: fileName.H:59
virtual bool read(const dictionary &)
Read the surfaceCourantNumber data.
#define WarningInFunction
Report a warning using Foam::Warning.
virtual bool execute()
Calculate the Courant number field and return true if successful.
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:629
dimensioned< scalar > dimensionedScalar
Dimensioned scalar obtained from generic dimensioned type.
#define Log
Definition: PDRblock.C:28
virtual bool read(const dictionary &dict)
Read optional controls.
static const objectRegistry * registry(const polyMesh &pMesh)
The parent registry containing all finite-area meshes on the polyMesh.
Definition: faMesh.C:144
Specialization of Foam::functionObject for an Foam::fvMesh, providing a reference to the Foam::fvMesh...
Base class for writing single files from the function objects.
Definition: writeFile.H:112
Edge integrate edgeField creating a areaField. Edge sum a edgeField creating a areaField.
Namespace for OpenFOAM.
GeometricField< scalar, faPatchField, areaMesh > areaScalarField
Definition: areaFieldsFwd.H:72
IOerror FatalIOError
Error stream (stdout output on all processes), with additional &#39;FOAM FATAL IO ERROR&#39; header text and ...
static constexpr const zero Zero
Global zero (0)
Definition: zero.H:127
virtual void writeTabbed(Ostream &os, const string &str) const
Write a tabbed string to stream.
Definition: writeFile.C:334