solverInfo.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) 2015-2016 OpenFOAM Foundation
9  Copyright (C) 2015-2020 OpenCFD Ltd.
10 -------------------------------------------------------------------------------
11 License
12  This file is part of OpenFOAM.
13 
14  OpenFOAM is free software: you can redistribute it and/or modify it
15  under the terms of the GNU General Public License as published by
16  the Free Software Foundation, either version 3 of the License, or
17  (at your option) any later version.
18 
19  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
20  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22  for more details.
23 
24  You should have received a copy of the GNU General Public License
25  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
26 
27 \*---------------------------------------------------------------------------*/
28 
29 #include "solverInfo.H"
31 
32 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
33 
34 namespace Foam
35 {
36 namespace functionObjects
37 {
38  defineTypeNameAndDebug(solverInfo, 0);
39 
41  (
42  functionObject,
43  solverInfo,
44  dictionary
45  );
46 }
47 }
48 
49 
50 // * * * * * * * * * * * * * Protected Member Functions * * * * * * * * * * //
51 
53 {
55  {
56  return;
57  }
58 
59  if (writtenHeader_)
60  {
61  writeBreak(file());
62  }
63  else
64  {
65  writeHeader(os, "Solver information");
66  }
67 
68  writeCommented(os, "Time");
69 
70  for (const word& fieldName : fieldSet_.selectionNames())
71  {
72  writeFileHeader<scalar>(os, fieldName);
73  writeFileHeader<vector>(os, fieldName);
74  writeFileHeader<sphericalTensor>(os, fieldName);
75  writeFileHeader<symmTensor>(os, fieldName);
76  writeFileHeader<tensor>(os, fieldName);
77  }
78 
79  os << endl;
80 
81  writtenHeader_ = true;
82 }
83 
84 
86 (
87  const word& fieldName
88 )
89 {
90  if (!writeResidualFields_)
91  {
92  return;
93  }
94 
95  const word residualName("initialResidual:" + fieldName);
96 
97  if (!mesh_.foundObject<IOField<scalar>>(residualName))
98  {
99  IOField<scalar>* fieldPtr =
100  new IOField<scalar>
101  (
102  IOobject
103  (
104  residualName,
105  mesh_.time().timeName(),
106  mesh_,
109  ),
110  Field<scalar>(mesh_.nCells(), Zero)
111  );
112 
113  fieldPtr->store();
114 
115  residualFieldNames_.insert(residualName);
116  }
117 }
118 
119 
120 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
121 
122 Foam::functionObjects::solverInfo::solverInfo
123 (
124  const word& name,
125  const Time& runTime,
126  const dictionary& dict
127 )
128 :
130  writeFile(obr_, name, typeName, dict),
131  fieldSet_(mesh_),
132  writeResidualFields_(false),
133  residualFieldNames_(),
134  initialised_(false)
135 {
136  read(dict);
137 }
138 
139 
140 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
141 
143 {
145  {
146  initialised_ = false;
147 
148  fieldSet_.read(dict);
149 
150  writeResidualFields_ =
151  dict.getOrDefault("writeResidualFields", false);
152 
153  residualFieldNames_.clear();
154 
155  return true;
156  }
157 
158  return false;
159 }
160 
161 
163 {
164  // Note: delaying initialisation until after first iteration so that
165  // we can find wildcard fields
166  if (!initialised_)
167  {
168  writeFileHeader(file());
169 
170  if (writeResidualFields_)
171  {
172  for (const word& fieldName : fieldSet_.selectionNames())
173  {
174  initialiseResidualField<scalar>(fieldName);
175  initialiseResidualField<vector>(fieldName);
176  initialiseResidualField<sphericalTensor>(fieldName);
177  initialiseResidualField<symmTensor>(fieldName);
178  initialiseResidualField<tensor>(fieldName);
179  }
180  }
181 
182  initialised_ = true;
183  }
184 
185  writeCurrentTime(file());
186 
187  for (const word& fieldName : fieldSet_.selectionNames())
188  {
189  updateSolverInfo<scalar>(fieldName);
190  updateSolverInfo<vector>(fieldName);
191  updateSolverInfo<sphericalTensor>(fieldName);
192  updateSolverInfo<symmTensor>(fieldName);
193  updateSolverInfo<tensor>(fieldName);
194  }
195 
196  file() << endl;
197 
198  return true;
199 }
200 
201 
203 {
204  for (const word& residualName : residualFieldNames_)
205  {
206  const auto* residualPtr =
207  mesh_.findObject<IOField<scalar>>(residualName);
208 
209  if (residualPtr)
210  {
211  volScalarField residual
212  (
213  IOobject
214  (
215  residualName,
216  mesh_.time().timeName(),
217  mesh_,
220  false
221  ),
222  mesh_,
225  );
226 
227  residual.primitiveFieldRef() = *residualPtr;
228  residual.correctBoundaryConditions();
229 
230  residual.write();
231  }
232  }
233 
234  return true;
235 }
236 
237 
238 // ************************************************************************* //
Foam::IOobject::NO_WRITE
Definition: IOobject.H:130
runTime
engineTime & runTime
Definition: createEngineTime.H:13
Foam::functionObjects::solverFieldSelection::updateSelection
virtual bool updateSelection()
Update the selection using current contents of obr_.
Definition: solverFieldSelection.C:54
Foam::IOobject
Defines the attributes of an object for which implicit objectRegistry management is supported,...
Definition: IOobject.H:104
Foam::dimless
const dimensionSet dimless(0, 0, 0, 0, 0, 0, 0)
Dimensionless.
Definition: dimensionSets.H:50
solverInfo.H
Foam::Time
Class to control time during OpenFOAM simulations that is also the top-level objectRegistry.
Definition: Time.H:73
Foam::functionObjects::solverInfo::read
virtual bool read(const dictionary &)
Read the controls.
Definition: solverInfo.C:142
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:62
Foam::functionObjects::writeFile::file
virtual OFstream & file()
Return access to the file (if only 1)
Definition: writeFile.C:237
Foam::IOField
A primitive field of type <T> with automated input and output.
Definition: foamVtkLagrangianWriter.H:61
Foam::functionObjects::writeFile::writtenHeader_
bool writtenHeader_
Flag to identify whether the header has been written.
Definition: writeFile.H:148
Foam::Zero
static constexpr const zero Zero
Global zero (0)
Definition: zero.H:131
Foam::read
bool read(const char *buf, int32_t &val)
Same as readInt32.
Definition: int32.H:108
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:350
Foam::functionObjects::fieldSelection::selectionNames
wordHashSet selectionNames() const
Return the current field selection.
Definition: fieldSelectionI.H:50
Foam::functionObjects::fvMeshFunctionObject
Specialization of Foam::functionObject for an Foam::fvMesh, providing a reference to the Foam::fvMesh...
Definition: fvMeshFunctionObject.H:64
Foam::zeroGradientFvPatchField
This boundary condition applies a zero-gradient condition from the patch internal field onto the patc...
Definition: zeroGradientFvPatchField.H:64
Foam::functionObjects::writeFile::writeHeader
virtual void writeHeader(Ostream &os, const string &str) const
Write a commented header to stream.
Definition: writeFile.C:299
Foam::functionObjects::writeFile::writeBreak
virtual void writeBreak(Ostream &os) const
Write a break marker to the stream.
Definition: writeFile.C:322
Foam::functionObjects::solverInfo::execute
virtual bool execute()
Execute, currently does nothing.
Definition: solverInfo.C:162
Foam::Field< scalar >
Foam::name
word name(const complex &c)
Return string representation of complex.
Definition: complex.C:76
Foam::dimensionedScalar
dimensioned< scalar > dimensionedScalar
Dimensioned scalar obtained from generic dimensioned type.
Definition: dimensionedScalarFwd.H:43
dict
dictionary dict
Definition: searchingEngine.H:14
Foam::dictionary
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition: dictionary.H:121
Foam::functionObjects::regionFunctionObject::read
virtual bool read(const dictionary &dict)
Read optional controls.
Definition: regionFunctionObject.C:173
addToRunTimeSelectionTable.H
Macros for easy insertion into run-time selection tables.
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::GeometricField::primitiveFieldRef
Internal::FieldType & primitiveFieldRef(const bool updateAccessTime=true)
Return a reference to the internal field.
Definition: GeometricField.C:766
Foam::functionObjects::writeFile::writeCommented
virtual void writeCommented(Ostream &os, const string &str) const
Write a commented string to stream.
Definition: writeFile.C:273
Foam::functionObjects::solverInfo::writeFileHeader
void writeFileHeader(Ostream &os)
Output file header information.
Definition: solverInfo.C:52
Foam::functionObjects::addToRunTimeSelectionTable
addToRunTimeSelectionTable(functionObject, ObukhovLength, dictionary)
Foam::functionObjects::solverInfo::write
virtual bool write()
Write the solverInfo.
Definition: solverInfo.C:202
Foam::functionObjects::solverInfo::fieldSet_
solverFieldSelection fieldSet_
Fields to process.
Definition: solverInfo.H:125
Foam::functionObjects::defineTypeNameAndDebug
defineTypeNameAndDebug(ObukhovLength, 0)
Foam::functionObjects::writeFile
Base class for writing single files from the function objects.
Definition: writeFile.H:119
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:56
Foam::GeometricField< scalar, fvPatchField, volMesh >
Foam::IOobject::NO_READ
Definition: IOobject.H:123
Foam::functionObjects::solverInfo::createResidualField
void createResidualField(const word &fieldName)
Create and store a residual field on the mesh database.
Definition: solverInfo.C:86