IOPosition.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration | Website: https://openfoam.org
5  \\ / A nd | Copyright (C) 2011-2024 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 
26 #include "IOPosition.H"
27 
28 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
29 
30 template<class CloudType>
32 :
33  regIOobject(io),
34  cloud_(c)
35 {}
36 
37 
38 template<class CloudType>
40 :
42  (
43  IOobject
44  (
45  "positions",
46  c.time().name(),
47  c,
48  IOobject::MUST_READ,
49  IOobject::NO_WRITE
50  )
51  ),
52  cloud_(c)
53 {}
54 
55 
56 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
57 
58 template<class CloudType>
60 {
61  return regIOobject::write(cloud_.size());
62 }
63 
64 
65 template<class CloudType>
67 {
68  os << cloud_.size() << nl << token::BEGIN_LIST << nl;
69 
70  forAllConstIter(typename CloudType, cloud_, iter)
71  {
72  iter().writePosition(os);
73  os << nl;
74  }
75 
76  os << token::END_LIST << endl;
77 
78  return os.good();
79 }
80 
81 
82 template<class CloudType>
84 {
85  static const char* funcName =
86  "IOPosition<CloudType>::readData(Istream&, CloudType&)";
87 
88  token firstToken(is);
89 
90  if (firstToken.isLabel())
91  {
92  label s = firstToken.labelToken();
93 
94  // Read beginning of contents
95  is.readBeginList(funcName);
96 
97  for (label i=0; i<s; i++)
98  {
99  // Read position only
100  c.append(new typename CloudType::value_type(is, false));
101  }
102 
103  // Read end of contents
104  is.readEndList(funcName);
105  }
106  else if (firstToken.isPunctuation())
107  {
108  if (firstToken.pToken() != token::BEGIN_LIST)
109  {
111  << "incorrect first token, '(', found "
112  << firstToken.info() << exit(FatalIOError);
113  }
114 
115  token lastToken(is);
116  while
117  (
118  !(
119  lastToken.isPunctuation()
120  && lastToken.pToken() == token::END_LIST
121  )
122  )
123  {
124  is.putBack(lastToken);
125 
126  // Read position only
127  c.append(new typename CloudType::value_type(is, false));
128  is >> lastToken;
129  }
130  }
131  else
132  {
134  << "incorrect first token, expected <int> or '(', found "
135  << firstToken.info() << exit(FatalIOError);
136  }
137 
138  // Check state of IOstream
139  is.check(funcName);
140 }
141 
142 
143 // ************************************************************************* //
#define forAllConstIter(Container, container, iter)
Iterate across all elements in the container object of type.
Definition: UList.H:477
Templated base class for dsmc cloud.
Definition: DSMCCloud.H:80
IOPosition(const IOobject &, const CloudType &)
Construct from components.
Definition: IOPosition.C:31
virtual bool writeData(Ostream &os) const
Pure virtual writaData function.
Definition: IOPosition.C:66
virtual bool write(const bool write=true) const
Write using setting from DB.
Definition: IOPosition.C:59
virtual bool readData(Istream &)
Inherit readData from regIOobject.
IOobject defines the attributes of an object for which implicit objectRegistry management is supporte...
Definition: IOobject.H:99
virtual bool check(const char *operation) const
Check IOstream status for given operation.
Definition: IOstream.C:92
bool good() const
Return true if next operation might succeed.
Definition: IOstream.H:330
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:60
char readEndList(const char *funcName)
Definition: Istream.C:148
char readBeginList(const char *funcName)
Definition: Istream.C:127
void putBack(const token &)
Put back token.
Definition: Istream.C:30
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:57
T value_type
Type of values the DLList contains.
Definition: UILList.H:166
regIOobject is an abstract class derived from IOobject to handle automatic object registration with t...
Definition: regIOobject.H:55
virtual bool write(const bool write=true) const
Write using setting from DB.
A token holds items read from Istream.
Definition: token.H:73
bool isLabel() const
Definition: tokenI.H:571
bool isPunctuation() const
Definition: tokenI.H:280
@ BEGIN_LIST
Definition: token.H:109
@ END_LIST
Definition: token.H:110
punctuationToken pToken() const
Definition: tokenI.H:285
label labelToken() const
Definition: tokenI.H:590
InfoProxy< token > info() const
Return info proxy.
Definition: token.H:422
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:346
gmvFile<< "tracers "<< particles.size()<< nl;forAllConstIter(Cloud< passiveParticle >, particles, iter){ gmvFile<< iter().position().x()<< " ";}gmvFile<< nl;forAllConstIter(Cloud< passiveParticle >, particles, iter){ gmvFile<< iter().position().y()<< " ";}gmvFile<< nl;forAllConstIter(Cloud< passiveParticle >, particles, iter){ gmvFile<< iter().position().z()<< " ";}gmvFile<< nl;forAll(lagrangianScalarNames, i){ word name=lagrangianScalarNames[i];IOField< scalar > s(IOobject(name, runTime.name(), cloud::prefix, mesh, IOobject::MUST_READ, IOobject::NO_WRITE))
const dimensionedScalar c
Speed of light in a vacuum.
void write(std::ostream &os, const bool binary, List< floatScalar > &fField)
Write floats ascii or binary.
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
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
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:257
word name(const bool)
Return a word representation of a bool.
Definition: boolIO.C:39
IOerror FatalIOError
static const char nl
Definition: Ostream.H:266