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 label nSlaves = (parallel ? Pstream::nProcs() : 0);
67 
68  // Using manual copyComponent(...) instead of fld.component() to support
69  // indirect lists etc.
70 
71  scalarField send(fld.size());
72 
73  if (Pstream::master())
74  {
75  // Serial output, or parallel (master)
76 
77  os.beginPart(partId, partName);
79 
80  for (direction cmpt=0; cmpt < point::nComponents; ++cmpt)
81  {
82  copyComponent(send, fld, cmpt);
83  os.writeList(send);
84 
85  for (int slave=1; slave < nSlaves; ++slave)
86  {
87  IPstream fromSlave(Pstream::commsTypes::scheduled, slave);
88  scalarField recv(fromSlave);
89  os.writeList(recv);
90  }
91  }
92  }
93  else if (nSlaves)
94  {
95  // Parallel (slaves)
96 
97  for (direction cmpt=0; cmpt < point::nComponents; ++cmpt)
98  {
99  copyComponent(send, fld, cmpt);
100 
101  OPstream toMaster
102  (
103  Pstream::commsTypes::scheduled,
104  Pstream::masterNo()
105  );
106 
107  toMaster << send;
108  }
109  }
110 
111  return true;
112 }
113 
114 
115 template<template<typename> class FieldContainer, class Type>
117 (
118  ensightFile& os,
119  const char* key,
120  const FieldContainer<Type>& fld,
121  bool parallel
122 )
123 {
124  parallel = parallel && Pstream::parRun();
125 
126  const label nSlaves = (parallel ? Pstream::nProcs() : 0);
127 
128  // Preliminary checks
129  {
130  bool hasField = !fld.empty();
131 
132  if (parallel)
133  {
134  reduce(hasField, orOp<bool>());
135  }
136 
137  // No field
138  if (!hasField) return false;
139  }
140 
141 
142  // Using manual copyComponent(...) instead of fld.component() to support
143  // indirect lists etc.
144 
145  scalarField send(fld.size());
146 
147  if (Pstream::master())
148  {
149  // Serial output, or parallel (master)
150 
151  os.writeKeyword(key);
152 
153  for (direction d=0; d < pTraits<Type>::nComponents; ++d)
154  {
156 
157  copyComponent(send, fld, cmpt);
158  os.writeList(send);
159 
160  for (label slave=1; slave < nSlaves; ++slave)
161  {
162  IPstream fromSlave(Pstream::commsTypes::scheduled, slave);
163  scalarField recv(fromSlave);
164  os.writeList(recv);
165  }
166  }
167  }
168  else if (nSlaves)
169  {
170  // Parallel (slaves)
171 
172  for (direction d=0; d < pTraits<Type>::nComponents; ++d)
173  {
175 
176  copyComponent(send, fld, cmpt);
177 
178  OPstream toMaster
179  (
180  Pstream::commsTypes::scheduled,
181  Pstream::masterNo()
182  );
183 
184  toMaster << send;
185  }
186  }
187 
188  return true;
189 }
190 
191 
192 template<class Type>
194 (
195  ensightFile& os,
196  const Field<Type>& fld,
197  const ensightFaces& part,
198  bool parallel
199 )
200 {
201  parallel = parallel && Pstream::parRun();
202 
203  // Preliminary checks: total() contains pre-reduced information
204  {
205  // No geometry
206  if (parallel ? !part.total() : !part.size()) return false;
207 
208  bool hasField = !fld.empty();
209 
210  if (parallel)
211  {
212  reduce(hasField, orOp<bool>());
213  }
214 
215  // No field
216  if (!hasField) return false;
217  }
218 
219 
220  if (Pstream::master())
221  {
222  os.beginPart(part.index());
223  }
224 
225  labelList localAddr;
226 
227  for (int typei=0; typei < ensightFaces::nTypes; ++typei)
228  {
229  const auto etype = ensightFaces::elemType(typei);
230 
232  (
233  os,
234  ensightFaces::key(etype),
235  SubField<Type>(fld, part.range(etype)),
236  parallel
237  );
238  }
239 
240  return true;
241 }
242 
243 
244 template<class Type>
246 (
247  ensightFile& os,
248  const Field<Type>& fld,
249  const ensightCells& part,
250  bool parallel
251 )
252 {
253  parallel = parallel && Pstream::parRun();
254 
255  // Preliminary checks: total() contains pre-reduced information
256  {
257  // No geometry
258  if (parallel ? !part.total() : !part.size()) return false;
259 
260  bool hasField = !fld.empty();
261 
262  if (parallel)
263  {
264  reduce(hasField, orOp<bool>());
265  }
266 
267  // No field
268  if (!hasField) return false;
269  }
270 
271  if (Pstream::master())
272  {
273  os.beginPart(part.index());
274  }
275 
276  for (int typei=0; typei < ensightCells::nTypes; ++typei)
277  {
278  const auto etype = ensightCells::elemType(typei);
279 
281  (
282  os,
283  ensightCells::key(etype),
284  UIndirectList<Type>(fld, part.cellIds(etype)),
285  parallel
286  );
287  }
288 
289  return true;
290 }
291 
292 
293 template<class Type>
295 (
296  ensightFile& os,
297  const Field<Type>& fld,
298  const ensightFaces& part,
299  bool parallel
300 )
301 {
302  parallel = parallel && Pstream::parRun();
303 
304  // Preliminary checks: total() contains pre-reduced information
305  {
306  // No geometry
307  if (parallel ? !part.total() : !part.size()) return false;
308 
309  bool hasField = !fld.empty();
310 
311  if (parallel)
312  {
313  reduce(hasField, orOp<bool>());
314  }
315 
316  // No field
317  if (!hasField) return false;
318  }
319 
320 
321  if (Pstream::master())
322  {
323  os.beginPart(part.index());
324  }
325 
326  for (int typei=0; typei < ensightFaces::nTypes; ++typei)
327  {
328  const auto etype = ensightFaces::elemType(typei);
329 
331  (
332  os,
333  ensightFaces::key(etype),
334  UIndirectList<Type>(fld, part.faceIds(etype)),
335  parallel
336  );
337  }
338 
339  return true;
340 }
341 
342 
343 // ************************************************************************* //
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::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:117
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:194
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:246
Foam::List< label >
Foam::direction
uint8_t direction
Definition: direction.H:47
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: fvMatrix.H:109
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