reorderPatches.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration | Website: https://openfoam.org
5  \\ / A nd | Copyright (C) 2024-2025 OpenFOAM Foundation
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
11  OpenFOAM is free software: you can redistribute it and/or modify it
12  under the terms of the GNU General Public License as published by
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
16  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19  for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 Application
25  reorderPatches
26 
27 Description
28  Utility to reorder the patches of a case
29 
30  The new patch order may be specified directly as a list of patch names
31  following the -patchOrder option or from the boundary file of a reference
32  case specified using the -referenceCase option with or without the
33  -referenceMesh or -referenceRegion options.
34 
35  This utility run either serial or parallel but either way the reference
36  case boundary file is read from the constant directory.
37 
38 Usage
39  \b reorderPatches
40 
41  Options:
42  - \par -patchOrder <patch names>
43  Specify the list of patch names in the new order.
44 
45  - \par -referenceCase <case path>
46  Specify the reference case path, if not specified the current case
47  is used.
48 
49  - \par -referenceMesh <name>
50  Specify the mesh in the meshes directory of the reference case.
51 
52  - \par -referenceRegion <name>
53  Specify the mesh region for the reference case or reference case mesh
54 
55  - \par -noOverwrite \n
56  Do not replace the old mesh with the new one, writing the new one
57  into a separate time directory
58 
59  - \par -region <name>
60  Specify an alternative mesh region.
61 
62 \*---------------------------------------------------------------------------*/
63 
64 #include "argList.H"
65 #include "fvMesh.H"
67 #include "timeSelector.H"
68 
69 using namespace Foam;
70 
71 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
72 
73 int main(int argc, char *argv[])
74 {
75  timeSelector::addOptions(true, false);
76 
78  (
79  "Utility to reorder the patches of a case.\n"
80  );
81 
82  #include "addNoOverwriteOption.H"
83  #include "addMeshOption.H"
84  #include "addRegionOption.H"
85 
87  (
88  "patchOrder",
89  "wordList",
90  "specify the list of patch names in the new order"
91  );
92 
94  (
95  "referenceCase",
96  "fileName",
97  "specify the reference case path"
98  );
99 
101  (
102  "referenceMesh",
103  "word",
104  "specify the reference case mesh in the meshes directory"
105  );
106 
108  (
109  "referenceRegion",
110  "word",
111  "specify the reference mesh region"
112  );
113 
114  #include "setRootCase.H"
115 
116  wordList referencePatchNames;
117 
118  if (args.optionFound("patchOrder"))
119  {
120  args.optionLookup("patchOrder")() >> referencePatchNames;
121  }
122  else if
123  (
124  args.optionFound("referenceCase")
125  || args.optionFound("referenceMesh")
126  || args.optionFound("referenceRegion")
127  )
128  {
129  const fileName referenceCasePath
130  (
131  args.optionLookupOrDefault<fileName>("referenceCase", args.path())
132  );
133  const fileName rootDirReference = referenceCasePath.path().toAbsolute();
134  const fileName caseDirReference = referenceCasePath.name();
135 
136  const string caseDirOrig = getEnv("FOAM_CASE");
137  const string caseNameOrig = getEnv("FOAM_CASENAME");
138  setEnv("FOAM_CASE", rootDirReference/caseDirReference, true);
139  setEnv("FOAM_CASENAME", caseDirReference, true);
140  Time referenceRunTime
141  (
143  rootDirReference,
144  caseDirReference,
145  false
146  );
147  setEnv("FOAM_CASE", caseDirOrig, true);
148  setEnv("FOAM_CASENAME", caseNameOrig, true);
149 
150  #include "setMeshPath.H"
151 
152  // Initialise the reference mesh path to the current mesh path
153  fileName referenceMeshDir(meshPath);
154 
155  // Optionally set the reference mesh path to that specified
156  if (args.optionFound("referenceMesh"))
157  {
158  referenceMeshDir = "meshes"/args["referenceMesh"];
159  }
160 
161  // Add the optional region to the reference mesh path
162  if (args.optionFound("referenceRegion"))
163  {
164  referenceMeshDir /= args["referenceRegion"];
165  }
166 
167  // Add polyMesh to the reference mesh path
168  referenceMeshDir /= polyMesh::meshSubDir;
169 
171  (
172  "boundary",
173  referenceRunTime.constant(),
174  referenceMeshDir,
175  referenceRunTime,
178  false
179  );
180 
181  if (ioObj.headerOk())
182  {
183  polyBoundaryMeshEntries patchEntries(ioObj);
184 
185  referencePatchNames.setSize(patchEntries.size());
186 
187  forAll(patchEntries, patchi)
188  {
189  referencePatchNames[patchi] = patchEntries[patchi].keyword();
190  }
191  }
192  else
193  {
195  << "Cannot find or read the boundary file for reference mesh "
196  << nl
197  << " " << referenceCasePath/referenceMeshDir
198  << exit(FatalError);
199  }
200  }
201  else
202  {
204  << "Reference patch order not specified"
205  << exit(FatalError);
206  }
207 
209 
210  // Select time if specified
212 
214 
215  #include "setNoOverwrite.H"
216 
217  const word oldInstance = mesh.pointsInstance();
218 
219  if (!overwrite)
220  {
221  runTime++;
222  }
223 
224  if
225  (
226  !Pstream::parRun()
227  && mesh.boundaryMesh().size() != referencePatchNames.size()
228  )
229  {
231  << "Number of reference patches " << referencePatchNames.size()
232  << " is not equal to the number of patches in the mesh "
233  << mesh.boundaryMesh().size()
234  << exit(FatalError);
235  }
236 
237  // Reorder the patches
238  labelList newToOldPatches(identityMap(mesh.boundaryMesh().size()));
239 
240  forAll(referencePatchNames, patchi)
241  {
242  const label oldPatchi =
243  mesh.boundaryMesh().findIndex(referencePatchNames[patchi]);
244 
245  if (oldPatchi != -1)
246  {
247  newToOldPatches[patchi] = oldPatchi;
248  }
249  else
250  {
252  << "Cannot find reference patch " << referencePatchNames[patchi]
253  << " in the mesh which has patches "
254  << mesh.boundaryMesh().names()
255  << exit(FatalError);
256  }
257  }
258  mesh.reorderPatches(newToOldPatches, false);
259 
260  if (overwrite)
261  {
262  mesh.setInstance(oldInstance);
263  }
264 
265  Info<< "Writing mesh to " << mesh.facesInstance() << endl;
266  mesh.write();
267 
268  Info<< "End\n" << endl;
269 
270  return 0;
271 }
272 
273 
274 // ************************************************************************* //
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:433
void size(const label)
Override size to be inconsistent with allocated storage.
Definition: ListI.H:164
void setSize(const label)
Reset size of List.
Definition: List.C:281
Class to control time during OpenFOAM simulations that is also the top-level objectRegistry.
Definition: Time.H:76
static word controlDictName
The default control dictionary name (normally "controlDict")
Definition: Time.H:208
static bool & parRun()
Is this a parallel run?
Definition: UPstream.H:399
label size() const
Return the number of elements in the UPtrList.
Definition: UPtrListI.H:29
static void addOption(const word &opt, const string &param="", const string &usage="")
Add to an option to validOptions with usage information.
Definition: argList.C:128
static void addNote(const string &)
Add extra notes for the usage information.
Definition: argList.C:159
bool optionFound(const word &opt) const
Return true if the named option is found.
Definition: argListI.H:114
fileName path() const
Return the path to the caseName.
Definition: argListI.H:66
IStringStream optionLookup(const word &opt) const
Return an IStringStream from the named option.
Definition: argListI.H:120
T optionLookupOrDefault(const word &opt, const T &deflt) const
Read a value from the named option if present.
Definition: argListI.H:294
A class for handling file names.
Definition: fileName.H:82
word name() const
Return file name (part beyond last /)
Definition: fileName.C:195
fileName & toAbsolute()
Convert from relative to absolute.
Definition: fileName.C:79
fileName path() const
Return directory path name (part before last /)
Definition: fileName.C:284
virtual bool write(const bool write=true) const
Write mesh using IO settings from time.
Definition: fvMesh.C:1785
virtual void reorderPatches(const labelUList &newToOld, const bool validBoundary)
Reorder and trim existing patches. If validBoundary the new.
Definition: fvMesh.C:1702
Foam::polyBoundaryMeshEntries.
label findIndex(const word &patchName) const
Find patch index given a name.
wordList names() const
Return the list of patch names.
const fileName & facesInstance() const
Return the current instance directory for faces.
Definition: polyMesh.C:994
const fileName & pointsInstance() const
Return the current instance directory for points.
Definition: polyMesh.C:988
const polyBoundaryMesh & boundaryMesh() const
Return boundary mesh.
Definition: polyMesh.H:401
static word meshSubDir
Return the mesh sub-directory name (usually "polyMesh")
Definition: polyMesh.H:273
void setInstance(const fileName &)
Set the instance for mesh files.
Definition: polyMeshIO.C:91
static void addOptions(const bool constant=true, const bool withZero=false)
Add the options handled by timeSelector to argList::validOptions.
Definition: timeSelector.C:114
static instantList selectIfPresent(Time &runTime, const argList &args)
If any time option provided return the set of times (as select0)
Definition: timeSelector.C:283
Templated form of IOobject providing type information for file reading and header type checking.
Definition: IOobject.H:530
A class for handling words, derived from string.
Definition: word.H:62
Foam::fvMesh mesh(Foam::IOobject(regionName, runTime.name(), runTime, Foam::IOobject::MUST_READ), false)
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:334
int main(int argc, char *argv[])
Definition: financialFoam.C:44
label patchi
Namespace for OpenFOAM.
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
bool setEnv(const word &name, const std::string &value, const bool overwrite)
Set an environment variable.
Definition: POSIX.C:115
intWM_LABEL_SIZE_t label
A label is an int32_t or int64_t as specified by the pre-processor macro WM_LABEL_SIZE.
Definition: label.H:59
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:258
messageStream Info
string getEnv(const word &)
Return environment variable of given name.
Definition: POSIX.C:97
error FatalError
labelList identityMap(const label len)
Create identity map (map[i] == i) of given length.
Definition: ListOps.C:104
static const char nl
Definition: Ostream.H:267
word meshPath
Definition: setMeshPath.H:1
const bool overwrite
Definition: setNoOverwrite.H:1
Foam::argList args(argc, argv)