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 "patchToPoly2DMesh.H"
51 #include "globalIndex.H"
52 #include "topoSet.H"
53 #include "processorMeshes.H"
54 
55 using namespace Foam;
56 
57 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
58 
59 enum ExtrudeMode
60 {
61  POLYMESH2D,
62  MESHEDSURFACE
63 };
64 
65 static const Enum<ExtrudeMode> ExtrudeModeNames
66 {
67  { ExtrudeMode::POLYMESH2D, "polyMesh2D" },
68  { ExtrudeMode::MESHEDSURFACE, "MeshedSurface" },
69 };
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 {
116  (
117  "Create a 3D mesh from a 2D mesh by extruding with specified thickness"
118  );
119 
120  argList::addArgument("surfaceFormat");
121 
122  #include "addOverwriteOption.H"
123 
124  argList::noFunctionObjects(); // Never use function objects
125 
126  #include "setRootCase.H"
127 
128  Info<< "Create time\n" << endl;
129 
130  Time runTimeExtruded
131  (
133  args.rootPath(),
134  args.caseName()
135  );
136 
137  // For safety
138  runTimeExtruded.functionObjects().off();
139 
140  const ExtrudeMode surfaceFormat = ExtrudeModeNames[args[1]];
141  const bool overwrite = args.found("overwrite");
142 
143  Info<< "Extruding from " << ExtrudeModeNames[surfaceFormat]
144  << " at time " << runTimeExtruded.timeName() << endl;
145 
146  IOdictionary extrude2DMeshDict
147  (
148  IOobject
149  (
150  "extrude2DMeshDict",
151  runTimeExtruded.system(),
152  runTimeExtruded,
156  )
157  );
158 
159  // Point generator
160  autoPtr<extrudeModel> model(extrudeModel::New(extrude2DMeshDict));
161 
163 
165 
166  autoPtr<polyTopoChange> meshMod;
167 
168  labelListList extrudeEdgePatches;
169 
170  if (surfaceFormat == MESHEDSURFACE)
171  {
172  fMesh.reset(new MeshedSurface<face>("MeshedSurface.obj"));
173 
174  EdgeMap<label> edgeRegionMap;
175  wordList patchNames(1, "default");
176  labelList patchSizes(1, fMesh().nEdges() - fMesh().nInternalEdges());
177 
178  const edgeList& edges = fMesh().edges();
179  forAll(edges, edgeI)
180  {
181  if (!fMesh().isInternalEdge(edgeI))
182  {
183  edgeRegionMap.insert(edges[edgeI], 0);
184  }
185  }
186 
187  patchToPoly2DMesh poly2DMesh
188  (
189  fMesh(),
190  patchNames,
191  patchSizes,
192  edgeRegionMap
193  );
194 
195  poly2DMesh.createMesh();
196 
198  (
199  IOobject
200  (
202  runTimeExtruded.constant(),
203  runTimeExtruded,
207  ),
208  std::move(poly2DMesh.points()),
209  std::move(poly2DMesh.faces()),
210  std::move(poly2DMesh.owner()),
211  std::move(poly2DMesh.neighbour())
212  );
213 
214  Info<< "Constructing patches." << endl;
215  List<polyPatch*> patches(poly2DMesh.patchNames().size());
216 
217  forAll(patches, patchi)
218  {
219  patches[patchi] = new polyPatch
220  (
221  poly2DMesh.patchNames()[patchi],
222  poly2DMesh.patchSizes()[patchi],
223  poly2DMesh.patchStarts()[patchi],
224  patchi,
225  mesh().boundaryMesh(),
226  polyPatch::typeName
227  );
228  }
229 
231  }
232  else if (surfaceFormat == POLYMESH2D)
233  {
235  (
236  IOobject
237  (
239  runTimeExtruded.timeName(),
240  runTimeExtruded,
242  )
243  );
244  }
245 
246  // Engine to extrude mesh
247  extrude2DMesh extruder(mesh(), extrude2DMeshDict, model());
248 
249  extruder.addFrontBackPatches();
250 
251  meshMod.reset(new polyTopoChange(mesh().boundaryMesh().size()));
252 
253  extruder.setRefinement(meshMod());
254 
255  // Create a mesh from topo changes.
256  autoPtr<mapPolyMesh> morphMap = meshMod().changeMesh(mesh(), false);
257 
258  mesh().updateMesh(morphMap());
259 
260  {
261  edgeCollapser collapser(mesh());
262 
263  const edgeList& edges = mesh().edges();
264  const pointField& points = mesh().points();
265 
266  const boundBox& bb = mesh().bounds();
267  const scalar mergeDim = 1e-4 * bb.minDim();
268 
269  bitSet collapseEdge(mesh().nEdges());
270  Map<point> collapsePointToLocation(mesh().nPoints());
271 
272  forAll(edges, edgeI)
273  {
274  const edge& e = edges[edgeI];
275 
276  scalar d = e.mag(points);
277 
278  if (d < mergeDim)
279  {
280  Info<< "Merging edge " << e << " since length " << d
281  << " << " << mergeDim << nl;
282 
283  collapseEdge.set(edgeI);
284  collapsePointToLocation.set(e[1], points[e[0]]);
285  }
286  }
287 
288  List<pointEdgeCollapse> allPointInfo;
290  labelList pointPriority(mesh().nPoints(), Zero);
291 
292  collapser.consistentCollapse
293  (
294  globalPoints,
295  pointPriority,
296  collapsePointToLocation,
297  collapseEdge,
298  allPointInfo
299  );
300 
301  polyTopoChange meshModCollapse(mesh());
302 
303  collapser.setRefinement(allPointInfo, meshModCollapse);
304 
305  // Create a mesh from topo changes.
306  autoPtr<mapPolyMesh> morphMap
307  = meshModCollapse.changeMesh(mesh(), false);
308 
309  mesh().updateMesh(morphMap());
310  }
311 
312  if (!overwrite)
313  {
314  ++runTimeExtruded;
315  }
316  else
317  {
318  mesh().setInstance("constant");
319  }
320 
321  // Take over refinement levels and write to new time directory.
322  Info<< "\nWriting extruded mesh to time = " << runTimeExtruded.timeName()
323  << nl << endl;
324 
325  mesh().write();
328 
329  Info<< "End\n" << endl;
330 
331  return 0;
332 }
333 
334 
335 // ************************************************************************* //
static void noFunctionObjects(bool addWithOption=false)
Remove &#39;-noFunctionObjects&#39; option and ignore any occurrences.
Definition: argList.C:561
static void addNote(const string &note)
Add extra notes for the usage information.
Definition: argList.C:476
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:529
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:1063
#define forAll(list, i)
Loop across all elements in list.
Definition: stdFoam.H:286
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:960
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:1068
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:365
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:956
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