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-2020 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  {
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>
113  label findIndexImpl
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  if (matcher.empty())
295  {
296  return labelList();
297  }
298  return indicesImpl(*this, matcher);
299 }
300 
301 
302 Foam::label Foam::coordinateSystems::findIndex(const keyType& key) const
303 {
304  if (key.empty())
305  {
306  return -1;
307  }
308 
309  if (key.isPattern())
310  {
311  // Find as regex
312  regExp matcher(key);
313  return findIndexImpl(*this, matcher);
314  }
315  else
316  {
317  // Find as literal string
318  const word& matcher = key;
319  return findIndexImpl(*this, matcher);
320  }
321 }
322 
323 
324 Foam::label Foam::coordinateSystems::findIndex(const wordRes& matcher) const
325 {
326  if (matcher.empty())
327  {
328  return -1;
329  }
330  return findIndexImpl(*this, matcher);
331 }
332 
333 
335 {
336  return findIndex(key) != -1;
337 }
338 
339 
342 {
343  const label index = this->findIndex(name);
344 
346  {
348  << "Global coordinate system: "
349  << name << "=" << index << endl;
350  }
351 
352  if (index < 0)
353  {
354  return nullptr;
355  }
356 
357  return this->operator()(index);
358 }
359 
360 
363 {
364  const label index = this->findIndex(name);
365 
366  if (index < 0)
367  {
369  << "Could not find coordinate system: " << name << nl
370  << "available coordinate systems: "
371  << flatOutput(names()) << nl << nl
372  << exit(FatalError);
373  }
375  {
377  << "Global coordinate system: "
378  << name << "=" << index << endl;
379  }
380 
381  return this->operator[](index);
382 }
383 
384 
386 {
387  const PtrList<coordinateSystem>& list = *this;
388 
389  wordList result(list.size());
390 
391  forAll(list, i)
392  {
393  result[i] = list[i].name();
394  }
395 
396  return result;
397  // return ListOps::create<word>(list, nameOp<coordinateSystem>());
398 }
399 
400 
402 {
403  if (key.empty())
404  {
405  return wordList();
406  }
407  else if (key.isPattern())
408  {
409  // Find as regex
410  regExp matcher(key);
411  return namesImpl(*this, matcher, false);
412  }
413  else
414  {
415  // Find as literal string
416  const word& matcher = key;
417  return namesImpl(*this, matcher, false);
418  }
419 }
420 
421 
423 {
424  return namesImpl(*this, matcher, false);
425 }
426 
427 
429 {
430  return namesImpl(*this, matcher, false);
431 }
432 
433 
435 {
436  const PtrList<coordinateSystem>& list = *this;
437 
438  os << nl << size() << nl << token::BEGIN_LIST;
439 
440  for (const coordinateSystem& csys : list)
441  {
442  os << nl;
443  csys.writeEntry(csys.name(), os);
444  }
445 
446  os << token::END_LIST << nl;
447 
448  return os.good();
449 }
450 
451 
453 (
455  const bool valid
456 ) const
457 {
458  // Force ASCII, uncompressed
460  (
462  valid
463  );
464 }
465 
466 
467 // ************************************************************************* //
Foam::expressions::patchExpr::debug
int debug
Static debugging option.
Foam::IOobject::NO_WRITE
Definition: IOobject.H:130
Foam::labelList
List< label > labelList
A List of labels.
Definition: List.H:71
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::coordinateSystems::findIndex
label findIndex(const keyType &key) const
Find and return index for the first match, return -1 if not found.
Definition: coordinateSystems.C:302
InfoInFunction
#define InfoInFunction
Report an information message using Foam::Info.
Definition: messageStream.H:325
Foam::output
static Ostream & output(Ostream &os, const IntRange< T > &range)
Definition: IntRanges.C:66
Foam::IOobject::name
const word & name() const
Return name.
Definition: IOobjectI.H:70
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:350
Foam::wordRe
A wordRe is a Foam::word, but can contain a regular expression for matching words or strings.
Definition: wordRe.H:72
Foam::regIOobject::store
bool store()
Definition: regIOobjectI.H:37
forAll
#define forAll(list, i)
Loop across all elements in list.
Definition: stdFoam.H:296
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:362
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::name
word name(const complex &c)
Return string representation of complex.
Definition: complex.C:76
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:122
Foam::sort
void sort(UList< T > &a)
Definition: UList.C:254
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< coordinateSystem >
Foam::coordinateSystems::writeData
bool writeData(Ostream &os) const
Write data.
Definition: coordinateSystems.C:434
Foam::regExpCxx
Wrapper around C++11 regular expressions.
Definition: regExpCxx.H:72
Foam::FatalError
error FatalError
Foam::coordinateSystems
A centralized collection of named coordinate systems.
Definition: coordinateSystems.H:78
Foam::regIOobject::writeObject
virtual bool writeObject(IOstreamOption streamOpt, const bool valid) const
Write using stream options.
Definition: regIOobjectWrite.C:37
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:217
Foam::coordinateSystems::writeObject
virtual bool writeObject(IOstreamOption streamOpt, const bool valid=true) const
Write using stream options.
Definition: coordinateSystems.C:453
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:88
Time.H
Foam::regIOobject
regIOobject is an abstract class derived from IOobject to handle automatic object registration with t...
Definition: regIOobject.H:71
Foam::IOstreamOption::ASCII
"ascii" (normal default)
Definition: IOstreamOption.H:72
FatalErrorInFunction
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:381
Foam::error::warnAboutAge
static void warnAboutAge(const char *what, const int version)
Emit warning on stderr about something being old.
Definition: error.C:41
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:385
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:77
Foam::List
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: BitOps.H:63
Foam::coordinateSystems::names
wordList names() const
A list of the coordinate-system names.
Definition: coordinateSystems.C:385
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:123
Foam::coordinateSystems::cfind
const coordinateSystem * cfind(const word &name) const
Return pointer to named coordinateSystem or nullptr on error.
Definition: coordinateSystems.C:341
FatalIOErrorInFunction
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:401
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:224
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:122
coordinateSystems.H
Foam::coordinateSystems::found
bool found(const keyType &key) const
Search if given key exists.
Definition: coordinateSystems.C:334
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