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-2024 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
33  back faces.
34 
35 \*---------------------------------------------------------------------------*/
36 
37 #include "argList.H"
38 #include "Time.H"
39 #include "polyMesh.H"
40 #include "extrude2DMesh.H"
41 #include "extrudeModel.H"
42 #include "polyTopoChange.H"
43 #include "MeshedSurface.H"
44 #include "edgeCollapser.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.setPoints(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  false
129  );
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.name() << 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 
155  autoPtr<polyMesh> mesh;
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  move(poly2DMesh.points()),
202  move(poly2DMesh.faces()),
203  move(poly2DMesh.owner()),
204  move(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 
224  mesh().addPatches(patches);
225  }
226  else if (surfaceFormat == POLYMESH2D)
227  {
228  mesh.set
229  (
230  new polyMesh
231  (
233  (
235  runTimeExtruded.name(),
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<polyTopoChangeMap> map = meshMod().changeMesh(mesh());
254 
255  extruder.updateZones();
256 
257  mesh().topoChange(map);
258 
259  {
260  edgeCollapser collapser(mesh());
261 
262  const edgeList& edges = mesh().edges();
263  const pointField& points = mesh().points();
264 
265  const boundBox& bb = mesh().bounds();
266  const scalar mergeDim = 1e-4 * bb.minDim();
267 
268  PackedBoolList collapseEdge(mesh().nEdges());
269  Map<point> collapsePointToLocation(mesh().nPoints());
270 
271  forAll(edges, edgeI)
272  {
273  const edge& e = edges[edgeI];
274 
275  scalar d = e.mag(points);
276 
277  if (d < mergeDim)
278  {
279  Info<< "Merging edge " << e << " since length " << d
280  << " << " << mergeDim << nl;
281 
282  collapseEdge[edgeI] = true;
283  collapsePointToLocation.set(e[1], points[e[0]]);
284  }
285  }
286 
287  List<pointEdgeCollapse> allPointInfo;
288  const globalIndex globalPoints(mesh().nPoints());
289  labelList pointPriority(mesh().nPoints(), 0);
290 
291  collapser.consistentCollapse
292  (
293  globalPoints,
294  pointPriority,
295  collapsePointToLocation,
296  collapseEdge,
297  allPointInfo
298  );
299 
300  polyTopoChange meshModCollapse(mesh());
301 
302  collapser.setRefinement(allPointInfo, meshModCollapse);
303 
304  // Create a mesh from topo changes.
305  autoPtr<polyTopoChangeMap> map = meshModCollapse.changeMesh(mesh());
306 
307  mesh().topoChange(map);
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.name()
321  << nl << endl;
322 
323  mesh().write();
324 
325  Info<< "End\n" << endl;
326 
327  return 0;
328 }
329 
330 
331 // ************************************************************************* //
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:434
bool insert(const Key &, const T &newElmt)
Insert a new hashedEntry.
Definition: HashTableI.H:80
IOdictionary is derived from dictionary and IOobject to give the dictionary automatic IO functionalit...
Definition: IOdictionary.H:57
IOobject defines the attributes of an object for which implicit objectRegistry management is supporte...
Definition: IOobject.H:99
A HashTable to objects of type <T> with a label key.
Definition: Map.H:52
Initialise the NamedEnum HashTable from the static list of names.
Definition: NamedEnum.H:54
A bit-packed bool list.
Class to control time during OpenFOAM simulations that is also the top-level objectRegistry.
Definition: Time.H:76
static word controlDictName
The default control dictionary name (normally "controlDict")
Definition: Time.H:208
const fileName & caseName() const
Return case name (parallel run) or global case (serial run)
Definition: argListI.H:48
bool optionFound(const word &opt) const
Return true if the named option is found.
Definition: argListI.H:114
static SLList< string > validArgs
A list of valid (mandatory) arguments.
Definition: argList.H:153
const fileName & rootPath() const
Return root path.
Definition: argListI.H:42
An auto-pointer similar to the STL auto_ptr but with automatic casting to a reference to the type and...
Definition: autoPtr.H:51
void set(T *)
Set pointer to that given.
Definition: autoPtrI.H:99
A bounding box defined in terms of the points at its extremities.
Definition: boundBox.H:59
scalar minDim() const
Smallest length/height/width dimension.
Definition: boundBoxI.H:108
Does polyTopoChanges to remove edges. Can remove faces due to edge collapse but can not remove cells ...
Definition: edgeCollapser.H:67
An edge is a list of two point labels. The functionality it provides supports the discretisation on a...
Definition: edge.H:61
Given a 2D mesh insert all the topology changes to extrude. Does not work in parallel.
Definition: extrude2DMesh.H:62
static autoPtr< extrudeModel > New(const dictionary &)
Select null constructed.
Calculates a unique integer (label so might not have enough room - 2G max) for processor + local inde...
Definition: globalIndex.H:64
Calculates points shared by more than two processor patches or cyclic patches.
Definition: globalPoints.H:101
Convert a primitivePatch into a 2D polyMesh.
Mesh consisting of general polyhedral cells.
Definition: polyMesh.H:80
static word defaultRegion
Return the default region name.
Definition: polyMesh.H:267
A patch is a list of labels that address the faces in the global face list.
Definition: polyPatch.H:70
Direct mesh changes based on v1.3 polyTopoChange syntax.
label collapseEdge(triSurface &surf, const scalar minLen)
Keep collapsing all edges < minLen.
int main(int argc, char *argv[])
Definition: financialFoam.C:44
label patchi
const pointField & points
label nPoints
const fvPatchList & patches
Namespace for OpenFOAM.
const doubleScalar e
Definition: doubleScalar.H:106
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:257
messageStream Info
static const char nl
Definition: Ostream.H:266
wordList patchNames(nPatches)
Foam::argList args(argc, argv)