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-2025 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 
74  #include "setRootCase.H"
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().boundaryMesh(),
175  polyPatch::typeName
176  );
177  }
178 
180  }
181  else if (surfaceFormat == extrudeSurfaceType::polyMesh2D)
182  {
183  mesh.set
184  (
185  new polyMesh
186  (
188  (
190  runTimeExtruded.name(),
191  runTimeExtruded,
193  )
194  )
195  );
196  }
197 
198  // Engine to extrude mesh
199  extrude2DMesh extruder(mesh(), extrude2DMeshDict, model());
200 
201  extruder.addFrontBackPatches();
202 
203  meshMod.set(new polyTopoChange(mesh().boundaryMesh().size()));
204 
205  extruder.setRefinement(meshMod());
206 
207  // Create a mesh from topo changes.
208  autoPtr<polyTopoChangeMap> map = meshMod().changeMesh(mesh());
209 
210  extruder.updateZones();
211 
212  mesh().topoChange(map);
213 
214  {
215  edgeCollapser collapser(mesh());
216 
217  const edgeList& edges = mesh().edges();
218  const pointField& points = mesh().points();
219 
220  const boundBox& bb = mesh().bounds();
221  const scalar mergeDim = 1e-4 * bb.minDim();
222 
223  PackedBoolList collapseEdge(mesh().nEdges());
224  Map<point> collapsePointToLocation(mesh().nPoints());
225 
226  forAll(edges, edgeI)
227  {
228  const edge& e = edges[edgeI];
229 
230  scalar d = e.mag(points);
231 
232  if (d < mergeDim)
233  {
234  Info<< "Merging edge " << e << " since length " << d
235  << " << " << mergeDim << nl;
236 
237  collapseEdge[edgeI] = true;
238  collapsePointToLocation.set(e[1], points[e[0]]);
239  }
240  }
241 
242  List<pointEdgeCollapse> allPointInfo;
244  labelList pointPriority(mesh().nPoints(), 0);
245 
246  collapser.consistentCollapse
247  (
248  globalPoints,
249  pointPriority,
250  collapsePointToLocation,
251  collapseEdge,
252  allPointInfo
253  );
254 
255  polyTopoChange meshModCollapse(mesh());
256 
257  collapser.setRefinement(allPointInfo, meshModCollapse);
258 
259  // Create a mesh from topo changes.
260  autoPtr<polyTopoChangeMap> map = meshModCollapse.changeMesh(mesh());
261 
262  mesh().topoChange(map);
263  }
264 
265  if (!overwrite)
266  {
267  runTimeExtruded++;
268  }
269  else
270  {
271  mesh().setInstance("constant");
272  }
273 
274  // Take over refinement levels and write to new time directory.
275  Info<< "\nWriting extruded mesh to time = " << runTimeExtruded.name()
276  << nl << endl;
277 
278  mesh().write();
279 
280  Info<< "End\n" << endl;
281 
282  return 0;
283 }
284 
285 
286 // ************************************************************************* //
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:433
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: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:63
static autoPtr< extrudeModel > New(const dictionary &)
Select null constructed.
virtual bool write(const bool write=true) const
Write mesh using IO settings from time.
Definition: fvMesh.C:1785
virtual void topoChange(const polyTopoChangeMap &map)
Update mesh corresponding to the given map.
Definition: fvMesh.C:1369
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:270
void addPatches(const List< polyPatch * > &, const bool validBoundary=true)
Add boundary patches.
Definition: polyMesh.C:1107
virtual const pointField & points() const
Return raw points.
Definition: polyMesh.C:1331
void setInstance(const fileName &)
Set the instance for mesh files.
Definition: polyMeshIO.C:91
const boundBox & bounds() const
Return mesh bounding box.
Definition: polyMesh.H:407
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.
const edgeList & edges() const
Return mesh edges. Uses calcEdges.
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:258
messageStream Info
MeshedSurface< face > meshedSurface
static const char nl
Definition: Ostream.H:267
wordList patchNames(nPatches)
const bool overwrite
Definition: setNoOverwrite.H:1
Foam::argList args(argc, argv)