ensightCells.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) 2016-2022 OpenCFD Ltd.
9 -------------------------------------------------------------------------------
10 License
11  This file is part of OpenFOAM.
12 
13  OpenFOAM is free software: you can redistribute it and/or modify it
14  under the terms of the GNU General Public License as published by
15  the Free Software Foundation, either version 3 of the License, or
16  (at your option) any later version.
17 
18  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
19  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
20  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21  for more details.
22 
23  You should have received a copy of the GNU General Public License
24  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
25 
26 \*---------------------------------------------------------------------------*/
27 
28 #include "ensightCells.H"
29 #include "error.H"
30 #include "bitSet.H"
31 #include "polyMesh.H"
32 #include "cellModel.H"
34 
35 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
36 
37 namespace Foam
38 {
39  defineTypeNameAndDebug(ensightCells, 0);
40 }
41 
42 const char* Foam::ensightCells::elemNames[5] =
43 {
44  "tetra4", "pyramid5", "penta6", "hexa8", "nfaced"
45 };
46 
47 static_assert
48 (
50  "Support exactly 5 cell types (tetra4, pyramid5, penta6, hexa8, nfaced)"
51 );
52 
53 
54 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
55 
56 void Foam::ensightCells::resizeAll()
57 {
58  // Assign sub-list offsets, determine overall size
59 
60  label len = 0;
61 
62  auto iter = offsets_.begin();
63 
64  *iter = 0;
65  for (const label n : sizes_)
66  {
67  len += n;
68 
69  *(++iter) = len;
70  }
71 
72  // The addressing space
73  addressing().resize(len, Zero);
74 }
75 
76 
77 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
78 
80 :
82  manifold_(false),
83  offsets_(Zero),
84  sizes_(Zero)
85 {}
86 
87 
88 Foam::ensightCells::ensightCells(const string& description)
89 :
90  ensightCells()
91 {
92  rename(description);
93 }
94 
95 
96 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
97 
99 {
101 
102  for (int typei = 0; typei < nTypes; ++typei)
103  {
104  count[typei] = size(elemType(typei));
105  }
106 
107  return count;
108 }
109 
110 
111 Foam::label Foam::ensightCells::totalSize() const noexcept
112 {
113  label count = 0;
114  for (label n : sizes_)
115  {
116  count += n;
117  }
118  return count;
119 }
120 
121 
123 {
124  clearOut();
125 
128  manifold_ = false;
129  sizes_ = Zero;
130  offsets_ = Zero;
131 }
132 
133 
135 {}
136 
137 
139 {
140  for (int typei = 0; typei < nTypes; ++typei)
141  {
142  sizes_[typei] = size(elemType(typei));
143  }
144  Foam::reduce(sizes_, sumOp<label>());
145 }
146 
147 
149 {
150  for (int typei=0; typei < nTypes; ++typei)
151  {
152  const labelRange sub(range(elemType(typei)));
153 
154  if (!sub.empty())
155  {
156  SubList<label> ids(addressing(), sub);
157 
158  Foam::sort(ids);
159  }
160  }
161 }
162 
163 
164 template<class Addressing>
165 void Foam::ensightCells::classifyImpl
166 (
167  const polyMesh& mesh,
168  const Addressing& cellIds
169 )
170 {
172 
173  // References to cell shape models
174  const cellModel& tet = cellModel::ref(cellModel::TET);
175  const cellModel& pyr = cellModel::ref(cellModel::PYR);
176  const cellModel& prism = cellModel::ref(cellModel::PRISM);
177  const cellModel& hex = cellModel::ref(cellModel::HEX);
178 
179  const cellShapeList& shapes = mesh.cellShapes();
180 
181  // Pass 1: Count the shapes
182 
183  sizes_ = Zero; // reset sizes
184  for (const label id : cellIds)
185  {
186  const cellModel& model = shapes[id].model();
187 
188  elemType etype(elemType::NFACED);
189  if (model == tet)
190  {
191  etype = elemType::TETRA4;
192  }
193  else if (model == pyr)
194  {
195  etype = elemType::PYRAMID5;
196  }
197  else if (model == prism)
198  {
199  etype = elemType::PENTA6;
200  }
201  else if (model == hex)
202  {
203  etype = elemType::HEXA8;
204  }
205 
206  ++sizes_[etype];
207  }
208 
209  resizeAll(); // adjust allocation
210  sizes_ = Zero; // reset sizes - use for local indexing here
211 
212 
213  // Pass 2: Assign cell-id per shape type
214 
215  for (const label id : cellIds)
216  {
217  const cellModel& model = shapes[id].model();
218 
219  elemType etype(elemType::NFACED);
220  if (model == tet)
221  {
222  etype = elemType::TETRA4;
223  }
224  else if (model == pyr)
225  {
226  etype = elemType::PYRAMID5;
227  }
228  else if (model == prism)
229  {
230  etype = elemType::PENTA6;
231  }
232  else if (model == hex)
233  {
234  etype = elemType::HEXA8;
235  }
236 
237  add(etype, id);
238  }
239 }
240 
241 
242 void Foam::ensightCells::classify(const polyMesh& mesh)
243 {
244  // All mesh cells
245  classifyImpl(mesh, labelRange(mesh.nCells()));
246 }
247 
248 
250 (
251  const polyMesh& mesh,
252  const labelUList& cellIds
253 )
254 {
255  classifyImpl(mesh, cellIds);
256 }
257 
258 
260 (
261  const polyMesh& mesh,
262  const bitSet& selection
263 )
264 {
265  classifyImpl(mesh, selection);
266 }
267 
268 
269 void Foam::ensightCells::writeDict(Ostream& os, const bool full) const
270 {
271  os.beginBlock(type());
272 
273  os.writeEntry("id", index()+1); // Ensight starts with 1
274  os.writeEntry("name", name());
275  os.writeEntry("size", size());
276 
277  if (full)
278  {
279  for (int typei=0; typei < ensightCells::nTypes; ++typei)
280  {
281  const auto etype = ensightCells::elemType(typei);
282 
284 
285  cellIds(etype).writeList(os, 0) << endEntry; // Flat output
286  }
287  }
288 
289  os.endBlock();
290 }
291 
292 
293 // ************************************************************************* //
void clear()
Clear element addressing.
Definition: ensightPart.H:101
void classify(const polyMesh &mesh)
Classify cell types and set the element lists.
Definition: ensightCells.C:235
A 1D vector of objects of type <T> with a fixed length <N>.
Definition: HashTable.H:107
void resize(const label len)
Adjust allocated size of list.
Definition: ListI.H:160
iterator begin() noexcept
Return an iterator to begin traversing the FixedList.
Definition: FixedListI.H:482
IOstream & hex(IOstream &io)
Definition: IOstream.H:545
static const manifoldCellsMeshObject & New(const polyMesh &mesh, Args &&... args)
Get existing or create a new MeshObject. Registered with typeName.
Definition: MeshObject.C:53
A range or interval of labels defined by a start and a size.
Definition: labelRange.H:52
void sort()
Sort element lists numerically.
Definition: ensightCells.C:141
const cellShapeList & cellShapes() const
Return cell shapes.
List< cellShape > cellShapeList
List of cellShape.
Definition: cellShapeList.H:32
Ostream & writeEntry(const keyType &key, const T &value)
Write a keyword/value entry.
Definition: Ostream.H:321
Ostream & endEntry(Ostream &os)
Write end entry (&#39;;&#39;) followed by newline.
Definition: Ostream.H:565
virtual void writeDict(Ostream &os, const bool full=false) const
Write information about the object as a dictionary, optionally write all element addresses.
Definition: ensightCells.C:262
ensightCells()
Default construct, with part index 0.
Definition: ensightCells.C:72
scalar range
static const cellModel & ref(const modelType model)
Look up reference to cellModel by enumeration. Fatal on failure.
Definition: cellModels.C:150
fileName::Type type(const fileName &name, const bool followLink=true)
Return the file type: DIRECTORY or FILE, normally following symbolic links.
Definition: POSIX.C:799
unsigned int count(const UList< bool > &bools, const bool val=true)
Count number of &#39;true&#39; entries.
Definition: BitOps.H:73
void clearOut()
Clear any demand-driven data.
Definition: ensightCells.C:127
dynamicFvMesh & mesh
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for expressions::valueTypeCode::INVALID.
Definition: exprTraits.C:127
FixedList< label, nTypes > sizes() const
Processor-local sizes per element type.
Definition: ensightCells.C:91
void sort(UList< T > &list)
Sort the list.
Definition: UList.C:296
Sorting/classification of cells (3D) into corresponding ensight element types.
Definition: ensightCells.H:49
virtual Ostream & endBlock()
Write end block group.
Definition: Ostream.C:108
elemType
Supported ensight &#39;Cell&#39; element types.
Definition: ensightCells.H:62
void reduce()
Sum element counts across all processes.
Definition: ensightCells.C:131
const labelList & addressing() const noexcept
Element addressing.
Definition: ensightPart.H:85
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:56
const direction noexcept
Definition: Scalar.H:258
void add(FieldField< Field1, typename typeOfSum< Type1, Type2 >::type > &f, const FieldField< Field1, Type1 > &f1, const FieldField< Field2, Type2 > &f2)
void clear()
Set addressable sizes to zero, free up addressing memory.
Definition: ensightCells.C:115
Base class for ensightCells, ensightFaces, ensightOutputSurfaces.
Definition: ensightPart.H:49
OBJstream os(runTime.globalPath()/outputName)
defineTypeNameAndDebug(combustionModel, 0)
static const char * key(const elemType etype) noexcept
The ensight element name for the specified &#39;Cell&#39; type.
Definition: ensightCellsI.H:42
bool manifold() const
True if any manifold cells detected (globally) Triggers demand-driven filtering if required...
A bitSet stores bits (elements with only two states) in packed internal format and supports a variety...
Definition: bitSet.H:59
label nCells() const noexcept
Number of mesh cells.
void reduce(const List< UPstream::commsStruct > &comms, T &value, const BinaryOp &bop, const int tag, const label comm)
Reduce inplace (cf. MPI Allreduce) using specified communication schedule.
static const char * elemNames[nTypes]
The ensight &#39;Cell&#39; element type names.
Definition: ensightCells.H:79
virtual Ostream & beginBlock(const keyType &kw)
Write begin block group with the given name.
Definition: Ostream.C:90
label n
Mesh consisting of general polyhedral cells.
Definition: polyMesh.H:74
label totalSize() const noexcept
The global size of all element types.
Definition: ensightCells.C:104
Namespace for OpenFOAM.
virtual Ostream & writeKeyword(const keyType &kw)
Write the keyword followed by an appropriate indentation.
Definition: Ostream.C:60
static constexpr int nTypes
Number of &#39;Cell&#39; element types (5)
Definition: ensightCells.H:74
static constexpr const zero Zero
Global zero (0)
Definition: zero.H:127