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-2024 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 bool Foam::coordinateSystems::readIOcontents()
50 {
51  if (isReadRequired() || (isReadOptional() && headerOk()))
52  {
53  // Attempt reading
54  }
55  else
56  {
57  return false;
58  }
59 
60 
61  // Do reading
62  Istream& is = readStream(word::null);
63 
64  if (headerClassName() == typeName)
65  {
66  this->readIstream(is, coordinateSystem::iNew());
67  close();
68  }
69  else if (headerClassName() == headerTypeCompat)
70  {
71  // Older (1806 and earlier) header name
72  if (error::master())
73  {
74  std::cerr
75  << "--> FOAM IOWarning :" << nl
76  << " Found header class name '" << headerTypeCompat
77  << "' instead of '" << typeName << "'" << nl;
78 
79  error::warnAboutAge("header class", 1806);
80  }
81 
82  this->readIstream(is, coordinateSystem::iNew());
83  close();
84  }
85  else
86  {
88  << "Unexpected class name " << headerClassName()
89  << " expected " << typeName
90  << " or " << headerTypeCompat << nl
91  << " while reading object " << name()
92  << exit(FatalIOError);
93  }
94 
95  return true;
96 }
97 
98 
99 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
100 
102 :
103  regIOobject(io)
104 {
105  readIOcontents();
106 }
107 
108 
110 :
112  (
113  IOobject
114  (
115  typeName,
116  obr.time().constant(),
117  obr,
118  IOobject::READ_IF_PRESENT,
119  IOobject::NO_WRITE
120  )
121  )
122 {}
123 
124 
126 (
127  const IOobject& io,
128  const PtrList<coordinateSystem>& content
129 )
130 :
131  regIOobject(io)
132 {
133  if (!readIOcontents())
134  {
135  static_cast<PtrList<coordinateSystem>&>(*this) = content;
136  }
137 }
138 
139 
141 (
142  const IOobject& io,
143  PtrList<coordinateSystem>&& content
144 )
145 :
146  regIOobject(io),
147  PtrList<coordinateSystem>(std::move(content))
148 {
149  readIOcontents();
150 }
152 
153 // * * * * * * * * * * * * * * * * Selectors * * * * * * * * * * * * * * * * //
154 
156 (
157  const objectRegistry& obr
158 )
159 {
160  // Previously registered?
161 
162  const coordinateSystems* ptr = obr.findObject<coordinateSystems>(typeName);
163 
164  if (ptr)
165  {
166  return *ptr;
167  }
168 
169  // Read construct from registry
170  return obr.store(new coordinateSystems(obr));
171 }
172 
173 
174 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
175 
177 {
178  if (key.empty())
179  {
180  return labelList();
181  }
182  return PtrListOps::findMatching(*this, key);
183 }
184 
185 
186 Foam::labelList Foam::coordinateSystems::indices(const wordRes& matcher) const
187 {
188  if (matcher.empty())
189  {
190  return labelList();
191  }
192  return PtrListOps::findMatching(*this, matcher);
193 }
194 
195 
196 Foam::label Foam::coordinateSystems::findIndex(const wordRe& key) const
197 {
198  if (key.empty())
199  {
200  return -1;
201  }
202  return PtrListOps::firstMatching(*this, key);
203 }
204 
205 
206 Foam::label Foam::coordinateSystems::findIndex(const wordRes& matcher) const
207 {
208  if (matcher.empty())
209  {
210  return -1;
211  }
212  return PtrListOps::firstMatching(*this, matcher);
213 }
214 
215 
216 bool Foam::coordinateSystems::found(const wordRe& key) const
217 {
218  return findIndex(key) != -1;
219 }
220 
221 
224 {
225  const label index =
226  (
227  name.empty() ? -1 : PtrListOps::firstMatching(*this, name)
228  );
229 
231  {
233  << "Global coordinate system: "
234  << name << '=' << index << endl;
235  }
236 
237  // Return nullptr if not found
239 }
240 
241 
244 {
245  const coordinateSystem* ptr = this->cfind(name);
246 
247  if (!ptr)
248  {
250  << "Could not find coordinate system: " << name << nl
251  << "available coordinate systems: "
252  << flatOutput(names()) << nl << nl
253  << exit(FatalError);
254  }
256  return *ptr;
257 }
258 
259 
261 {
262  return PtrListOps::names(*this); // match any/all
263 }
264 
265 
267 {
268  if (key.empty())
269  {
270  return wordList();
271  }
272  return PtrListOps::names(*this, key);
273 }
274 
275 
276 Foam::wordList Foam::coordinateSystems::names(const wordRes& matcher) const
277 {
278  if (matcher.empty())
279  {
280  return wordList();
281  }
282  return PtrListOps::names(*this, matcher);
283 }
284 
285 
286 bool Foam::coordinateSystems::writeData(Ostream& os) const
287 {
288  const PtrList<coordinateSystem>& list = *this;
289 
290  os << nl << size() << nl << token::BEGIN_LIST;
291 
292  for (const coordinateSystem& csys : list)
293  {
294  os << nl;
295  csys.writeEntry(csys.name(), os);
296  }
297 
298  os << token::END_LIST << nl;
299 
300  return os.good();
301 }
302 
303 
305 (
307  const bool writeOnProc
308 ) const
309 {
310  // Force ASCII, uncompressed
312  (
314  writeOnProc
315  );
316 }
317 
318 
319 // ************************************************************************* //
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:608
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 headerOk()
Read and check header info. Does not check the headerClassName.
Definition: regIOobject.C:505
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
bool isReadOptional() const noexcept
True if (LAZY_READ) bits are set [same as READ_IF_PRESENT].
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
coordinateSystems(const coordinateSystems &)=delete
No copy construct.
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)
bool isReadRequired() const noexcept
True if (MUST_READ | READ_MODIFIED) bits are set.
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:637
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
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:68
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:517
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:180
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.