Loading...
Searching...
No Matches
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-------------------------------------------------------------------------------
11License
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
36namespace Foam
37{
40
41// File-local
42
43//- Header name for 1806 and earlier
44static const char* headerTypeCompat = "IOPtrList<coordinateSystem>";
45
46
47// * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
48
49bool 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
65 {
66 this->readIstream(is, coordinateSystem::iNew());
67 close();
68 }
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()
93 }
94
95 return true;
97
98
99// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
100
102:
105 readIOcontents();
106}
107
108
110:
112 (
114 (
115 typeName,
116 obr.time().constant(),
117 obr,
118 IOobject::READ_IF_PRESENT,
119 IOobject::NO_WRITE
121 )
122{}
123
124
126(
127 const IOobject& io,
128 const PtrList<coordinateSystem>& content
129)
130:
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();
151
152
153// * * * * * * * * * * * * * * * * Selectors * * * * * * * * * * * * * * * * //
154
156(
157 const objectRegistry& obr
158)
159{
160 // Previously registered?
161
163
164 if (ptr)
165 {
166 return *ptr;
167 }
168
169 // Read construct from registry
170 return obr.store(new coordinateSystems(obr));
172
173
174// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
175
177{
178 if (key.empty())
179 {
180 return labelList();
182 return PtrListOps::findMatching(*this, key);
183}
184
185
186Foam::labelList Foam::coordinateSystems::indices(const wordRes& matcher) const
187{
188 if (matcher.empty())
189 {
190 return labelList();
192 return PtrListOps::findMatching(*this, matcher);
193}
194
195
196Foam::label Foam::coordinateSystems::findIndex(const wordRe& key) const
197{
198 if (key.empty())
199 {
200 return -1;
202 return PtrListOps::firstMatching(*this, key);
203}
204
205
206Foam::label Foam::coordinateSystems::findIndex(const wordRes& matcher) const
207{
208 if (matcher.empty())
209 {
210 return -1;
212 return PtrListOps::firstMatching(*this, matcher);
213}
214
215
216bool 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
230 if (coordinateSystem::debug)
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
262 return PtrListOps::names(*this); // match any/all
263}
264
265
267{
268 if (key.empty())
269 {
270 return wordList();
272 return PtrListOps::names(*this, key);
273}
274
275
276Foam::wordList Foam::coordinateSystems::names(const wordRes& matcher) const
277{
278 if (matcher.empty())
279 {
280 return wordList();
282 return PtrListOps::names(*this, matcher);
283}
284
285
286bool 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;
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// ************************************************************************* //
Functions to operate on Pointer Lists.
bool isReadOptional() const noexcept
True if (LAZY_READ) bits are set [same as READ_IF_PRESENT].
bool isReadRequired() const noexcept
True if (MUST_READ | READ_MODIFIED) bits are set.
@ READ_IF_PRESENT
Reading is optional [identical to LAZY_READ].
@ NO_WRITE
Ignore writing from objectRegistry::writeObject().
Defines the attributes of an object for which implicit objectRegistry management is supported,...
Definition IOobject.H:191
const Time & time() const noexcept
Return Time associated with the objectRegistry.
Definition IOobject.C:456
const word & name() const noexcept
Return the object name.
Definition IOobjectI.H:205
const word & headerClassName() const noexcept
Return name of the class name read from header.
Definition IOobjectI.H:223
A simple container for options an IOstream can normally have.
@ ASCII
"ascii" (normal default)
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition Ostream.H:59
A list of pointers to objects of type <T>, with allocation/deallocation management of the pointers....
Definition PtrList.H:67
void readIstream(Istream &is, const INew &inew)
Definition PtrListIO.C:30
constexpr PtrList() noexcept
Definition PtrListI.H:29
bool empty() const noexcept
True if List is empty (ie, size() is zero).
Definition UList.H:701
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
label size() const noexcept
Definition UPtrListI.H:106
Base class for coordinate system specification, the default coordinate system type is cartesian .
A centralized collection of named coordinate systems.
labelList indices(const wordRe &key) const
Find and return indices for all matches.
const coordinateSystem * cfind(const word &name) const
Return pointer to named coordinateSystem or nullptr on error.
virtual bool writeObject(IOstreamOption streamOpt, const bool writeOnProc=true) const
Write using stream options.
static const coordinateSystems & New(const objectRegistry &obr)
Return previously registered or read construct from "constant".
bool writeData(Ostream &os) const
Write data.
const coordinateSystem & lookup(const word &name) const
Return reference to named coordinateSystem or FatalErrror.
coordinateSystems(const coordinateSystems &)=delete
No copy construct.
wordList names() const
A list of the coordinate-system names.
label findIndex(const wordRe &key) const
Find and return index for the first match, return -1 if not found.
bool found(const wordRe &key) const
Search if given key exists.
static bool master(const int communicator=-1)
Like Pstream::master but with a Pstream::parRun guard in case Pstream has not yet been initialised.
Definition error.C:53
static bool warnAboutAge(const int version) noexcept
Test if an age warning should be emitted.
Definition error.C:67
Registry of regIOobjects.
const Type * findObject(const word &name, const bool recursive=false) const
Return const pointer to the object of the given Type.
regIOobject is an abstract class derived from IOobject to handle automatic object registration with t...
Definition regIOobject.H:71
virtual bool writeObject(IOstreamOption streamOpt, const bool writeOnProc) const
Write using stream options.
void close()
Close Istream.
regIOobject(const IOobject &io, const bool isTimeObject=false)
Construct from IOobject. The optional flag adds special handling if the object is the top-level regIO...
Definition regIOobject.C:43
bool headerOk()
Read and check header info. Does not check the headerClassName.
bool store()
Register object with its registry and transfer ownership to the registry.
@ BEGIN_LIST
Begin list [isseparator].
Definition token.H:174
@ END_LIST
End list [isseparator].
Definition token.H:175
A wordRe is a Foam::word, but can contain a regular expression for matching words or strings.
Definition wordRe.H:81
A List of wordRe with additional matching capabilities.
Definition wordRes.H:56
A class for handling words, derived from Foam::string.
Definition word.H:66
static const word null
An empty word.
Definition word.H:84
#define defineTypeName(Type)
Define the typeName.
Definition className.H:113
static const char * headerTypeCompat
Header name for 1806 and earlier.
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition error.H:629
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition error.H:600
OBJstream os(runTime.globalPath()/outputName)
const auto & io
auto & names
#define InfoInFunction
Report an information message using Foam::Info.
labelList findMatching(const UPtrList< T > &list, const UnaryMatchPredicate &matcher)
Extract list indices for all items with 'name()' that matches.
label firstMatching(const UPtrList< T > &list, const UnaryMatchPredicate &matcher)
Find first list item with 'name()' that matches, -1 on failure.
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.
Different types of constants.
Namespace for OpenFOAM.
List< word > wordList
List of word.
Definition fileName.H:60
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
const word GlobalIOList< Tuple2< scalar, vector > >::typeName("scalarVectorTable")
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition Ostream.H:519
FlatOutput::OutputAdaptor< Container, Delimiters > flatOutput(const Container &obj, Delimiters delim)
Global flatOutput() function with specified output delimiters.
Definition FlatOutput.H:217
IOerror FatalIOError
Error stream (stdout output on all processes), with additional 'FOAM FATAL IO ERROR' header text and ...
error FatalError
Error stream (stdout output on all processes), with additional 'FOAM FATAL ERROR' header text and sta...
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for expressions::valueTypeCode::INVALID.
Definition exprTraits.C:127
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition errorManip.H:125
constexpr char nl
The newline '\n' character (0x0a).
Definition Ostream.H:50