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-2026 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 class extrudeSurfaceType
54 {
55  polyMesh2D,
57 };
58 
59 static const NamedEnum<extrudeSurfaceType, 2> extrudeSurfaceTypeNames
60 {
61  "polyMesh2D",
62  "MeshedSurface"
63 };
64 
65 
66 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
67 
68 int main(int argc, char *argv[])
69 {
70  argList::validArgs.append("surfaceFormat");
71 
72  #include "addNoOverwriteOption.H"
73 
75 
76  Info<< "Create time\n" << endl;
77 
78  Time runTimeExtruded
79  (
81  args.rootPath(),
82  args.caseName(),
83  false
84  );
85 
86  const extrudeSurfaceType surfaceFormat = extrudeSurfaceTypeNames[args[1]];
87  #include "setNoOverwrite.H"
88 
89  Info<< "Extruding from " << extrudeSurfaceTypeNames[surfaceFormat]
90  << " at time " << runTimeExtruded.name() << endl;
91 
92  IOdictionary extrude2DMeshDict
93  (
94  IOobject
95  (
96  "extrude2DMeshDict",
97  runTimeExtruded.system(),
98  runTimeExtruded,
101  false
102  )
103  );
104 
105  // Point generator
106  autoPtr<extrudeModel> model(extrudeModel::New(extrude2DMeshDict));
107 
109 
111 
112  autoPtr<polyTopoChange> meshMod;
113 
114  labelListList extrudeEdgePatches;
115 
116  if (surfaceFormat == extrudeSurfaceType::meshedSurface)
117  {
118  fMesh.set(new MeshedSurface<face>("MeshedSurface.obj"));
119 
120  EdgeMap<label> edgeRegionMap;
121  wordList patchNames(1, "default");
122  labelList patchSizes(1, fMesh().nEdges() - fMesh().nInternalEdges());
123 
124  const edgeList& edges = fMesh().edges();
125  forAll(edges, edgeI)
126  {
127  if (!fMesh().isInternalEdge(edgeI))
128  {
129  edgeRegionMap.insert(edges[edgeI], 0);
130  }
131  }
132 
133  patchToPoly2DMesh poly2DMesh
134  (
135  fMesh(),
136  patchNames,
137  patchSizes,
138  edgeRegionMap
139  );
140 
141  poly2DMesh.createMesh();
142 
143  mesh.set
144  (
145  new polyMesh
146  (
147  IOobject
148  (
150  runTimeExtruded.constant(),
151  runTimeExtruded,
154  false
155  ),
156  move(poly2DMesh.points()),
157  move(poly2DMesh.faces()),
158  move(poly2DMesh.owner()),
159  move(poly2DMesh.neighbour())
160  )
161  );
162 
163  Info<< "Constructing patches." << endl;
164  List<polyPatch*> patches(poly2DMesh.patchNames().size());
165 
167  {
168  patches[patchi] = new polyPatch
169  (
170  poly2DMesh.patchNames()[patchi],
171  poly2DMesh.patchSizes()[patchi],
172  poly2DMesh.patchStarts()[patchi],
173  patchi,
174  mesh().boundary()
175  );
176  }
177 
179  }
180  else if (surfaceFormat == extrudeSurfaceType::polyMesh2D)
181  {
182  mesh.set
183  (
184  new polyMesh
185  (
187  (
189  runTimeExtruded.name(),
190  runTimeExtruded,
192  )
193  )
194  );
195  }
196 
197  // Engine to extrude mesh
198  extrude2DMesh extruder(mesh(), extrude2DMeshDict, model());
199 
200  extruder.addFrontBackPatches();
201 
202  meshMod.set(new polyTopoChange(mesh().boundary().size()));
203 
204  extruder.setRefinement(meshMod());
205 
206  // Create a mesh from topo changes.
207  autoPtr<polyTopoChangeMap> map = meshMod().changeMesh(mesh());
208 
209  extruder.updateZones();
210 
211  mesh().topoChange(map);
212 
213  {
214  edgeCollapser collapser(mesh());
215 
216  const edgeList& edges = mesh().edges();
217  const pointField& points = mesh().points();
218 
219  const boundBox& bb = mesh().bounds();
220  const scalar mergeDim = 1e-4 * bb.minDim();
221 
222  PackedBoolList collapseEdge(mesh().nEdges());
223  Map<point> collapsePointToLocation(mesh().nPoints());
224 
225  forAll(edges, edgeI)
226  {
227  const edge& e = edges[edgeI];
228 
229  scalar d = e.mag(points);
230 
231  if (d < mergeDim)
232  {
233  Info<< "Merging edge " << e << " since length " << d
234  << " << " << mergeDim << nl;
235 
236  collapseEdge[edgeI] = true;
237  collapsePointToLocation.set(e[1], points[e[0]]);
238  }
239  }
240 
241  List<pointEdgeCollapse> allPointInfo;
243  labelList pointPriority(mesh().nPoints(), 0);
244 
245  collapser.consistentCollapse
246  (
247  globalPoints,
248  pointPriority,
249  collapsePointToLocation,
250  collapseEdge,
251  allPointInfo
252  );
253 
254  polyTopoChange meshModCollapse(mesh());
255 
256  collapser.setRefinement(allPointInfo, meshModCollapse);
257 
258  // Create a mesh from topo changes.
259  autoPtr<polyTopoChangeMap> map = meshModCollapse.changeMesh(mesh());
260 
261  mesh().topoChange(map);
262  }
263 
264  if (!overwrite)
265  {
266  runTimeExtruded++;
267  }
268  else
269  {
270  mesh().setInstance("constant");
271  }
272 
273  // Take over refinement levels and write to new time directory.
274  Info<< "\nWriting extruded mesh to time = " << runTimeExtruded.name()
275  << nl << endl;
276 
277  mesh().write();
278 
279  Info<< "End\n" << endl;
280 
281  return 0;
282 }
283 
284 
285 // ************************************************************************* //
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:449
bool set(const Key &, const T &newElmt)
Set a new hashedEntry, overwriting existing entries.
Definition: HashTableI.H:91
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:55
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
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:60
scalar minDim() const
Smallest length/height/width dimension.
Definition: boundBoxI.H:136
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:63
static autoPtr< extrudeModel > New(const dictionary &)
Select null constructed.
virtual void topoChange(const polyTopoChangeMap &map)
Update mesh corresponding to the given map.
Definition: fvMesh.C:1335
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:78
static word defaultRegion
Return the default region name.
Definition: polyMesh.H:260
void addPatches(const List< polyPatch * > &, const bool validBoundary=true)
Add boundary patches.
Definition: polyMesh.C:1091
virtual const pointField & points() const
Return raw points.
Definition: polyMesh.C:1295
void setInstance(const fileName &)
Set the instance for mesh files.
Definition: polyMeshIO.C:102
const boundBox & bounds() const
Return mesh bounding box.
Definition: polyMesh.H:399
A patch is a list of labels that address the faces in the global face list.
Definition: polyPatch.H:71
Direct mesh changes based on v1.3 polyTopoChange syntax.
const edgeList & edges() const
Return mesh edges. Uses calcEdges.
virtual bool write(const bool write=true) const
Write using setting from DB.
label collapseEdge(triSurface &surf, const scalar minLen)
Keep collapsing all edges < minLen.
Foam::fvMesh mesh(Foam::IOobject(regionName, runTime.name(), runTime, Foam::IOobject::MUST_READ), false)
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:288
messageStream Info
MeshedSurface< face > meshedSurface
static const char nl
Definition: Ostream.H:297
wordList patchNames(nPatches)
faceListList boundary(nPatches)
const bool overwrite
Definition: setNoOverwrite.H:1
Foam::argList args(argc, argv)