extrude2DMesh.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration | Website: https://openfoam.org
5  \\ / A nd | Copyright (C) 2011-2019 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 #include "IOdictionary.H"
48 
49 using namespace Foam;
50 
51 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
52 
53 enum ExtrudeMode
54 {
55  POLYMESH2D,
56  MESHEDSURFACE
57 };
58 
59 namespace Foam
60 {
61  template<>
62  const char* NamedEnum<ExtrudeMode, 2>::names[] =
63  {
64  "polyMesh2D",
65  "MeshedSurface"
66  };
67 }
68 
69 static const NamedEnum<ExtrudeMode, 2> ExtrudeModeNames;
70 
71 
72 //pointField moveInitialPoints
73 //(
74 // primitiveFacePatch& fMesh,
75 // const extrudeModel& model
76 //)
77 //{
78 // pointField layer0Points(fMesh.nPoints());
79 // pointField layer1Points(fMesh.nPoints());
80 // pointField displacement(fMesh.nPoints());
81 
82 // forAll(layer0Points, pointi)
83 // {
84 // const labelList& meshPoints = fMesh.meshPoints();
85 // label meshPointi = meshPoints[pointi];
86 
87 // layer0Points[meshPointi] = model
88 // (
89 // fMesh.points()[meshPointi],
90 // fMesh.pointNormals()[pointi],
91 // 0
92 // );
93 
94 // layer1Points[meshPointi] = model
95 // (
96 // fMesh.points()[meshPointi],
97 // fMesh.pointNormals()[pointi],
98 // 1
99 // );
100 
101 // displacement[pointi] =
102 // layer1Points[meshPointi]
103 // - layer0Points[meshPointi];
104 // }
105 
106 // fMesh.movePoints(layer0Points);
107 
108 // return displacement;
109 //}
110 
111 
112 
113 int main(int argc, char *argv[])
114 {
115  argList::validArgs.append("surfaceFormat");
116 
117  #include "addOverwriteOption.H"
118 
119  #include "setRootCase.H"
120 
121  Info<< "Create time\n" << endl;
122 
123  Time runTimeExtruded
124  (
126  args.rootPath(),
127  args.caseName()
128  );
129 
130  runTimeExtruded.functionObjects().off();
131 
132  const ExtrudeMode surfaceFormat = ExtrudeModeNames[args[1]];
133  const bool overwrite = args.optionFound("overwrite");
134 
135  Info<< "Extruding from " << ExtrudeModeNames[surfaceFormat]
136  << " at time " << runTimeExtruded.timeName() << endl;
137 
138  IOdictionary extrude2DMeshDict
139  (
140  IOobject
141  (
142  "extrude2DMeshDict",
143  runTimeExtruded.system(),
144  runTimeExtruded,
147  false
148  )
149  );
150 
151  // Point generator
152  autoPtr<extrudeModel> model(extrudeModel::New(extrude2DMeshDict));
153 
155 
157 
158  autoPtr<polyTopoChange> meshMod;
159 
160  labelListList extrudeEdgePatches;
161 
162  if (surfaceFormat == MESHEDSURFACE)
163  {
164  fMesh.set(new MeshedSurface<face>("MeshedSurface.obj"));
165 
166  EdgeMap<label> edgeRegionMap;
167  wordList patchNames(1, "default");
168  labelList patchSizes(1, fMesh().nEdges() - fMesh().nInternalEdges());
169 
170  const edgeList& edges = fMesh().edges();
171  forAll(edges, edgeI)
172  {
173  if (!fMesh().isInternalEdge(edgeI))
174  {
175  edgeRegionMap.insert(edges[edgeI], 0);
176  }
177  }
178 
179  patchToPoly2DMesh poly2DMesh
180  (
181  fMesh(),
182  patchNames,
183  patchSizes,
184  edgeRegionMap
185  );
186 
187  poly2DMesh.createMesh();
188 
189  mesh.set
190  (
191  new polyMesh
192  (
193  IOobject
194  (
196  runTimeExtruded.constant(),
197  runTimeExtruded,
200  false
201  ),
202  move(poly2DMesh.points()),
203  move(poly2DMesh.faces()),
204  move(poly2DMesh.owner()),
205  move(poly2DMesh.neighbour())
206  )
207  );
208 
209  Info<< "Constructing patches." << endl;
210  List<polyPatch*> patches(poly2DMesh.patchNames().size());
211 
213  {
214  patches[patchi] = new polyPatch
215  (
216  poly2DMesh.patchNames()[patchi],
217  poly2DMesh.patchSizes()[patchi],
218  poly2DMesh.patchStarts()[patchi],
219  patchi,
220  mesh().boundaryMesh(),
221  polyPatch::typeName
222  );
223  }
224 
226  }
227  else if (surfaceFormat == POLYMESH2D)
228  {
229  mesh.set
230  (
231  new polyMesh
232  (
234  (
236  runTimeExtruded.timeName(),
237  runTimeExtruded,
239  )
240  )
241  );
242  }
243 
244  // Engine to extrude mesh
245  extrude2DMesh extruder(mesh(), extrude2DMeshDict, model());
246 
247  extruder.addFrontBackPatches();
248 
249  meshMod.set(new polyTopoChange(mesh().boundaryMesh().size()));
250 
251  extruder.setRefinement(meshMod());
252 
253  // Create a mesh from topo changes.
254  autoPtr<mapPolyMesh> morphMap = meshMod().changeMesh(mesh(), false);
255 
256  mesh().updateMesh(morphMap);
257 
258  {
259  edgeCollapser collapser(mesh());
260 
261  const edgeList& edges = mesh().edges();
262  const pointField& points = mesh().points();
263 
264  const boundBox& bb = mesh().bounds();
265  const scalar mergeDim = 1e-4 * bb.minDim();
266 
267  PackedBoolList collapseEdge(mesh().nEdges());
268  Map<point> collapsePointToLocation(mesh().nPoints());
269 
270  forAll(edges, edgeI)
271  {
272  const edge& e = edges[edgeI];
273 
274  scalar d = e.mag(points);
275 
276  if (d < mergeDim)
277  {
278  Info<< "Merging edge " << e << " since length " << d
279  << " << " << mergeDim << nl;
280 
281  collapseEdge[edgeI] = true;
282  collapsePointToLocation.set(e[1], points[e[0]]);
283  }
284  }
285 
286  List<pointEdgeCollapse> allPointInfo;
288  labelList pointPriority(mesh().nPoints(), 0);
289 
290  collapser.consistentCollapse
291  (
292  globalPoints,
293  pointPriority,
294  collapsePointToLocation,
295  collapseEdge,
296  allPointInfo
297  );
298 
299  polyTopoChange meshModCollapse(mesh());
300 
301  collapser.setRefinement(allPointInfo, meshModCollapse);
302 
303  // Create a mesh from topo changes.
304  autoPtr<mapPolyMesh> morphMap
305  = meshModCollapse.changeMesh(mesh(), false);
306 
307  mesh().updateMesh(morphMap);
308  }
309 
310  if (!overwrite)
311  {
312  runTimeExtruded++;
313  }
314  else
315  {
316  mesh().setInstance("constant");
317  }
318 
319  // Take over refinement levels and write to new time directory.
320  Info<< "\nWriting extruded mesh to time = " << runTimeExtruded.timeName()
321  << nl << endl;
322 
323  mesh().write();
324 
325  Info<< "End\n" << endl;
326 
327  return 0;
328 }
329 
330 
331 // ************************************************************************* //
const polyBoundaryMesh & boundaryMesh() const
Return boundary mesh.
Definition: polyMesh.H:434
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:434
void off()
Switch the function objects off.
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:309
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
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:153
A bounding box defined in terms of the points at its extremities.
Definition: boundBox.H:58
patches[0]
virtual bool write(const bool write=true) const
Write mesh using IO settings from time.
Definition: fvMesh.C:1035
Initialise the NamedEnum HashTable from the static list of names.
Definition: NamedEnum.H:51
Class to control time during OpenFOAM simulations that is also the top-level objectRegistry.
Definition: Time.H:68
IOdictionary is derived from dictionary and IOobject to give the dictionary automatic IO functionalit...
Definition: IOdictionary.H:52
virtual const pointField & points() const
Return raw points.
Definition: polyMesh.C:1131
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:795
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
label nPoints
wordList patchNames(nPatches)
scalar mag(const pointField &) const
Return scalar magnitude.
Definition: edgeI.H:181
static word controlDictName
The default control dictionary name (normally "controlDict")
Definition: Time.H:197
void addPatches(const List< polyPatch *> &, const bool validBoundary=true)
Add boundary patches.
Definition: polyMesh.C:909
static const char nl
Definition: Ostream.H:260
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
const edgeList & edges() const
Return mesh edges. Uses calcEdges.
Calculates points shared by more than two processor patches or cyclic patches.
Definition: globalPoints.H:100
const boundBox & bounds() const
Return mesh bounding box.
Definition: polyMesh.H:440
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
const functionObjectList & functionObjects() const
Return the list of function objects.
Definition: Time.H:409
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:52
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:92
scalar minDim() const
Smallest length/height/width dimension.
Definition: boundBoxI.H:102
Namespace for OpenFOAM.
A HashTable to objects of type <T> with a label key.
Definition: Map.H:49