extrude2DMeshApp.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  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 Note
32  Not sure about the walking of the faces to create the front and back faces.
33 
34 \*---------------------------------------------------------------------------*/
35 
36 #include "argList.H"
37 #include "Time.H"
38 #include "polyMesh.H"
39 #include "extrude2DMesh.H"
40 #include "extrudeModel.H"
41 #include "polyTopoChange.H"
42 #include "MeshedSurface.H"
43 #include "edgeCollapser.H"
44 #include "addPatchCellLayer.H"
45 #include "patchToPoly2DMesh.H"
46 #include "globalIndex.H"
47 
48 using namespace Foam;
49 
50 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
51 
52 enum ExtrudeMode
53 {
54  POLYMESH2D,
55  MESHEDSURFACE
56 };
57 
58 namespace Foam
59 {
60  template<>
61  const char* NamedEnum<ExtrudeMode, 2>::names[] =
62  {
63  "polyMesh2D",
64  "MeshedSurface"
65  };
66 }
67 
68 static const NamedEnum<ExtrudeMode, 2> ExtrudeModeNames;
69 
70 
71 //pointField moveInitialPoints
72 //(
73 // primitiveFacePatch& fMesh,
74 // const extrudeModel& model
75 //)
76 //{
77 // pointField layer0Points(fMesh.nPoints());
78 // pointField layer1Points(fMesh.nPoints());
79 // pointField displacement(fMesh.nPoints());
80 
81 // forAll(layer0Points, pointi)
82 // {
83 // const labelList& meshPoints = fMesh.meshPoints();
84 // label meshPointi = meshPoints[pointi];
85 
86 // layer0Points[meshPointi] = model
87 // (
88 // fMesh.points()[meshPointi],
89 // fMesh.pointNormals()[pointi],
90 // 0
91 // );
92 
93 // layer1Points[meshPointi] = model
94 // (
95 // fMesh.points()[meshPointi],
96 // fMesh.pointNormals()[pointi],
97 // 1
98 // );
99 
100 // displacement[pointi] =
101 // layer1Points[meshPointi]
102 // - layer0Points[meshPointi];
103 // }
104 
105 // fMesh.movePoints(layer0Points);
106 
107 // return displacement;
108 //}
109 
110 
111 
112 int main(int argc, char *argv[])
113 {
114  argList::validArgs.append("surfaceFormat");
115 
116  #include "addOverwriteOption.H"
117 
118  #include "setRootCase.H"
119 
120  Info<< "Create time\n" << endl;
121 
122  Time runTimeExtruded
123  (
125  args.rootPath(),
126  args.caseName()
127  );
128 
129  runTimeExtruded.functionObjects().off();
130 
131  const ExtrudeMode surfaceFormat = ExtrudeModeNames[args[1]];
132  const bool overwrite = args.optionFound("overwrite");
133 
134  Info<< "Extruding from " << ExtrudeModeNames[surfaceFormat]
135  << " at time " << runTimeExtruded.timeName() << endl;
136 
137  IOdictionary extrude2DMeshDict
138  (
139  IOobject
140  (
141  "extrude2DMeshDict",
142  runTimeExtruded.system(),
143  runTimeExtruded,
146  false
147  )
148  );
149 
150  // Point generator
151  autoPtr<extrudeModel> model(extrudeModel::New(extrude2DMeshDict));
152 
154 
156 
157  autoPtr<polyTopoChange> meshMod;
158 
159  labelListList extrudeEdgePatches;
160 
161  if (surfaceFormat == MESHEDSURFACE)
162  {
163  fMesh.set(new MeshedSurface<face>("MeshedSurface.obj"));
164 
165  EdgeMap<label> edgeRegionMap;
166  wordList patchNames(1, "default");
167  labelList patchSizes(1, fMesh().nEdges() - fMesh().nInternalEdges());
168 
169  const edgeList& edges = fMesh().edges();
170  forAll(edges, edgeI)
171  {
172  if (!fMesh().isInternalEdge(edgeI))
173  {
174  edgeRegionMap.insert(edges[edgeI], 0);
175  }
176  }
177 
178  patchToPoly2DMesh poly2DMesh
179  (
180  fMesh(),
181  patchNames,
182  patchSizes,
183  edgeRegionMap
184  );
185 
186  poly2DMesh.createMesh();
187 
188  mesh.set
189  (
190  new polyMesh
191  (
192  IOobject
193  (
195  runTimeExtruded.constant(),
196  runTimeExtruded,
199  false
200  ),
201  xferMove(poly2DMesh.points()),
202  xferMove(poly2DMesh.faces()),
203  xferMove(poly2DMesh.owner()),
204  xferMove(poly2DMesh.neighbour())
205  )
206  );
207 
208  Info<< "Constructing patches." << endl;
209  List<polyPatch*> patches(poly2DMesh.patchNames().size());
210 
212  {
213  patches[patchi] = new polyPatch
214  (
215  poly2DMesh.patchNames()[patchi],
216  poly2DMesh.patchSizes()[patchi],
217  poly2DMesh.patchStarts()[patchi],
218  patchi,
219  mesh().boundaryMesh(),
220  polyPatch::typeName
221  );
222  }
223 
225  }
226  else if (surfaceFormat == POLYMESH2D)
227  {
228  mesh.set
229  (
230  new polyMesh
231  (
233  (
235  runTimeExtruded.timeName(),
236  runTimeExtruded,
238  )
239  )
240  );
241  }
242 
243  // Engine to extrude mesh
244  extrude2DMesh extruder(mesh(), extrude2DMeshDict, model());
245 
246  extruder.addFrontBackPatches();
247 
248  meshMod.set(new polyTopoChange(mesh().boundaryMesh().size()));
249 
250  extruder.setRefinement(meshMod());
251 
252  // Create a mesh from topo changes.
253  autoPtr<mapPolyMesh> morphMap = meshMod().changeMesh(mesh(), false);
254 
255  mesh().updateMesh(morphMap);
256 
257  {
258  edgeCollapser collapser(mesh());
259 
260  const edgeList& edges = mesh().edges();
261  const pointField& points = mesh().points();
262 
263  const boundBox& bb = mesh().bounds();
264  const scalar mergeDim = 1e-4 * bb.minDim();
265 
266  PackedBoolList collapseEdge(mesh().nEdges());
267  Map<point> collapsePointToLocation(mesh().nPoints());
268 
269  forAll(edges, edgeI)
270  {
271  const edge& e = edges[edgeI];
272 
273  scalar d = e.mag(points);
274 
275  if (d < mergeDim)
276  {
277  Info<< "Merging edge " << e << " since length " << d
278  << " << " << mergeDim << nl;
279 
280  collapseEdge[edgeI] = true;
281  collapsePointToLocation.set(e[1], points[e[0]]);
282  }
283  }
284 
285  List<pointEdgeCollapse> allPointInfo;
287  labelList pointPriority(mesh().nPoints(), 0);
288 
289  collapser.consistentCollapse
290  (
291  globalPoints,
292  pointPriority,
293  collapsePointToLocation,
294  collapseEdge,
295  allPointInfo
296  );
297 
298  polyTopoChange meshModCollapse(mesh());
299 
300  collapser.setRefinement(allPointInfo, meshModCollapse);
301 
302  // Create a mesh from topo changes.
303  autoPtr<mapPolyMesh> morphMap
304  = meshModCollapse.changeMesh(mesh(), false);
305 
306  mesh().updateMesh(morphMap);
307  }
308 
309  if (!overwrite)
310  {
311  runTimeExtruded++;
312  }
313  else
314  {
315  mesh().setInstance("constant");
316  }
317 
318  // Take over refinement levels and write to new time directory.
319  Info<< "\nWriting extruded mesh to time = " << runTimeExtruded.timeName()
320  << nl << endl;
321 
322  mesh().write();
323 
324  Info<< "End\n" << endl;
325 
326  return 0;
327 }
328 
329 
330 // ************************************************************************* //
const functionObjectList & functionObjects() const
Return the list of function objects.
Definition: Time.H:426
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:428
const boundBox & bounds() const
Return mesh bounding box.
Definition: polyMesh.H:427
void off()
Switch the function objects off.
const edgeList & edges() const
Return mesh edges. Uses calcEdges.
Addressing for all faces on surface of mesh. Can either be read from polyMesh or from triSurface...
Definition: boundaryMesh.H:59
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
Does polyTopoChanges to remove edges. Can remove faces due to edge collapse but can not remove cells ...
Definition: edgeCollapser.H:66
label collapseEdge(triSurface &surf, const scalar minLen)
Keep collapsing all edges < minLen.
static SLList< string > validArgs
A list of valid (mandatory) arguments.
Definition: argList.H:154
A bounding box defined in terms of the points at its extremities.
Definition: boundBox.H:58
patches[0]
Initialise the NamedEnum HashTable from the static list of names.
Definition: NamedEnum.H:52
scalar minDim() const
Smallest length/height/width dimension.
Definition: boundBoxI.H:102
Class to control time during OpenFOAM simulations that is also the top-level objectRegistry.
Definition: Time.H:68
const polyBoundaryMesh & boundaryMesh() const
Return boundary mesh.
Definition: polyMesh.H:421
virtual const pointField & points() const
Return raw points.
Definition: polyMesh.C:979
IOdictionary is derived from dictionary and IOobject to give the dictionary automatic IO functionalit...
Definition: IOdictionary.H:53
bool insert(const Key &, const T &newElmt)
Insert a new hashedEntry.
Definition: HashTableI.H:80
Calculates a unique integer (label so might not have enough room - 2G max) for processor + local inde...
Definition: globalIndex.H:63
virtual void updateMesh(const mapPolyMesh &mpm)
Update mesh corresponding to the given map.
Definition: fvMesh.C:800
dynamicFvMesh & mesh
static autoPtr< extrudeModel > New(const dictionary &)
Select null constructed.
An edge is a list of two point labels. The functionality it provides supports the discretisation on a...
Definition: edge.H:58
Xfer< T > xferMove(T &)
Construct by transferring the contents of the arg.
label nPoints
wordList patchNames(nPatches)
static word controlDictName
The default control dictionary name (normally "controlDict")
Definition: Time.H:201
static const char nl
Definition: Ostream.H:262
scalar mag(const pointField &) const
Return scalar magnitude.
Definition: edgeI.H:163
Convert a primitivePatch into a 2D polyMesh.
void set(T *)
Set pointer to that given.
Definition: autoPtrI.H:99
void setInstance(const fileName &)
Set the instance for mesh files.
Definition: polyMeshIO.C:32
void addPatches(const List< polyPatch * > &, const bool validBoundary=true)
Add boundary patches.
Definition: polyMesh.C:878
Calculates points shared by more than two processor patches or cyclic patches.
Definition: globalPoints.H:100
A bit-packed bool list.
label patchi
Given a 2D mesh insert all the topology changes to extrude. Does not work in parallel.
Definition: extrude2DMesh.H:61
Direct mesh changes based on v1.3 polyTopoChange syntax.
messageStream Info
An auto-pointer similar to the STL auto_ptr but with automatic casting to a reference to the type and...
Definition: PtrList.H:53
Mesh consisting of general polyhedral cells.
Definition: polyMesh.H:74
A patch is a list of labels that address the faces in the global face list.
Definition: polyPatch.H:66
IOobject defines the attributes of an object for which implicit objectRegistry management is supporte...
Definition: IOobject.H:91
virtual bool write() const
Write mesh using IO settings from time.
Definition: fvMesh.C:870
Namespace for OpenFOAM.
A HashTable to objects of type <T> with a label key.
Definition: Map.H:49