Skip to content

Commit f47e1b0

Browse files
author
Henry Weller
committed
mergeMeshes: Extended to merge lists of meshes
Description Merges meshes without stitching. Usage \b mergeMeshes [OPTION] Options: - \par -doc Display the documentation in browser - \par -srcDoc Display the source documentation in browser - \par -help Print the usage - \par -case \<dir\> Select a case directory instead of the current working directory - \par -region \<name\> Specify an alternative mesh region. - \par -addRegions "'(region1 region2 ... regionN)'" Specify list of region meshes to merge. - \par -addCases "'(\"casePath1\" \"casePath2\" ... \"casePathN\")'" Specify list of case meshes to merge. - \par -addCaseRegions "'((\"casePath1\" region1) (\"casePath2\" region2)" Specify list of case region meshes to merge.
1 parent a671b73 commit f47e1b0

File tree

5 files changed

+208
-157
lines changed

5 files changed

+208
-157
lines changed

applications/utilities/mesh/manipulation/mergeMeshes/createTimes.H

Lines changed: 0 additions & 23 deletions
This file was deleted.

applications/utilities/mesh/manipulation/mergeMeshes/mergeMeshes.C

Lines changed: 134 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
========= |
33
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
44
\\ / O peration | Website: https://openfoam.org
5-
\\ / A nd | Copyright (C) 2011-2022 OpenFOAM Foundation
5+
\\ / A nd | Copyright (C) 2011-2024 OpenFOAM Foundation
66
\\/ M anipulation |
77
-------------------------------------------------------------------------------
88
License
@@ -25,130 +25,187 @@ Application
2525
mergeMeshes
2626
2727
Description
28-
Merges two meshes.
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.
2957
3058
\*---------------------------------------------------------------------------*/
3159

3260
#include "argList.H"
3361
#include "Time.H"
62+
#include "timeSelector.H"
3463
#include "mergePolyMesh.H"
3564

3665
using namespace Foam;
3766

38-
void getRootCase(fileName& casePath)
39-
{
40-
casePath.clean();
41-
42-
if (casePath.empty() || casePath == ".")
43-
{
44-
// handle degenerate form and '.'
45-
casePath = cwd();
46-
}
47-
else if (casePath[0] != '/' && casePath.name() == "..")
48-
{
49-
// avoid relative cases ending in '..' - makes for very ugly names
50-
casePath = cwd()/casePath;
51-
casePath.clean();
52-
}
53-
}
54-
55-
5667
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
5768

5869
int main(int argc, char *argv[])
5970
{
60-
argList::addNote
61-
(
62-
"merge two meshes"
63-
);
71+
argList::addNote("Merge meshes without stitching");
6472

6573
argList::noParallel();
6674
#include "addOverwriteOption.H"
75+
#include "addRegionOption.H"
6776

68-
argList::validArgs.append("masterCase");
6977
argList::addOption
7078
(
71-
"masterRegion",
72-
"name",
73-
"specify alternative mesh region for the master mesh"
79+
"addRegions",
80+
"'(region1 region2 ... regionN)'"
81+
"list of regions to merge"
7482
);
7583

76-
argList::validArgs.append("addCase");
7784
argList::addOption
7885
(
79-
"addRegion",
80-
"name",
81-
"specify alternative mesh region for the additional mesh"
86+
"addCases",
87+
"'(\"casePath1\" \"casePath2\" ... \"casePathN\")'",
88+
"list of cases to merge"
8289
);
8390

84-
argList args(argc, argv);
85-
if (!args.check())
86-
{
87-
FatalError.exit();
88-
}
89-
90-
const bool overwrite = args.optionFound("overwrite");
91-
92-
fileName masterCase = args[1];
93-
word masterRegion = polyMesh::defaultRegion;
94-
args.optionReadIfPresent("masterRegion", masterRegion);
95-
96-
fileName addCase = args[2];
97-
word addRegion = polyMesh::defaultRegion;
98-
args.optionReadIfPresent("addRegion", addRegion);
99-
100-
getRootCase(masterCase);
101-
getRootCase(addCase);
102-
103-
Info<< "Master: " << masterCase << " region " << masterRegion << nl
104-
<< "mesh to add: " << addCase << " region " << addRegion << endl;
91+
argList::addOption
92+
(
93+
"addCaseRegions",
94+
"'((\"casePath1\" region1) (\"casePath2\" region2)"
95+
"... (\"casePathN\" regionN))'",
96+
"list of case regions to merge"
97+
);
10598

106-
#include "createTimes.H"
99+
#include "setRootCase.H"
107100

108-
Info<< "Reading master mesh for time = " << runTimeMaster.name() << nl;
101+
const wordList regions
102+
(
103+
args.optionLookupOrDefault<wordList>("addRegions", wordList::null())
104+
);
109105

110-
Info<< "Create mesh\n" << endl;
111-
mergePolyMesh masterMesh
106+
const fileNameList cases
112107
(
113-
IOobject
108+
args.optionLookupOrDefault<fileNameList>
114109
(
115-
masterRegion,
116-
runTimeMaster.name(),
117-
runTimeMaster
110+
"addCases",
111+
fileNameList::null()
118112
)
119113
);
120-
const word oldInstance = masterMesh.pointsInstance();
121-
122-
123-
Info<< "Reading mesh to add for time = " << runTimeToAdd.name() << nl;
124114

125-
Info<< "Create mesh\n" << endl;
126-
polyMesh meshToAdd
115+
List<Tuple2<fileName, word>> caseRegions
127116
(
128-
IOobject
117+
args.optionLookupOrDefault<List<Tuple2<fileName, word>>>
129118
(
130-
addRegion,
131-
runTimeToAdd.name(),
132-
runTimeToAdd
119+
"addCaseRegions",
120+
List<Tuple2<fileName, word>>::null()
133121
)
134122
);
135123

124+
forAll(cases, i)
125+
{
126+
caseRegions.append({cases[i], polyMesh::defaultRegion});
127+
}
128+
129+
#include "createTimeNoFunctionObjects.H"
130+
131+
// Select time if specified
132+
timeSelector::selectIfPresent(runTime, args);
133+
134+
#include "createNamedPolyMesh.H"
135+
136+
const bool overwrite = args.optionFound("overwrite");
137+
const word oldInstance = mesh.pointsInstance();
138+
136139
if (!overwrite)
137140
{
138-
runTimeMaster++;
141+
runTime++;
142+
}
143+
144+
// Construct the mergePolyMesh class for the current mesh
145+
mergePolyMesh mergeMeshes(mesh);
146+
147+
148+
// Add all the specified region meshes
149+
forAll(regions, i)
150+
{
151+
Info<< "Create polyMesh for region " << regions[i] << endl;
152+
polyMesh meshToAdd
153+
(
154+
IOobject
155+
(
156+
regions[i],
157+
runTime.name(),
158+
runTime
159+
)
160+
);
161+
162+
Info<< "Adding mesh " << meshToAdd.objectPath() << endl;
163+
mergeMeshes.addMesh(meshToAdd);
139164
}
140165

141-
Info<< "Writing combined mesh to " << runTimeMaster.name() << endl;
166+
// Add all the specified case meshes
167+
forAll(caseRegions, i)
168+
{
169+
const fileName& addCase = caseRegions[i].first();
170+
const word& addRegion = caseRegions[i].second();
171+
172+
const fileName addCasePath(addCase.path());
173+
const fileName addCaseName(addCase.name());
174+
175+
// Construct the time for the new case without reading the controlDict
176+
Time runTimeToAdd
177+
(
178+
// Time::controlDictName,
179+
addCasePath,
180+
addCaseName,
181+
false
182+
);
183+
184+
Info<< "Create polyMesh for case " << runTimeToAdd.path() << endl;
185+
polyMesh meshToAdd
186+
(
187+
IOobject
188+
(
189+
addRegion,
190+
runTimeToAdd.name(),
191+
runTimeToAdd
192+
)
193+
);
194+
195+
Info<< "Adding mesh " << meshToAdd.objectPath() << endl;
196+
mergeMeshes.addMesh(meshToAdd);
197+
}
142198

143-
masterMesh.addMesh(meshToAdd);
144-
masterMesh.merge();
199+
Info << nl << "Merging all meshes" << endl;
200+
mergeMeshes.merge();
145201

146202
if (overwrite)
147203
{
148-
masterMesh.setInstance(oldInstance);
204+
mesh.setInstance(oldInstance);
149205
}
150206

151-
masterMesh.write();
207+
Info<< "Writing mesh to " << mesh.facesInstance() << endl;
208+
mesh.write();
152209

153210
Info<< "End\n" << endl;
154211

0 commit comments

Comments
 (0)