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-2023 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>
58 (
59  const bool /* writeOnProc */
60 ) const
61 {
62  return regIOobject::write(cloud_.size() > 0);
63 }
64 
65 
66 template<class CloudType>
68 {
69  os << cloud_.size() << nl << token::BEGIN_LIST << nl;
70 
71  switch (geometryType_)
72  {
73  case cloud::geometryType::COORDINATES:
74  {
75  for (const auto& p : cloud_)
76  {
77  p.writeCoordinates(os);
78  os << nl;
79  }
80  break;
81  }
82  case cloud::geometryType::POSITIONS:
83  {
84  for (const auto& p : cloud_)
85  {
86  p.writePosition(os);
87  os << nl;
88  }
89  break;
90  }
91  }
92 
93  os << token::END_LIST << endl;
94 
95  return os.good();
96 }
97 
98 
99 template<class CloudType>
101 {
102  const polyMesh& mesh = c.pMesh();
103 
104  token tok(is);
105 
106  const bool newFormat = (geometryType_ == cloud::geometryType::COORDINATES);
107 
108  if (tok.isLabel())
109  {
110  const label len = tok.labelToken();
111 
112  // Read beginning of contents
114 
115  for (label i=0; i<len; ++i)
116  {
117  // Read position only
118  c.append
119  (
120  new typename CloudType::particleType
121  (
122  mesh,
123  is,
124  false,
125  newFormat
126  )
127  );
128  }
129 
130  // Read end of contents
132  }
133  else if (tok.isPunctuation(token::BEGIN_LIST))
134  {
135  is >> tok;
136  while (!tok.isPunctuation(token::END_LIST))
137  {
138  is.putBack(tok);
139 
140  // Read position only
141  c.append
142  (
143  new typename CloudType::particleType(mesh, is, false, newFormat)
144  );
145  is >> tok;
146  }
147  }
148  else
149  {
151  << "incorrect first token, expected <int> or '(', found "
152  << tok.info() << nl
153  << exit(FatalIOError);
154  }
155 
156  is.check(FUNCTION_NAME);
157 }
158 
159 
160 // ************************************************************************* //
ParcelType particleType
Definition: Cloud.H:130
bool isPunctuation() const noexcept
Token is PUNCTUATION.
Definition: tokenI.H:561
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:125
geometryType
Cloud geometry type (internal or IO representations)
Definition: cloud.H:62
virtual bool check(const char *operation) const
Check IOstream status for given operation.
Definition: IOstream.C:45
char readEndList(const char *funcName)
End read of list data, ends with &#39;)&#39; or &#39;}&#39;.
Definition: Istream.C:192
An Istream is an abstract base class for all input systems (streams, files, token lists etc)...
Definition: Istream.H:57
constexpr char nl
The newline &#39;\n&#39; character (0x0a)
Definition: Ostream.H:50
A token holds an item read from Istream.
Definition: token.H:65
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:531
IOPosition(const CloudType &c, cloud::geometryType geomType=cloud::geometryType::COORDINATES)
Construct from cloud.
Definition: IOPosition.C:26
word timeName
Definition: getTimeIndex.H:3
void write(vtk::formatter &fmt, const Type &val, const label n=1)
Component-wise write of a value (N times)
void putBack(const token &tok)
Put back a token (copy). Only a single put back is permitted.
Definition: Istream.C:71
dynamicFvMesh & mesh
A cloud is a registry collection of lagrangian particles.
Definition: cloud.H:53
virtual bool writeData(Ostream &os) const
Pure virtual writeData function.
Definition: IOPosition.C:60
char readBeginList(const char *funcName)
Begin read of list data, starts with &#39;(&#39; or &#39;{&#39;.
Definition: Istream.C:171
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:56
OBJstream os(runTime.globalPath()/outputName)
#define FUNCTION_NAME
virtual bool write(const bool writeOnProc=true) const
Write using setting from DB.
Definition: IOPosition.C:51
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:627
const dimensionedScalar c
Speed of light in a vacuum.
label labelToken() const
Return label value.
Definition: tokenI.H:615
bool isLabel() const noexcept
Token is LABEL.
Definition: tokenI.H:599
regIOobject is an abstract class derived from IOobject to handle automatic object registration with t...
Definition: regIOobject.H:66
Mesh consisting of general polyhedral cells.
Definition: polyMesh.H:74
volScalarField & p
virtual void readData(Istream &, CloudType &)
Definition: IOPosition.C:93
Defines the attributes of an object for which implicit objectRegistry management is supported...
Definition: IOobject.H:172
Templated base class for dsmc cloud.
Definition: DSMCCloud.H:67
IOerror FatalIOError
Error stream (stdout output on all processes), with additional &#39;FOAM FATAL IO ERROR&#39; header text and ...