ensightFaces.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 "ensightFaces.H"
29 #include "error.H"
30 #include "polyMesh.H"
31 
32 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
33 
34 namespace Foam
35 {
36  defineTypeNameAndDebug(ensightFaces, 0);
37 }
38 
39 const char* Foam::ensightFaces::elemNames[3] =
40 {
41  "tria3", "quad4", "nsided"
42 };
43 
44 static_assert
45 (
47  "Support exactly 3 face types (tria3, quad4, nsided)"
48 );
49 
50 
51 // * * * * * * * * * * * * * * * Local Functions * * * * * * * * * * * * * * //
52 
53 namespace
54 {
55 
56 // Trivial shape classifier
57 inline Foam::ensightFaces::elemType whatType(const Foam::face& f)
58 {
59  return
60  (
61  f.size() == 3
62  ? Foam::ensightFaces::elemType::TRIA3
63  : f.size() == 4
64  ? Foam::ensightFaces::elemType::QUAD4
65  : Foam::ensightFaces::elemType::NSIDED
66  );
67 }
68 
69 } // End anonymous namespace
70 
71 
72 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
73 
74 void Foam::ensightFaces::resizeAll()
75 {
76  // Invalidate any previous face ordering
77  faceOrder_.clear();
78 
79  // Invalidate any previous flipMap
80  flipMap_.clear();
81 
82  // Assign sub-list offsets, determine overall size
83 
84  label len = 0;
85 
86  auto iter = offsets_.begin();
87 
88  *iter = 0;
89  for (const label n : sizes_)
90  {
91  len += n;
92 
93  *(++iter) = len;
94  }
95 
96  // The addressing space
97  addressing().resize(len, Zero);
98 }
99 
100 
101 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
102 
104 :
105  ensightPart(),
106  faceOrder_(),
107  flipMap_(),
108  offsets_(Zero),
109  sizes_(Zero)
110 {}
111 
112 
113 Foam::ensightFaces::ensightFaces(const string& description)
114 :
115  ensightFaces()
116 {
117  rename(description);
118 }
119 
120 
121 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
122 
124 {
126 
127  forAll(count, typei)
128  {
129  count[typei] = size(elemType(typei));
130  }
131 
132  return count;
133 }
134 
135 
136 Foam::label Foam::ensightFaces::total() const
137 {
138  label nTotal = 0;
139  forAll(sizes_, typei)
140  {
141  nTotal += sizes_[typei];
142  }
143  return nTotal;
144 }
145 
146 
148 {
149  clearOut();
150 
152 
153  faceOrder_.clear();
154  flipMap_.clear();
155  sizes_ = Zero;
156  offsets_ = Zero;
157 }
158 
159 
161 {}
162 
163 
165 {
166  // No listCombineGather, listCombineScatter for FixedList
167  forAll(sizes_, typei)
168  {
169  sizes_[typei] = size(elemType(typei));
170  Foam::reduce(sizes_[typei], sumOp<label>());
171  }
172 }
173 
174 
176 {
177  // Some extra safety
178  if (faceOrder_.size() != size())
179  {
180  faceOrder_.clear();
181  }
182  if (flipMap_.size() != size())
183  {
184  flipMap_.clear();
185  }
186 
187  // Sort by face Ids.
188  // Use to reorder flip maps and face-order too.
189 
190  for (int typei=0; typei < nTypes; ++typei)
191  {
192  const labelRange sub(range(elemType(typei)));
193 
194  if (!sub.empty())
195  {
196  SubList<label> ids(sub, addressing());
197  labelList order(Foam::sortedOrder(ids));
198 
199  ids = reorder<labelList>(order, ids);
200 
201  // Sort flip map as well
202  if (!flipMap_.empty())
203  {
204  SubList<bool> flips(flipMap_, sub);
205  flips = reorder<boolList>(order, flips);
206  }
207 
208  // Sort face ordering as well
209  if (!faceOrder_.empty())
210  {
211  SubList<label> faceOrder(faceOrder_, sub);
212  faceOrder = reorder<labelList>(order, faceOrder);
213  }
214  }
215  }
216 }
217 
218 
220 {
221  const label len = faces.size();
222 
223  // Pass 1: Count the shapes
224 
225  sizes_ = Zero; // reset sizes
226  for (label listi = 0; listi < len; ++listi)
227  {
228  const auto etype = whatType(faces[listi]);
229 
230  ++sizes_[etype];
231  }
232 
233  resizeAll(); // adjust allocation
234  sizes_ = Zero; // reset sizes - use for local indexing here
235 
236  // Pass 2: Assign face-id per shape type
237 
238  for (label listi = 0; listi < len; ++listi)
239  {
240  const auto etype = whatType(faces[listi]);
241 
242  add(etype, listi);
243  }
244 }
245 
246 
248 (
249  const UList<face>& faces,
250  const labelRange& range
251 )
252 {
253  const labelRange slice(range.subset0(faces.size()));
254 
255  // Operate on a local slice
256  classify(SubList<face>(slice, faces));
257 
258  // Fixup to use the real faceIds instead of the 0-based slice
259  incrAddressing(slice.start());
260 }
261 
262 
264 (
265  const UList<face>& faces,
266  const labelUList& addr,
267  const boolList& flipMap,
268  const bitSet& exclude
269 )
270 {
271  const label len = addr.size();
272  const bool useFlip = (len == flipMap.size());
273 
274  // Pass 1: Count the shapes
275 
276  sizes_ = Zero; // reset sizes
277  for (label listi = 0; listi < len; ++listi)
278  {
279  const label faceId = addr[listi];
280 
281  if (!exclude.test(faceId))
282  {
283  const auto etype = whatType(faces[faceId]);
284 
285  ++sizes_[etype];
286  }
287  }
288 
289  resizeAll(); // adjust allocation
290  sizes_ = Zero; // reset sizes - use for local indexing here
291 
292  label nUsed = addressing().size();
293 
294  if (useFlip)
295  {
296  flipMap_.resize(nUsed);
297  flipMap_ = false;
298  }
299 
300  faceOrder_.resize(nUsed);
301 
302  // Pass 2: Assign face-id per shape type
303  // - also record the face order
304 
305  nUsed = 0;
306  for (label listi = 0; listi < len; ++listi)
307  {
308  const label faceId = addr[listi];
309 
310  if (!exclude.test(faceId))
311  {
312  const bool doFlip = useFlip && flipMap.test(listi);
313 
314  const auto etype = whatType(faces[faceId]);
315 
316  const label idx = add(etype, faceId, doFlip);
317 
318  faceOrder_[nUsed] = idx;
319  ++nUsed;
320  }
321  }
322 }
323 
324 
325 void Foam::ensightFaces::writeDict(Ostream& os, const bool full) const
326 {
327  os.beginBlock(type());
328 
329  os.writeEntry("id", index()+1); // Ensight starts with 1
330  os.writeEntry("name", name());
331  os.writeEntry("size", size());
332 
333  if (full)
334  {
335  for (int typei=0; typei < ensightFaces::nTypes; ++typei)
336  {
337  const auto etype = ensightFaces::elemType(typei);
338 
340 
341  faceIds(etype).writeList(os, 0) << endEntry; // Flat output
342  }
343  }
344 
345  os.endBlock();
346 }
347 
348 
349 // ************************************************************************* //
Foam::ensightPart::addressing
const labelList & addressing() const noexcept
Element addressing.
Definition: ensightPart.H:76
Foam::ensightFaces::nTypes
static constexpr int nTypes
Number of 'Face' element types (3)
Definition: ensightFaces.H:89
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
ensightFaces.H
Foam::Zero
static constexpr const zero Zero
Global zero (0)
Definition: zero.H:131
Foam::SubList
A List obtained as a section of another List.
Definition: SubList.H:54
Foam::ensightFaces::key
static const char * key(const elemType etype)
The ensight element name for the specified 'Face' type.
Definition: ensightFacesI.H:49
Foam::ensightFaces::clearOut
void clearOut()
Clear any demand-driven data.
Definition: ensightFaces.C:160
Foam::Ostream::beginBlock
virtual Ostream & beginBlock(const keyType &kw)
Write begin block group with the given name.
Definition: Ostream.C:91
Foam::bitSet::test
bool test(const label pos) const
Test value at specified position, never auto-vivify entries.
Definition: bitSetI.H:520
polyMesh.H
Foam::ensightFaces
Sorting/classification of faces (2D) into corresponding ensight types.
Definition: ensightFaces.H:71
Foam::ensightFaces::writeDict
virtual void writeDict(Ostream &os, const bool full=false) const
Definition: ensightFaces.C:325
Foam::sumOp
Definition: ops.H:213
forAll
#define forAll(list, i)
Loop across all elements in list.
Definition: stdFoam.H:296
Foam::ensightFaces::total
label total() const
The global size of all element types.
Definition: ensightFaces.C:136
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
error.H
Foam::ensightFaces::reduce
void reduce()
Sum element counts across all processes.
Definition: ensightFaces.C:164
faceId
label faceId(-1)
Foam::ensightFaces::sizes
FixedList< label, nTypes > sizes() const
Processor-local sizes per element type.
Definition: ensightFaces.C:123
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::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)
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::ensightFaces::ensightFaces
ensightFaces()
Default construct, with part index 0.
Definition: ensightFaces.C:103
Foam::ensightFaces::elemType
elemType
Supported ensight 'Face' element types.
Definition: ensightFaces.H:81
range
scalar range
Definition: LISASMDCalcMethod1.H:12
f
labelList f(nPoints)
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< label >
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::ensightFaces::clear
void clear()
Set addressable sizes to zero, free up addressing memory.
Definition: ensightFaces.C:147
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< face >
Foam::ensightFaces::sort
void sort()
Inplace sort element lists numerically.
Definition: ensightFaces.C:175
Foam::ensightFaces::elemNames
static const char * elemNames[nTypes]
The ensight 'Face' element type names.
Definition: ensightFaces.H:92
Foam::List::clear
void clear()
Clear the list, i.e. set size to zero.
Definition: ListI.H:116
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::face
A face is a list of labels corresponding to mesh vertices.
Definition: face.H:72
Foam::sortedOrder
labelList sortedOrder(const UList< T > &input)
Return the (stable) sort order for the list.
Foam::UList::size
void size(const label n)
Older name for setAddressableSize.
Definition: UList.H:114
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:56
Foam::ensightPart::clear
void clear()
Clear element addressing.
Definition: ensightPart.H:88
Foam::IntRange::start
IntType start() const noexcept
The (inclusive) lower value of the range.
Definition: IntRangeI.H:408
Foam::defineTypeNameAndDebug
defineTypeNameAndDebug(combustionModel, 0)
Foam::ensightFaces::classify
void classify(const UList< face > &faces)
Classify the face types and set the element lists.
Definition: ensightFaces.C:219