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 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 "Time.H"
31 
32 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
33 
34 namespace Foam
35 {
36  defineTypeName(coordinateSystems);
37 }
38 
39 // File-local
40 
41 //- Header name for 1806 and earlier
42 static const char* headerTypeCompat = "IOPtrList<coordinateSystem>";
43 
44 
45 // * * * * * * * * * * * * * * * Local Functions * * * * * * * * * * * * * * //
46 
47 namespace Foam
48 {
49  // Templated implementation for names() - file-scope
50  template<class UnaryMatchPredicate>
51  static wordList namesImpl
52  (
53  const PtrList<coordinateSystem>& list,
54  const UnaryMatchPredicate& matcher,
55  const bool doSort
56  )
57  {
58  const label len = list.size();
59 
60  wordList output(len);
61 
62  label count = 0;
63  for (label i = 0; i < len; ++i)
64  {
65  const word& itemName = list[i].name();
66 
67  if (matcher(itemName))
68  {
69  output[count++] = itemName;
70  }
71  }
72 
73  output.resize(count);
74 
75  if (doSort)
76  {
77  Foam::sort(output);
78  }
79 
80  return output;
81  }
82 
83 
84  // Templated implementation for indices() - file-scope
85  template<class UnaryMatchPredicate>
86  static labelList indicesImpl
87  (
88  const PtrList<coordinateSystem>& list,
89  const UnaryMatchPredicate& matcher
90  )
91  {
92  const label len = list.size();
93 
94  labelList output(len);
95 
96  label count = 0;
97  for (label i = 0; i < len; ++i)
98  {
99  if (matcher(list[i].name()))
100  {
101  output[count++] = i;
102  }
103  }
104 
105  output.resize(count);
106 
107  return output;
108  }
109 
110 
111  // Templated implementation for findIndex() - file-scope
112  template<class UnaryMatchPredicate>
114  (
115  const PtrList<coordinateSystem>& list,
116  const UnaryMatchPredicate& matcher
117  )
118  {
119  const label len = list.size();
120 
121  for (label i = 0; i < len; ++i)
122  {
123  if (matcher(list[i].name()))
124  {
125  return i;
126  }
127  }
128 
129  return -1;
130  }
131 
132 } // End namespace Foam
133 
134 
135 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
136 
137 
138 void Foam::coordinateSystems::readFromStream(const bool valid)
139 {
140  Istream& is = readStream(word::null, valid);
141 
142  if (valid)
143  {
144  if (headerClassName() == typeName)
145  {
146  this->readIstream(is, coordinateSystem::iNew());
147  close();
148  }
149  else if (headerClassName() == headerTypeCompat)
150  {
151  // Older (1806 and earlier) header name
152  std::cerr
153  << "--> FOAM IOWarning :" << nl
154  << " Found header class name '" << headerTypeCompat
155  << "' instead of '" << typeName << "'" << nl;
156 
157  error::warnAboutAge("header class", 1806);
158 
159  this->readIstream(is, coordinateSystem::iNew());
160  close();
161  }
162  else
163  {
165  << "unexpected class name " << headerClassName()
166  << " expected " << typeName
167  << " or " << headerTypeCompat << nl
168  << " while reading object " << name()
169  << exit(FatalIOError);
170  }
171  }
172 }
173 
174 
175 bool Foam::coordinateSystems::readObject(const IOobject& io)
176 {
177  if
178  (
179  (
180  io.readOpt() == IOobject::MUST_READ
181  || io.readOpt() == IOobject::MUST_READ_IF_MODIFIED
182  )
183  || (io.readOpt() == IOobject::READ_IF_PRESENT && headerOk())
184  )
185  {
186  readFromStream();
187  return true;
188  }
189 
190  return false;
191 }
192 
193 
194 
195 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
196 
197 Foam::coordinateSystems::coordinateSystems(const IOobject& io)
198 :
199  regIOobject(io),
201 {
202  readObject(io);
203 }
204 
205 
206 Foam::coordinateSystems::coordinateSystems
207 (
208  const IOobject& io,
209  const PtrList<coordinateSystem>& content
210 )
211 :
212  regIOobject(io),
214 {
215  if (!readObject(io))
216  {
217  static_cast<PtrList<coordinateSystem>&>(*this) = content;
218  }
219 }
220 
221 
222 Foam::coordinateSystems::coordinateSystems
223 (
224  const IOobject& io,
225  PtrList<coordinateSystem>&& content
226 )
227 :
228  regIOobject(io),
229  PtrList<coordinateSystem>(std::move(content))
230 {
231  readObject(io);
232 }
233 
234 
235 // * * * * * * * * * * * * * * * * Selectors * * * * * * * * * * * * * * * * //
236 
238 (
239  const objectRegistry& obr
240 )
241 {
242  // Previously registered?
243 
244  const coordinateSystems* ptr = obr.findObject<coordinateSystems>(typeName);
245 
246  if (ptr)
247  {
248  return *ptr;
249  }
250 
251  // Read construct from registry
252  return obr.store
253  (
255  (
256  IOobject
257  (
258  typeName,
259  obr.time().constant(),
260  obr,
263  )
264  )
265  );
266 }
267 
268 
269 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
270 
272 {
273  if (key.empty())
274  {
275  return labelList();
276  }
277  else if (key.isPattern())
278  {
279  // Match as regex
280  regExp matcher(key);
281  return indicesImpl(*this, matcher);
282  }
283  else
284  {
285  // Compare as literal string
286  const word& matcher = key;
287  return indicesImpl(*this, matcher);
288  }
289 }
290 
291 
293 {
294  return indicesImpl(*this, matcher);
295 }
296 
297 
299 {
300  if (key.empty())
301  {
302  return -1;
303  }
304 
305  if (key.isPattern())
306  {
307  // Find as regex
308  regExp matcher(key);
309  return findIndexImpl(*this, matcher);
310  }
311  else
312  {
313  // Find as literal string
314  const word& matcher = key;
315  return findIndexImpl(*this, matcher);
316  }
317 }
318 
319 
321 {
322  return findIndexImpl(*this, matcher);
323 }
324 
325 
327 {
328  return findIndex(key) != -1;
329 }
330 
331 
334 {
335  const label index = this->findIndex(name);
336 
338  {
340  << "Global coordinate system: "
341  << name << "=" << index << endl;
342  }
343 
344  if (index < 0)
345  {
346  return nullptr;
347  }
348 
349  return this->operator()(index);
350 }
351 
352 
355 {
356  const label index = this->findIndex(name);
357 
358  if (index < 0)
359  {
361  << "Could not find coordinate system: " << name << nl
362  << "available coordinate systems: "
363  << flatOutput(names()) << nl << nl
364  << exit(FatalError);
365  }
367  {
369  << "Global coordinate system: "
370  << name << "=" << index << endl;
371  }
372 
373  return this->operator[](index);
374 }
375 
376 
378 {
379  const PtrList<coordinateSystem>& list = *this;
380 
381  wordList result(list.size());
382 
383  forAll(list, i)
384  {
385  result[i] = list[i].name();
386  }
387 
388  return result;
389  // return ListOps::create<word>(list, nameOp<coordinateSystem>());
390 }
391 
392 
394 {
395  if (key.empty())
396  {
397  return wordList();
398  }
399  else if (key.isPattern())
400  {
401  // Find as regex
402  regExp matcher(key);
403  return namesImpl(*this, matcher, false);
404  }
405  else
406  {
407  // Find as literal string
408  const word& matcher = key;
409  return namesImpl(*this, matcher, false);
410  }
411 }
412 
413 
415 {
416  return namesImpl(*this, matcher, false);
417 }
418 
419 
421 {
422  return namesImpl(*this, matcher, false);
423 }
424 
425 
427 {
428  const PtrList<coordinateSystem>& list = *this;
429 
430  os << nl << size() << nl << token::BEGIN_LIST;
431 
432  for (const coordinateSystem& csys : list)
433  {
434  os << nl;
435  csys.writeEntry(csys.name(), os);
436  }
437 
438  os << token::END_LIST << nl;
439 
440  return os.good();
441 }
442 
443 
445 (
449  const bool valid
450 ) const
451 {
452  // Force ASCII writing
454  (
456  ver,
458  valid
459  );
460 }
461 
462 
463 // ************************************************************************* //
Foam::expressions::patchExpr::debug
int debug
Static debugging option.
Foam::IOstreamOption::UNCOMPRESSED
compression = false
Definition: IOstreamOption.H:73
Foam::IOobject::NO_WRITE
Definition: IOobject.H:130
Foam::labelList
List< label > labelList
A List of labels.
Definition: List.H:74
Foam::IOobject
Defines the attributes of an object for which implicit objectRegistry management is supported,...
Definition: IOobject.H:104
Foam::namesImpl
static wordList namesImpl(const PtrList< coordinateSystem > &list, const UnaryMatchPredicate &matcher, const bool doSort)
Definition: coordinateSystems.C:52
Foam::regIOobject::writeObject
virtual bool writeObject(IOstream::streamFormat, IOstream::versionNumber, IOstream::compressionType, const bool valid) const
Write using given format, version and compression.
Definition: regIOobjectWrite.C:39
Foam::coordinateSystems::findIndex
label findIndex(const keyType &key) const
Find and return index for the first match, return -1 if not found.
Definition: coordinateSystems.C:298
InfoInFunction
#define InfoInFunction
Report an information message using Foam::Info.
Definition: messageStream.H:316
Foam::IOobject::name
const word & name() const
Return name.
Definition: IOobjectI.H:46
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:62
Foam::coordinateSystems::indices
labelList indices(const keyType &key) const
Find and return indices for all matches.
Definition: coordinateSystems.C:271
Foam::keyType::isPattern
bool isPattern() const
The keyType is treated as a pattern, not as literal string.
Definition: keyTypeI.H:128
Foam::objectRegistry::time
const Time & time() const
Return time.
Definition: objectRegistry.H:186
Foam::FatalIOError
IOerror FatalIOError
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:337
Foam::wordRe
A wordRe is a Foam::word, but can contain a regular expression for matching words or strings.
Definition: wordRe.H:72
Foam::coordinateSystems::lookupPtr
const coordinateSystem * lookupPtr(const word &name) const
Return pointer to named coordinateSystem or nullptr on error.
Definition: coordinateSystems.C:333
forAll
#define forAll(list, i)
Loop across all elements in list.
Definition: stdFoam.H:290
Foam::findIndexImpl
label findIndexImpl(const faPatchList &list, const UnaryMatchPredicate &matcher)
Definition: faBoundaryMesh.C:95
Foam::indicesImpl
static labelList indicesImpl(const faPatchList &list, const UnaryMatchPredicate &matcher)
Definition: faBoundaryMesh.C:68
Foam::coordinateSystems::lookup
const coordinateSystem & lookup(const word &name) const
Return reference to named coordinateSystem or FatalErrror.
Definition: coordinateSystems.C:354
Foam::wordList
List< word > wordList
A List of words.
Definition: fileName.H:59
Foam::objectRegistry
Registry of regIOobjects.
Definition: objectRegistry.H:60
Foam::keyType
A class for handling keywords in dictionaries.
Definition: keyType.H:60
headerTypeCompat
static const char * headerTypeCompat
Header name for 1806 and earlier.
Definition: coordinateSystems.C:42
Foam::label
intWM_LABEL_SIZE_t label
A label is an int32_t or int64_t as specified by the pre-processor macro WM_LABEL_SIZE.
Definition: label.H:62
Foam::IOstreamOption::versionNumber
Representation of a major/minor version number.
Definition: IOstreamOption.H:79
Foam::name
word name(const complex &c)
Return string representation of complex.
Definition: complex.C:76
Foam::defineTypeName
defineTypeName(coordinateRotation)
Foam::IOobject::READ_IF_PRESENT
Definition: IOobject.H:122
Foam::sort
void sort(UList< T > &a)
Definition: UList.C:241
Foam::PtrList< coordinateSystem >::readIstream
void readIstream(Istream &is, const INew &inew)
Read from Istream using Istream constructor class.
Definition: PtrListIO.C:38
Foam::PtrList< coordinateSystem >
Foam::coordinateSystems::writeData
bool writeData(Ostream &os) const
Write data.
Definition: coordinateSystems.C:426
Foam::regExpCxx
Wrapper around C++11 regular expressions.
Definition: regExpCxx.H:72
Foam::List::resize
void resize(const label newSize)
Adjust allocated size of list.
Definition: ListI.H:139
Foam::IOstreamOption::streamFormat
streamFormat
Data format (ascii | binary)
Definition: IOstreamOption.H:64
Foam::FatalError
error FatalError
Foam::coordinateSystems
A centralized collection of named coordinate systems.
Definition: coordinateSystems.H:77
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::exit
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:130
Foam::IOobject::headerClassName
const word & headerClassName() const
Return name of the class name read from header.
Definition: IOobjectI.H:64
Time.H
Foam::regIOobject
regIOobject is an abstract class derived from IOobject to handle automatic object registration with t...
Definition: regIOobject.H:67
Foam::regIOobject::store
void store()
Transfer ownership of this object to its registry.
Definition: regIOobjectI.H:37
Foam::IOstreamOption::ASCII
"ascii"
Definition: IOstreamOption.H:66
FatalErrorInFunction
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:355
Foam::error::warnAboutAge
static void warnAboutAge(const char *what, const int version)
Emit warning on stderr about something being old.
Definition: error.C:40
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::coordinateSystems::writeObject
virtual bool writeObject(IOstream::streamFormat, IOstream::versionNumber ver, IOstream::compressionType, const bool valid=true) const
Write data.
Definition: coordinateSystems.C:445
Foam::nl
constexpr char nl
Definition: Ostream.H:372
Foam::flatOutput
FlatOutput< Container > flatOutput(const Container &obj, label len=0)
Global flatOutput function.
Definition: FlatOutput.H:85
Foam::regIOobject::close
void close()
Close Istream.
Definition: regIOobjectRead.C:182
Foam::BitOps::count
unsigned int count(const UList< bool > &bools, const bool val=true)
Count number of 'true' entries.
Definition: BitOps.H:74
Foam::List
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: HashTable.H:102
Foam::coordinateSystems::names
wordList names() const
A list of the coordinate-system names.
Definition: coordinateSystems.C:377
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:77
Foam::IOobject::MUST_READ_IF_MODIFIED
Definition: IOobject.H:121
Foam::token::END_LIST
End list [isseparator].
Definition: token.H:118
Foam::IOstreamOption::compressionType
compressionType
Compression treatment (UNCOMPRESSED | COMPRESSED)
Definition: IOstreamOption.H:71
FatalIOErrorInFunction
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:375
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:56
Foam::regIOobject::readStream
Istream & readStream(const word &, const bool valid=true)
Return Istream and check object type against that given.
Definition: regIOobjectRead.C:140
Foam::IOstream::good
bool good() const
Return true if next operation might succeed.
Definition: IOstream.H:216
Foam::coordinateSystems::New
static const coordinateSystems & New(const objectRegistry &obr)
Return previously registered or read construct from "constant".
Definition: coordinateSystems.C:238
Foam::TimePaths::constant
const word & constant() const
Return constant name.
Definition: TimePathsI.H:88
Foam::token::BEGIN_LIST
Begin list [isseparator].
Definition: token.H:117
coordinateSystems.H
Foam::coordinateSystems::found
bool found(const keyType &key) const
Search if given key exists.
Definition: coordinateSystems.C:326
Foam::coordinateSystem
Base class for coordinate system specification, the default coordinate system type is cartesian .
Definition: coordinateSystem.H:122
Foam::IOobject::MUST_READ
Definition: IOobject.H:120