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-2022 OpenCFD Ltd.
9-------------------------------------------------------------------------------
10License
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
36namespace Foam
37{
38 //- Binary output
39 static inline void writeMeasured_binary
40 (
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 (
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 // Gather sizes (offsets irrelevant)
111 const globalIndex procAddr(nLocalParcels, globalIndex::gatherOnly{});
112
113
114 DynamicList<point> positions;
115 positions.reserve(Pstream::master() ? procAddr.maxSize() : nLocalParcels);
116
117 // Extract positions
118 if (parcelsPtr)
119 {
120 const auto& parcels = *parcelsPtr;
121
122 positions.resize_nocopy(parcels.size()); // same as nLocalParcels
123
124 auto outIter = positions.begin();
125
126 for (const passiveParticle& p : parcels)
127 {
128 *outIter = p.position();
129 ++outIter;
130 }
131
132 parcelsPtr.reset(nullptr);
133 }
134
135 if (Pstream::master())
136 {
137 ensightFile& os = output();
138 const bool isBinaryOutput = (os.format() == IOstream::BINARY);
139
140 label parcelId = 0;
141
142 if (isBinaryOutput)
143 {
144 // NB: binary write is Ensight6 - first ids, then positions
145
146 // 1-index
147 for (label id = 1; id <= nTotParcels; ++id)
148 {
149 os.write(id);
150 }
151
152 // Write master data
153 writeMeasured_binary(os, positions);
154 }
155 else
156 {
157 // NB: ascii write is (id + position) together
158
159 // Write master data
160 parcelId = writeMeasured_ascii(os, parcelId, positions);
161 }
162
163
164 // Receive and write
165 for (const label proci : procAddr.subProcs())
166 {
167 positions.resize_nocopy(procAddr.localSize(proci));
168 UIPstream::read
169 (
170 UPstream::commsTypes::scheduled,
171 proci,
172 positions.data_bytes(),
173 positions.size_bytes()
174 );
175
176 if (isBinaryOutput)
177 {
178 writeMeasured_binary(os, positions);
179 }
180 else
181 {
182 parcelId = writeMeasured_ascii(os, parcelId, positions);
183 }
184 }
185 }
186 else
187 {
188 // Send
189 UOPstream::write
190 (
191 UPstream::commsTypes::scheduled,
192 UPstream::masterNo(),
193 positions.cdata_bytes(),
194 positions.size_bytes()
195 );
196 }
197
198 return true;
199}
200
201
202// ************************************************************************* //
globalIndex procAddr(aMesh.nFaces())
Base cloud calls templated on particle type.
Definition: Cloud.H:68
A 1D vector of objects of type <T> that resizes itself as necessary to accept the new objects.
Definition: DynamicList.H:72
void reserve(const label len)
Definition: DynamicListI.H:333
virtual Ostream & write(const char c)
Write character.
Definition: OBJstream.C:78
A 1D vector of objects of type <T>, where the size of the vector is known and can be used for subscri...
Definition: UList.H:94
Pointer management similar to std::unique_ptr, with some additional methods and type checking.
Definition: autoPtr.H:66
void reset(autoPtr< T > &&other) noexcept
Delete managed object and set to new given pointer.
Definition: autoPtrI.H:117
Ensight output with specialized write() for strings, integers and floats. Correctly handles binary wr...
Definition: ensightFile.H:55
Mesh data needed to do the Finite Volume discretisation.
Definition: fvMesh.H:91
Calculates a unique integer (label so might not have enough room - 2G max) for processor + local inde...
Definition: globalIndex.H:68
Copy of base particle.
A class for handling words, derived from Foam::string.
Definition: word.H:68
volScalarField & p
dynamicFvMesh & mesh
A collection of functions for writing clouds as ensight file content.
OBJstream os(runTime.globalPath()/outputName)
const pointField & points
bool writeCloudPositions(const fvMesh &mesh, const word &cloudName, bool exists, autoPtr< ensightFile > &output)
Write cloud positions.
Namespace for OpenFOAM.
static label writeMeasured_ascii(ensightFile &os, label pointId, const UList< point > &points)
ASCII output. Id + position together.
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:633
static void writeMeasured_binary(ensightFile &os, const UList< point > &points)
Binary output.
T returnReduce(const T &value, const BinaryOp &bop, const int tag=UPstream::msgType(), const label comm=UPstream::worldComm)
Reduce (copy) and return value.
const word cloudName(propsDict.get< word >("cloud"))