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  if (IOobjectOption::isReadRequired(readOrigin))
161  {
162  readOrigin = IOobjectOption::READ_IF_PRESENT;
163  }
164  }
165  else
166  {
167  // Using top-level dictionary
168  dictPtr = &dict;
169  }
170 
171 
172  // The coordinate-system type (if not cartesian)
173  word modelType;
174  dictPtr->readIfPresent("type", modelType, keyType::LITERAL);
175 
176  return coordinateSystem::New
177  (
178  modelType,
179  *dictPtr,
180  readOrigin,
181  obrPtr
182  );
183 }
184 
185 
187 Foam::coordinateSystem::NewIfPresent
188 (
189  const dictionary& dict,
190  const word& dictName,
191  const objectRegistry* obrPtr
192 )
193 {
194  const dictionary* dictPtr = nullptr;
195 
196  if
197  (
198  dictName.empty()
199  || (dictPtr = dict.findDict(dictName, keyType::LITERAL)) == nullptr
200  )
201  {
202  return nullptr;
203  }
204 
205  // The coordinate-system type (if not cartesian)
206  word modelType;
207  dictPtr->readIfPresent("type", modelType, keyType::LITERAL);
208 
209  return coordinateSystem::New
210  (
211  modelType,
212  *dictPtr,
214  obrPtr
215  );
216 }
217 
218 
219 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
220 
222 Foam::coordinateSystem::New
223 (
224  Istream& is,
225  IOobjectOption::readOption readOrigin
226 )
227 {
228  const word csName(is);
229  const dictionary dict(is);
230 
231  // The coordinate-system type (if not cartesian)
232  word modelType;
233  dict.readIfPresent("type", modelType, keyType::LITERAL);
234 
235  auto cs = coordinateSystem::New(modelType, dict, readOrigin);
236  cs->rename(csName);
238  return cs;
239 }
240 
241 
243 Foam::coordinateSystem::New
244 (
245  const word& modelType,
246  const objectRegistry& obr,
247  const dictionary& dict,
248  IOobjectOption::readOption readOrigin
249 )
250 {
251  return New(modelType, dict, readOrigin, &obr);
252 }
253 
254 
256 Foam::coordinateSystem::New
257 (
258  const word& modelType,
259  const dictionary& dict,
260  IOobjectOption::readOption readOrigin
261 )
262 {
263  return New(modelType, dict, readOrigin, nullptr);
264 }
265 
266 
268 Foam::coordinateSystem::New
269 (
270  const objectRegistry& obr,
271  const dictionary& dict,
272  const word& dictName,
273  IOobjectOption::readOption readOrigin
274 )
275 {
276  return New(dict, dictName, readOrigin, &obr);
277 }
278 
279 
281 Foam::coordinateSystem::New
282 (
283  const dictionary& dict,
284  const word& dictName,
285  IOobjectOption::readOption readOrigin
286 )
287 {
288  return New(dict, dictName, readOrigin, nullptr);
289 }
290 
291 
293 Foam::coordinateSystem::NewIfPresent
294 (
295  const objectRegistry& obr,
296  const dictionary& dict,
297  const word& dictName
298 )
299 {
300  return NewIfPresent(dict, dictName, &obr);
301 }
302 
303 
305 Foam::coordinateSystem::NewIfPresent
306 (
307  const dictionary& dict,
308  const word& dictName
309 )
310 {
311  return NewIfPresent(dict, dictName, nullptr);
312 }
313 
314 
315 // ************************************************************************* //
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:120
dict_pointer dictPtr() const noexcept
Pointer to the found entry as a dictionary, nullptr otherwise.
Definition: dictionary.H:259
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:49
const word dictName("faMeshDefinition")
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:487
tmp< DimensionedField< TypeR, GeoMesh > > New(const tmp< DimensionedField< TypeR, GeoMesh >> &tdf1, 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:453
static bool warnAboutAge(const int version) noexcept
Test if an age warning should be emitted.
Definition: error.C:51
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:37
A class for handling words, derived from Foam::string.
Definition: word.H:63
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...
bool isReadRequired() const noexcept
True if (MUST_READ | MUST_READ_IF_MODIFIED) bits are set.
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:615
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:120
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.