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-2021 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 {
43  "tetra4", "pyramid5", "penta6", "hexa8", "nfaced"
44 };
45 
46 static_assert
47 (
49  "Support exactly 5 cell types (tetra4, pyramid5, penta6, hexa8, nfaced)"
50 );
51 
52 
53 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
54 
55 void Foam::ensightCells::resizeAll()
56 {
57  // Assign sub-list offsets, determine overall size
58 
59  label len = 0;
60 
61  auto iter = offsets_.begin();
62 
63  *iter = 0;
64  for (const label n : sizes_)
65  {
66  len += n;
67 
68  *(++iter) = len;
69  }
70 
71  // The addressing space
72  addressing().resize(len, Zero);
73 }
74 
75 
76 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
77 
79 :
80  ensightPart(),
81  offsets_(Zero),
82  sizes_(Zero)
83 {}
84 
85 
86 Foam::ensightCells::ensightCells(const string& description)
87 :
88  ensightCells()
89 {
90  rename(description);
91 }
92 
93 
94 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
95 
97 {
99 
100  forAll(count, typei)
101  {
102  count[typei] = size(elemType(typei));
103  }
104 
105  return count;
106 }
107 
108 
109 Foam::label Foam::ensightCells::total() const
110 {
111  label nTotal = 0;
112  forAll(sizes_, typei)
113  {
114  nTotal += sizes_[typei];
115  }
116  return nTotal;
117 }
118 
119 
121 {
122  clearOut();
123 
125 
126  sizes_ = Zero;
127  offsets_ = Zero;
128 }
129 
130 
132 {}
133 
134 
136 {
137  // No listCombineGather, listCombineScatter for FixedList
138  forAll(sizes_, typei)
139  {
140  sizes_[typei] = size(elemType(typei));
141  Foam::reduce(sizes_[typei], sumOp<label>());
142  }
143 }
144 
145 
147 {
148  for (int typei=0; typei < nTypes; ++typei)
149  {
150  const labelRange sub(range(elemType(typei)));
151 
152  if (!sub.empty())
153  {
154  SubList<label> ids(addressing(), sub);
155 
156  Foam::sort(ids);
157  }
158  }
159 }
160 
161 
162 template<class Addressing>
163 void Foam::ensightCells::classifyImpl
164 (
165  const polyMesh& mesh,
166  const Addressing& cellIds
167 )
168 {
169  // References to cell shape models
172  const cellModel& prism = cellModel::ref(cellModel::PRISM);
174 
175  const cellShapeList& shapes = mesh.cellShapes();
176 
177  // Pass 1: Count the shapes
178 
179  sizes_ = Zero; // reset sizes
180  for (const label id : cellIds)
181  {
182  const cellModel& model = shapes[id].model();
183 
184  elemType etype(NFACED);
185  if (model == tet)
186  {
187  etype = TETRA4;
188  }
189  else if (model == pyr)
190  {
191  etype = PYRAMID5;
192  }
193  else if (model == prism)
194  {
195  etype = PENTA6;
196  }
197  else if (model == hex)
198  {
199  etype = HEXA8;
200  }
201 
202  ++sizes_[etype];
203  }
204 
205  resizeAll(); // adjust allocation
206  sizes_ = Zero; // reset sizes - use for local indexing here
207 
208 
209  // Pass 2: Assign cell-id per shape type
210 
211  for (const label id : cellIds)
212  {
213  const cellModel& model = shapes[id].model();
214 
215  elemType etype(NFACED);
216  if (model == tet)
217  {
218  etype = TETRA4;
219  }
220  else if (model == pyr)
221  {
222  etype = PYRAMID5;
223  }
224  else if (model == prism)
225  {
226  etype = PENTA6;
227  }
228  else if (model == hex)
229  {
230  etype = HEXA8;
231  }
232 
233  add(etype, id);
234  }
235 }
236 
237 
239 {
240  // All mesh cells
241  classifyImpl(mesh, labelRange(mesh.nCells()));
242 }
243 
244 
246 (
247  const polyMesh& mesh,
248  const labelUList& cellIds
249 )
250 {
251  classifyImpl(mesh, cellIds);
252 }
253 
254 
256 (
257  const polyMesh& mesh,
258  const bitSet& selection
259 )
260 {
261  classifyImpl(mesh, selection);
262 }
263 
264 
265 void Foam::ensightCells::writeDict(Ostream& os, const bool full) const
266 {
267  os.beginBlock(type());
268 
269  os.writeEntry("id", index()+1); // Ensight starts with 1
270  os.writeEntry("name", name());
271  os.writeEntry("size", size());
272 
273  if (full)
274  {
275  for (int typei=0; typei < ensightCells::nTypes; ++typei)
276  {
277  const auto etype = ensightCells::elemType(typei);
278 
280 
281  cellIds(etype).writeList(os, 0) << endEntry; // Flat output
282  }
283  }
284 
285  os.endBlock();
286 }
287 
288 
289 // ************************************************************************* //
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 noexcept
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::List::resize
void resize(const label len)
Adjust allocated size of list.
Definition: ListI.H:139
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:54
Foam::ensightCells::key
static const char * key(const elemType etype)
The ensight element name for the specified 'Cell' type.
Definition: ensightCellsI.H:43
Foam::ensightCells::sort
void sort()
Sort element lists numerically.
Definition: ensightCells.C:146
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:265
Foam::FixedList::begin
iterator begin() noexcept
Return an iterator to begin traversing the FixedList.
Definition: FixedListI.H:524
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 noexcept
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:78
cellModel.H
Foam::sort
void sort(UList< T > &a)
Definition: UList.C:261
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:109
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::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
os
OBJstream os(runTime.globalPath()/outputName)
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:353
Foam::hex
IOstream & hex(IOstream &io)
Definition: IOstream.H:446
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:395
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:131
Foam::Ostream::writeEntry
Ostream & writeEntry(const keyType &key, const T &value)
Write a keyword/value entry.
Definition: Ostream.H:236
Foam::name
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for INVALID.
Definition: exprTraits.C:59
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:238
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:96
Foam::defineTypeNameAndDebug
defineTypeNameAndDebug(combustionModel, 0)
Foam::ensightCells::reduce
void reduce()
Sum element counts across all processes.
Definition: ensightCells.C:135
Foam::ensightCells::clear
void clear()
Set addressable sizes to zero, free up addressing memory.
Definition: ensightCells.C:120
ensightCells.H