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 -------------------------------------------------------------------------------
11 License
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 
27 Application
28  mergeMeshes
29 
30 Group
31  grpMeshManipulationUtilities
32 
33 Description
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 
44 using namespace Foam;
45 
46 void 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 
66 int main(int argc, char *argv[])
67 {
69  (
70  "Merge two meshes"
71  );
72 
73  #include "addOverwriteOption.H"
74 
75  argList::addArgument("masterCase");
77  (
78  "masterRegion",
79  "name",
80  "Specify alternative mesh region for the master mesh"
81  );
82 
83  argList::addArgument("addCase");
85  (
86  "addRegion",
87  "name",
88  "Specify alternative mesh region for the additional mesh"
89  );
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  {
100  FatalError.exit();
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 =
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  (
139  IOobject
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  (
151  IOobject
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 // ************************************************************************* //
static void addNote(const string &note)
Add extra notes for the usage information.
Definition: argList.C:462
static void removeFiles(const polyMesh &mesh)
Helper: remove all procAddressing files from mesh instance.
A class for handling file names.
Definition: fileName.H:71
error FatalError
Error stream (stdout output on all processes), with additional &#39;FOAM FATAL ERROR&#39; header text and sta...
constexpr char nl
The newline &#39;\n&#39; character (0x0a)
Definition: Ostream.H:49
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:487
Add a given mesh to the original mesh to create a single new mesh.
Definition: mergePolyMesh.H:50
T getOrDefault(const word &optName, const T &deflt) const
Get a value from the named option if present, or return default.
Definition: argListI.H:300
void exit(const int errNo=1)
Exit : can be called for any error to exit program.
Definition: error.C:327
dimensionedScalar pos(const dimensionedScalar &ds)
static void removeFiles(const polyMesh &)
Helper: remove all sets files from mesh instance.
Definition: topoSet.C:618
A class for handling words, derived from Foam::string.
Definition: word.H:63
static word defaultRegion
Return the default region name.
Definition: polyMesh.H:397
Extract command arguments and options from the supplied argc and argv parameters. ...
Definition: argList.H:118
static void addOption(const word &optName, const string &param="", const string &usage="", bool advanced=false)
Add an option to validOptions with usage information.
Definition: argList.C:385
virtual bool write(const token &tok)=0
Write token to stream or otherwise handle it.
const fileName & caseName() const noexcept
Return case name (parallel run) or global case (serial run)
Definition: argListI.H:62
static std::string name(const std::string &str)
Return basename (part beyond last /), including its extension.
Definition: fileNameI.H:192
static bool clean(std::string &str)
Cleanup filename string, possibly applies other transformations such as changing the path separator e...
Definition: fileName.C:192
An instant of time. Contains the time value and name. Uses Foam::Time when formatting the name...
Definition: instant.H:53
T get(const label index) const
Get a value from the argument at index.
Definition: argListI.H:271
static void addArgument(const string &argName, const string &usage="")
Append a (mandatory) argument to validArgs.
Definition: argList.C:351
fileName cwd()
The physical or logical current working directory path name.
Definition: POSIX.C:590
messageStream Info
Information stream (stdout output on master, null elsewhere)
Mesh consisting of general polyhedral cells.
Definition: polyMesh.H:73
bool readIfPresent(const word &optName, T &val) const
Read a value from the named option if present.
Definition: argListI.H:316
Foam::argList args(argc, argv)
Defines the attributes of an object for which implicit objectRegistry management is supported...
Definition: IOobject.H:171
bool check(bool checkArgs=argList::argsMandatory(), bool checkOpts=true) const
Check the parsed command-line for mandatory arguments and that all the options are correct...
Definition: argList.C:2118
bool found(const word &optName) const
Return true if the named option is found.
Definition: argListI.H:171
Namespace for OpenFOAM.