dataCloudTemplates.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) 2018-2020 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 "cloud.H"
29#include "IOField.H"
30#include "OFstream.H"
31#include "ListOps.H"
32#include "pointField.H"
33#include "vectorField.H"
34
35// * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
36
37template<class Type>
38void Foam::functionObjects::dataCloud::writePointValue
39(
40 Ostream& os,
41 const vector& pt,
42 const Type& val
43)
44{
45 os << pt.x() << ' ' << pt.y() << ' ' << pt.z();
46
47 for (direction cmpt=0; cmpt < pTraits<Type>::nComponents; ++cmpt)
48 {
49 os << ' ' << component(val, cmpt);
50 }
51 os << nl;
52}
53
54
55template<class Type>
56void Foam::functionObjects::dataCloud::writeList
57(
58 Ostream& os,
59 const vectorField& points,
60 const List<Type>& field
61)
62{
63 const label len = field.size();
64
65 for (label pointi=0; pointi<len; ++pointi)
66 {
67 writePointValue(os, points[pointi], field[pointi]);
68 }
69}
70
71
72template<class Type>
73void Foam::functionObjects::dataCloud::writeListParallel
74(
75 Ostream& os,
76 const vectorField& points,
77 const List<Type>& field
78)
79{
80 if (Pstream::master())
81 {
83
84 vectorField recvPoints;
85 Field<Type> recvField;
86
87 // Receive and write
88 for (const int slave : Pstream::subProcs())
89 {
90 IPstream fromSlave(Pstream::commsTypes::blocking, slave);
91
92 fromSlave >> recvPoints >> recvField;
93
94 writeList(os, recvPoints, recvField);
95 }
96 }
97 else
98 {
99 // Send to master
100 OPstream toMaster
101 (
104 );
105
106 toMaster
107 << points << field;
108 }
109}
110
111
112template<class Type>
113void Foam::functionObjects::dataCloud::writeList
114(
115 Ostream& os,
116 const vectorField& points,
117 const List<Type>& field,
118 const bitSet& selected
119)
120{
121 for (const label pointi : selected)
122 {
123 writePointValue(os, points[pointi], field[pointi]);
124 }
125}
126
127
128template<class Type>
129void Foam::functionObjects::dataCloud::writeListParallel
130(
131 Ostream& os,
132 const vectorField& points,
133 const List<Type>& field,
134 const bitSet& selected
135)
136{
137 if (Pstream::master())
138 {
139 writeList(os, points, field, selected);
140
141 vectorField recvPoints;
142 Field<Type> recvField;
143
144 // Receive and write
145 for (const int slave : Pstream::subProcs())
146 {
147 IPstream fromSlave(Pstream::commsTypes::blocking, slave);
148
149 fromSlave >> recvPoints >> recvField;
150
151 writeList(os, recvPoints, recvField);
152 }
153 }
154 else
155 {
156 // Send to master
157 OPstream toMaster
158 (
161 );
162
163 toMaster
164 << subset(selected, points)
165 << subset(selected, field);
166 }
167}
168
169
170template<class Type>
171bool Foam::functionObjects::dataCloud::writeField
172(
173 const fileName& outputName,
174 const objectRegistry& obrTmp
175) const
176{
177 const auto* pointsPtr = cloud::findIOPosition(obrTmp);
178
179 if (!pointsPtr)
180 {
181 // This should be impossible
182 return false;
183 }
184
185 // Fields are not always on all processors (eg, multi-component parcels).
186 // Thus need to resolve between all processors.
187
188 const List<Type>* fldPtr = obrTmp.findObject<IOField<Type>>(fieldName_);
189 const List<Type>& values = (fldPtr ? *fldPtr : List<Type>());
190
191 if (!returnReduce((fldPtr != nullptr), orOp<bool>()))
192 {
193 return false;
194 }
195
196 autoPtr<OFstream> osPtr;
197
198 if (Pstream::master())
199 {
200 osPtr.reset(new OFstream(outputName));
201 osPtr->precision(precision_);
202
203 *(osPtr) << "# x y z " << fieldName_ << nl;
204 }
205
206
207 if (applyFilter_)
208 {
209 writeListParallel(osPtr.ref(), *pointsPtr, values, parcelAddr_);
210 }
211 else
212 {
213 writeListParallel(osPtr.ref(), *pointsPtr, values);
214 }
215
216 return true;
217}
218
219
220// ************************************************************************* //
Various functions to operate on Lists.
UPstream::rangeType subProcs() const noexcept
Range of sub-processes indices associated with PstreamBuffers.
static constexpr int masterNo() noexcept
Process index of the master (always 0)
Definition: UPstream.H:451
static const IOField< point > * findIOPosition(const objectRegistry &obr)
Locate the "position" IOField within object registry.
Definition: cloud.H:153
splitCell * master() const
Definition: splitCell.H:113
A Vector of values with scalar precision, where scalar is float/double depending on the compilation f...
rDeltaTY field()
word outputName("finiteArea-edges.obj")
OBJstream os(runTime.globalPath()/outputName)
const pointField & points
List< T > values(const HashTable< T, Key, Hash > &tbl, const bool doSort=false)
List of values from HashTable, optionally sorted.
Definition: HashOps.H:149
void writeList(vtk::formatter &fmt, const UList< uint8_t > &values)
Write a list of uint8_t values.
void writeListParallel(vtk::formatter &fmt, const UList< Type > &values)
Write a list of values.
void component(FieldField< Field, typename FieldField< Field, Type >::cmptType > &sf, const FieldField< Field, Type > &f, const direction d)
List< T > subset(const BoolListType &select, const UList< T > &input, const bool invert=false)
Extract elements of the input list when select is true.
Field< vector > vectorField
Specialisation of Field<T> for vector.
uint8_t direction
Definition: direction.H:56
T returnReduce(const T &value, const BinaryOp &bop, const int tag=UPstream::msgType(), const label comm=UPstream::worldComm)
Reduce (copy) and return value.
constexpr char nl
The newline '\n' character (0x0a)
Definition: Ostream.H:53