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-2015 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 
65  #include "addOverwriteOption.H"
66 
67  argList::validArgs.append("masterCase");
69  (
70  "masterRegion",
71  "name",
72  "specify alternative mesh region for the master mesh"
73  );
74 
75  argList::validArgs.append("addCase");
77  (
78  "addRegion",
79  "name",
80  "specify alternative mesh region for the additional mesh"
81  );
82 
83  argList args(argc, argv);
84  if (!args.check())
85  {
86  FatalError.exit();
87  }
88 
89  const bool overwrite = args.optionFound("overwrite");
90 
91  fileName masterCase = args[1];
92  word masterRegion = polyMesh::defaultRegion;
93  args.optionReadIfPresent("masterRegion", masterRegion);
94 
95  fileName addCase = args[2];
96  word addRegion = polyMesh::defaultRegion;
97  args.optionReadIfPresent("addRegion", addRegion);
98 
99  // Since we don't use argList processor directory detection, add it to
100  // the casename ourselves so it triggers the logic inside TimePath.
101  const fileName& cName = args.caseName();
102  std::string::size_type pos = cName.find("processor");
103  if (pos != string::npos && pos != 0)
104  {
105  fileName processorName = cName.substr(pos, cName.size()-pos);
106  masterCase += '/' + processorName;
107  addCase += '/' + processorName;
108  }
109 
110 
111  getRootCase(masterCase);
112  getRootCase(addCase);
113 
114  Info<< "Master: " << masterCase << " region " << masterRegion << nl
115  << "mesh to add: " << addCase << " region " << addRegion << endl;
116 
117  #include "createTimes.H"
118 
119  Info<< "Reading master mesh for time = " << runTimeMaster.timeName() << nl;
120 
121  Info<< "Create mesh\n" << endl;
122  mergePolyMesh masterMesh
123  (
124  IOobject
125  (
126  masterRegion,
127  runTimeMaster.timeName(),
128  runTimeMaster
129  )
130  );
131  const word oldInstance = masterMesh.pointsInstance();
132 
133 
134  Info<< "Reading mesh to add for time = " << runTimeToAdd.timeName() << nl;
135 
136  Info<< "Create mesh\n" << endl;
137  polyMesh meshToAdd
138  (
139  IOobject
140  (
141  addRegion,
142  runTimeToAdd.timeName(),
143  runTimeToAdd
144  )
145  );
146 
147  if (!overwrite)
148  {
149  runTimeMaster++;
150  }
151 
152  Info<< "Writing combined mesh to " << runTimeMaster.timeName() << endl;
153 
154  masterMesh.addMesh(meshToAdd);
155  masterMesh.merge();
156 
157  if (overwrite)
158  {
159  masterMesh.setInstance(oldInstance);
160  }
161 
162  masterMesh.write();
163 
164  Info<< "\nEnd\n" << endl;
165 
166  return 0;
167 }
168 
169 
170 // ************************************************************************* //
Foam::argList::validArgs
static SLList< string > validArgs
A list of valid (mandatory) arguments.
Definition: argList.H:143
Foam::IOobject
IOobject defines the attributes of an object for which implicit objectRegistry management is supporte...
Definition: IOobject.H:91
Foam::word
A class for handling words, derived from string.
Definition: word.H:59
Foam::fileName
A class for handling file names.
Definition: fileName.H:69
Foam::argList::addOption
static void addOption(const word &opt, const string &param="", const string &usage="")
Add to an option to validOptions with usage information.
Definition: argList.C:108
Foam::argList::addNote
static void addNote(const string &)
Add extra notes for the usage information.
Definition: argList.C:139
Foam::polyMesh::defaultRegion
static word defaultRegion
Return the default region name.
Definition: polyMesh.H:306
mergePolyMesh.H
addOverwriteOption.H
Foam::argList
Extract command arguments and options from the supplied argc and argv parameters.
Definition: argList.H:97
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
Foam::polyMesh
Mesh consisting of general polyhedral cells.
Definition: polyMesh.H:74
Foam::fileName::name
word name() const
Return file name (part beyond last /)
Definition: fileName.C:212
Foam::mergePolyMesh
Add a given mesh to the original mesh to create a single new mesh.
Definition: mergePolyMesh.H:51
Foam::Ostream::write
virtual Ostream & write(const token &)=0
Write next token to stream.
Foam::nl
static const char nl
Definition: Ostream.H:260
Foam::Info
messageStream Info
argList.H
main
int main(int argc, char *argv[])
Definition: postCalc.C:54
size_type
graph_traits< Graph >::vertices_size_type size_type
Definition: SloanRenumber.C:73
Foam::FatalError
error FatalError
Foam
Namespace for OpenFOAM.
Definition: combustionModel.C:30
Foam::error::exit
void exit(const int errNo=1)
Exit : can be called for any error to exit program.
Definition: error.C:165
Foam::argList::optionFound
bool optionFound(const word &opt) const
Return true if the named option is found.
Definition: argListI.H:108
Foam::cwd
fileName cwd()
Return current working directory path name.
Definition: POSIX.C:246
Foam::argList::caseName
const fileName & caseName() const
Return case name (parallel run) or global case (serial run)
Definition: argListI.H:42
Foam::fileName::clean
bool clean()
Cleanup file name.
Definition: fileName.C:97
args
Foam::argList args(argc, argv)
Foam::argList::optionReadIfPresent
bool optionReadIfPresent(const word &opt, T &) const
Read a value from the named option if present.
Definition: argListI.H:198
Foam::argList::check
bool check(bool checkArgs=true, bool checkOpts=true) const
Check argument list.
Definition: argList.C:1184
Foam::pos
dimensionedScalar pos(const dimensionedScalar &ds)
Definition: dimensionedScalar.C:190