IOPosition.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-2017 OpenFOAM Foundation
9  Copyright (C) 2017-2021 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 
29 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
30 
31 template<class CloudType>
33 (
34  const CloudType& c,
35  cloud::geometryType geomType
36 )
37 :
39  (
40  IOobject
41  (
42  cloud::geometryTypeNames[geomType],
43  c.time().timeName(),
44  c,
45  IOobject::MUST_READ,
46  IOobject::NO_WRITE
47  )
48  ),
49  geometryType_(geomType),
50  cloud_(c)
51 {}
52 
53 
54 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
55 
56 template<class CloudType>
57 bool Foam::IOPosition<CloudType>::write(const bool valid) const
58 {
59  return regIOobject::write(cloud_.size());
60 }
61 
62 
63 template<class CloudType>
65 {
66  os << cloud_.size() << nl << token::BEGIN_LIST << nl;
67 
68  switch (geometryType_)
69  {
70  case cloud::geometryType::COORDINATES:
71  {
72  forAllConstIters(cloud_, iter)
73  {
74  iter().writeCoordinates(os);
75  os << nl;
76  }
77  break;
78  }
79  case cloud::geometryType::POSITIONS:
80  {
81  forAllConstIters(cloud_, iter)
82  {
83  iter().writePosition(os);
84  os << nl;
85  }
86  break;
87  }
88  }
89 
90  os << token::END_LIST << endl;
91 
92  return os.good();
93 }
94 
95 
96 template<class CloudType>
98 {
99  const polyMesh& mesh = c.pMesh();
100 
101  token tok(is);
102 
103  const bool newFormat = (geometryType_ == cloud::geometryType::COORDINATES);
104 
105  if (tok.isLabel())
106  {
107  const label len = tok.labelToken();
108 
109  // Read beginning of contents
111 
112  for (label i=0; i<len; ++i)
113  {
114  // Read position only
115  c.append
116  (
117  new typename CloudType::particleType
118  (
119  mesh,
120  is,
121  false,
122  newFormat
123  )
124  );
125  }
126 
127  // Read end of contents
129  }
130  else if (tok.isPunctuation(token::BEGIN_LIST))
131  {
132  is >> tok;
133  while (!tok.isPunctuation(token::END_LIST))
134  {
135  is.putBack(tok);
136 
137  // Read position only
138  c.append
139  (
140  new typename CloudType::particleType(mesh, is, false, newFormat)
141  );
142  is >> tok;
143  }
144  }
145  else
146  {
148  << "incorrect first token, expected <int> or '(', found "
149  << tok.info() << nl
150  << exit(FatalIOError);
151  }
152 
153  is.check(FUNCTION_NAME);
154 }
155 
156 
157 // ************************************************************************* //
Foam::IOobject
Defines the attributes of an object for which implicit objectRegistry management is supported,...
Definition: IOobject.H:169
Foam::token::labelToken
label labelToken() const
Return label value.
Definition: tokenI.H:513
Foam::token::isLabel
bool isLabel() const noexcept
Token is LABEL.
Definition: tokenI.H:497
Foam::IOPosition::writeData
virtual bool writeData(Ostream &os) const
Definition: IOPosition.C:64
Foam::Istream::readBeginList
char readBeginList(const char *funcName)
Begin read of list data, starts with '(' or '{'.
Definition: Istream.C:148
Foam::Istream::readEndList
char readEndList(const char *funcName)
End read of list data, ends with ')' or '}'.
Definition: Istream.C:169
Foam::FatalIOError
IOerror FatalIOError
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:369
Foam::token
A token holds an item read from Istream.
Definition: token.H:68
Foam::polyMesh
Mesh consisting of general polyhedral cells.
Definition: polyMesh.H:77
Foam::token::info
InfoProxy< token > info() const
Return info proxy for printing token information to a stream.
Definition: token.H:586
Foam::Istream
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:61
Foam::cloud::geometryType
geometryType
Cloud geometry type (internal or IO representations)
Definition: cloud.H:74
Foam::token::isPunctuation
bool isPunctuation() const noexcept
Token is PUNCTUATION.
Definition: tokenI.H:459
Foam::DSMCCloud
Templated base class for dsmc cloud.
Definition: DSMCCloud.H:71
Foam::IOPosition::write
virtual bool write(const bool valid=true) const
Definition: IOPosition.C:57
Foam::IOstream::check
virtual bool check(const char *operation) const
Check IOstream status for given operation.
Definition: IOstream.C:58
os
OBJstream os(runTime.globalPath()/outputName)
mesh
dynamicFvMesh & mesh
Definition: createDynamicFvMesh.H:6
Foam::Cloud::particleType
ParticleType particleType
Definition: Cloud.H:114
Foam::exit
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:130
Foam::IOPosition::IOPosition
IOPosition(const CloudType &c, cloud::geometryType geomType=cloud::geometryType::COORDINATES)
Construct from cloud.
Definition: IOPosition.C:33
Foam::regIOobject
regIOobject is an abstract class derived from IOobject to handle automatic object registration with t...
Definition: regIOobject.H:73
Foam::nl
constexpr char nl
Definition: Ostream.H:404
forAllConstIters
forAllConstIters(mixture.phases(), phase)
Definition: pEqn.H:28
Foam::Istream::putBack
void putBack(const token &tok)
Put back a token. Only a single put back is permitted.
Definition: Istream.C:70
FUNCTION_NAME
#define FUNCTION_NAME
Definition: messageStream.H:295
Foam::vtk::write
void write(vtk::formatter &fmt, const Type &val, const label n=1)
Component-wise write of a value (N times)
Definition: foamVtkOutputTemplates.C:36
Foam::constant::universal::c
const dimensionedScalar c
Speed of light in a vacuum.
FatalIOErrorInFunction
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:473
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:56
Foam::IOPosition::readData
virtual void readData(Istream &, CloudType &)
Definition: IOPosition.C:97