fvExprDriverIO.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) 2010-2018 Bernhard Gschaider
9  Copyright (C) 2019-2021 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 "fvExprDriver.H"
30 #include "fvExprDriverWriter.H"
31 #include "cellSet.H"
32 #include "faceSet.H"
33 #include "pointSet.H"
34 
35 // * * * * * * * * * * * * Protected Member Functions * * * * * * * * * * * //
36 
39 (
40  const word& name,
41  enum topoSetSource::sourceType setType
42 ) const
43 {
44  refPtr<labelList> selected;
45 
46  // Zones first
47  // - cheap to handle (no IO) and can simply reference their labels
48 
49  switch (setType)
50  {
51  case topoSetSource::sourceType::CELLZONE_SOURCE:
52  {
53  const auto& zones = mesh().cellZones();
54  const word& zoneTypeName = cellZone::typeName;
55 
56  const label zoneID = zones.findZoneID(name);
57  if (zoneID < 0)
58  {
60  << "No " << zoneTypeName << " named "
61  << name << "found. Has zones: " << zones.names() << endl
62  << exit(FatalError);
63  }
64 
65  selected.cref(zones[zoneID]);
66  break;
67  }
68 
69  case topoSetSource::sourceType::FACEZONE_SOURCE:
70  {
71  const auto& zones = mesh().faceZones();
72  const word& zoneTypeName = faceZone::typeName;
73 
74  const label zoneID = zones.findZoneID(name);
75  if (zoneID < 0)
76  {
78  << "No " << zoneTypeName << " named "
79  << name << "found. Has zones: " << zones.names() << endl
80  << exit(FatalError);
81  }
82 
83  selected.cref(zones[zoneID]);
84  break;
85  }
86 
87  case topoSetSource::sourceType::POINTZONE_SOURCE:
88  {
89  const auto& zones = mesh().pointZones();
90  const word& zoneTypeName = pointZone::typeName;
91 
92  const label zoneID = zones.findZoneID(name);
93  if (zoneID < 0)
94  {
96  << "No " << zoneTypeName << " named "
97  << name << "found. Has zones: " << zones.names() << endl
98  << exit(FatalError);
99  }
100 
101  selected.cref(zones[zoneID]);
102  break;
103  }
104 
105  default:
106  break;
107  }
108 
109 
110  if (selected.good())
111  {
112  return selected;
113  }
114 
115 
117 
118  switch (setType)
119  {
120  case topoSetSource::sourceType::CELLSET_SOURCE:
121  {
122  typedef cellSet classType;
123 
124  if (!io.isHeaderClass<classType>())
125  {
127  << "Error reading " << classType::typeName
128  << " <" << name << "> : found "
129  << io.headerClassName() << nl
130  << exit(FatalError);
131  }
132 
133  classType set(io);
134  selected.reset(refPtr<labelList>::New(set.sortedToc()));
135  break;
136  }
137 
138  case topoSetSource::sourceType::FACESET_SOURCE:
139  {
140  typedef faceSet classType;
141 
142  if (!io.isHeaderClass<classType>())
143  {
145  << "Error reading " << classType::typeName
146  << " <" << name << "> : found "
147  << io.headerClassName() << nl
148  << exit(FatalError);
149  }
150 
151  classType set(io);
152  selected.reset(refPtr<labelList>::New(set.sortedToc()));
153  break;
154  }
155 
156  case topoSetSource::sourceType::POINTSET_SOURCE:
157  {
158  typedef pointSet classType;
159 
160  if (!io.isHeaderClass<classType>())
161  {
163  << "Error reading " << classType::typeName
164  << " <" << name << "> : found "
165  << io.headerClassName() << nl
166  << exit(FatalError);
167  }
168 
169  classType set(io);
170  selected.reset(refPtr<labelList>::New(set.sortedToc()));
171  break;
172  }
173 
174  default:
175  {
177  << "Unexpected sourceType: " << int(setType) << nl
178  << " for set <" << name << ">" << nl
179  << exit(FatalError);
180  break;
181  }
182  }
183 
184  return selected;
185 }
186 
187 
188 Foam::word Foam::expressions::fvExprDriver::getHeaderClassName
189 (
190  const polyMesh& mesh,
191  const word& name
192 )
193 {
194  IOobject io
195  (
196  name,
197  mesh.time().timeName(),
198  mesh,
201  );
202  io.typeHeaderOk<regIOobject>(false);
203 
204  DebugInfo
205  << "Registry: " << mesh.path()
206  << " Name: " << name
207  << " Time: " << mesh.time().timeName()
208  << " Path: " << io.localFilePath(io.headerClassName())
209  << " Class: " << io.headerClassName() << endl;
210 
211  return io.headerClassName();
212 }
213 
214 
215 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
216 
218 (
219  Ostream& os,
220  bool debug
221 ) const
222 {
223  // Write "variables", even if empty
224  writeVariableStrings(os, "variables");
225 
226  if (debug)
227  {
228  os.writeEntry("variableValues", variables_);
229  }
230 
231  if (!storedVariables_.empty() || !delayedVariables_.empty())
232  {
233  const_cast<fvExprDriver&>
234  (
235  *this
236  ).updateSpecialVariables(true);
237  }
238 
239  if (!storedVariables_.empty())
240  {
241  os.writeEntry("storedVariables", storedVariables_);
242  }
243 
244  if (!delayedVariables_.empty())
245  {
246  List<exprResultDelayed> list(delayedVariables_.size());
247 
248  auto outIter = list.begin();
249 
250  forAllConstIters(delayedVariables_, iter)
251  {
252  *outIter = *iter;
253  ++outIter;
254  }
255 
256  os.writeEntry("delayedVariables", list);
257  }
258 
259  if (!globalScopes_.empty())
260  {
261  os.writeEntry("globalScopes", globalScopes_);
262  }
263 
264  // Write "functions<scalar>" ...
265  writeFunctions(os);
266 
267  return os;
268 }
269 
270 
272 {
273  if (!writer_ && hasDataToWrite())
274  {
275  writer_.reset(new fvExprDriverWriter(name + "_" + this->type(), *this));
276  }
277 }
278 
279 
281 {
282  if (writer_ && mesh().time().writeTime())
283  {
284  writer_->write();
285  }
286 }
287 
288 
289 // ************************************************************************* //
static IOobject findIOobject(const polyMesh &mesh, const word &name, IOobjectOption::readOption rOpt=IOobjectOption::MUST_READ, IOobjectOption::writeOption wOpt=IOobjectOption::NO_WRITE)
Find IOobject in the polyMesh/sets (used as constructor helper)
Definition: topoSet.C:311
A list of face labels.
Definition: faceSet.H:47
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:125
A set of point labels.
Definition: pointSet.H:47
error FatalError
Error stream (stdout output on all processes), with additional &#39;FOAM FATAL ERROR&#39; header text and sta...
refPtr< labelList > getTopoSetLabels(const word &name, enum topoSetSource::sourceType setType) const
Get the labels associated with the topo set.
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:598
constexpr char nl
The newline &#39;\n&#39; character (0x0a)
Definition: Ostream.H:50
virtual const fvMesh & mesh() const =0
The mesh we are attached to.
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:531
bool good() const noexcept
True if pointer/reference is non-null.
Definition: refPtr.H:221
Ignore writing from objectRegistry::writeObject()
const T & cref() const
Return const reference to the object or to the contents of a (non-null) managed pointer.
Definition: refPtrI.H:216
A class for managing references or pointers (no reference counting)
Definition: HashPtrTable.H:49
fileName::Type type(const fileName &name, const bool followLink=true)
Return the file type: DIRECTORY or FILE, normally following symbolic links.
Definition: POSIX.C:799
dynamicFvMesh & mesh
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
void tryWrite() const
Write data if appropriate Should enable exact restarts.
Registered input/output for an expressions::fvExprDriver.
#define DebugInfo
Report an information message using Foam::Info.
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:56
int debug
Static debugging option.
OBJstream os(runTime.globalPath()/outputName)
const faceZoneMesh & faceZones() const noexcept
Return face zone mesh.
Definition: polyMesh.H:670
const pointZoneMesh & pointZones() const noexcept
Return point zone mesh.
Definition: polyMesh.H:662
Ostream & writeCommon(Ostream &os, bool debug=false) const
Write "variables", "storedVariables", "delayedVariables", "globalScopes" if they are present...
A collection of cell labels.
Definition: cellSet.H:47
sourceType
Enumeration defining the types of sources.
Definition: topoSetSource.H:72
const cellZoneMesh & cellZones() const noexcept
Return cell zone mesh.
Definition: polyMesh.H:678
regIOobject is an abstract class derived from IOobject to handle automatic object registration with t...
Definition: regIOobject.H:66
List< label > sortedToc(const UList< bool > &bools)
Return the (sorted) values corresponding to &#39;true&#39; entries.
Definition: BitOps.C:195
void createWriterAndRead(const word &name)
Create a writer for this object.
Mesh consisting of general polyhedral cells.
Definition: polyMesh.H:74
IOobject io("surfaceFilmProperties", mesh.time().constant(), mesh, IOobject::READ_IF_PRESENT, IOobject::NO_WRITE, IOobject::NO_REGISTER)
Defines the attributes of an object for which implicit objectRegistry management is supported...
Definition: IOobject.H:172
forAllConstIters(mixture.phases(), phase)
Definition: pEqn.H:28
void reset(T *p=nullptr) noexcept
Delete managed pointer and set to new given pointer.
Definition: refPtrI.H:314