coordinateSystemNew.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-2015 OpenFOAM Foundation
9  Copyright (C) 2018-2022 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 "objectRegistry.H"
30 #include "cartesianCS.H"
31 #include "indirectCS.H"
32 
33 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
34 
36 Foam::coordinateSystem::New
37 (
38  const word& modelType,
39  const dictionary& dict,
40  IOobjectOption::readOption readOrigin,
41  const objectRegistry* obrPtr
42 )
43 {
44  // Direct dispatch
45  // - treat missing modelType as 'cartesian'
46 
47  if (modelType.empty())
48  {
49  return autoPtr<coordinateSystem>
50  (
51  new coordSystem::cartesian(dict, readOrigin)
52  );
53  }
54 
55 
56  // Dispatch with objectRegistry reference (if possible)
57  if (obrPtr)
58  {
59  auto* ctorPtr = registryConstructorTable(modelType);
60 
61  if (ctorPtr)
62  {
63  return autoPtr<coordinateSystem>
64  (
65  ctorPtr(*obrPtr, dict, readOrigin)
66  );
67  }
68  }
69 
70  // Regular dispatch
71  // Note: everything with a registry constructor also has a
72  // dictionary constructor, so just need to print those on error.
73 
74  auto* ctorPtr = dictionaryConstructorTable(modelType);
75 
76  if (!ctorPtr)
77  {
79  (
80  dict,
81  "coordinate system",
82  modelType,
83  *dictionaryConstructorTablePtr_
84  ) << exit(FatalIOError);
85  }
86 
87  return autoPtr<coordinateSystem>(ctorPtr(dict, readOrigin));
88 }
89 
90 
92 Foam::coordinateSystem::New
93 (
94  const dictionary& dict,
95  const word& dictName,
96  IOobjectOption::readOption readOrigin,
97  const objectRegistry* obrPtr
98 )
99 {
100  const dictionary* dictPtr = nullptr;
101 
102  // If dictName is non-empty: treat as mandatory
103  // Include fallback handling of 'coordinateSystem' sub-dictionary
104  // In 1806 and earlier, this was handled (rather poorly) in the
105  // coordinateSystem constructor itself
106 
107  if (!dictName.empty())
108  {
109  const auto finder = dict.csearch(dictName, keyType::LITERAL);
110 
111  if (finder.isDict())
112  {
113  dictPtr = finder.dictPtr();
114  }
115  else
116  {
117  // Missing or primitive entry: trigger fatal error
118  dictPtr = &(dict.subDict(dictName, keyType::LITERAL));
119  }
120  }
121  else
122  {
123  // Search for "coordinateSystem" sub-dictionary
124 
125  const auto finder =
126  dict.csearch(coordinateSystem::typeName, keyType::LITERAL);
127 
128  if (finder.isDict())
129  {
130  dictPtr = finder.dictPtr();
131  }
132  else if (finder.good())
133  {
134  const word csName(finder.ref().stream());
135 
136  // Deprecated, unsupported syntax
137  if (error::master())
138  {
139  std::cerr
140  << "--> FOAM IOWarning :" << nl
141  << " Ignoring '" << coordinateSystem::typeName
142  << "' as a keyword. Perhaps you meant this instead?" << nl
143  << '{' << nl
144  << " type " << coordSystem::indirect::typeName
145  << ';' << nl
146  << " name " << csName << ';' << nl
147  << '}' << nl
148  << std::endl;
149 
150  error::warnAboutAge("syntax change", 1806);
151  }
152  }
153  }
154 
155 
156  if (dictPtr)
157  {
158  // Using a sub-dictionary
159  // - the 'origin' can be optional
160  readOrigin = IOobjectOption::lazierRead(readOrigin);
161  }
162  else
163  {
164  // Using top-level dictionary
165  dictPtr = &dict;
166  }
167 
168 
169  // The coordinate-system type (if not cartesian)
170  word modelType;
171  dictPtr->readIfPresent("type", modelType, keyType::LITERAL);
172 
173  return coordinateSystem::New
174  (
175  modelType,
176  *dictPtr,
177  readOrigin,
178  obrPtr
179  );
180 }
181 
182 
184 Foam::coordinateSystem::NewIfPresent
185 (
186  const dictionary& dict,
187  const word& dictName,
188  const objectRegistry* obrPtr
189 )
190 {
191  const dictionary* dictPtr = nullptr;
192 
193  if
194  (
195  dictName.empty()
196  || (dictPtr = dict.findDict(dictName, keyType::LITERAL)) == nullptr
197  )
198  {
199  return nullptr;
200  }
201 
202  // The coordinate-system type (if not cartesian)
203  word modelType;
204  dictPtr->readIfPresent("type", modelType, keyType::LITERAL);
205 
206  return coordinateSystem::New
207  (
208  modelType,
209  *dictPtr,
211  obrPtr
212  );
213 }
214 
215 
216 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
217 
219 Foam::coordinateSystem::New
220 (
221  Istream& is,
222  IOobjectOption::readOption readOrigin
223 )
224 {
225  const word csName(is);
226  const dictionary dict(is);
227 
228  // The coordinate-system type (if not cartesian)
229  word modelType;
230  dict.readIfPresent("type", modelType, keyType::LITERAL);
231 
232  auto cs = coordinateSystem::New(modelType, dict, readOrigin);
233  cs->rename(csName);
235  return cs;
236 }
237 
238 
240 Foam::coordinateSystem::New
241 (
242  const word& modelType,
243  const objectRegistry& obr,
244  const dictionary& dict,
245  IOobjectOption::readOption readOrigin
246 )
247 {
248  return New(modelType, dict, readOrigin, &obr);
249 }
250 
251 
253 Foam::coordinateSystem::New
254 (
255  const word& modelType,
256  const dictionary& dict,
257  IOobjectOption::readOption readOrigin
258 )
259 {
260  return New(modelType, dict, readOrigin, nullptr);
261 }
262 
263 
265 Foam::coordinateSystem::New
266 (
267  const objectRegistry& obr,
268  const dictionary& dict,
269  const word& dictName,
270  IOobjectOption::readOption readOrigin
271 )
272 {
273  return New(dict, dictName, readOrigin, &obr);
274 }
275 
276 
278 Foam::coordinateSystem::New
279 (
280  const dictionary& dict,
281  const word& dictName,
282  IOobjectOption::readOption readOrigin
283 )
284 {
285  return New(dict, dictName, readOrigin, nullptr);
286 }
287 
288 
290 Foam::coordinateSystem::NewIfPresent
291 (
292  const objectRegistry& obr,
293  const dictionary& dict,
294  const word& dictName
295 )
296 {
297  return NewIfPresent(dict, dictName, &obr);
298 }
299 
300 
302 Foam::coordinateSystem::NewIfPresent
303 (
304  const dictionary& dict,
305  const word& dictName
306 )
307 {
308  return NewIfPresent(dict, dictName, nullptr);
309 }
310 
311 
312 // ************************************************************************* //
dictionary dict
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:125
A list of keyword definitions, which are a keyword followed by a number of values (eg...
Definition: dictionary.H:129
dict_pointer dictPtr() const noexcept
Pointer to the found entry as a dictionary, nullptr otherwise.
Definition: dictionary.H:268
An Istream is an abstract base class for all input systems (streams, files, token lists etc)...
Definition: Istream.H:57
constexpr char nl
The newline &#39;\n&#39; character (0x0a)
Definition: Ostream.H:50
const word dictName("faMeshDefinition")
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:531
tmp< DimensionedField< TypeR, GeoMesh > > New(const tmp< DimensionedField< TypeR, GeoMesh >> &tf1, const word &name, const dimensionSet &dimensions, const bool initCopy=false)
Global function forwards to reuseTmpDimensionedField::New.
const dictionary & subDict(const word &keyword, enum keyType::option matchOpt=keyType::REGEX) const
Find and return a sub-dictionary.
Definition: dictionary.C:441
static bool warnAboutAge(const int version) noexcept
Test if an age warning should be emitted.
Definition: error.C:67
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
A class for handling words, derived from Foam::string.
Definition: word.H:63
Reading is optional [identical to LAZY_READ].
String literal.
Definition: keyType.H:82
bool readIfPresent(const word &keyword, T &val, enum keyType::option matchOpt=keyType::REGEX) const
Find an entry if present, and assign to T val. FatalIOError if it is found and the number of tokens i...
static readOption lazierRead(readOption opt) noexcept
Downgrade readOption optional (LAZY_READ), leaves NO_READ intact.
const_searcher csearch(const word &keyword, enum keyType::option matchOpt=keyType::REGEX) const
Search dictionary for given keyword.
Registry of regIOobjects.
#define FatalIOErrorInLookup(ios, lookupTag, lookupName, lookupTable)
Report an error message using Foam::FatalIOError.
Definition: error.H:635
const dictionary * findDict(const word &keyword, enum keyType::option matchOpt=keyType::REGEX) const
Find and return a sub-dictionary pointer if present (and a sub-dictionary) otherwise return nullptr...
Definition: dictionaryI.H:124
IOerror FatalIOError
Error stream (stdout output on all processes), with additional &#39;FOAM FATAL IO ERROR&#39; header text and ...
readOption
Enumeration defining read preferences.