faMeshNew.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) 2022-2024 OpenCFD Ltd.
9 -------------------------------------------------------------------------------
10 License
11  This file is part of OpenFOAM.
12 
13  OpenFOAM is free software: you can redistribute it and/or modify it
14  under the terms of the GNU General Public License as published by
15  the Free Software Foundation, either version 3 of the License, or
16  (at your option) any later version.
17 
18  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
19  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
20  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21  for more details.
22 
23  You should have received a copy of the GNU General Public License
24  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
25 
26 \*---------------------------------------------------------------------------*/
27 
28 #include "faMesh.H"
29 #include "polyMesh.H"
30 #include "fileOperation.H"
31 
32 // * * * * * * * * * * * * * * * * Selectors * * * * * * * * * * * * * * * * //
33 
34 bool Foam::faMesh::hasSystemFiles
35 (
36  const word& meshName,
37  const polyMesh& pMesh
38 )
39 {
40  // Expect
41  // - system/finite-area/<region>/faSchemes
42  // - system/finite-area/<region>/faSolution
43 
44  // The directory relative to polyMesh (not Time)
45  const fileName relativeDir
46  (
48  );
49 
50  DebugInfo<< "check system files: " << relativeDir << nl;
51 
52  IOobject systemIOobject
53  (
54  "any-name",
55  pMesh.time().system(),
56  relativeDir,
57  pMesh,
61  );
62 
63  const fileOperation& fp = Foam::fileHandler();
64 
65  bool looksValid = true;
66 
67  // Global files: system/{faSchemes,faSolution}
68  for
69  (
70  const word& expect
71  : List<word>
72  ({
73  {"faSchemes"},
74  {"faSolution"}
75  })
76  )
77  {
78  systemIOobject.resetHeader(expect);
79 
80  fileName found
81  (
82  fp.filePath
83  (
84  true, // global
85  systemIOobject,
86  expect // typeName (ununsed?)
87  )
88  );
89 
90  if (found.empty())
91  {
92  looksValid = false;
93  }
94  }
95 
96  // Only needed on master
97  Pstream::broadcast(looksValid);
98 
99  return looksValid;
100 }
101 
102 
103 bool Foam::faMesh::hasMeshFiles
104 (
105  const word& meshName,
106  const polyMesh& pMesh
107 )
108 {
109  // As well as system/finite-area/{faSchemes,faSolution}
110  //
111  // expect these:
112  // - instance/finite-area/<region>/faMesh/faceLabels
113  // - instance/finite-area/<region>/faMesh/faBoundary
114 
115 
116  // Not required...
117  // bool looksValid = hasSystemFiles(meshName, pMesh);
118 
119  bool looksValid = true;
120 
121  // The mesh directory relative to polyMesh (not Time)
122  const fileName relativeDir
123  (
124  faMesh::meshDir(word::null, meshName)
125  );
126 
127  if (looksValid)
128  {
129  DebugInfo<< "check mesh files: " << relativeDir << nl;
130 
131  const fileOperation& fp = Foam::fileHandler();
132 
133  // The geometry instance for faMesh/faceLabels
134  // Must use READ_IF_PRESENT to avoid aborting if not available
135 
136  const word instance = pMesh.time().findInstance
137  (
138  // Searching from Time, so need polyMesh region too
139  pMesh.regionName()/relativeDir,
140  "faceLabels",
142  );
143 
144  IOobject meshIOobject
145  (
146  "any-name",
147  instance,
148  relativeDir,
149  pMesh,
153  );
154 
155  for
156  (
157  const wordPair& expect
158  : List<wordPair>
159  ({
160  {"faceLabels", "labelList"},
161  {"faBoundary", "faBoundaryMesh"}
162  })
163  )
164  {
165  const word& dataFile = expect.first();
166  const word& dataClass = expect.second();
167 
168  meshIOobject.resetHeader(dataFile);
169 
170  fileName found
171  (
172  fp.filePath
173  (
174  false, // non-global
175  meshIOobject,
176  dataClass // typeName (ununsed?)
177  )
178  );
179 
180  if (found.empty())
181  {
182  looksValid = false;
183  }
184  }
185 
186  // Everybody needs it, or they all fail
187  Pstream::reduceAnd(looksValid);
188  }
189 
190  return looksValid;
191 }
192 
193 
195 (
196  const word& meshName,
197  const polyMesh& pMesh
198 )
199 {
200  if (faMesh::hasMeshFiles(meshName, pMesh))
201  {
202  return autoPtr<faMesh>::New(meshName, pMesh);
203  }
204 
205  return nullptr;
206 }
207 
208 
210 (
211  const polyMesh& pMesh
212 )
213 {
214  return TryNew(polyMesh::defaultRegion, pMesh);
215 }
216 
217 
218 // ************************************************************************* //
const word & regionName() const
The mesh region name or word::null if polyMesh::defaultRegion.
Definition: polyMesh.C:847
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()
Ignore writing from objectRegistry::writeObject()
static void broadcast(Type &value, const label comm=UPstream::worldComm)
Broadcast content (contiguous or non-contiguous) to all communicator ranks. Does nothing in non-paral...
static void reduceAnd(bool &value, const label communicator=worldComm)
Logical (and) reduction (MPI_AllReduce)
fileName meshDir() const
Return the local mesh directory (dbDir()/meshSubDir)
Definition: faMesh.C:1016
static word defaultRegion
Return the default region name.
Definition: polyMesh.H:406
Reading is optional [identical to LAZY_READ].
static const word null
An empty word.
Definition: word.H:84
Pair< word > wordPair
A pair of words.
Definition: Pair.H:55
#define DebugInfo
Report an information message using Foam::Info.
static autoPtr< faMesh > TryNew(const word &meshName, const polyMesh &pMesh)
Read construction from polyMesh if all files are available.
Definition: faMeshNew.C:188
IOobject(const IOobject &)=default
Copy construct.
bool found(const word &name, bool recursive=false) const
Same as contains()
static autoPtr< T > New(Args &&... args)
Construct autoPtr with forwarding arguments.
Definition: autoPtr.H:178
static const word & prefix() noexcept
The prefix to the parent registry name: finite-area.
Definition: faMesh.C:66
bool found
Do not request registration (bool: false)