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-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  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 -addMeshes "'(mesh1 mesh2 ... meshN)'"
50  Specify list of meshes to merge.
51 
52  - \par -addRegions "'(region1 region2 ... regionN)'"
53  Specify list of region meshes to merge.
54 
55  - \par -addCases "'(\"casePath1\" \"casePath2\" ... \"casePathN\")'"
56  Specify list of case meshes to merge.
57 
58  - \par -addCaseMeshes "'((\"casePath1\" mesh1) (\"casePath2\" mesh2) ... \
59  (\"casePathN\" meshN))'"
60  Specify list of case meshes to merge.
61 
62  - \par -addCaseRegions "'((\"casePath1\" region1) (\"casePath2\" region2)"
63  Specify list of case region meshes to merge.
64 
65  - \par -addCaseMeshRegions "'((\"casePath1\" mesh1 region1) \
66  (\"casePath2\" mesh2 region2) ... (\"casePathN\" meshN regionN))'"
67  Specify list of case mesh regions to merge.
68 
69 \*---------------------------------------------------------------------------*/
70 
71 #include "argList.H"
72 #include "Time.H"
73 #include "Tuple3.H"
74 #include "timeSelector.H"
75 #include "mergePolyMesh.H"
76 
77 using namespace Foam;
78 
79 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
80 
81 int main(int argc, char *argv[])
82 {
83  argList::addNote("Merge meshes without stitching");
84 
86 
87  #include "addMeshOption.H"
88  #include "addRegionOption.H"
89  #include "addNoOverwriteOption.H"
90 
92 
94  (
95  "addMeshes",
96  "'(mesh1 mesh2 ... meshN)'",
97  "list of meshes to merge"
98  );
99 
101  (
102  "addRegions",
103  "'(region1 region2 ... regionN)'",
104  "list of regions to merge"
105  );
106 
108  (
109  "addMeshRegions",
110  "'((mesh1 region1) (mesh2 region2) ... (mesh3 regionN))'",
111  "list of mesh regions to merge"
112  );
113 
115  (
116  "addCases",
117  "'(\"casePath1\" \"casePath2\" ... \"casePathN\")'",
118  "list of cases to merge"
119  );
120 
122  (
123  "addCaseMeshes",
124  "'((\"casePath1\" mesh1) (\"casePath2\" mesh2)"
125  "... (\"casePathN\" meshN))'",
126  "list of case meshes to merge"
127  );
128 
130  (
131  "addCaseRegions",
132  "'((\"casePath1\" region1) (\"casePath2\" region2)"
133  "... (\"casePathN\" regionN))'",
134  "list of case regions to merge"
135  );
136 
138  (
139  "addCaseMeshRegions",
140  "'((\"casePath1\" mesh1 region1) (\"casePath2\" mesh2 region2)"
141  "... (\"casePathN\" meshN regionN))'",
142  "list of case mesh regions to merge"
143  );
144 
145  #include "setRootCase.H"
146 
147  const wordList meshes
148  (
150  (
151  "addMeshes",
153  )
154  );
155 
156  const wordList regions
157  (
159  (
160  "addRegions",
162  )
163  );
164 
165  List<Tuple2<word, word>> meshRegions
166  (
168  (
169  "addMeshRegions",
170  List<Tuple2<word, word>>::null()
171  )
172  );
173 
174  const fileNameList cases
175  (
177  (
178  "addCases",
180  )
181  );
182 
183  const List<Tuple2<fileName, word>> caseMeshes
184  (
186  (
187  "addCaseMeshes",
189  )
190  );
191 
192  const List<Tuple2<fileName, word>> caseRegions
193  (
195  (
196  "addCaseRegions",
198  )
199  );
200 
201  List<Tuple3<fileName, word, word>> caseMeshRegions
202  (
204  (
205  "addCaseMeshRegions",
207  )
208  );
209 
210  forAll(meshes, i)
211  {
212  meshRegions.append({meshes[i], polyMesh::defaultRegion});
213  }
214 
215  forAll(regions, i)
216  {
217  meshRegions.append({word::null, regions[i]});
218  }
219 
220  forAll(cases, i)
221  {
222  caseMeshRegions.append({cases[i], word::null, polyMesh::defaultRegion});
223  }
224 
225  forAll(caseMeshes, i)
226  {
227  caseMeshRegions.append
228  (
229  {
230  caseMeshes[i].first(),
231  caseMeshes[i].second(),
233  }
234  );
235  }
236 
237  forAll(caseRegions, i)
238  {
239  caseMeshRegions.append
240  (
241  {
242  caseRegions[i].first(),
243  word::null,
244  caseRegions[i].second()
245  }
246  );
247  }
248 
250 
251  // Select time if specified
253 
254  #include "createSpecifiedPolyMesh.H"
255 
256  #include "setNoOverwrite.H"
257  const word oldInstance = mesh.pointsInstance();
258 
259  if (!overwrite)
260  {
261  runTime++;
262  }
263 
264  // Construct the mergePolyMesh class for the current mesh
265  mergePolyMesh mergeMeshes(mesh);
266 
267  // Add all the specified mesh regions
268  forAll(meshRegions, i)
269  {
270  const fileName addLocal =
271  meshRegions[i].first() != word::null
272  ? "meshes"/meshRegions[i].first()
273  : fileName::null;
274  const word& addRegion = meshRegions[i].second();
275 
276  Info<< "Reading polyMesh " << addLocal/addRegion << endl;
277  polyMesh meshToAdd
278  (
279  IOobject
280  (
281  addRegion,
282  runTime.name(),
283  addLocal,
284  runTime
285  )
286  );
287 
288  Info<< "Adding mesh " << meshToAdd.objectPath() << endl;
289  mergeMeshes.addMesh(meshToAdd);
290  }
291 
292  // Add all the specified case meshes
293  forAll(caseMeshRegions, i)
294  {
295  const fileName& addCase = caseMeshRegions[i].first();
296  const fileName addLocal =
297  caseMeshRegions[i].second() != word::null
298  ? "meshes"/caseMeshRegions[i].second()
299  : fileName::null;
300  const word& addRegion = caseMeshRegions[i].third();
301 
302  const fileName addCasePath(addCase.path());
303  const fileName addCaseName(addCase.name());
304 
305  // Construct the time for the new case without reading the controlDict
306  Time runTimeToAdd
307  (
309  addCasePath,
310  addCaseName,
311  false
312  );
313 
314  Info<< "Reading polyMesh for case "
315  << runTimeToAdd.path()/"constant"/addLocal/addRegion << endl;
316  polyMesh meshToAdd
317  (
318  IOobject
319  (
320  addRegion,
321  runTimeToAdd.name(),
322  addLocal,
323  runTimeToAdd
324  )
325  );
326 
327  Info<< "Adding mesh " << meshToAdd.objectPath() << endl;
328  mergeMeshes.addMesh(meshToAdd);
329  }
330 
331  Info << nl << "Merging all meshes" << endl;
332  mergeMeshes.merge();
333 
334  if (overwrite)
335  {
336  mesh.setInstance(oldInstance);
337  }
338 
339  Info<< "Writing mesh to " << mesh.facesInstance() << endl;
340  mesh.write();
341 
342  Info<< "End\n" << endl;
343 
344  return 0;
345 }
346 
347 
348 // ************************************************************************* //
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:433
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
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
A 3-tuple for storing three objects of different types.
Definition: Tuple3.H:60
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
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:294
A class for handling file names.
Definition: fileName.H:82
word name() const
Return file name (part beyond last /)
Definition: fileName.C:195
static const fileName null
An empty fileName.
Definition: fileName.H:97
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
Merge meshes into a single mesh without stitching.
Definition: mergePolyMesh.H:50
Mesh consisting of general polyhedral cells.
Definition: polyMesh.H:80
const fileName & facesInstance() const
Return the current instance directory for faces.
Definition: polyMesh.C:994
static word defaultRegion
Return the default region name.
Definition: polyMesh.H:270
const fileName & pointsInstance() const
Return the current instance directory for points.
Definition: polyMesh.C:988
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
A class for handling words, derived from string.
Definition: word.H:62
static const word null
An empty word.
Definition: word.H:77
Foam::fvMesh mesh(Foam::IOobject(regionName, runTime.name(), runTime, Foam::IOobject::MUST_READ), false)
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:258
messageStream Info
labelList second(const UList< labelPair > &p)
Definition: patchToPatch.C:49
static const char nl
Definition: Ostream.H:267
const bool overwrite
Definition: setNoOverwrite.H:1
Foam::argList args(argc, argv)