FreeFOAM The Cross-Platform CFD Toolkit
doExtrude2DMesh.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) 1991-2010 OpenCFD Ltd.
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  extrude2DMesh
26 
27 Description
28  Takes 2D mesh (all faces 2 points only, no front and back faces) and
29  creates a 3D mesh by extruding with specified thickness.
30 
31 Usage
32 
33  - extrude2DMesh <thickness>
34 
35  @param <thickness> \n
36  Thickness (in metre) of slab.
37 
38  @param -case <dir> \n
39  Case directory.
40 
41  @param -parallel \n
42  Run in parallel.
43 
44  @param -help \n
45  Display help message.
46 
47  @param -doc \n
48  Display Doxygen API documentation page for this application.
49 
50  @param -srcDoc \n
51  Display Doxygen source documentation page for this application.
52 
53 Note
54  Not sure about the walking of the faces to create the front and back faces.
55  Tested on one .ccm file.
56 
57 \*---------------------------------------------------------------------------*/
58 
59 #include <OpenFOAM/argList.H>
60 #include <OpenFOAM/Time.H>
61 #include <OpenFOAM/polyMesh.H>
63 #include "extrude2DMesh.H"
65 
66 using namespace Foam;
67 
68 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
69 
70 // Main program:
71 
72 int main(int argc, char *argv[])
73 {
74  argList::validArgs.append("thickness");
75  argList::validOptions.insert("overwrite", "");
76 # include <OpenFOAM/setRootCase.H>
77 # include <OpenFOAM/createTime.H>
78  runTime.functionObjects().off();
79 # include <OpenFOAM/createPolyMesh.H>
80  const word oldInstance = mesh.pointsInstance();
81 
82  scalar thickness(readScalar(IStringStream(args.additionalArgs()[0])()));
83  bool overwrite = args.optionFound("overwrite");
84 
85 
86  // Check that mesh is 2D
87  // ~~~~~~~~~~~~~~~~~~~~~
88 
89  const faceList& faces = mesh.faces();
90  forAll(faces, faceI)
91  {
92  if (faces[faceI].size() != 2)
93  {
95  << "Face " << faceI << " size " << faces[faceI].size()
96  << " is not of size 2 so mesh is not proper two-dimensional."
97  << exit(FatalError);
98  }
99  }
100 
101 
102  // Find extrude direction
103  // ~~~~~~~~~~~~~~~~~~~~~~
104 
105  scalar minRange = GREAT;
106  direction extrudeDir = 4; //illegal value.
107 
108  for (direction dir = 0; dir < 3; dir++)
109  {
110  scalarField cmpts(mesh.points().component(dir));
111 
112  scalar range = max(cmpts)-min(cmpts);
113 
114  Info<< "Direction:" << dir << " range:" << range << endl;
115 
116  if (range < minRange)
117  {
118  minRange = range;
119  extrudeDir = dir;
120  }
121  }
122 
123  Info<< "Extruding in direction " << extrudeDir
124  << " with thickness " << thickness << nl
125  << endl;
126 
127 
128 
130 
131 
132  // Add front and back patch
133  // ~~~~~~~~~~~~~~~~~~~~~~~~
134 
135  label frontPatchI = patches.findPatchID("frontAndBack");
136 
137  if (frontPatchI == -1)
138  {
139  // Add patch.
140  List<polyPatch*> newPatches(patches.size()+1);
141 
142  forAll(patches, patchI)
143  {
144  const polyPatch& pp = patches[patchI];
145 
146  newPatches[patchI] = pp.clone
147  (
148  patches,
149  newPatches.size(),
150  pp.size(),
151  pp.start()
152  ).ptr();
153  }
154 
155  frontPatchI = patches.size();
156 
157  newPatches[frontPatchI] = new emptyPolyPatch
158  (
159  "frontAndBack",
160  0,
161  mesh.nFaces(),
162  frontPatchI,
163  patches
164  );
165 
166  Info<< "Adding empty patch " << newPatches[frontPatchI]->name()
167  << " at index " << frontPatchI
168  << " for front and back faces." << nl << endl;
169 
171  mesh.addPatches(newPatches);
172  }
173 
174 
175 
176  // Topo changes container. Initialise with number of patches.
177  polyTopoChange meshMod(mesh.boundaryMesh().size());
178 
179  // Engine to extrude mesh
180  extrude2DMesh extruder(mesh);
181 
182  // Insert changes into meshMod
183  extruder.setRefinement
184  (
185  extrudeDir,
186  thickness,
187  frontPatchI,
188  meshMod
189  );
190 
191  // Create a mesh from topo changes.
192  autoPtr<mapPolyMesh> morphMap = meshMod.changeMesh(mesh, false);
193 
194  mesh.updateMesh(morphMap);
195 
196  if (!overwrite)
197  {
198  runTime++;
199  }
200  else
201  {
202  mesh.setInstance(oldInstance);
203  }
204 
205  // Take over refinement levels and write to new time directory.
206  Pout<< "Writing extruded mesh to time " << runTime.timeName() << nl
207  << endl;
208 
209  mesh.write();
210 
211  Pout<< "End\n" << endl;
212 
213  return 0;
214 }
215 
216 
217 // ************************ vim: set sw=4 sts=4 et: ************************ //