ensightOutputCloud.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 "ensightOutputCloud.H"
29 #include "fvMesh.H"
30 #include "Cloud.H"
31 #include "passiveParticle.H"
32 #include "globalIndex.H"
33 
34 // * * * * * * * * * * * * * * * Local Functions * * * * * * * * * * * * * * //
35 
36 namespace Foam
37 {
38  //- Binary output
39  static inline void writeMeasured_binary
40  (
41  ensightFile& os,
42  const UList<point>& points
43  )
44  {
45  for (const point& p : points)
46  {
47  os.write(p.x());
48  os.write(p.y());
49  os.write(p.z());
50  }
51  }
52 
53  //- ASCII output. Id + position together
54  static inline label writeMeasured_ascii
55  (
56  ensightFile& os,
57  label pointId,
58  const UList<point>& points
59  )
60  {
61  for (const point& p : points)
62  {
63  os.write(++pointId, 8); // 1-index and an unusual width
64  os.write(p.x());
65  os.write(p.y());
66  os.write(p.z());
67  os.newline();
68  }
69 
70  return pointId;
71  }
72 
73 } // End namespace Foam
74 
75 
76 // * * * * * * * * * * * * * * * Global Functions * * * * * * * * * * * * * //
77 
79 (
80  const fvMesh& mesh,
81  const word& cloudName,
82  bool exists,
84 )
85 {
86  label nLocalParcels(0);
88 
89  if (exists)
90  {
91  parcelsPtr.reset(new Cloud<passiveParticle>(mesh, cloudName, false));
92  nLocalParcels = parcelsPtr().size();
93  }
94 
95  // Total number of parcels on all processes
96  const label nTotParcels = returnReduce(nLocalParcels, sumOp<label>());
97 
98  if (Pstream::master())
99  {
100  ensightFile& os = output();
101  os.beginParticleCoordinates(nTotParcels);
102  }
103 
104  if (!nTotParcels)
105  {
106  return false; // DONE
107  }
108 
109 
110  // Size information (offsets are irrelevant)
111  const globalIndex procAddr
112  (
113  UPstream::listGatherValues<label>(nLocalParcels),
114  globalIndex::SIZES
115  );
116 
117 
118  DynamicList<point> positions;
119  positions.reserve(Pstream::master() ? procAddr.maxSize() : nLocalParcels);
120 
121  // Extract positions
122  if (parcelsPtr)
123  {
124  const auto& parcels = *parcelsPtr;
125 
126  positions.resize_nocopy(parcels.size()); // same as nLocalParcels
127 
128  auto outIter = positions.begin();
129 
130  for (const passiveParticle& p : parcels)
131  {
132  *outIter = p.position();
133  ++outIter;
134  }
135 
136  parcelsPtr.reset(nullptr);
137  }
138 
139  if (Pstream::master())
140  {
141  ensightFile& os = output();
142  const bool isBinaryOutput = (os.format() == IOstream::BINARY);
143 
144  label parcelId = 0;
145 
146  if (isBinaryOutput)
147  {
148  // NB: binary write is Ensight6 - first ids, then positions
149 
150  // 1-index
151  for (label id = 1; id <= nTotParcels; ++id)
152  {
153  os.write(id);
154  }
155 
156  // Write master data
157  writeMeasured_binary(os, positions);
158  }
159  else
160  {
161  // NB: ascii write is (id + position) together
162 
163  // Write master data
164  parcelId = writeMeasured_ascii(os, parcelId, positions);
165  }
166 
167 
168  // Receive and write
169  for (const label proci : procAddr.subProcs())
170  {
171  positions.resize_nocopy(procAddr.localSize(proci));
173  (
174  UPstream::commsTypes::scheduled,
175  proci,
176  positions.data_bytes(),
177  positions.size_bytes()
178  );
179 
180  if (isBinaryOutput)
181  {
182  writeMeasured_binary(os, positions);
183  }
184  else
185  {
186  parcelId = writeMeasured_ascii(os, parcelId, positions);
187  }
188  }
189  }
190  else
191  {
192  // Send
194  (
195  UPstream::commsTypes::scheduled,
196  Pstream::masterNo(),
197  positions.cdata_bytes(),
198  positions.size_bytes()
199  );
200  }
201 
202  return true;
203 }
204 
205 
206 // ************************************************************************* //
p
volScalarField & p
Definition: createFieldRefs.H:8
Foam::exists
bool exists(const fileName &name, const bool checkGzip=true, const bool followLink=true)
Does the name exist (as DIRECTORY or FILE) in the file system?
Definition: MSwindows.C:625
Foam::output
static Ostream & output(Ostream &os, const IntRange< T > &range)
Definition: IntRanges.C:66
cloudName
const word cloudName(propsDict.get< word >("cloud"))
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:65
Foam::returnReduce
T returnReduce(const T &Value, const BinaryOp &bop, const int tag=Pstream::msgType(), const label comm=UPstream::worldComm)
Definition: PstreamReduceOps.H:94
Foam::DynamicList< point >
globalIndex.H
Foam::passiveParticle
Copy of base particle.
Definition: passiveParticle.H:53
Cloud.H
Foam::globalIndex::localSize
label localSize() const
My local size.
Definition: globalIndexI.H:187
Foam::DynamicList::reserve
void reserve(const label len)
Definition: DynamicListI.H:333
Foam::OBJstream::write
virtual Ostream & write(const char c)
Write character.
Definition: OBJstream.C:78
Foam::sumOp
Definition: ops.H:213
Foam::globalIndex::maxSize
label maxSize() const
Global max of localSizes.
Definition: globalIndexI.H:193
Foam::blockMeshTools::read
void read(Istream &, label &val, const dictionary &)
In-place read with dictionary lookup.
Definition: blockMeshTools.C:57
passiveParticle.H
os
OBJstream os(runTime.globalPath()/outputName)
mesh
dynamicFvMesh & mesh
Definition: createDynamicFvMesh.H:6
Foam::fvMesh
Mesh data needed to do the Finite Volume discretisation.
Definition: fvMesh.H:85
fvMesh.H
Foam::ensightFile
Ensight output with specialized write() for strings, integers and floats. Correctly handles binary wr...
Definition: ensightFile.H:52
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::globalIndex
Calculates a unique integer (label so might not have enough room - 2G max) for processor + local inde...
Definition: globalIndex.H:68
Foam::ensightOutput::writeCloudPositions
bool writeCloudPositions(const fvMesh &mesh, const word &cloudName, bool exists, autoPtr< ensightFile > &output)
Write cloud positions.
Definition: ensightOutputCloud.C:79
Foam::globalIndex::subProcs
labelRange subProcs() const noexcept
Range of process indices for addressed sub-offsets (processes)
Definition: globalIndexI.H:121
Foam::autoPtr::reset
void reset(autoPtr< T > &&other) noexcept
Delete managed object and set to new given pointer.
Definition: autoPtrI.H:117
Foam::autoPtr
Pointer management similar to std::unique_ptr, with some additional methods and type checking.
Definition: HashPtrTable.H:53
Foam::Vector< scalar >
Foam::UList
A 1D vector of objects of type <T>, where the size of the vector is known and can be used for subscri...
Definition: HashTable.H:103
Foam::Cloud< passiveParticle >
points
const pointField & points
Definition: gmvOutputHeader.H:1
ensightOutputCloud.H
A collection of functions for writing clouds as ensight file content.
Foam::vtk::write
void write(vtk::formatter &fmt, const Type &val, const label n=1)
Component-wise write of a value (N times)
Definition: foamVtkOutputTemplates.C:36
Foam::writeMeasured_binary
static void writeMeasured_binary(ensightFile &os, const UList< point > &points)
Binary output.
Definition: ensightOutputCloud.C:40
Foam::DynamicList::resize_nocopy
void resize_nocopy(const label len)
Definition: DynamicListI.H:363
Foam::writeMeasured_ascii
static label writeMeasured_ascii(ensightFile &os, label pointId, const UList< point > &points)
ASCII output. Id + position together.
Definition: ensightOutputCloud.C:55