steadyParticleTracksTemplates.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) 2011-2016 OpenFOAM Foundation
9  Copyright (C) 2016 OpenCFD Ltd.
10 -------------------------------------------------------------------------------
11 License
12  This file is part of OpenFOAM.
13 
14  OpenFOAM is free software: you can redistribute it and/or modify it
15  under the terms of the GNU General Public License as published by
16  the Free Software Foundation, either version 3 of the License, or
17  (at your option) any later version.
18 
19  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
20  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22  for more details.
23 
24  You should have received a copy of the GNU General Public License
25  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
26 
27 \*---------------------------------------------------------------------------*/
28 
30 
31 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
32 
33 template<class Type>
34 bool Foam::fieldOk(const IOobjectList& cloudObjs, const word& name)
35 {
36  return cloudObjs.cfindObject<IOField<Type>>(name) != nullptr;
37 }
38 
39 
40 template<class Type>
42 (
43  const word& name,
44  const IOobjectList cloudObjs
45 )
46 {
47  const IOobject* obj = cloudObjs.cfindObject<IOField<Type>>(name);
48  if (obj != nullptr)
49  {
50  IOField<Type> newField(*obj);
51  return tmp<Field<Type>>::New(std::move(newField));
52  }
53 
55  << "Error: cloud field name " << name
56  << " not found or the wrong type"
57  << abort(FatalError);
58 
59  return Field<Type>::null();
60 }
61 
62 
63 template<class Type>
65 (
66  PtrList<List<Type>>& values,
67  const List<word>& fieldNames,
68  const IOobjectList& cloudObjs
69 )
70 {
71  forAll(fieldNames, fieldi)
72  {
73  const word& fieldName = fieldNames[fieldi];
74 
75  const IOobject* obj = cloudObjs.cfindObject<IOField<Type>>(fieldName);
76  if (obj != nullptr)
77  {
78  Info<< " reading field " << fieldName << endl;
79  IOField<Type> newField(*obj);
80  values.set(fieldi, new List<Type>(std::move(newField)));
81  }
82  else
83  {
85  << "Unable to read field " << fieldName
86  << abort(FatalError);
87  }
88  }
89 }
90 
91 
92 template<class Type>
93 void Foam::writeVTK(OFstream& os, const Type& value)
94 {
95  os << value.component(0);
96  for (label i=1; i<pTraits<Type>::nComponents; i++)
97  {
98  os << ' ' << value.component(i);
99  }
100 }
101 
102 
103 template<class Type>
105 (
106  OFstream& os,
107  const PtrList<List<Type>>& values,
108  const List<List<label>>& addr,
109  const List<word>& fieldNames
110 )
111 {
112  label step = max(floor(8/pTraits<Type>::nComponents), 1);
113 
114  forAll(values, fieldi)
115  {
116  Info<< " writing field " << fieldNames[fieldi] << endl;
117  os << nl << fieldNames[fieldi] << ' '
118  << int(pTraits<Type>::nComponents) << ' '
119  << values[fieldi].size() << " float" << nl;
120  label offset = 0;
121  forAll(addr, tracki)
122  {
123  const List<label> ids(addr[tracki]);
124 
125  List<Type> data(UIndirectList<Type>(values[fieldi], ids));
126  label nData = data.size() - 1;
127  forAll(data, i)
128  {
129  writeVTK<Type>(os, data[i]);
130  if (((i + 1) % step == 0) || (i == nData))
131  {
132  os << nl;
133  }
134  else
135  {
136  os << ' ';
137  }
138  }
139  offset += ids.size();
140  }
141  }
142 }
143 
144 
145 template<class Type>
147 (
148  OFstream& os,
149  const List<List<label>>& addr,
150  const List<word>& userFieldNames,
151  const IOobjectList& cloudObjs
152 )
153 {
154  IOobjectList objects(cloudObjs.lookupClass(IOField<Type>::typeName));
155 
156  if (objects.size())
157  {
158  DynamicList<word> fieldNames(objects.size());
159  forAll(userFieldNames, i)
160  {
161  const IOobject* obj = objects.findObject(userFieldNames[i]);
162  if (obj != nullptr)
163  {
164  fieldNames.append(obj->name());
165  }
166  }
167  fieldNames.shrink();
168 
169  PtrList<List<Type>> values(fieldNames.size());
170  readFields<Type>(values, fieldNames, cloudObjs);
171 
172  writeVTKFields<Type>
173  (
174  os,
175  values,
176  addr,
177  fieldNames
178  );
179  }
180 }
181 
182 
183 // ************************************************************************* //
Foam::tmp
A class for managing temporary objects.
Definition: PtrList.H:57
Foam::HashTableOps::values
List< T > values(const HashTable< T, Key, Hash > &tbl, const bool doSort=false)
Definition: HashOps.H:164
Foam::endl
Ostream & endl(Ostream &os)
Definition: Ostream.H:381
Foam::readParticleField
tmp< Field< Type > > readParticleField(const word &name, const IOobjectList cloudObjs)
forAll
#define forAll(list, i)
Definition: stdFoam.H:349
Foam::writeVTK
void writeVTK(OFstream &os, const Type &value)
steadyParticleTracksTemplates.H
Foam::Info
messageStream Info
Foam::writeVTKFields
void writeVTKFields(OFstream &os, const PtrList< List< Type >> &values, const List< List< label >> &addr, const List< word > &fieldNames)
Foam::fieldOk
bool fieldOk(const IOobjectList &cloudObjs, const word &name)
Foam::max
label max(const labelHashSet &set, label maxValue=labelMin)
Definition: hashSets.C:40
Foam::FatalError
error FatalError
fieldNames
const wordRes fieldNames(propsDict.getOrDefault< wordRes >("fields", wordRes()))
os
OBJstream os(runTime.globalPath()/outputName)
Foam::abort
errorManip< error > abort(error &err)
Definition: errorManip.H:139
Foam::readFields
void readFields(const typename GeoFieldType::Mesh &mesh, const IOobjectList &objects, const wordHashSet &selectedFields, LIFOStack< regIOobject * > &storedObjects)
Definition: ReadFieldsTemplates.C:305
Foam::New
tmp< DimensionedField< TypeR, GeoMesh > > New(const tmp< DimensionedField< TypeR, GeoMesh >> &tdf1, const word &name, const dimensionSet &dimensions)
Definition: DimensionedFieldReuseFunctions.H:100
FatalErrorInFunction
#define FatalErrorInFunction
Definition: error.H:465
Foam::nl
constexpr char nl
Definition: Ostream.H:424
Foam::List::set
std::enable_if< std::is_same< bool, TypeT >::value, bool >::type set(const label i, bool val=true)
Definition: List.H:337
Foam::name
word name(const expressions::valueTypeCode typeCode)
Definition: exprTraits.C:52
Foam::Field< Type >::null
static const Field< Type > & null()
Definition: FieldI.H:24
Foam::processFields
void processFields(OFstream &os, const List< List< label >> &addr, const List< word > &userFieldNames, const IOobjectList &cloudObjs)