ensightOutputTemplates.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) 2019-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 "ensightOutput.H"
29 #include "ensightPTraits.H"
30 
31 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
32 
33 template<template<typename> class FieldContainer, class Type>
35 (
36  scalarField& res,
37  const FieldContainer<Type>& input,
38  const direction cmpt
39 )
40 {
41  res.resize(input.size());
42 
43  auto iter = res.begin();
44 
45  for (const Type& val : input)
46  {
47  *iter = component(val, cmpt);
48  ++iter;
49  }
50 }
51 
52 
53 template<template<typename> class FieldContainer>
55 (
56  ensightGeoFile& os,
57  const label partId,
58  const word& partName,
59  const label nPoints,
60  const FieldContainer<Foam::point>& fld,
61  bool parallel
62 )
63 {
64  parallel = parallel && Pstream::parRun();
65 
66  const IntRange<int> senders =
67  (
68  parallel
69  ? Pstream::subProcs()
70  : IntRange<int>()
71  );
72 
73 
74  // Using manual copyComponent(...) instead of fld.component() to support
75  // indirect lists etc.
76 
77  scalarField send(fld.size());
78 
79  if (Pstream::master())
80  {
81  os.beginPart(partId, partName);
83 
84  for (direction cmpt=0; cmpt < point::nComponents; ++cmpt)
85  {
86  // Main
87  copyComponent(send, fld, cmpt);
88  os.writeList(send);
89 
90  // Others
91  for (const int proci : senders)
92  {
93  IPstream fromOther(Pstream::commsTypes::scheduled, proci);
94  scalarField recv(fromOther);
95 
96  os.writeList(recv);
97  }
98  }
99  }
100  else if (senders)
101  {
102  // Send from other (parallel)
103 
104  for (direction cmpt=0; cmpt < point::nComponents; ++cmpt)
105  {
106  copyComponent(send, fld, cmpt);
107 
108  OPstream toMaster
109  (
110  Pstream::commsTypes::scheduled,
111  Pstream::masterNo()
112  );
113 
114  toMaster << send;
115  }
116  }
117 
118  return true;
119 }
120 
121 
122 template<template<typename> class FieldContainer, class Type>
124 (
125  ensightFile& os,
126  const char* key,
127  const FieldContainer<Type>& fld,
128  bool parallel
129 )
130 {
131  parallel = parallel && Pstream::parRun();
132 
133  const IntRange<int> senders =
134  (
135  parallel
136  ? Pstream::subProcs()
137  : IntRange<int>()
138  );
139 
140 
141  // Preliminary checks
142  {
143  bool hasField = !fld.empty();
144 
145  if (parallel)
146  {
147  reduce(hasField, orOp<bool>());
148  }
149 
150  // No field
151  if (!hasField) return false;
152  }
153 
154 
155  // Using manual copyComponent(...) instead of fld.component() to support
156  // indirect lists etc.
157 
158  scalarField send(fld.size());
159 
160  if (Pstream::master())
161  {
162  os.writeKeyword(key);
163 
164  for (direction d=0; d < pTraits<Type>::nComponents; ++d)
165  {
167 
168  // Main
169  copyComponent(send, fld, cmpt);
170  os.writeList(send);
171 
172  // Others
173  for (const int proci : senders)
174  {
175  IPstream fromOther(Pstream::commsTypes::scheduled, proci);
176  scalarField recv(fromOther);
177 
178  os.writeList(recv);
179  }
180  }
181  }
182  else if (senders)
183  {
184  // Send from other (parallel)
185 
186  for (direction d=0; d < pTraits<Type>::nComponents; ++d)
187  {
189 
190  copyComponent(send, fld, cmpt);
191 
192  OPstream toMaster
193  (
194  Pstream::commsTypes::scheduled,
195  Pstream::masterNo()
196  );
197 
198  toMaster << send;
199  }
200  }
201 
202  return true;
203 }
204 
205 
206 template<class Type>
208 (
209  ensightFile& os,
210  const Field<Type>& fld,
211  const ensightFaces& part,
212  bool parallel
213 )
214 {
215  parallel = parallel && Pstream::parRun();
216 
217  // Preliminary checks: total() contains pre-reduced information
218  {
219  // No geometry
220  if (parallel ? !part.total() : !part.size()) return false;
221 
222  bool hasField = !fld.empty();
223 
224  if (parallel)
225  {
226  reduce(hasField, orOp<bool>());
227  }
228 
229  // No field
230  if (!hasField) return false;
231  }
232 
233 
234  if (Pstream::master())
235  {
236  os.beginPart(part.index());
237  }
238 
239  labelList localAddr;
240 
241  for (int typei=0; typei < ensightFaces::nTypes; ++typei)
242  {
243  const auto etype = ensightFaces::elemType(typei);
244 
246  (
247  os,
248  ensightFaces::key(etype),
249  SubField<Type>(fld, part.range(etype)),
250  parallel
251  );
252  }
253 
254  return true;
255 }
256 
257 
258 template<class Type>
260 (
261  ensightFile& os,
262  const Field<Type>& fld,
263  const ensightCells& part,
264  bool parallel
265 )
266 {
267  parallel = parallel && Pstream::parRun();
268 
269  // Preliminary checks: total() contains pre-reduced information
270  {
271  // No geometry
272  if (parallel ? !part.total() : !part.size()) return false;
273 
274  bool hasField = !fld.empty();
275 
276  if (parallel)
277  {
278  reduce(hasField, orOp<bool>());
279  }
280 
281  // No field
282  if (!hasField) return false;
283  }
284 
285  if (Pstream::master())
286  {
287  os.beginPart(part.index());
288  }
289 
290  for (int typei=0; typei < ensightCells::nTypes; ++typei)
291  {
292  const auto etype = ensightCells::elemType(typei);
293 
295  (
296  os,
297  ensightCells::key(etype),
298  UIndirectList<Type>(fld, part.cellIds(etype)),
299  parallel
300  );
301  }
302 
303  return true;
304 }
305 
306 
307 template<class Type>
309 (
310  ensightFile& os,
311  const Field<Type>& fld,
312  const ensightFaces& part,
313  bool parallel
314 )
315 {
316  parallel = parallel && Pstream::parRun();
317 
318  // Preliminary checks: total() contains pre-reduced information
319  {
320  // No geometry
321  if (parallel ? !part.total() : !part.size()) return false;
322 
323  bool hasField = !fld.empty();
324 
325  if (parallel)
326  {
327  reduce(hasField, orOp<bool>());
328  }
329 
330  // No field
331  if (!hasField) return false;
332  }
333 
334 
335  if (Pstream::master())
336  {
337  os.beginPart(part.index());
338  }
339 
340  for (int typei=0; typei < ensightFaces::nTypes; ++typei)
341  {
342  const auto etype = ensightFaces::elemType(typei);
343 
345  (
346  os,
347  ensightFaces::key(etype),
348  UIndirectList<Type>(fld, part.faceIds(etype)),
349  parallel
350  );
351  }
352 
353  return true;
354 }
355 
356 
357 // ************************************************************************* //
Foam::ensightPart::index
label index() const
The index in a list (0-based)
Definition: ensightPart.H:124
Foam::component
void component(FieldField< Field, typename FieldField< Field, Type >::cmptType > &sf, const FieldField< Field, Type > &f, const direction d)
Definition: FieldFieldFunctions.C:44
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:62
Foam::ensightGeoFile::beginPart
void beginPart(const label index, const string &description)
Begin a "part" (0-based index), with a description.
Definition: ensightGeoFile.C:97
Foam::IntRange
An interval of (signed) integers defined by a start and a size.
Definition: IntRange.H:63
Foam::OPstream
Output inter-processor communications stream.
Definition: OPstream.H:52
Foam::ensightOutput::Detail::writeFieldComponents
bool writeFieldComponents(ensightFile &os, const char *key, const FieldContainer< Type > &fld, bool parallel)
Write field content (component-wise) for the given ensight element type.
Definition: ensightOutputTemplates.C:124
Foam::ensightGeoFile::beginCoordinates
void beginCoordinates(const label npoints)
Begin a "coordinates" block.
Definition: ensightGeoFile.C:108
Foam::ensightFaces
Sorting/classification of faces (2D) into corresponding ensight types.
Definition: ensightFaces.H:68
Foam::ensightFaces::range
labelRange range(const elemType etype) const
Processor-local offset/size of element type.
Definition: ensightFacesI.H:70
Foam::ensightFile::writeKeyword
virtual Ostream & writeKeyword(const keyType &key)
Definition: ensightFile.C:320
Foam::ensightFaces::total
label total() const
The global size of all element types.
Definition: ensightFaces.C:130
nPoints
label nPoints
Definition: gmvOutputHeader.H:2
Foam::SubField
SubField is a Field obtained as a section of another Field.
Definition: Field.H:64
Foam::ensightPTraits
Conversion of OpenFOAM pTraits into the Ensight equivalent.
Definition: ensightPTraits.H:54
Foam::ensightGeoFile
Specialized Ensight output with extra geometry file header.
Definition: ensightGeoFile.H:48
ensightOutput.H
Foam::Field< scalar >
ensightPTraits.H
Foam::ensightCells::total
label total() const
The global size of all element types.
Definition: ensightCells.C:107
Foam::ensightCells::cellIds
const labelList & cellIds() const
Processor-local cell ids of all elements.
Definition: ensightCellsI.H:71
Foam::ensightCells
Sorting/classification of cells (3D) into corresponding ensight element types.
Definition: ensightCells.H:54
fld
gmvFile<< "tracers "<< particles.size()<< nl;for(const passiveParticle &p :particles){ gmvFile<< p.position().x()<< ' ';}gmvFile<< nl;for(const passiveParticle &p :particles){ gmvFile<< p.position().y()<< ' ';}gmvFile<< nl;for(const passiveParticle &p :particles){ gmvFile<< p.position().z()<< ' ';}gmvFile<< nl;for(const word &name :lagrangianScalarNames){ IOField< scalar > fld(IOobject(name, runTime.timeName(), cloud::prefix, mesh, IOobject::MUST_READ, IOobject::NO_WRITE))
Definition: gmvOutputLagrangian.H:23
Foam::ensightOutput::Detail::writeFaceSubField
bool writeFaceSubField(ensightFile &os, const Field< Type > &fld, const ensightFaces &part, bool parallel)
Definition: ensightOutputTemplates.C:208
reduce
reduce(hasMovingMesh, orOp< bool >())
Foam::ensightFaces::faceIds
const labelList & faceIds() const
Processor-local face ids of all elements.
Definition: ensightFacesI.H:76
Foam::ensightFile
Ensight output with specialized write() for strings, integers and floats. Correctly handles binary wr...
Definition: ensightFile.H:52
Foam::ensightCells::size
label size(const elemType etype) const
Processor-local size of the specified element type.
Definition: ensightCellsI.H:59
Foam::ensightCells::elemType
elemType
Supported ensight 'Cell' element types.
Definition: ensightCells.H:64
Foam::ensightFaces::elemType
elemType
Supported ensight 'Face' element types.
Definition: ensightFaces.H:78
Foam::ensightOutput::Detail::copyComponent
void copyComponent(scalarField &res, const FieldContainer< Type > &input, const direction cmpt)
Copy specified field component into a scalarField.
Definition: ensightOutputTemplates.C:35
Foam::ensightOutput::writeField
bool writeField(ensightFile &os, const Field< Type > &fld, const ensightCells &part, bool parallel)
Definition: ensightOutputTemplates.C:260
Foam::List< label >
Foam::direction
uint8_t direction
Definition: direction.H:52
Foam::input
static Istream & input(Istream &is, IntRange< T > &range)
Definition: IntRanges.C:55
Foam::ensightFile::beginPart
void beginPart(const label index)
Begin a part (0-based index internally).
Definition: ensightFile.C:354
Foam::UIndirectList
A List with indirect addressing.
Definition: faMatrix.H:60
Foam::IPstream
Input inter-processor communications stream.
Definition: IPstream.H:52
Foam::ensightFile::writeList
void writeList(const UList< label > &field)
Write a list of integers as float values.
Definition: ensightFile.C:382
Foam::orOp
Definition: ops.H:234
Foam::ensightOutput::Detail::writeCoordinates
bool writeCoordinates(ensightGeoFile &os, const label partId, const word &partName, const label nPoints, const FieldContainer< Foam::point > &fld, bool parallel)
Write coordinates (component-wise) for the given part.
Definition: ensightOutputTemplates.C:55
Foam::ensightFaces::size
label size(const elemType etype) const
Processor-local size of the specified element type.
Definition: ensightFacesI.H:64