mergeMeshes.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) 2011-2024 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  mergeMeshes
26 
27 Description
28  Merges meshes without stitching.
29 
30 Usage
31  \b mergeMeshes [OPTION]
32 
33  Options:
34  - \par -doc
35  Display the documentation in browser
36 
37  - \par -srcDoc
38  Display the source documentation in browser
39 
40  - \par -help
41  Print the usage
42 
43  - \par -case <dir>
44  Select a case directory instead of the current working directory
45 
46  - \par -region <name>
47  Specify an alternative mesh region.
48 
49  - \par -addRegions "'(region1 region2 ... regionN)'"
50  Specify list of region meshes to merge.
51 
52  - \par -addCases "'(\"casePath1\" \"casePath2\" ... \"casePathN\")'"
53  Specify list of case meshes to merge.
54 
55  - \par -addCaseRegions "'((\"casePath1\" region1) (\"casePath2\" region2)"
56  Specify list of case region meshes to merge.
57 
58 \*---------------------------------------------------------------------------*/
59 
60 #include "argList.H"
61 #include "Time.H"
62 #include "timeSelector.H"
63 #include "mergePolyMesh.H"
64 
65 using namespace Foam;
66 
67 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
68 
69 int main(int argc, char *argv[])
70 {
71  argList::addNote("Merge meshes without stitching");
72 
74 
75  #include "addRegionOption.H"
76  #include "addOverwriteOption.H"
77 
79 
81  (
82  "addRegions",
83  "'(region1 region2 ... regionN)'"
84  "list of regions to merge"
85  );
86 
88  (
89  "addCases",
90  "'(\"casePath1\" \"casePath2\" ... \"casePathN\")'",
91  "list of cases to merge"
92  );
93 
95  (
96  "addCaseRegions",
97  "'((\"casePath1\" region1) (\"casePath2\" region2)"
98  "... (\"casePathN\" regionN))'",
99  "list of case regions to merge"
100  );
101 
102  #include "setRootCase.H"
103 
104  const wordList regions
105  (
107  );
108 
109  const fileNameList cases
110  (
112  (
113  "addCases",
115  )
116  );
117 
118  List<Tuple2<fileName, word>> caseRegions
119  (
121  (
122  "addCaseRegions",
124  )
125  );
126 
127  forAll(cases, i)
128  {
129  caseRegions.append({cases[i], polyMesh::defaultRegion});
130  }
131 
133 
134  // Select time if specified
136 
137  #include "createNamedPolyMesh.H"
138 
139  const bool overwrite = args.optionFound("overwrite");
140  const word oldInstance = mesh.pointsInstance();
141 
142  if (!overwrite)
143  {
144  runTime++;
145  }
146 
147  // Construct the mergePolyMesh class for the current mesh
148  mergePolyMesh mergeMeshes(mesh);
149 
150 
151  // Add all the specified region meshes
152  forAll(regions, i)
153  {
154  Info<< "Create polyMesh for region " << regions[i] << endl;
155  polyMesh meshToAdd
156  (
157  IOobject
158  (
159  regions[i],
160  runTime.name(),
161  runTime
162  )
163  );
164 
165  Info<< "Adding mesh " << meshToAdd.objectPath() << endl;
166  mergeMeshes.addMesh(meshToAdd);
167  }
168 
169  // Add all the specified case meshes
170  forAll(caseRegions, i)
171  {
172  const fileName& addCase = caseRegions[i].first();
173  const word& addRegion = caseRegions[i].second();
174 
175  const fileName addCasePath(addCase.path());
176  const fileName addCaseName(addCase.name());
177 
178  // Construct the time for the new case without reading the controlDict
179  Time runTimeToAdd
180  (
182  addCasePath,
183  addCaseName,
184  false
185  );
186 
187  Info<< "Create polyMesh for case " << runTimeToAdd.path() << endl;
188  polyMesh meshToAdd
189  (
190  IOobject
191  (
192  addRegion,
193  runTimeToAdd.name(),
194  runTimeToAdd
195  )
196  );
197 
198  Info<< "Adding mesh " << meshToAdd.objectPath() << endl;
199  mergeMeshes.addMesh(meshToAdd);
200  }
201 
202  Info << nl << "Merging all meshes" << endl;
203  mergeMeshes.merge();
204 
205  if (overwrite)
206  {
207  mesh.setInstance(oldInstance);
208  }
209 
210  Info<< "Writing mesh to " << mesh.facesInstance() << endl;
211  mesh.write();
212 
213  Info<< "End\n" << endl;
214 
215  return 0;
216 }
217 
218 
219 // ************************************************************************* //
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:434
IOobject defines the attributes of an object for which implicit objectRegistry management is supporte...
Definition: IOobject.H:99
static const List< word > & null()
Return a null List.
Definition: ListI.H:118
virtual Ostream & write(const char)=0
Write character.
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
A 2-tuple for storing two objects of different types.
Definition: Tuple2.H:66
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
static void noParallel()
Remove the parallel options.
Definition: argList.C:175
T optionLookupOrDefault(const word &opt, const T &deflt) const
Read a value from the named option if present.
Definition: argListI.H:243
A class for handling file names.
Definition: fileName.H:82
word name() const
Return file name (part beyond last /)
Definition: fileName.C:195
fileName path() const
Return directory path name (part before last /)
Definition: fileName.C:265
Merge meshes into a single mesh without stitching.
Definition: mergePolyMesh.H:50
Mesh consisting of general polyhedral cells.
Definition: polyMesh.H:80
static word defaultRegion
Return the default region name.
Definition: polyMesh.H:267
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
A class for handling words, derived from string.
Definition: word.H:62
int main(int argc, char *argv[])
Definition: financialFoam.C:44
Namespace for OpenFOAM.
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:257
messageStream Info
static const char nl
Definition: Ostream.H:266
Foam::argList args(argc, argv)