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-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 "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  { "tria3", "quad4", "nsided" };
41 
42 static_assert
43 (
45  "Support exactly 3 face types (tria3, quad4, nsided)"
46 );
47 
48 
49 // * * * * * * * * * * * * * * * Local Functions * * * * * * * * * * * * * * //
50 
51 namespace
52 {
53 
54 // Trivial shape classifier
55 static inline Foam::ensightFaces::elemType whatType(const Foam::face& f)
56 {
57  return
58  (
59  f.size() == 3
60  ? Foam::ensightFaces::elemType::TRIA3
61  : f.size() == 4
62  ? Foam::ensightFaces::elemType::QUAD4
63  : Foam::ensightFaces::elemType::NSIDED
64  );
65 }
66 
67 } // End anonymous namespace
68 
69 
70 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
71 
72 void Foam::ensightFaces::resizeAll()
73 {
74  // Assign sub-list offsets, determine overall size
75 
76  label len = 0;
77 
78  auto iter = offsets_.begin();
79 
80  *iter = 0;
81  for (const label n : sizes_)
82  {
83  len += n;
84 
85  *(++iter) = len;
86  }
87 
88  // The addressing space
89  addressing().resize(len, Zero);
90 
91  // Normally assume no flipMap
92  flipMap_.clear();
93 }
94 
95 
96 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
97 
99 :
100  ensightPart(),
101  flipMap_(),
102  offsets_(Zero),
103  sizes_(Zero)
104 {}
105 
106 
107 Foam::ensightFaces::ensightFaces(const string& description)
108 :
109  ensightFaces()
110 {
111  rename(description);
112 }
113 
114 
115 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
116 
118 {
120 
121  forAll(count, typei)
122  {
123  count[typei] = size(elemType(typei));
124  }
125 
126  return count;
127 }
128 
129 
130 Foam::label Foam::ensightFaces::total() const
131 {
132  label n = 0;
133  forAll(sizes_, typei)
134  {
135  n += sizes_[typei];
136  }
137  return n;
138 }
139 
140 
142 {
143  clearOut();
144 
146 
147  flipMap_.clear();
148  sizes_ = Zero;
149  offsets_ = Zero;
150 }
151 
152 
154 {}
155 
156 
158 {
159  // No listCombineGather, listCombineScatter for FixedList
160  forAll(sizes_, typei)
161  {
162  sizes_[typei] = size(elemType(typei));
163  Foam::reduce(sizes_[typei], sumOp<label>());
164  }
165 }
166 
167 
169 {
170  const bool useFlip = (size() == flipMap_.size());
171 
172  if (useFlip)
173  {
174  // Must sort flip map as well
175  labelList order;
176 
177  for (int typei=0; typei < nTypes; ++typei)
178  {
179  const labelRange sub(range(elemType(typei)));
180 
181  if (!sub.empty())
182  {
183  SubList<label> ids(addressing(), sub);
184  SubList<bool> flips(flipMap_, sub);
185 
186  Foam::sortedOrder(ids, order);
187 
188  ids = reorder<labelList>(order, ids);
189  flips = reorder<boolList>(order, flips);
190  }
191  }
192  }
193  else
194  {
195  flipMap_.clear(); // Extra safety
196 
197  // No flip-maps, simply sort addresses
198  for (int typei=0; typei < nTypes; ++typei)
199  {
200  const labelRange sub(range(elemType(typei)));
201 
202  if (!sub.empty())
203  {
204  SubList<label> ids(addressing(), sub);
205  Foam::sort(ids);
206  }
207  }
208  }
209 }
210 
211 
213 {
214  const label len = faces.size();
215 
216  // Pass 1: Count the shapes
217 
218  sizes_ = Zero; // reset sizes
219  for (label listi = 0; listi < len; ++listi)
220  {
221  const auto etype = whatType(faces[listi]);
222 
223  ++sizes_[etype];
224  }
225 
226  resizeAll(); // adjust allocation
227  sizes_ = Zero; // reset sizes - use for local indexing here
228 
229 
230  // Pass 2: Assign face-id per shape type
231 
232  for (label listi = 0; listi < len; ++listi)
233  {
234  const auto etype = whatType(faces[listi]);
235 
236  add(etype, listi);
237  }
238 }
239 
240 
242 (
243  const UList<face>& faces,
244  const labelRange& range
245 )
246 {
247  const labelRange slice(range.subset0(faces.size()));
248 
249  // Operate on a local slice
250  classify(SubList<face>(slice, faces));
251 
252  // Fixup to use the real faceIds instead of the 0-based slice
253  incrAddressing(slice.start());
254 }
255 
256 
258 (
259  const UList<face>& faces,
260  const labelUList& addr,
261  const boolList& flipMap,
262  const bitSet& exclude
263 )
264 {
265  const label len = addr.size();
266  const bool useFlip = (len == flipMap.size());
267 
268  // Pass 1: Count the shapes
269 
270  sizes_ = Zero; // reset sizes
271  for (label listi = 0; listi < len; ++listi)
272  {
273  const label faceId = addr[listi];
274 
275  if (!exclude.test(faceId))
276  {
277  const auto etype = whatType(faces[faceId]);
278 
279  ++sizes_[etype];
280  }
281  }
282 
283  resizeAll(); // adjust allocation
284  sizes_ = Zero; // reset sizes - use for local indexing here
285 
286  if (useFlip)
287  {
288  flipMap_.resize(len);
289  flipMap_ = false;
290  }
291  else
292  {
293  flipMap_.clear(); // Extra safety
294  }
295 
296  // Pass 2: Assign face-id per shape type
297 
298  for (label listi = 0; listi < len; ++listi)
299  {
300  const label faceId = addr[listi];
301  const bool doFlip = useFlip && flipMap[listi];
302 
303  if (!exclude.test(faceId))
304  {
305  const auto etype = whatType(faces[faceId]);
306 
307  add(etype, faceId, doFlip);
308  }
309  }
310 }
311 
312 
313 void Foam::ensightFaces::writeDict(Ostream& os, const bool full) const
314 {
315  os.beginBlock(type());
316 
317  os.writeEntry("id", index()+1); // Ensight starts with 1
318  os.writeEntry("name", name());
319  os.writeEntry("size", size());
320 
321  if (full)
322  {
323  for (int typei=0; typei < ensightFaces::nTypes; ++typei)
324  {
325  const auto etype = ensightFaces::elemType(typei);
326 
327  os.writeKeyword(ensightFaces::key(etype));
328 
329  faceIds(etype).writeList(os, 0) << endEntry; // Flat output
330  }
331  }
332 
333  os.endBlock();
334 }
335 
336 
337 // ************************************************************************* //
Foam::ensightPart::addressing
const labelList & addressing() const
Element addressing.
Definition: ensightPart.H:76
Foam::ensightFaces::nTypes
static constexpr int nTypes
Number of 'Face' element types (3)
Definition: ensightFaces.H:86
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
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:53
Foam::ensightFaces::key
static const char * key(const elemType etype)
The ensight element name for the specified 'Face' type.
Definition: ensightFacesI.H:46
Foam::ensightFaces::clearOut
void clearOut()
Clear any demand-driven data.
Definition: ensightFaces.C:153
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:68
Foam::ensightFaces::writeDict
virtual void writeDict(Ostream &os, const bool full=false) const
Definition: ensightFaces.C:313
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:130
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:157
Foam::name
word name(const complex &c)
Return string representation of complex.
Definition: complex.C:76
faceId
label faceId(-1)
Foam::sort
void sort(UList< T > &a)
Definition: UList.C:254
Foam::ensightFaces::sizes
FixedList< label, nTypes > sizes() const
Processor-local sizes per element type.
Definition: ensightFaces.C:117
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::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
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:98
Foam::ensightFaces::elemType
elemType
Supported ensight 'Face' element types.
Definition: ensightFaces.H:78
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:376
Foam::ensightFaces::clear
void clear()
Set addressable sizes to zero, free up addressing memory.
Definition: ensightFaces.C:141
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()
Sort element lists numerically.
Definition: ensightFaces.C:168
Foam::ensightFaces::elemNames
static const char * elemNames[nTypes]
The ensight 'Face' element type names.
Definition: ensightFaces.H:89
Foam::List::clear
void clear()
Clear the list, i.e. set size to zero.
Definition: ListI.H:115
Foam::Ostream::writeEntry
Ostream & writeEntry(const keyType &key, const T &value)
Write a keyword/value entry.
Definition: Ostream.H:232
Foam::UList::size
void size(const label n) noexcept
Override size to be inconsistent with allocated storage.
Definition: UListI.H:360
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::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:212