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-2020 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"
33 
34 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
35 
36 namespace Foam
37 {
38  defineTypeNameAndDebug(ensightCells, 0);
39 }
40 
41 const char* Foam::ensightCells::elemNames[5] =
42  { "tetra4", "pyramid5", "penta6", "hexa8", "nfaced" };
43 
44 static_assert
45 (
47  "Support exactly 5 cell types (tetra4, pyramid5, penta6, hexa8, nfaced)"
48 );
49 
50 
51 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
52 
53 void Foam::ensightCells::resizeAll()
54 {
55  // Assign sub-list offsets, determine overall size
56 
57  label len = 0;
58 
59  auto iter = offsets_.begin();
60 
61  *iter = 0;
62  for (const label n : sizes_)
63  {
64  len += n;
65 
66  *(++iter) = len;
67  }
68 
69  // The addressing space
70  addressing().resize(len, Zero);
71 }
72 
73 
74 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
75 
77 :
78  ensightPart(),
79  offsets_(Zero),
80  sizes_(Zero)
81 {}
82 
83 
84 Foam::ensightCells::ensightCells(const string& description)
85 :
86  ensightCells()
87 {
88  rename(description);
89 }
90 
91 
92 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
93 
95 {
97 
98  forAll(count, typei)
99  {
100  count[typei] = size(elemType(typei));
101  }
102 
103  return count;
104 }
105 
106 
107 Foam::label Foam::ensightCells::total() const
108 {
109  label n = 0;
110  forAll(sizes_, typei)
111  {
112  n += sizes_[typei];
113  }
114  return n;
115 }
116 
117 
119 {
120  clearOut();
121 
123 
124  sizes_ = Zero;
125  offsets_ = Zero;
126 }
127 
128 
130 {}
131 
132 
134 {
135  // No listCombineGather, listCombineScatter for FixedList
136  forAll(sizes_, typei)
137  {
138  sizes_[typei] = size(elemType(typei));
139  Foam::reduce(sizes_[typei], sumOp<label>());
140  }
141 }
142 
143 
145 {
146  for (int typei=0; typei < nTypes; ++typei)
147  {
148  const labelRange sub(range(elemType(typei)));
149 
150  if (!sub.empty())
151  {
152  SubList<label> ids(addressing(), sub);
153 
154  Foam::sort(ids);
155  }
156  }
157 }
158 
159 
160 template<class Addressing>
161 void Foam::ensightCells::classifyImpl
162 (
163  const polyMesh& mesh,
164  const Addressing& cellIds
165 )
166 {
167  // References to cell shape models
170  const cellModel& prism = cellModel::ref(cellModel::PRISM);
172 
173  const cellShapeList& shapes = mesh.cellShapes();
174 
175  // Pass 1: Count the shapes
176 
177  sizes_ = Zero; // reset sizes
178  for (const label id : cellIds)
179  {
180  const cellModel& model = shapes[id].model();
181 
182  elemType etype(NFACED);
183  if (model == tet)
184  {
185  etype = TETRA4;
186  }
187  else if (model == pyr)
188  {
189  etype = PYRAMID5;
190  }
191  else if (model == prism)
192  {
193  etype = PENTA6;
194  }
195  else if (model == hex)
196  {
197  etype = HEXA8;
198  }
199 
200  ++sizes_[etype];
201  }
202 
203  resizeAll(); // adjust allocation
204  sizes_ = Zero; // reset sizes - use for local indexing here
205 
206 
207  // Pass 2: Assign cell-id per shape type
208 
209  for (const label id : cellIds)
210  {
211  const cellModel& model = shapes[id].model();
212 
213  elemType etype(NFACED);
214  if (model == tet)
215  {
216  etype = TETRA4;
217  }
218  else if (model == pyr)
219  {
220  etype = PYRAMID5;
221  }
222  else if (model == prism)
223  {
224  etype = PENTA6;
225  }
226  else if (model == hex)
227  {
228  etype = HEXA8;
229  }
230 
231  add(etype, id);
232  }
233 }
234 
235 
237 {
238  // All mesh cells
239  classifyImpl(mesh, labelRange(mesh.nCells()));
240 }
241 
242 
244 (
245  const polyMesh& mesh,
246  const labelUList& cellIds
247 )
248 {
249  classifyImpl(mesh, cellIds);
250 }
251 
252 
254 (
255  const polyMesh& mesh,
256  const bitSet& selection
257 )
258 {
259  classifyImpl(mesh, selection);
260 }
261 
262 
263 void Foam::ensightCells::writeDict(Ostream& os, const bool full) const
264 {
265  os.beginBlock(type());
266 
267  os.writeEntry("id", index()+1); // Ensight starts with 1
268  os.writeEntry("name", name());
269  os.writeEntry("size", size());
270 
271  if (full)
272  {
273  for (int typei=0; typei < ensightCells::nTypes; ++typei)
274  {
275  const auto etype = ensightCells::elemType(typei);
276 
277  os.writeKeyword(ensightCells::key(etype));
278 
279  cellIds(etype).writeList(os, 0) << endEntry; // Flat output
280  }
281  }
282 
283  os.endBlock();
284 }
285 
286 
287 // ************************************************************************* //
Foam::UList::writeList
Ostream & writeList(Ostream &os, const label shortLen=0) const
Write List, with line-breaks in ASCII when length exceeds shortLen.
Definition: UListIO.C:79
Foam::ensightPart::addressing
const labelList & addressing() const
Element addressing.
Definition: ensightPart.H:76
Foam::bitSet
A bitSet stores bits (elements with only two states) in packed internal format and supports a variety...
Definition: bitSet.H:63
Foam::ensightPart::rename
void rename(const string &value)
Change the part name or description.
Definition: ensightPart.H:166
Foam::FixedList::begin
iterator begin()
Return an iterator to begin traversing the FixedList.
Definition: FixedListI.H:506
Foam::Zero
static constexpr const zero Zero
Global zero (0)
Definition: zero.H:131
Foam::cellModel::HEX
hex
Definition: cellModel.H:81
Foam::SubList
A List obtained as a section of another List.
Definition: SubList.H:53
Foam::ensightCells::key
static const char * key(const elemType etype)
The ensight element name for the specified 'Cell' type.
Definition: ensightCellsI.H:41
Foam::ensightCells::sort
void sort()
Sort element lists numerically.
Definition: ensightCells.C:144
Foam::Ostream::beginBlock
virtual Ostream & beginBlock(const keyType &kw)
Write begin block group with the given name.
Definition: Ostream.C:91
polyMesh.H
bitSet.H
Foam::cellModel::TET
tet
Definition: cellModel.H:85
Foam::polyMesh
Mesh consisting of general polyhedral cells.
Definition: polyMesh.H:77
Foam::sumOp
Definition: ops.H:213
forAll
#define forAll(list, i)
Loop across all elements in list.
Definition: stdFoam.H:296
Foam::ensightCells::elemNames
static const char * elemNames[nTypes]
The ensight 'Cell' element type names.
Definition: ensightCells.H:77
Foam::ensightCells::writeDict
virtual void writeDict(Ostream &os, const bool full=false) const
Definition: ensightCells.C:263
n
label n
Definition: TABSMDCalcMethod2.H:31
Foam::reduce
void reduce(const List< UPstream::commsStruct > &comms, T &Value, const BinaryOp &bop, const int tag, const label comm)
Definition: PstreamReduceOps.H:51
Foam::primitiveMesh::nCells
label nCells() const
Number of mesh cells.
Definition: primitiveMeshI.H:96
Foam::ensightCells::nTypes
static constexpr int nTypes
Number of 'Cell' element types (5)
Definition: ensightCells.H:74
error.H
Foam::ensightCells::ensightCells
ensightCells()
Default construct, with part index 0.
Definition: ensightCells.C:76
cellModel.H
Foam::name
word name(const complex &c)
Return string representation of complex.
Definition: complex.C:76
Foam::sort
void sort(UList< T > &a)
Definition: UList.C:254
Foam::cellModel::PRISM
prism
Definition: cellModel.H:83
Foam::IntRange::empty
bool empty() const noexcept
True if range is empty (zero-sized)
Definition: IntRangeI.H:436
Foam::labelRange
A range or interval of labels defined by a start and a size.
Definition: labelRange.H:55
Foam::ensightCells::total
label total() const
The global size of all element types.
Definition: ensightCells.C:107
Foam::ensightCells
Sorting/classification of cells (3D) into corresponding ensight element types.
Definition: ensightCells.H:54
Foam::cellModel::ref
static const cellModel & ref(const modelType model)
Look up reference to cellModel by enumeration. Fatal on failure.
Definition: cellModels.C:157
Foam::List::resize
void resize(const label newSize)
Adjust allocated size of list.
Definition: ListI.H:139
Foam::Ostream::endBlock
virtual Ostream & endBlock()
Write end block group.
Definition: Ostream.C:109
Foam::add
void add(FieldField< Field1, typename typeOfSum< Type1, Type2 >::type > &f, const FieldField< Field1, Type1 > &f1, const FieldField< Field2, Type2 > &f2)
Definition: FieldFieldFunctions.C:939
mesh
dynamicFvMesh & mesh
Definition: createDynamicFvMesh.H:6
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::Ostream::writeKeyword
virtual Ostream & writeKeyword(const keyType &kw)
Write the keyword followed by an appropriate indentation.
Definition: Ostream.C:57
Foam::primitiveMesh::cellShapes
const cellShapeList & cellShapes() const
Return cell shapes.
Definition: primitiveMesh.C:354
Foam::hex
IOstream & hex(IOstream &io)
Definition: IOstream.H:437
Foam::ensightCells::elemType
elemType
Supported ensight 'Cell' element types.
Definition: ensightCells.H:64
Foam::cellModel::PYR
pyr
Definition: cellModel.H:84
range
scalar range
Definition: LISASMDCalcMethod1.H:12
Foam::BitOps::count
unsigned int count(const UList< bool > &bools, const bool val=true)
Count number of 'true' entries.
Definition: BitOps.H:77
Foam::List< cellShape >
Foam::ensightPart
Base class for ensightCells, ensightFaces, ensightOutputSurfaces.
Definition: ensightPart.H:53
Foam::endEntry
Ostream & endEntry(Ostream &os)
Write end entry (';') followed by newline.
Definition: Ostream.H:376
Foam::type
fileName::Type type(const fileName &name, const bool followLink=true)
Return the file type: DIRECTORY or FILE, normally following symbolic links.
Definition: MSwindows.C:590
Foam::FixedList
A 1D vector of objects of type <T> with a fixed length <N>.
Definition: HashTable.H:104
Foam::UList< label >
Foam::ensightCells::clearOut
void clearOut()
Clear any demand-driven data.
Definition: ensightCells.C:129
Foam::Ostream::writeEntry
Ostream & writeEntry(const keyType &key, const T &value)
Write a keyword/value entry.
Definition: Ostream.H:232
Foam::cellModel
Maps a geometry to a set of cell primitives.
Definition: cellModel.H:72
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:56
Foam::ensightCells::classify
void classify(const polyMesh &mesh)
Classify cell types and set the element lists.
Definition: ensightCells.C:236
Foam::ensightPart::clear
void clear()
Clear element addressing.
Definition: ensightPart.H:88
Foam::ensightCells::sizes
FixedList< label, nTypes > sizes() const
Processor-local sizes per element type.
Definition: ensightCells.C:94
Foam::defineTypeNameAndDebug
defineTypeNameAndDebug(combustionModel, 0)
Foam::ensightCells::reduce
void reduce()
Sum element counts across all processes.
Definition: ensightCells.C:133
Foam::ensightCells::clear
void clear()
Set addressable sizes to zero, free up addressing memory.
Definition: ensightCells.C:118
ensightCells.H