coordinateSystems.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) 2011-2016 OpenFOAM Foundation
9  Copyright (C) 2018-2022 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 "coordinateSystems.H"
30 #include "predicates.H"
31 #include "PtrListOps.H"
32 #include "Time.H"
33 
34 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
35 
36 namespace Foam
37 {
38  defineTypeName(coordinateSystems);
39 }
40 
41 // File-local
42 
43 //- Header name for 1806 and earlier
44 static const char* headerTypeCompat = "IOPtrList<coordinateSystem>";
45 
46 
47 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
48 
49 void Foam::coordinateSystems::readFromStream(const bool readOnProc)
50 {
51  Istream& is = readStream(word::null, readOnProc);
52 
53  if (readOnProc)
54  {
55  if (headerClassName() == typeName)
56  {
57  this->readIstream(is, coordinateSystem::iNew());
58  close();
59  }
60  else if (headerClassName() == headerTypeCompat)
61  {
62  // Older (1806 and earlier) header name
63  if (error::master())
64  {
65  std::cerr
66  << "--> FOAM IOWarning :" << nl
67  << " Found header class name '" << headerTypeCompat
68  << "' instead of '" << typeName << "'" << nl;
69 
70  error::warnAboutAge("header class", 1806);
71  }
72 
73  this->readIstream(is, coordinateSystem::iNew());
74  close();
75  }
76  else
77  {
79  << "unexpected class name " << headerClassName()
80  << " expected " << typeName
81  << " or " << headerTypeCompat << nl
82  << " while reading object " << name()
83  << exit(FatalIOError);
84  }
85  }
86 }
87 
88 
89 bool Foam::coordinateSystems::readContents()
90 {
91  if (isReadRequired() || (isReadOptional() && headerOk()))
92  {
93  readFromStream();
94  return true;
95  }
96 
97  return false;
98 }
99 
100 
101 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
102 
103 Foam::coordinateSystems::coordinateSystems(const IOobject& io)
104 :
105  regIOobject(io),
107 {
108  readContents();
109 }
110 
111 
112 Foam::coordinateSystems::coordinateSystems(const objectRegistry& obr)
113 :
115  (
116  IOobject
117  (
118  typeName,
119  obr.time().constant(),
120  obr,
121  IOobject::READ_IF_PRESENT,
122  IOobject::NO_WRITE
123  )
124  )
125 {}
126 
127 
128 Foam::coordinateSystems::coordinateSystems
129 (
130  const IOobject& io,
131  const PtrList<coordinateSystem>& content
132 )
133 :
134  regIOobject(io),
136 {
137  if (!readContents())
138  {
139  static_cast<PtrList<coordinateSystem>&>(*this) = content;
140  }
141 }
142 
143 
144 Foam::coordinateSystems::coordinateSystems
145 (
146  const IOobject& io,
147  PtrList<coordinateSystem>&& content
148 )
149 :
150  regIOobject(io),
151  PtrList<coordinateSystem>(std::move(content))
152 {
153  readContents();
154 }
156 
157 // * * * * * * * * * * * * * * * * Selectors * * * * * * * * * * * * * * * * //
158 
160 (
161  const objectRegistry& obr
162 )
163 {
164  // Previously registered?
165 
166  const coordinateSystems* ptr = obr.findObject<coordinateSystems>(typeName);
167 
168  if (ptr)
169  {
170  return *ptr;
171  }
172 
173  // Read construct from registry
174  return obr.store(new coordinateSystems(obr));
175 }
176 
177 
178 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
179 
181 {
182  if (key.empty())
183  {
184  return labelList();
185  }
186  return PtrListOps::findMatching(*this, key);
187 }
188 
189 
190 Foam::labelList Foam::coordinateSystems::indices(const wordRes& matcher) const
191 {
192  if (matcher.empty())
193  {
194  return labelList();
195  }
196  return PtrListOps::findMatching(*this, matcher);
197 }
198 
199 
200 Foam::label Foam::coordinateSystems::findIndex(const wordRe& key) const
201 {
202  if (key.empty())
203  {
204  return -1;
205  }
206  return PtrListOps::firstMatching(*this, key);
207 }
208 
209 
210 Foam::label Foam::coordinateSystems::findIndex(const wordRes& matcher) const
211 {
212  if (matcher.empty())
213  {
214  return -1;
215  }
216  return PtrListOps::firstMatching(*this, matcher);
217 }
218 
219 
220 bool Foam::coordinateSystems::found(const wordRe& key) const
221 {
222  return findIndex(key) != -1;
223 }
224 
225 
228 {
229  const label index =
230  (
231  name.empty() ? -1 : PtrListOps::firstMatching(*this, name)
232  );
233 
235  {
237  << "Global coordinate system: "
238  << name << '=' << index << endl;
239  }
240 
241  // Return nullptr if not found
243 }
244 
245 
248 {
249  const coordinateSystem* ptr = this->cfind(name);
250 
251  if (!ptr)
252  {
254  << "Could not find coordinate system: " << name << nl
255  << "available coordinate systems: "
256  << flatOutput(names()) << nl << nl
257  << exit(FatalError);
258  }
260  return *ptr;
261 }
262 
263 
265 {
266  return PtrListOps::names(*this); // match any/all
267 }
268 
269 
271 {
272  if (key.empty())
273  {
274  return wordList();
275  }
276  return PtrListOps::names(*this, key);
277 }
278 
279 
280 Foam::wordList Foam::coordinateSystems::names(const wordRes& matcher) const
281 {
282  if (matcher.empty())
283  {
284  return wordList();
285  }
286  return PtrListOps::names(*this, matcher);
287 }
288 
289 
290 bool Foam::coordinateSystems::writeData(Ostream& os) const
291 {
292  const PtrList<coordinateSystem>& list = *this;
293 
294  os << nl << size() << nl << token::BEGIN_LIST;
295 
296  for (const coordinateSystem& csys : list)
297  {
298  os << nl;
299  csys.writeEntry(csys.name(), os);
300  }
301 
302  os << token::END_LIST << nl;
303 
304  return os.good();
305 }
306 
307 
309 (
311  const bool writeOnProc
312 ) const
313 {
314  // Force ASCII, uncompressed
316  (
318  writeOnProc
319  );
320 }
321 
322 
323 // ************************************************************************* //
Base class for coordinate system specification, the default coordinate system type is cartesian ...
wordList names() const
A list of the coordinate-system names.
const T * test(const label i) const
Return const pointer to element (can be nullptr), or nullptr for out-of-range access (ie...
Definition: UPtrListI.H:127
labelList findMatching(const UPtrList< T > &list, const UnaryMatchPredicate &matcher)
Extract list indices for all items with &#39;name()&#39; that matches.
List< word > names(const UPtrList< T > &list, const UnaryMatchPredicate &matcher)
List of names generated by calling name() for each list item and filtered for matches.
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:125
error FatalError
Error stream (stdout output on all processes), with additional &#39;FOAM FATAL ERROR&#39; header text and sta...
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:598
const word & name() const noexcept
Return the object name.
Definition: IOobjectI.H:195
constexpr char nl
The newline &#39;\n&#39; character (0x0a)
Definition: Ostream.H:50
A centralized collection of named coordinate systems.
"ascii" (normal default)
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:531
bool store()
Register object with its registry and transfer ownership to the registry.
Definition: regIOobjectI.H:36
Begin list [isseparator].
Definition: token.H:161
const Type * findObject(const word &name, const bool recursive=false) const
Return const pointer to the object of the given Type.
Functions to operate on Pointer Lists.
A simple container for options an IOstream can normally have.
virtual bool writeObject(IOstreamOption streamOpt, const bool writeOnProc) const
Write using stream options.
void readIstream(Istream &is, const INew &inew)
Read from Istream using Istream constructor class.
Definition: PtrListIO.C:30
static bool warnAboutAge(const int version) noexcept
Test if an age warning should be emitted.
Definition: error.C:67
defineTypeName(manifoldCellsMeshObject)
label findIndex(const wordRe &key) const
Find and return index for the first match, return -1 if not found.
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for expressions::valueTypeCode::INVALID.
Definition: exprTraits.C:127
static bool master(const label communicator=-1)
Like Pstream::master but with a Pstream::parRun guard in case Pstream has not yet been initialised...
Definition: error.C:53
void close()
Close Istream.
A class for handling words, derived from Foam::string.
Definition: word.H:63
static const word null
An empty word.
Definition: word.H:84
End list [isseparator].
Definition: token.H:162
static const char * headerTypeCompat
Header name for 1806 and earlier.
A wordRe is a Foam::word, but can contain a regular expression for matching words or strings...
Definition: wordRe.H:78
int debug
Static debugging option.
OBJstream os(runTime.globalPath()/outputName)
const coordinateSystem & lookup(const word &name) const
Return reference to named coordinateSystem or FatalErrror.
List< word > wordList
List of word.
Definition: fileName.H:59
const word & headerClassName() const noexcept
Return name of the class name read from header.
Definition: IOobjectI.H:213
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:627
auto key(const Type &t) -> typename std::enable_if< std::is_enum< Type >::value, typename std::underlying_type< Type >::type >::type
Definition: foamGltfBase.H:103
A list of pointers to objects of type <T>, with allocation/deallocation management of the pointers...
Definition: List.H:55
bool good() const noexcept
True if next operation might succeed.
Definition: IOstream.H:281
regIOobject is an abstract class derived from IOobject to handle automatic object registration with t...
Definition: regIOobject.H:66
virtual bool writeObject(IOstreamOption streamOpt, const bool writeOnProc=true) const
Write using stream options.
const coordinateSystem * cfind(const word &name) const
Return pointer to named coordinateSystem or nullptr on error.
bool found(const wordRe &key) const
Search if given key exists.
label findIndex(const ListType &input, typename ListType::const_reference val, const label start=0)
Deprecated(2017-10) search for first occurrence of the given element.
Definition: ListOps.H:485
List< label > labelList
A List of labels.
Definition: List.H:62
IOobject io("surfaceFilmProperties", mesh.time().constant(), mesh, IOobject::READ_IF_PRESENT, IOobject::NO_WRITE, IOobject::NO_REGISTER)
Registry of regIOobjects.
Defines the attributes of an object for which implicit objectRegistry management is supported...
Definition: IOobject.H:172
static const coordinateSystems & New(const objectRegistry &obr)
Return previously registered or read construct from "constant".
bool writeData(Ostream &os) const
Write data.
Istream & readStream(const word &, const bool readOnProc=true)
Return Istream and check object type against that given.
labelList indices(const wordRe &key) const
Find and return indices for all matches.
Namespace for OpenFOAM.
label firstMatching(const UPtrList< T > &list, const UnaryMatchPredicate &matcher)
Find first list item with &#39;name()&#39; that matches, -1 on failure.
FlatOutput::OutputAdaptor< Container, Delimiters > flatOutput(const Container &obj, Delimiters delim)
Global flatOutput() function with specified output delimiters.
Definition: FlatOutput.H:225
IOerror FatalIOError
Error stream (stdout output on all processes), with additional &#39;FOAM FATAL IO ERROR&#39; header text and ...
#define InfoInFunction
Report an information message using Foam::Info.