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.valid())
111  {
112  return selected;
113  }
114 
115 
116  IOobject io(topoSet::findIOobject(mesh(), name));
117 
118  switch (setType)
119  {
120  case topoSetSource::sourceType::CELLSET_SOURCE:
121  {
122  typedef cellSet classType;
123 
124  if (classType::typeName != io.headerClassName())
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 (classType::typeName != io.headerClassName())
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 (classType::typeName != io.headerClassName())
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<IOobject>(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().outputTime())
283  {
284  writer_->write();
285  }
286 }
287 
288 
289 // ************************************************************************* //
Foam::expressions::patchExpr::debug
int debug
Static debugging option.
Foam::IOobject::NO_WRITE
Definition: IOobject.H:195
Foam::IOobject
Defines the attributes of an object for which implicit objectRegistry management is supported,...
Definition: IOobject.H:169
Foam::topoSetSource::sourceType
sourceType
Enumeration defining the types of sources.
Definition: topoSetSource.H:74
Foam::expressions::fvExprDriverWriter
Registered input/output for an expressions::fvExprDriver.
Definition: fvExprDriverWriter.H:54
Foam::BitOps::set
void set(List< bool > &bools, const labelRange &range)
Set the specified range 'on' in a boolList.
Definition: BitOps.C:37
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:65
fvExprDriverWriter.H
Foam::IOobject::typeHeaderOk
bool typeHeaderOk(const bool checkType=true, const bool search=true, const bool verbose=true)
Read header (uses typeFilePath to find file) and check its info.
Definition: IOobjectTemplates.C:39
Foam::Time::timeName
static word timeName(const scalar t, const int precision=precision_)
Definition: Time.C:780
Foam::faceSet
A list of face labels.
Definition: faceSet.H:51
Foam::expressions::fvExprDriver
Base driver for parsing value expressions associated with an fvMesh.
Definition: fvExprDriver.H:136
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:369
Foam::IOobject::headerClassName
const word & headerClassName() const noexcept
Return name of the class name read from header.
Definition: IOobjectI.H:83
fvExprDriver.H
Foam::expressions::fvExprDriver::getTopoSetLabels
refPtr< labelList > getTopoSetLabels(const word &name, enum topoSetSource::sourceType setType) const
Get the labels associated with the topo set.
Definition: fvExprDriverIO.C:39
Foam::polyMesh
Mesh consisting of general polyhedral cells.
Definition: polyMesh.H:77
Foam::refPtr::valid
bool valid() const noexcept
Identical to good(), or bool operator.
Definition: refPtr.H:300
Foam::refPtr::reset
void reset(refPtr< T > &&other) noexcept
Clear existing and transfer ownership.
Definition: refPtrI.H:303
Foam::expressions::fvExprDriver::mesh
virtual const fvMesh & mesh() const =0
The mesh we are attached to.
Foam::refPtr::cref
const T & cref() const
Definition: refPtrI.H:189
Foam::expressions::fvExprDriver::writeCommon
Ostream & writeCommon(Ostream &os, bool debug=false) const
Definition: fvExprDriverIO.C:218
faceSet.H
Foam::FatalError
error FatalError
os
OBJstream os(runTime.globalPath()/outputName)
mesh
dynamicFvMesh & mesh
Definition: createDynamicFvMesh.H:6
zoneID
const labelIOList & zoneID
Definition: interpolatedFaces.H:22
Foam::cellSet
A collection of cell labels.
Definition: cellSet.H:51
Foam::exit
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:130
Foam::expressions::fvExprDriver::createWriterAndRead
void createWriterAndRead(const word &name)
Create a writer for this object.
Definition: fvExprDriverIO.C:271
FatalErrorInFunction
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:453
DebugInfo
#define DebugInfo
Report an information message using Foam::Info.
Definition: messageStream.H:382
Foam::nl
constexpr char nl
Definition: Ostream.H:404
forAllConstIters
forAllConstIters(mixture.phases(), phase)
Definition: pEqn.H:28
Foam::pointSet
A set of point labels.
Definition: pointSet.H:51
Foam::expressions::fvExprDriver::tryWrite
void tryWrite() const
Definition: fvExprDriverIO.C:280
Foam::List
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: BitOps.H:63
Foam::type
fileName::Type type(const fileName &name, const bool followLink=true)
Return the file type: DIRECTORY or FILE, normally following symbolic links.
Definition: MSwindows.C:590
Foam::name
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for INVALID.
Definition: exprTraits.C:59
Foam::fvMesh::time
const Time & time() const
Return the top-level database.
Definition: fvMesh.H:280
cellSet.H
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:56
Foam::IOobject::path
fileName path() const
The complete path.
Definition: IOobject.C:511
Foam::refPtr
A class for managing references or pointers (no reference counting)
Definition: PtrList.H:60
Foam::IOobject::MUST_READ
Definition: IOobject.H:185
pointSet.H