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-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 "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 valid)
50 {
51  Istream& is = readStream(word::null, valid);
52 
53  if (valid)
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::readObject(const IOobject& io)
90 {
91  if
92  (
93  (
94  io.readOpt() == IOobject::MUST_READ
95  || io.readOpt() == IOobject::MUST_READ_IF_MODIFIED
96  )
97  || (io.readOpt() == IOobject::READ_IF_PRESENT && headerOk())
98  )
99  {
100  readFromStream();
101  return true;
102  }
103 
104  return false;
105 }
106 
107 
108 
109 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
110 
111 Foam::coordinateSystems::coordinateSystems(const IOobject& io)
112 :
113  regIOobject(io),
115 {
116  readObject(io);
117 }
118 
119 
120 Foam::coordinateSystems::coordinateSystems
121 (
122  const IOobject& io,
123  const PtrList<coordinateSystem>& content
124 )
125 :
126  regIOobject(io),
128 {
129  if (!readObject(io))
130  {
131  static_cast<PtrList<coordinateSystem>&>(*this) = content;
132  }
133 }
134 
135 
136 Foam::coordinateSystems::coordinateSystems
137 (
138  const IOobject& io,
139  PtrList<coordinateSystem>&& content
140 )
141 :
142  regIOobject(io),
143  PtrList<coordinateSystem>(std::move(content))
144 {
145  readObject(io);
146 }
147 
148 
149 // * * * * * * * * * * * * * * * * Selectors * * * * * * * * * * * * * * * * //
150 
152 (
153  const objectRegistry& obr
154 )
155 {
156  // Previously registered?
157 
158  const coordinateSystems* ptr = obr.findObject<coordinateSystems>(typeName);
159 
160  if (ptr)
161  {
162  return *ptr;
163  }
164 
165  // Read construct from registry
166  return obr.store
167  (
169  (
170  IOobject
171  (
172  typeName,
173  obr.time().constant(),
174  obr,
177  )
178  )
179  );
180 }
181 
182 
183 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
184 
186 {
187  if (key.empty())
188  {
189  return labelList();
190  }
191  return PtrListOps::findMatching(*this, key);
192 }
193 
194 
196 {
197  if (matcher.empty())
198  {
199  return labelList();
200  }
201  return PtrListOps::findMatching(*this, matcher);
202 }
203 
204 
206 {
207  if (key.empty())
208  {
209  return -1;
210  }
211  return PtrListOps::firstMatching(*this, key);
212 }
213 
214 
215 Foam::label Foam::coordinateSystems::findIndex(const wordRes& matcher) const
216 {
217  if (matcher.empty())
218  {
219  return -1;
220  }
221  return PtrListOps::firstMatching(*this, matcher);
222 }
223 
224 
226 {
227  return findIndex(key) != -1;
228 }
229 
230 
233 {
234  const label index =
235  (
236  name.empty() ? -1 : PtrListOps::firstMatching(*this, name)
237  );
238 
240  {
242  << "Global coordinate system: "
243  << name << '=' << index << endl;
244  }
245 
246  if (index < 0)
247  {
248  return nullptr;
249  }
250 
251  return this->operator()(index);
252 }
253 
254 
257 {
258  const coordinateSystem* ptr = this->cfind(name);
259 
260  if (!ptr)
261  {
263  << "Could not find coordinate system: " << name << nl
264  << "available coordinate systems: "
265  << flatOutput(names()) << nl << nl
266  << exit(FatalError);
267  }
268 
269  return *ptr;
270 }
271 
272 
274 {
275  return PtrListOps::names(*this); // match any/all
276 }
277 
278 
280 {
281  if (key.empty())
282  {
283  return wordList();
284  }
285  return PtrListOps::names(*this, key);
286 }
287 
288 
290 {
291  if (matcher.empty())
292  {
293  return wordList();
294  }
295  return PtrListOps::names(*this, matcher);
296 }
297 
298 
300 {
301  const PtrList<coordinateSystem>& list = *this;
302 
303  os << nl << size() << nl << token::BEGIN_LIST;
304 
305  for (const coordinateSystem& csys : list)
306  {
307  os << nl;
308  csys.writeEntry(csys.name(), os);
309  }
310 
311  os << token::END_LIST << nl;
312 
313  return os.good();
314 }
315 
316 
318 (
320  const bool valid
321 ) const
322 {
323  // Force ASCII, uncompressed
325  (
327  valid
328  );
329 }
330 
331 
332 // ************************************************************************* //
Foam::expressions::patchExpr::debug
int debug
Static debugging option.
Foam::IOobject::NO_WRITE
Definition: IOobject.H:195
Foam::labelList
List< label > labelList
A List of labels.
Definition: List.H:67
Foam::IOobject
Defines the attributes of an object for which implicit objectRegistry management is supported,...
Definition: IOobject.H:169
InfoInFunction
#define InfoInFunction
Report an information message using Foam::Info.
Definition: messageStream.H:350
Foam::coordinateSystems::found
bool found(const wordRe &key) const
Search if given key exists.
Definition: coordinateSystems.C:225
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:65
Foam::coordinateSystems::indices
labelList indices(const wordRe &key) const
Find and return indices for all matches.
Definition: coordinateSystems.C:185
Foam::glTF::key
auto key(const Type &t) -> typename std::enable_if< std::is_enum< Type >::value, typename std::underlying_type< Type >::type >::type
Definition: foamGltfBase.H:108
Foam::PtrListOps::findMatching
labelList findMatching(const UPtrList< T > &list, const UnaryMatchPredicate &matcher)
Extract list indices for all items with 'name()' that matches.
Foam::FatalIOError
IOerror FatalIOError
PtrListOps.H
Functions to operate on Pointer Lists.
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
Foam::wordRe
A wordRe is a Foam::word, but can contain a regular expression for matching words or strings.
Definition: wordRe.H:80
Foam::regIOobject::store
bool store()
Definition: regIOobjectI.H:37
Foam::IOstream::good
bool good() const noexcept
True if next operation might succeed.
Definition: IOstream.H:233
Foam::coordinateSystems::lookup
const coordinateSystem & lookup(const word &name) const
Return reference to named coordinateSystem or FatalErrror.
Definition: coordinateSystems.C:256
Foam::wordList
List< word > wordList
A List of words.
Definition: fileName.H:62
Foam::objectRegistry
Registry of regIOobjects.
Definition: objectRegistry.H:60
headerTypeCompat
static const char * headerTypeCompat
Header name for 1806 and earlier.
Definition: coordinateSystems.C:44
Foam::findIndex
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:406
Foam::defineTypeName
defineTypeName(ensightPart)
Foam::IOobject::READ_IF_PRESENT
Definition: IOobject.H:187
Foam::IOstreamOption
The IOstreamOption is a simple container for options an IOstream can normally have.
Definition: IOstreamOption.H:63
Foam::PtrList< coordinateSystem >::readIstream
void readIstream(Istream &is, const INew &inew)
Read from Istream using Istream constructor class.
Definition: PtrListIO.C:38
Foam::PtrList
A list of pointers to objects of type <T>, with allocation/deallocation management of the pointers....
Definition: List.H:59
Foam::coordinateSystems::writeData
bool writeData(Ostream &os) const
Write data.
Definition: coordinateSystems.C:299
Foam::FatalError
error FatalError
Foam::coordinateSystems
A centralized collection of named coordinate systems.
Definition: coordinateSystems.H:78
os
OBJstream os(runTime.globalPath()/outputName)
Foam::regIOobject::writeObject
virtual bool writeObject(IOstreamOption streamOpt, const bool valid) const
Write using stream options.
Definition: regIOobjectWrite.C:36
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::flatOutput
FlatOutput::OutputAdaptor< Container, Delimiters > flatOutput(const Container &obj, Delimiters delim)
Global flatOutput() function with specified output delimiters.
Definition: FlatOutput.H:216
Foam::coordinateSystems::writeObject
virtual bool writeObject(IOstreamOption streamOpt, const bool valid=true) const
Write using stream options.
Definition: coordinateSystems.C:318
Foam::exit
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:130
Foam::IOobject::name
const word & name() const noexcept
Return name.
Definition: IOobjectI.H:65
Time.H
Foam::regIOobject
regIOobject is an abstract class derived from IOobject to handle automatic object registration with t...
Definition: regIOobject.H:73
Foam::IOstreamOption::ASCII
"ascii" (normal default)
Definition: IOstreamOption.H:72
FatalErrorInFunction
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:453
Foam::objectRegistry::findObject
const Type * findObject(const word &name, const bool recursive=false) const
Return const pointer to the object of the given Type.
Definition: objectRegistryTemplates.C:401
Foam::nl
constexpr char nl
Definition: Ostream.H:404
Foam::regIOobject::close
void close()
Close Istream.
Definition: regIOobjectRead.C:171
Foam::List< label >
Foam::error::master
static bool master(const label communicator=-1)
Definition: error.C:41
Foam::coordinateSystems::names
wordList names() const
A list of the coordinate-system names.
Definition: coordinateSystems.C:273
Foam::wordRes
A List of wordRe with additional matching capabilities.
Definition: wordRes.H:51
Foam::word::null
static const word null
An empty word.
Definition: word.H:80
Foam::PtrListOps::firstMatching
label firstMatching(const UPtrList< T > &list, const UnaryMatchPredicate &matcher)
Find first list item with 'name()' that matches, -1 on failure.
Foam::IOobject::MUST_READ_IF_MODIFIED
Definition: IOobject.H:186
Foam::token::END_LIST
End list [isseparator].
Definition: token.H:156
Foam::name
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for INVALID.
Definition: exprTraits.C:59
Foam::coordinateSystems::cfind
const coordinateSystem * cfind(const word &name) const
Return pointer to named coordinateSystem or nullptr on error.
Definition: coordinateSystems.C:232
Foam::error::warnAboutAge
static bool warnAboutAge(const int version) noexcept
Test if an age warning should be emitted.
Definition: error.C:55
FatalIOErrorInFunction
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:473
predicates.H
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:56
Foam::coordinateSystems::New
static const coordinateSystems & New(const objectRegistry &obr)
Return previously registered or read construct from "constant".
Definition: coordinateSystems.C:152
Foam::TimePaths::constant
const word & constant() const
Return constant name.
Definition: TimePathsI.H:96
Foam::token::BEGIN_LIST
Begin list [isseparator].
Definition: token.H:155
Foam::PtrListOps::names
List< word > names(const UPtrList< T > &list, const UnaryMatchPredicate &matcher)
coordinateSystems.H
Foam::objectRegistry::time
const Time & time() const noexcept
Return time registry.
Definition: objectRegistry.H:178
Foam::coordinateSystem
Base class for coordinate system specification, the default coordinate system type is cartesian .
Definition: coordinateSystem.H:132
Foam::coordinateSystems::findIndex
label findIndex(const wordRe &key) const
Find and return index for the first match, return -1 if not found.
Definition: coordinateSystems.C:205
Foam::IOobject::MUST_READ
Definition: IOobject.H:185