mergeMeshes.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) 2011-2016 OpenFOAM Foundation
9 Copyright (C) 2016-2021 OpenCFD Ltd.
10-------------------------------------------------------------------------------
11License
12 This file is part of OpenFOAM.
13
14 OpenFOAM is free software: you can redistribute it and/or modify it
15 under the terms of the GNU General Public License as published by
16 the Free Software Foundation, either version 3 of the License, or
17 (at your option) any later version.
18
19 OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
20 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22 for more details.
23
24 You should have received a copy of the GNU General Public License
25 along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
26
27Application
28 mergeMeshes
29
30Group
31 grpMeshManipulationUtilities
32
33Description
34 Merges two meshes.
35
36\*---------------------------------------------------------------------------*/
37
38#include "argList.H"
39#include "Time.H"
40#include "mergePolyMesh.H"
41#include "topoSet.H"
42#include "processorMeshes.H"
43
44using namespace Foam;
45
46void getRootCase(fileName& casePath)
47{
48 casePath.clean(); // Remove unneeded ".."
49
50 if (casePath.empty() || casePath == ".")
51 {
52 // handle degenerate form and '.'
53 casePath = cwd();
54 }
55 else if (casePath[0] != '/' && casePath.name() == "..")
56 {
57 // avoid relative cases ending in '..' - makes for very ugly names
58 casePath = cwd()/casePath;
59 casePath.clean(); // Remove unneeded ".."
60 }
61}
62
63
64// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
65
66int main(int argc, char *argv[])
67{
68 argList::addNote
69 (
70 "Merge two meshes"
71 );
72
73 #include "addOverwriteOption.H"
74
75 argList::addArgument("masterCase");
76 argList::addOption
77 (
78 "masterRegion",
79 "name",
80 "Specify alternative mesh region for the master mesh"
81 );
82
83 argList::addArgument("addCase");
84 argList::addOption
85 (
86 "addRegion",
87 "name",
88 "Specify alternative mesh region for the additional mesh"
89 );
90 argList::addOption
91 (
92 "resultTime",
93 "time",
94 "Specify a time for the resulting mesh"
95 );
96
97 argList args(argc, argv);
98 if (!args.check())
99 {
101 }
102
103 const bool overwrite = args.found("overwrite");
104
105 auto masterCase = args.get<fileName>(1);
106 auto addCase = args.get<fileName>(2);
107
108 const word masterRegion =
109 args.getOrDefault<word>("masterRegion", polyMesh::defaultRegion);
110
111 const word addRegion =
112 args.getOrDefault<word>("addRegion", polyMesh::defaultRegion);
113
114 // Since we don't use argList processor directory detection, add it to
115 // the casename ourselves so it triggers the logic inside TimePath.
116 const fileName& cName = args.caseName();
117 const auto pos = cName.find("processor");
118 if (pos != string::npos && pos != 0)
119 {
120 fileName processorName = cName.substr(pos);
121 masterCase += '/' + processorName;
122 addCase += '/' + processorName;
123 }
124
125
126 getRootCase(masterCase);
127 getRootCase(addCase);
128
129 Info<< "Master: " << masterCase << " region " << masterRegion << nl
130 << "mesh to add: " << addCase << " region " << addRegion << endl;
131
132 #include "createTimes.H"
133
134 Info<< "Reading master mesh for time = " << runTimeMaster.timeName() << nl;
135
136 Info<< "Create mesh\n" << endl;
137 mergePolyMesh masterMesh
138 (
140 (
141 masterRegion,
142 runTimeMaster.timeName(),
143 runTimeMaster
144 )
145 );
146
147 Info<< "Reading mesh to add for time = " << runTimeToAdd.timeName() << nl;
148 Info<< "Create mesh\n" << endl;
149 polyMesh meshToAdd
150 (
152 (
153 addRegion,
154 runTimeToAdd.timeName(),
155 runTimeToAdd
156 )
157 );
158
159 word meshInstance = masterMesh.pointsInstance();
160
161 const bool specifiedInstance =
162 (
163 !overwrite
164 && args.readIfPresent("resultTime", meshInstance)
165 );
166
167 if (specifiedInstance)
168 {
169 runTimeMaster.setTime(instant(meshInstance), 0);
170 }
171 else if (!overwrite)
172 {
173 runTimeMaster++;
174 }
175
176 Info<< "Writing combined mesh to " << runTimeMaster.timeName() << endl;
177
178 masterMesh.addMesh(meshToAdd);
179 masterMesh.merge();
180
181 if (overwrite || specifiedInstance)
182 {
183 masterMesh.setInstance(meshInstance);
184 }
185
186 masterMesh.write();
187 topoSet::removeFiles(masterMesh);
188 processorMeshes::removeFiles(masterMesh);
189
190 Info<< "End\n" << endl;
191
192 return 0;
193}
194
195
196// ************************************************************************* //
Defines the attributes of an object for which implicit objectRegistry management is supported,...
Definition: IOobject.H:170
virtual bool write(const token &tok)=0
Write token to stream or otherwise handle it.
Extract command arguments and options from the supplied argc and argv parameters.
Definition: argList.H:124
T get(const label index) const
Get a value from the argument at index.
Definition: argListI.H:278
bool check(bool checkArgs=argList::argsMandatory(), bool checkOpts=true) const
Definition: argList.C:1913
bool found(const word &optName) const
Return true if the named option is found.
Definition: argListI.H:178
bool readIfPresent(const word &optName, T &val) const
Read a value from the named option if present.
Definition: argListI.H:323
T getOrDefault(const word &optName, const T &deflt) const
Get a value from the named option if present, or return default.
Definition: argListI.H:307
const fileName & caseName() const noexcept
Return case name (parallel run) or global case (serial run)
Definition: argListI.H:69
void exit(const int errNo=1)
Exit : can be called for any error to exit program.
Definition: error.C:331
A class for handling file names.
Definition: fileName.H:76
static bool clean(std::string &str)
Definition: fileName.C:199
static std::string name(const std::string &str)
Return basename (part beyond last /), including its extension.
Definition: fileNameI.H:199
An instant of time. Contains the time value and name. Uses Foam::Time when formatting the name.
Definition: instant.H:56
Add a given mesh to the original mesh to create a single new mesh.
Definition: mergePolyMesh.H:57
Mesh consisting of general polyhedral cells.
Definition: polyMesh.H:81
A class for handling words, derived from Foam::string.
Definition: word.H:68
Namespace for OpenFOAM.
fileName cwd()
The physical or logical current working directory path name.
Definition: MSwindows.C:476
dimensionedScalar pos(const dimensionedScalar &ds)
messageStream Info
Information stream (stdout output on master, null elsewhere)
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:372
error FatalError
constexpr char nl
The newline '\n' character (0x0a)
Definition: Ostream.H:53
Foam::argList args(argc, argv)