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 // ************************************************************************* //
Foam::IOobject
Defines the attributes of an object for which implicit objectRegistry management is supported,...
Definition: IOobject.H:165
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:63
Foam::fileName
A class for handling file names.
Definition: fileName.H:71
Foam::polyMesh::defaultRegion
static word defaultRegion
Definition: polyMesh.H:314
mergePolyMesh.H
Foam::argList::getOrDefault
T getOrDefault(const word &optName, const T &deflt) const
Definition: argListI.H:300
topoSet.H
addOverwriteOption.H
Foam::argList::caseName
const fileName & caseName() const noexcept
Definition: argListI.H:62
Foam::fileName::name
static std::string name(const std::string &str)
Definition: fileNameI.H:192
Foam::argList::addNote
static void addNote(const string &note)
Definition: argList.C:405
Foam::argList
Extract command arguments and options from the supplied argc and argv parameters.
Definition: argList.H:119
Foam::endl
Ostream & endl(Ostream &os)
Definition: Ostream.H:381
processorMeshes.H
Foam::argList::get
T get(const label index) const
Definition: argListI.H:271
Foam::argList::readIfPresent
bool readIfPresent(const word &optName, T &val) const
Definition: argListI.H:316
Foam::polyMesh
Mesh consisting of general polyhedral cells.
Definition: polyMesh.H:73
Foam::argList::addArgument
static void addArgument(const string &argName, const string &usage="")
Definition: argList.C:294
Foam::mergePolyMesh
Add a given mesh to the original mesh to create a single new mesh.
Definition: mergePolyMesh.H:50
Foam::Info
messageStream Info
argList.H
Foam::FatalError
error FatalError
Foam::Ostream::write
virtual bool write(const token &tok)=0
Foam
Definition: atmBoundaryLayer.C:26
Foam::error::exit
void exit(const int errNo=1)
Definition: error.C:324
Time.H
Foam::nl
constexpr char nl
Definition: Ostream.H:424
Foam::processorMeshes::removeFiles
static void removeFiles(const polyMesh &mesh)
Definition: processorMeshes.C:267
Foam::fileName::clean
static bool clean(std::string &str)
Definition: fileName.C:192
Foam::cwd
fileName cwd()
Definition: POSIX.C:509
Foam::instant
An instant of time. Contains the time value and name.
Definition: instant.H:48
Foam::argList::check
bool check(bool checkArgs=argList::argsMandatory(), bool checkOpts=true) const
Definition: argList.C:1901
Foam::argList::addOption
static void addOption(const word &optName, const string &param="", const string &usage="", bool advanced=false)
Definition: argList.C:328
args
Foam::argList args(argc, argv)
Foam::topoSet::removeFiles
static void removeFiles(const polyMesh &)
Definition: topoSet.C:628
Foam::argList::found
bool found(const word &optName) const
Definition: argListI.H:171
Foam::pos
dimensionedScalar pos(const dimensionedScalar &ds)
Definition: dimensionedScalar.C:170