surfaceFormatsCore.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-2012 OpenFOAM Foundation
9  Copyright (C) 2017-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 "surfaceFormatsCore.H"
30 #include "Time.H"
31 #include "ListOps.H"
32 #include "surfMesh.H"
33 #include "stringListOps.H"
34 
35 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
36 
38 
39 
40 // * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * //
41 
43 (
44  ISstream& is,
45  const char comment
46 )
47 {
49  do
50  {
51  is.getLine(line);
52  }
53  while ((line.empty() || line[0] == comment) && is.good());
54 
55  return line;
56 }
57 
58 
60 (
61  const surfZoneList& patches,
62  const wordRes& allow,
63  const wordRes& deny
64 )
65 {
66  return
68  (
69  patches,
70  allow,
71  deny,
73  );
74 }
75 
76 
77 #if 0
78 Foam::fileName Foam::fileFormats::surfaceFormatsCore::localMeshFileName
79 (
80  const word& surfName
81 )
82 {
83  const word name(surfName.size() ? surfName : surfaceRegistry::defaultName);
84 
85  return fileName
86  (
88  / name + "." + nativeExt
89  );
90 }
91 
92 
93 Foam::fileName Foam::fileFormats::surfaceFormatsCore::findMeshInstance
94 (
95  const Time& t,
96  const word& surfName
97 )
98 {
99  fileName localName = localMeshFileName(surfName);
100 
101  // Search back through the time directories list to find the time
102  // closest to and lower than current time
103 
104  instantList ts = t.times();
105  label instanceI;
106 
107  for (instanceI = ts.size()-1; instanceI >= 0; --instanceI)
108  {
109  if (ts[instanceI].value() <= t.timeOutputValue())
110  {
111  break;
112  }
113  }
114 
115  // Noting that the current directory has already been searched
116  // for mesh data, start searching from the previously stored time directory
117 
118  if (instanceI >= 0)
119  {
120  for (label i = instanceI; i >= 0; --i)
121  {
122  if (isFile(t.path()/ts[i].name()/localName))
123  {
124  return ts[i].name();
125  }
126  }
127  }
128 
129  return t.constant();
130 }
131 
132 
133 Foam::fileName Foam::fileFormats::surfaceFormatsCore::findMeshFile
134 (
135  const Time& t,
136  const word& surfName
137 )
138 {
139  fileName localName = localMeshFileName(surfName);
140 
141  // Search back through the time directories list to find the time
142  // closest to and lower than current time
143 
144  instantList ts = t.times();
145  label instanceI;
146 
147  for (instanceI = ts.size()-1; instanceI >= 0; --instanceI)
148  {
149  if (ts[instanceI].value() <= t.timeOutputValue())
150  {
151  break;
152  }
153  }
154 
155  // Noting that the current directory has already been searched
156  // for mesh data, start searching from the previously stored time directory
157 
158  if (instanceI >= 0)
159  {
160  for (label i = instanceI; i >= 0; --i)
161  {
162  fileName testName(t.path()/ts[i].name()/localName);
163 
164  if (isFile(testName))
165  {
166  return testName;
167  }
168  }
169  }
170 
171  // fallback to "constant"
172  return t.path()/t.constant()/localName;
173 }
174 #endif
175 
176 
178 (
179  const IOobject& io,
180  const fileName& f,
181  const bool isGlobal
182 )
183 {
184  fileName fName(f);
185  fName.expand();
186  if (!fName.isAbsolute())
187  {
188  // Is the specified file:
189  // - local to the cwd?
190  // - local to the case dir?
191  // - or just another name?
192  fName = fileHandler().filePath
193  (
194  isGlobal,
195  IOobject(io, fName),
196  word::null
197  );
198  }
199  return fName;
200 }
201 
202 
204 (
205  const IOobject& io,
206  const bool isGlobal
207 )
208 {
209  fileName fName
210  (
211  isGlobal
212  ? io.globalFilePath(word::null)
213  : io.localFilePath(word::null)
214  );
215 
216  if (!exists(fName))
217  {
218  fName.clear();
219  }
220 
221  return fName;
222 }
223 
224 
226 (
227  const IOobject& io,
228  const dictionary& dict,
229  const bool isGlobal
230 )
231 {
232  fileName fName;
233  if (dict.readIfPresent("file", fName, keyType::LITERAL))
234  {
235  fName = relativeFilePath(io, fName, isGlobal);
236  }
237  else
238  {
239  fName =
240  (
241  isGlobal
242  ? io.globalFilePath(word::null)
243  : io.localFilePath(word::null)
244  );
245  }
246 
247  if (!exists(fName))
248  {
249  fName.clear();
250  }
251 
252  return fName;
253 }
254 
255 
257 (
258  const IOobject& io,
259  const bool isGlobal
260 )
261 {
262  fileName fName
263  (
264  isGlobal
265  ? io.globalFilePath(word::null)
266  : io.localFilePath(word::null)
267  );
268 
269  if (fName.empty())
270  {
272  << "Cannot find surface starting from "
273  << io.objectPath() << nl
274  << exit(FatalError);
275  }
276 
277  return fName;
278 }
279 
280 
282 (
283  const IOobject& io,
284  const dictionary& dict,
285  const bool isGlobal
286 )
287 {
288  fileName fName;
289  if (dict.readIfPresent("file", fName, keyType::LITERAL))
290  {
291  const fileName rawFName(fName);
292 
293  fName = relativeFilePath(io, rawFName, isGlobal);
294 
295  if (!exists(fName))
296  {
298  << "Cannot find surface " << rawFName
299  << " starting from " << io.objectPath() << nl
300  << exit(FatalError);
301  }
302  }
303  else
304  {
305  fName =
306  (
307  isGlobal
308  ? io.globalFilePath(word::null)
309  : io.localFilePath(word::null)
310  );
311 
312  if (!exists(fName))
313  {
315  << "Cannot find surface starting from "
316  << io.objectPath() << nl
317  << exit(FatalError);
318  }
319  }
320 
321  return fName;
322 }
323 
324 
326 (
327  const wordHashSet& available,
328  const word& fileType,
329  const bool verbose,
330  const char* functionName
331 )
332 {
333  if (available.found(fileType))
334  {
335  return true;
336  }
337  else if (verbose)
338  {
339  Info<< "Unknown file type";
340 
341  if (functionName)
342  {
343  Info<< " for " << functionName;
344  }
345 
346  Info<< " : " << fileType << nl
347  << "Valid types: " << flatOutput(available.sortedToc()) << nl
348  << nl;
349  }
350 
351  return false;
352 }
353 
354 
355 // ************************************************************************* //
static labelList getSelectedPatches(const surfZoneList &patches, const wordRes &allow, const wordRes &deny=wordRes())
Return ids for zone/patch that match by name.
static const word prefix
The prefix to local: surfaces.
dictionary dict
A line primitive.
Definition: line.H:52
A class for handling file names.
Definition: fileName.H:72
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:125
error FatalError
Error stream (stdout output on all processes), with additional &#39;FOAM FATAL ERROR&#39; header text and sta...
A list of keyword definitions, which are a keyword followed by a number of values (eg...
Definition: dictionary.H:129
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:598
bool found(const Key &key) const
Same as contains()
Definition: HashTable.H:1354
constexpr char nl
The newline &#39;\n&#39; character (0x0a)
Definition: Ostream.H:50
refPtr< fileOperation > fileHandler(std::nullptr_t)
Delete current file handler - forwards to fileOperation::handler()
static word nativeExt
The file extension corresponding to &#39;native&#39; surface format.
Operations on lists of strings.
static bool isAbsolute(const std::string &str)
Return true if filename starts with a &#39;/&#39; or &#39;\&#39; or (windows-only) with a filesystem-root.
Definition: fileNameI.H:129
Extract name (as a word) from an object, typically using its name() method.
Definition: word.H:340
static string getLineNoComment(ISstream &is, const char comment='#')
Read non-empty and non-comment line.
Various functions to operate on Lists.
static fileName checkFile(const IOobject &io, const bool isGlobal=true)
Return fileName to load IOobject from.
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for expressions::valueTypeCode::INVALID.
Definition: exprTraits.C:127
static bool checkSupport(const wordHashSet &available, const word &fileType, const bool verbose=false, const char *functionName=nullptr)
Verbose checking of fileType in the list of available types.
static word meshSubDir
Return the mesh sub-directory name (normally "surfMesh")
Definition: surfMesh.H:178
A class for handling words, derived from Foam::string.
Definition: word.H:63
labelList findMatching(const StringListType &input, const wordRes::filter &pred, AccessOp aop=identityOp())
Return ids for items with matching names.
static const word null
An empty word.
Definition: word.H:84
bool exists(const fileName &name, const bool checkGzip=true, const bool followLink=true)
Does the name exist (as DIRECTORY or FILE) in the file system?
Definition: POSIX.C:835
A List of wordRe with additional matching capabilities.
Definition: wordRes.H:53
String literal.
Definition: keyType.H:82
ISstream & getLine(std::string &str, char delim='\n')
Raw, low-level getline (until delimiter) into a string.
Definition: ISstreamI.H:69
labelList f(nPoints)
static fileName relativeFilePath(const IOobject &io, const fileName &f, const bool isGlobal=true)
Return fileName.
bool isFile(const fileName &name, const bool checkGzip=true, const bool followLink=true)
Does the name exist as a FILE in the file system?
Definition: POSIX.C:877
Generic input stream using a standard (STL) stream.
Definition: ISstream.H:51
string & expand(const bool allowEmpty=false)
Inplace expand initial tags, tildes, and all occurrences of environment variables as per stringOps::e...
Definition: string.C:166
bool good() const noexcept
True if next operation might succeed.
Definition: IOstream.H:281
List< Key > sortedToc() const
The table of contents (the keys) in sorted order.
Definition: HashTable.C:139
const polyBoundaryMesh & patches
static word defaultName
The default surface name: default.
messageStream Info
Information stream (stdout output on master, null elsewhere)
IOobject io("surfaceFilmProperties", mesh.time().constant(), mesh, IOobject::READ_IF_PRESENT, IOobject::NO_WRITE, IOobject::NO_REGISTER)
List< instant > instantList
List of instants.
Definition: instantList.H:41
Defines the attributes of an object for which implicit objectRegistry management is supported...
Definition: IOobject.H:172
A class for handling character strings derived from std::string.
Definition: string.H:72
static fileName findFile(const IOobject &io, const bool isGlobal=true)
Use IOobject information to resolve file to load from, or empty if the file does not exist...
FlatOutput::OutputAdaptor< Container, Delimiters > flatOutput(const Container &obj, Delimiters delim)
Global flatOutput() function with specified output delimiters.
Definition: FlatOutput.H:225