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 -------------------------------------------------------------------------------
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 "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 
37 template<class Type>
38 void 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 
55 template<class Type>
56 void 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 
72 template<class Type>
73 void Foam::functionObjects::dataCloud::writeListParallel
74 (
75  Ostream& os,
76  const vectorField& points,
77  const List<Type>& field
78 )
79 {
80  if (Pstream::master())
81  {
82  writeList(os, points, field);
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 
112 template<class Type>
113 void 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 
128 template<class Type>
129 void 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 
170 template<class Type>
171 bool 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 // ************************************************************************* //
Foam::vtk::writeListParallel
void writeListParallel(vtk::formatter &fmt, const UList< uint8_t > &values)
Write a list of uint8_t values.
Definition: foamVtkOutput.C:126
Foam::UPstream::commsTypes::blocking
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::UPstream::masterNo
static constexpr int masterNo() noexcept
Process index of the master (always 0)
Definition: UPstream.H:452
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::HashTableOps::values
List< T > values(const HashTable< T, Key, Hash > &tbl, const bool doSort=false)
List of values from HashTable, optionally sorted.
Definition: HashOps.H:149
Foam::subset
List< T > subset(const BoolListType &select, const UList< T > &input, const bool invert=false)
Extract elements of the input list when select is true.
cloud.H
Foam::UPstream::master
static bool master(const label communicator=worldComm)
Am I the master process.
Definition: UPstream.H:458
OFstream.H
Foam::vectorField
Field< vector > vectorField
Specialisation of Field<T> for vector.
Definition: primitiveFieldsFwd.H:54
Foam::UPstream::subProcs
static rangeType subProcs(const label communicator=worldComm)
Range of process indices for sub-processes.
Definition: UPstream.H:516
field
rDeltaTY field()
Foam::vtk::writeList
void writeList(vtk::formatter &fmt, const UList< uint8_t > &values)
Write a list of uint8_t values.
Definition: foamVtkOutput.C:112
Foam::vector
Vector< scalar > vector
A scalar version of the templated Vector.
Definition: vector.H:51
pointField.H
Foam::nl
constexpr char nl
Definition: Ostream.H:385
IOField.H
vectorField.H
points
const pointField & points
Definition: gmvOutputHeader.H:1
Foam::direction
uint8_t direction
Definition: direction.H:52
Foam::cloud::findIOPosition
static const IOField< point > * findIOPosition(const objectRegistry &obr)
Locate the "position" IOField within object registry.
Definition: cloud.H:153
ListOps.H
Various functions to operate on Lists.