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 | Copyright (C) 2011-2015 OpenFOAM Foundation
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
11  OpenFOAM is free software: you can redistribute it and/or modify it
12  under the terms of the GNU General Public License as published by
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
16  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19  for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 \*---------------------------------------------------------------------------*/
25 
27 
28 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
29 
30 namespace Foam
31 {
32 
33 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
34 
35 template<class Type>
36 bool fieldOk(const IOobjectList& cloudObjs, const word& name)
37 {
38  IOobjectList objects(cloudObjs.lookupClass(IOField<Type>::typeName));
39 
40  return (objects.lookup(name) != NULL);
41 }
42 
43 
44 template<class Type>
45 tmp<Field<Type> > readParticleField
46 (
47  const word& name,
48  const IOobjectList cloudObjs
49 )
50 {
51  IOobjectList objects(cloudObjs.lookupClass(IOField<Type>::typeName));
52 
53  const IOobject* obj = objects.lookup(name);
54  if (obj != NULL)
55  {
56  IOField<Type> newField(*obj);
57  return tmp<Field<Type> >(new Field<Type>(newField.xfer()));
58  }
59 
61  << "error: cloud field name " << name << " not found"
62  << abort(FatalError);
63 
64  return Field<Type>::null();
65 }
66 
67 
68 template<class Type>
69 void readFields
70 (
71  PtrList<List<Type> >& values,
72  const List<word>& fieldNames,
73  const IOobjectList& cloudObjs
74 )
75 {
76  IOobjectList objects(cloudObjs.lookupClass(IOField<Type>::typeName));
77 
78  forAll(fieldNames, j)
79  {
80  const IOobject* obj = objects.lookup(fieldNames[j]);
81  if (obj != NULL)
82  {
83  Info<< " reading field " << fieldNames[j] << endl;
84  IOField<Type> newField(*obj);
85  values.set(j, new List<Type>(newField.xfer()));
86  }
87  else
88  {
90  << "Unable to read field " << fieldNames[j]
91  << abort(FatalError);
92  }
93  }
94 }
95 
96 
97 template<class Type>
98 void writeVTK(OFstream& os, const Type& value)
99 {
100  os << value.component(0);
101  for (label i=1; i<pTraits<Type>::nComponents; i++)
102  {
103  os << ' ' << value.component(i);
104  }
105 }
106 
107 
108 template<class Type>
109 void writeVTKFields
110 (
111  OFstream& os,
112  const PtrList<List<Type> >& values,
113  const List<List<label> >& addr,
114  const List<word>& fieldNames
115 )
116 {
117  label step = max(floor(8/pTraits<Type>::nComponents), 1);
118 
119  forAll(values, fieldI)
120  {
121  Info<< " writing field " << fieldNames[fieldI] << endl;
122  os << nl << fieldNames[fieldI] << ' ' << pTraits<Type>::nComponents
123  << ' ' << values[fieldI].size() << " float" << nl;
124  label offset = 0;
125  forAll(addr, trackI)
126  {
127  const List<label> ids(addr[trackI]);
128 
129  List<Type> data(UIndirectList<Type>(values[fieldI], ids));
130  label nData = data.size() - 1;
131  forAll(data, i)
132  {
133  writeVTK<Type>(os, data[i]);
134  if (((i + 1) % step == 0) || (i == nData))
135  {
136  os << nl;
137  }
138  else
139  {
140  os << ' ';
141  }
142  }
143  offset += ids.size();
144  }
145  }
146 }
147 
148 
149 template<class Type>
150 void processFields
151 (
152  OFstream& os,
153  const List<List<label> >& addr,
154  const List<word>& userFieldNames,
155  const IOobjectList& cloudObjs
156 )
157 {
158  IOobjectList objects(cloudObjs.lookupClass(IOField<Type>::typeName));
159 
160  if (objects.size())
161  {
162  DynamicList<word> fieldNames(objects.size());
163  forAll(userFieldNames, i)
164  {
165  IOobject* obj = objects.lookup(userFieldNames[i]);
166  if (obj != NULL)
167  {
168  fieldNames.append(obj->name());
169  }
170  }
171  fieldNames.shrink();
172 
173  PtrList<List<Type> > values(fieldNames.size());
174  readFields<Type>(values, fieldNames, cloudObjs);
175 
176  writeVTKFields<Type>
177  (
178  os,
179  values,
180  addr,
181  fieldNames
182  );
183  }
184 }
185 
186 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
187 
188 } // End namespace Foam
189 
190 // ************************************************************************* //
forAll
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:406
Foam::Field< Type >::typeName
static const char *const typeName
Definition: Field.H:94
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
Foam::readParticleField
tmp< Field< Type > > readParticleField(const word &name, const IOobjectList cloudObjs)
Foam::writeVTK
void writeVTK(OFstream &os, const Type &value)
steadyParticleTracksTemplates.H
Foam::label
intWM_LABEL_SIZE_t label
A label is an int32_t or int64_t as specified by the pre-processor macro WM_LABEL_SIZE.
Definition: label.H:59
Foam::List::append
void append(const T &)
Append an element at the end of the list.
Foam::nl
static const char nl
Definition: Ostream.H:260
Foam::Info
messageStream Info
Foam::writeVTKFields
void writeVTKFields(OFstream &os, const PtrList< List< Type > > &values, const List< SortableList< scalar > > &agePerTrack, const List< word > &fieldNames)
Foam::fieldOk
bool fieldOk(const IOobjectList &cloudObjs, const word &name)
Foam::FatalError
error FatalError
Foam
Namespace for OpenFOAM.
Definition: combustionModel.C:30
Foam::abort
errorManip< error > abort(error &err)
Definition: errorManip.H:131
Foam::max
dimensioned< Type > max(const dimensioned< Type > &, const dimensioned< Type > &)
FatalErrorInFunction
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:318
Foam::fieldNames
wordList fieldNames(const IOobjectList &objects, const bool syncPar)
Get sorted names of fields of type. If syncPar and running in parallel.
Definition: ReadFields.C:36
Foam::Field::null
static const Field< Type > & null()
Return a null field.
Definition: Field.H:100
List
Definition: Test.C:19
Foam::List::size
void size(const label)
Override size to be inconsistent with allocated storage.
Foam::processFields
void processFields(OFstream &os, const List< SortableList< scalar > > &agePerTrack, const List< word > &userFieldNames, const IOobjectList &cloudObjs)
Foam::readFields
void readFields(const typename GeoMesh::Mesh &mesh, const IOobjectList &objects, PtrList< GeometricField< Type, PatchField, GeoMesh > > &fields, const bool readOldTime)
Definition: readFields.C:33
Foam::name
word name(const complex &)
Return a string representation of a complex.
Definition: complex.C:47