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 | Copyright (C) 2011-2016 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 two meshes.
29 
30 \*---------------------------------------------------------------------------*/
31 
32 #include "argList.H"
33 #include "Time.H"
34 #include "mergePolyMesh.H"
35 
36 using namespace Foam;
37 
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 
56 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
57 
58 int main(int argc, char *argv[])
59 {
61  (
62  "merge two meshes"
63  );
64 
66  #include "addOverwriteOption.H"
67 
68  argList::validArgs.append("masterCase");
70  (
71  "masterRegion",
72  "name",
73  "specify alternative mesh region for the master mesh"
74  );
75 
76  argList::validArgs.append("addCase");
78  (
79  "addRegion",
80  "name",
81  "specify alternative mesh region for the additional mesh"
82  );
83 
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;
105 
106  #include "createTimes.H"
107 
108  Info<< "Reading master mesh for time = " << runTimeMaster.timeName() << nl;
109 
110  Info<< "Create mesh\n" << endl;
111  mergePolyMesh masterMesh
112  (
113  IOobject
114  (
115  masterRegion,
116  runTimeMaster.timeName(),
117  runTimeMaster
118  )
119  );
120  const word oldInstance = masterMesh.pointsInstance();
121 
122 
123  Info<< "Reading mesh to add for time = " << runTimeToAdd.timeName() << nl;
124 
125  Info<< "Create mesh\n" << endl;
126  polyMesh meshToAdd
127  (
128  IOobject
129  (
130  addRegion,
131  runTimeToAdd.timeName(),
132  runTimeToAdd
133  )
134  );
135 
136  if (!overwrite)
137  {
138  runTimeMaster++;
139  }
140 
141  Info<< "Writing combined mesh to " << runTimeMaster.timeName() << endl;
142 
143  masterMesh.addMesh(meshToAdd);
144  masterMesh.merge();
145 
146  if (overwrite)
147  {
148  masterMesh.setInstance(oldInstance);
149  }
150 
151  masterMesh.write();
152 
153  Info<< "End\n" << endl;
154 
155  return 0;
156 }
157 
158 
159 // ************************************************************************* //
A class for handling file names.
Definition: fileName.H:69
error FatalError
bool clean()
Cleanup file name.
Definition: fileName.C:77
bool optionReadIfPresent(const word &opt, T &) const
Read a value from the named option if present.
Definition: argListI.H:198
static word defaultRegion
Return the default region name.
Definition: polyMesh.H:306
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:253
static void noParallel()
Remove the parallel options.
Definition: argList.C:146
static SLList< string > validArgs
A list of valid (mandatory) arguments.
Definition: argList.H:154
Add a given mesh to the original mesh to create a single new mesh.
Definition: mergePolyMesh.H:51
void exit(const int errNo=1)
Exit : can be called for any error to exit program.
Definition: error.C:168
word name() const
Return file name (part beyond last /)
Definition: fileName.C:179
bool optionFound(const word &opt) const
Return true if the named option is found.
Definition: argListI.H:108
A class for handling words, derived from string.
Definition: word.H:59
Extract command arguments and options from the supplied argc and argv parameters. ...
Definition: argList.H:102
bool check(bool checkArgs=true, bool checkOpts=true) const
Check argument list.
Definition: argList.C:1170
static void addOption(const word &opt, const string &param="", const string &usage="")
Add to an option to validOptions with usage information.
Definition: argList.C:93
static const char nl
Definition: Ostream.H:262
fileName cwd()
Return current working directory path name.
Definition: POSIX.C:246
messageStream Info
virtual Ostream & write(const token &)=0
Write next token to stream.
Mesh consisting of general polyhedral cells.
Definition: polyMesh.H:74
static void addNote(const string &)
Add extra notes for the usage information.
Definition: argList.C:124
Foam::argList args(argc, argv)
IOobject defines the attributes of an object for which implicit objectRegistry management is supporte...
Definition: IOobject.H:91
Namespace for OpenFOAM.