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 | www.openfoam.com
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8  Copyright (C) 2011-2016 OpenFOAM Foundation
9  Copyright (C) 2016 OpenCFD Ltd.
10 -------------------------------------------------------------------------------
11 License
12  This file is part of OpenFOAM.
13 
14  OpenFOAM is free software: you can redistribute it and/or modify it
15  under the terms of the GNU General Public License as published by
16  the Free Software Foundation, either version 3 of the License, or
17  (at your option) any later version.
18 
19  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
20  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22  for more details.
23 
24  You should have received a copy of the GNU General Public License
25  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
26 
27 Application
28  extrude2DMesh
29 
30 Group
31  grpMeshGenerationUtilities
32 
33 Description
34  Create a 3D mesh by extruding a 2D mesh with specified thickness.
35  For the 2D mesh, all faces are 2 points only, no front and back faces.
36 
37 Note
38  Not sure about the walking of the faces to create the front and back faces.
39 
40 \*---------------------------------------------------------------------------*/
41 
42 #include "argList.H"
43 #include "Time.H"
44 #include "polyMesh.H"
45 #include "extrude2DMesh.H"
46 #include "extrudeModel.H"
47 #include "polyTopoChange.H"
48 #include "MeshedSurface.H"
49 #include "edgeCollapser.H"
50 #include "addPatchCellLayer.H"
51 #include "patchToPoly2DMesh.H"
52 #include "globalIndex.H"
53 #include "topoSet.H"
54 #include "processorMeshes.H"
55 
56 using namespace Foam;
57 
58 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
59 
60 enum ExtrudeMode
61 {
62  POLYMESH2D,
63  MESHEDSURFACE
64 };
65 
66 static const Enum<ExtrudeMode> ExtrudeModeNames
67 {
68  { ExtrudeMode::POLYMESH2D, "polyMesh2D" },
69  { ExtrudeMode::MESHEDSURFACE, "MeshedSurface" },
70 };
71 
72 
73 //pointField moveInitialPoints
74 //(
75 // primitiveFacePatch& fMesh,
76 // const extrudeModel& model
77 //)
78 //{
79 // pointField layer0Points(fMesh.nPoints());
80 // pointField layer1Points(fMesh.nPoints());
81 // pointField displacement(fMesh.nPoints());
82 
83 // forAll(layer0Points, pointi)
84 // {
85 // const labelList& meshPoints = fMesh.meshPoints();
86 // label meshPointi = meshPoints[pointi];
87 
88 // layer0Points[meshPointi] = model
89 // (
90 // fMesh.points()[meshPointi],
91 // fMesh.pointNormals()[pointi],
92 // 0
93 // );
94 
95 // layer1Points[meshPointi] = model
96 // (
97 // fMesh.points()[meshPointi],
98 // fMesh.pointNormals()[pointi],
99 // 1
100 // );
101 
102 // displacement[pointi] =
103 // layer1Points[meshPointi]
104 // - layer0Points[meshPointi];
105 // }
106 
107 // fMesh.movePoints(layer0Points);
108 
109 // return displacement;
110 //}
111 
112 
113 
114 int main(int argc, char *argv[])
115 {
117  (
118  "Create a 3D mesh from a 2D mesh by extruding with specified thickness"
119  );
120 
121  argList::addArgument("surfaceFormat");
122 
123  #include "addOverwriteOption.H"
124 
125  argList::noFunctionObjects(); // Never use function objects
126 
127  #include "setRootCase.H"
128 
129  Info<< "Create time\n" << endl;
130 
131  Time runTimeExtruded
132  (
134  args.rootPath(),
135  args.caseName()
136  );
137 
138  // For safety
139  runTimeExtruded.functionObjects().off();
140 
141  const ExtrudeMode surfaceFormat = ExtrudeModeNames[args[1]];
142  const bool overwrite = args.found("overwrite");
143 
144  Info<< "Extruding from " << ExtrudeModeNames[surfaceFormat]
145  << " at time " << runTimeExtruded.timeName() << endl;
146 
147  IOdictionary extrude2DMeshDict
148  (
149  IOobject
150  (
151  "extrude2DMeshDict",
152  runTimeExtruded.system(),
153  runTimeExtruded,
157  )
158  );
159 
160  // Point generator
161  autoPtr<extrudeModel> model(extrudeModel::New(extrude2DMeshDict));
162 
164 
166 
167  autoPtr<polyTopoChange> meshMod;
168 
169  labelListList extrudeEdgePatches;
170 
171  if (surfaceFormat == MESHEDSURFACE)
172  {
173  fMesh.reset(new MeshedSurface<face>("MeshedSurface.obj"));
174 
175  EdgeMap<label> edgeRegionMap;
176  wordList patchNames(1, "default");
177  labelList patchSizes(1, fMesh().nEdges() - fMesh().nInternalEdges());
178 
179  const edgeList& edges = fMesh().edges();
180  forAll(edges, edgeI)
181  {
182  if (!fMesh().isInternalEdge(edgeI))
183  {
184  edgeRegionMap.insert(edges[edgeI], 0);
185  }
186  }
187 
188  patchToPoly2DMesh poly2DMesh
189  (
190  fMesh(),
191  patchNames,
192  patchSizes,
193  edgeRegionMap
194  );
195 
196  poly2DMesh.createMesh();
197 
199  (
200  IOobject
201  (
203  runTimeExtruded.constant(),
204  runTimeExtruded,
208  ),
209  std::move(poly2DMesh.points()),
210  std::move(poly2DMesh.faces()),
211  std::move(poly2DMesh.owner()),
212  std::move(poly2DMesh.neighbour())
213  );
214 
215  Info<< "Constructing patches." << endl;
216  List<polyPatch*> patches(poly2DMesh.patchNames().size());
217 
218  forAll(patches, patchi)
219  {
220  patches[patchi] = new polyPatch
221  (
222  poly2DMesh.patchNames()[patchi],
223  poly2DMesh.patchSizes()[patchi],
224  poly2DMesh.patchStarts()[patchi],
225  patchi,
226  mesh().boundaryMesh(),
227  polyPatch::typeName
228  );
229  }
230 
232  }
233  else if (surfaceFormat == POLYMESH2D)
234  {
236  (
237  IOobject
238  (
240  runTimeExtruded.timeName(),
241  runTimeExtruded,
243  )
244  );
245  }
246 
247  // Engine to extrude mesh
248  extrude2DMesh extruder(mesh(), extrude2DMeshDict, model());
249 
250  extruder.addFrontBackPatches();
251 
252  meshMod.reset(new polyTopoChange(mesh().boundaryMesh().size()));
253 
254  extruder.setRefinement(meshMod());
255 
256  // Create a mesh from topo changes.
257  autoPtr<mapPolyMesh> morphMap = meshMod().changeMesh(mesh(), false);
258 
259  mesh().updateMesh(morphMap());
260 
261  {
262  edgeCollapser collapser(mesh());
263 
264  const edgeList& edges = mesh().edges();
265  const pointField& points = mesh().points();
266 
267  const boundBox& bb = mesh().bounds();
268  const scalar mergeDim = 1e-4 * bb.minDim();
269 
270  bitSet collapseEdge(mesh().nEdges());
271  Map<point> collapsePointToLocation(mesh().nPoints());
272 
273  forAll(edges, edgeI)
274  {
275  const edge& e = edges[edgeI];
276 
277  scalar d = e.mag(points);
278 
279  if (d < mergeDim)
280  {
281  Info<< "Merging edge " << e << " since length " << d
282  << " << " << mergeDim << nl;
283 
284  collapseEdge.set(edgeI);
285  collapsePointToLocation.set(e[1], points[e[0]]);
286  }
287  }
288 
289  List<pointEdgeCollapse> allPointInfo;
291  labelList pointPriority(mesh().nPoints(), Zero);
292 
293  collapser.consistentCollapse
294  (
295  globalPoints,
296  pointPriority,
297  collapsePointToLocation,
298  collapseEdge,
299  allPointInfo
300  );
301 
302  polyTopoChange meshModCollapse(mesh());
303 
304  collapser.setRefinement(allPointInfo, meshModCollapse);
305 
306  // Create a mesh from topo changes.
307  autoPtr<mapPolyMesh> morphMap
308  = meshModCollapse.changeMesh(mesh(), false);
309 
310  mesh().updateMesh(morphMap());
311  }
312 
313  if (!overwrite)
314  {
315  ++runTimeExtruded;
316  }
317  else
318  {
319  mesh().setInstance("constant");
320  }
321 
322  // Take over refinement levels and write to new time directory.
323  Info<< "\nWriting extruded mesh to time = " << runTimeExtruded.timeName()
324  << nl << endl;
325 
326  mesh().write();
329 
330  Info<< "End\n" << endl;
331 
332  return 0;
333 }
334 
335 
336 // ************************************************************************* //
static void noFunctionObjects(bool addWithOption=false)
Remove &#39;-noFunctionObjects&#39; option and ignore any occurrences.
Definition: argList.C:547
static void addNote(const string &note)
Add extra notes for the usage information.
Definition: argList.C:462
static void removeFiles(const polyMesh &mesh)
Helper: remove all procAddressing files from mesh instance.
void off()
Switch the function objects off.
constexpr char nl
The newline &#39;\n&#39; character (0x0a)
Definition: Ostream.H:50
Addressing for all faces on surface of mesh. Can either be read from polyMesh or from triSurface...
Definition: boundaryMesh.H:58
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:531
Does polyTopoChanges to remove edges. Can remove faces due to edge collapse but can not remove cells ...
Definition: edgeCollapser.H:64
label collapseEdge(triSurface &surf, const scalar minLen)
Keep collapsing all edges < minLen.
A bounding box defined in terms of min/max extrema points.
Definition: boundBox.H:63
Ignore writing from objectRegistry::writeObject()
Class to control time during OpenFOAM simulations that is also the top-level objectRegistry.
Definition: Time.H:69
IOdictionary is derived from dictionary and IOobject to give the dictionary automatic IO functionalit...
Definition: IOdictionary.H:50
virtual const pointField & points() const
Return raw points.
Definition: polyMesh.C:1078
#define forAll(list, i)
Loop across all elements in list.
Definition: stdFoam.H:421
void reset(T *p=nullptr) noexcept
Delete managed object and set to new given pointer.
Definition: autoPtrI.H:37
bool insert(const Key &key, const T &obj)
Copy insert a new entry, not overwriting existing entries.
Definition: HashTableI.H:152
Calculates a unique integer (label so might not have enough room - 2G max) for processor + local inde...
Definition: globalIndex.H:61
static void removeFiles(const polyMesh &)
Helper: remove all sets files from mesh instance.
Definition: topoSet.C:693
const dimensionedScalar e
Elementary charge.
Definition: createFields.H:11
virtual void updateMesh(const mapPolyMesh &mpm)
Update mesh corresponding to the given map.
Definition: fvMesh.C:1005
dynamicFvMesh & mesh
const pointField & points
An edge is a list of two vertex labels. This can correspond to a directed graph edge or an edge on a ...
Definition: edge.H:59
const functionObjectList & functionObjects() const noexcept
Return the list of function objects.
Definition: Time.H:714
const polyBoundaryMesh & boundaryMesh() const noexcept
Return boundary mesh.
Definition: polyMesh.H:609
label nPoints
static word defaultRegion
Return the default region name.
Definition: polyMesh.H:406
wordList patchNames(nPatches)
static word controlDictName
The default control dictionary name (normally "controlDict")
Definition: Time.H:268
const fileName & caseName() const noexcept
Return case name (parallel run) or global case (serial run)
Definition: argListI.H:62
virtual bool write(const bool writeOnProc=true) const
Write mesh using IO settings from time.
Definition: fvMesh.C:1113
Convert a primitivePatch into a 2D polyMesh.
const fileName & rootPath() const noexcept
Return root path.
Definition: argListI.H:56
static autoPtr< extrudeModel > New(const dictionary &dict)
Select null constructed.
const edgeList & edges() const
Return mesh edges. Uses calcEdges.
Calculates points shared by more than two processor patches or cyclic patches.
Definition: globalPoints.H:98
Given a 2D mesh insert all the topology changes to extrude. Does not work in parallel.
Definition: extrude2DMesh.H:59
void setInstance(const fileName &instance, const IOobjectOption::writeOption wOpt=IOobject::AUTO_WRITE)
Set the instance for mesh files.
Definition: polyMeshIO.C:29
A bitSet stores bits (elements with only two states) in packed internal format and supports a variety...
Definition: bitSet.H:59
Enum is a wrapper around a list of names/values that represent particular enumeration (or int) values...
Definition: error.H:64
Direct mesh changes based on v1.3 polyTopoChange syntax.
const polyBoundaryMesh & patches
Nothing to be read.
static void addArgument(const string &argName, const string &usage="")
Append a (mandatory) argument to validArgs.
Definition: argList.C:351
messageStream Info
Information stream (stdout output on master, null elsewhere)
const boundBox & bounds() const noexcept
Return mesh bounding box.
Definition: polyMesh.H:617
Pointer management similar to std::unique_ptr, with some additional methods and type checking...
Definition: HashPtrTable.H:48
static autoPtr< T > New(Args &&... args)
Construct autoPtr with forwarding arguments.
Definition: autoPtr.H:178
A patch is a list of labels that address the faces in the global face list.
Definition: polyPatch.H:69
void addPatches(polyPatchList &plist, const bool validBoundary=true)
Add boundary patches.
Definition: polyMesh.C:971
Foam::argList args(argc, argv)
Defines the attributes of an object for which implicit objectRegistry management is supported...
Definition: IOobject.H:180
scalar minDim() const
Smallest length/height/width dimension.
Definition: boundBoxI.H:216
Do not request registration (bool: false)
bool found(const word &optName) const
Return true if the named option is found.
Definition: argListI.H:171
Namespace for OpenFOAM.
A HashTable to objects of type <T> with a label key.
static constexpr const zero Zero
Global zero (0)
Definition: zero.H:127